Skip to content

Commit

Permalink
Add message to assertions that use equals.
Browse files Browse the repository at this point in the history
  • Loading branch information
woess committed May 10, 2024
1 parent 097092b commit 031e4a4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ final E find(K key, int hash) {
}

final TrieNode<K, V, E> put(K key, int hash, E entry) {
assert key != null && hash(key) == hash && key(entry).equals(key);
assert key != null && hash(key) == hash && key(entry).equals(key) : Arrays.asList(entry, key);
return put(key, hash, entry, 0);
}

Expand Down Expand Up @@ -211,7 +211,7 @@ private String toStringIndent(int indent) {
}

final TrieNode<K, V, E> combine(K key1, int hash1, E entry1, K key2, int hash2, E entry2, int shift) {
assert !key1.equals(key2);
assert !key1.equals(key2) : Arrays.asList(key1, key2);
if (hash1 != hash2) {
int pos1 = pos(hash1, shift);
int pos2 = pos(hash2, shift);
Expand Down Expand Up @@ -394,7 +394,7 @@ E find(K key, int hash, int shift) {
return null;
} else {
E entry = (E) entries[index];
assert entry != null && key(entry).equals(key);
assert entry != null && key(entry).equals(key) : Arrays.asList(entry, key);
return entry;
}
}
Expand All @@ -408,7 +408,7 @@ TrieNode<K, V, E> put(K key, int hash, E entry, int shift) {
return new HashCollisionNode<>(hash, copyAndAppend(entries, entry));
} else {
E e = (E) entries[index];
assert e != null && key(e).equals(key);
assert e != null && key(e).equals(key) : Arrays.asList(e, key);
if (e.equals(entry)) {
return this;
} else {
Expand All @@ -427,7 +427,7 @@ TrieNode<K, V, E> remove(K key, int hash, int shift) {
if (index < 0) {
return this;
} else {
assert entries[index] != null && key((E) entries[index]).equals(key);
assert entries[index] != null && key((E) entries[index]).equals(key) : Arrays.asList(entries[index], key);
assert entries.length >= 2;
if (entries.length == 2) {
return new BitmapNode<>(bit(this.hashcode, shift), copyAndRemove(entries, index));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
package com.oracle.truffle.object;

import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -155,6 +156,7 @@ private TriePropertyMap(int size, TrieNode<Object, Property, LinkedPropertyEntry
}

private boolean verify() {
assert root != null;
assert (size == 0 && head == null && tail == null) || (size != 0 && head != null && tail != null) : "size=" + size + ", head=" + head + ", tail=" + tail;
assert head == null || head == getEntry(head.getKey());
assert tail == null || tail == getEntry(tail.getKey());
Expand Down Expand Up @@ -211,7 +213,7 @@ public Property get(Object key) {
@Override
public LinkedPropertyEntry getEntry(Object key) {
LinkedPropertyEntry entry = root.find(key, hash(key));
assert entry == null || entry.getKey().equals(key);
assert entry == null || entry.getKey().equals(key) : Arrays.asList(entry, key);
return entry;
}

Expand Down Expand Up @@ -268,17 +270,13 @@ private TriePropertyMap copyAndPutImpl(Object key, Property value) {
newTail = tail;

newEntry = existing.withValue(value);
assert !newEntry.equals(existing);
if (existing.getPrevKey() != null) {
assert getEntry(existing.getPrevKey()).getNextKey().equals(key);
} else {
assert existing == head;
assert !newEntry.equals(existing) : Arrays.asList(newEntry, existing);
if (existing.getPrevKey() == null) {
assert existing == head : Arrays.asList(existing, head);
newHead = newEntry;
}
if (existing.getNextKey() != null) {
assert getEntry(existing.getNextKey()).getPrevKey().equals(key);
} else {
assert existing == tail;
if (existing.getNextKey() == null) {
assert existing == tail : Arrays.asList(existing, tail);
newTail = newEntry;
}
}
Expand Down Expand Up @@ -329,7 +327,6 @@ public TriePropertyMap copyAndRemove(Object key) {
}
}
newRoot = newRoot.remove(key, hash);
assert newRoot != null;
return new TriePropertyMap(size - 1, newRoot, newHead, newTail);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
package com.oracle.truffle.object;

import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -115,7 +116,7 @@ public V get(Object key) {

Map.Entry<K, V> getEntry(K key) {
var entry = root.find(key, hash(key));
assert entry == null || entry.getKey().equals(key);
assert entry == null || entry.getKey().equals(key) : Arrays.asList(entry, key);
return entry;
}

Expand Down Expand Up @@ -151,7 +152,7 @@ private UnorderedTrieMap<K, V> copyAndPutImpl(K key, V value, BiFunction<K, V, M
// replacing an existing entry
newSize = size;
newEntry = newEntry(key, value, entryFactory);
assert !newEntry.equals(existing);
assert !newEntry.equals(existing) : Arrays.asList(newEntry, existing);
}
}
newRoot = newRoot.put(key, hash, newEntry);
Expand All @@ -174,7 +175,6 @@ public UnorderedTrieMap<K, V> copyAndRemove(K key) {
}
var newRoot = root;
newRoot = newRoot.remove(key, hash);
assert newRoot != null;
return new UnorderedTrieMap<>(size - 1, newRoot);
}
}
Expand All @@ -191,7 +191,6 @@ UnorderedTrieMap<K, V> copyAndRemoveEntry(Map.Entry<K, V> entry) {
}
var newRoot = root;
newRoot = newRoot.remove(key, hash);
assert newRoot != null;
return new UnorderedTrieMap<>(size - 1, newRoot);
}
}
Expand Down Expand Up @@ -297,6 +296,8 @@ public String toString() {
}

private boolean verify() {
assert size >= 0;
assert root != null;
if (VERIFY) {
assert root.count() == size : root.count() + " != " + size;
assert root.verify(0);
Expand Down

0 comments on commit 031e4a4

Please sign in to comment.