Skip to content

Commit 3677581

Browse files
committed
Adds unit tests.
Switches from cursor() to iterator() for hashCode() and equals(). Adds toString().
1 parent 55fc060 commit 3677581

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/main/java/org/javimmutable/collections/array/JImmutableTrieArray.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import org.javimmutable.collections.SplitableIterator;
4545
import org.javimmutable.collections.common.AbstractJImmutableArray;
4646
import org.javimmutable.collections.common.MutableDelta;
47-
import org.javimmutable.collections.cursors.Cursors;
47+
import org.javimmutable.collections.iterators.IteratorHelper;
4848
import org.javimmutable.collections.iterators.TransformIterator;
4949
import org.javimmutable.collections.serialization.JImmutableArrayProxy;
5050

@@ -193,19 +193,19 @@ public void checkInvariants()
193193
@Override
194194
public boolean equals(Object o)
195195
{
196-
return (o == this) || ((o instanceof JImmutableArray) && Cursors.areEqual(cursor(), ((JImmutableArray)o).cursor()));
196+
return (o == this) || ((o instanceof JImmutableArray) && IteratorHelper.iteratorEquals(iterator(), ((JImmutableArray)o).iterator()));
197197
}
198198

199199
@Override
200200
public int hashCode()
201201
{
202-
return Cursors.computeHashCode(cursor());
202+
return IteratorHelper.iteratorHashCode(iterator());
203203
}
204204

205205
@Override
206206
public String toString()
207207
{
208-
return Cursors.makeString(cursor());
208+
return IteratorHelper.iteratorToString(iterator());
209209
}
210210

211211
private Object writeReplace()

src/main/java/org/javimmutable/collections/array/LeafTrieNode.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.javimmutable.collections.Holder;
4040
import org.javimmutable.collections.Holders;
4141
import org.javimmutable.collections.JImmutableMap;
42+
import org.javimmutable.collections.MapEntry;
4243
import org.javimmutable.collections.SplitableIterator;
4344
import org.javimmutable.collections.common.MutableDelta;
4445
import org.javimmutable.collections.cursors.SingleValueCursor;
@@ -220,6 +221,12 @@ public int hashCode()
220221
return result;
221222
}
222223

224+
@Override
225+
public String toString()
226+
{
227+
return MapEntry.makeToString(this);
228+
}
229+
223230
private TrieNode<T> withValue(T newValue)
224231
{
225232
return new LeafTrieNode<>(index, newValue, shift);

src/test/java/org/javimmutable/collections/array/JImmutableTrieArrayTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,15 @@ public void testTrimming()
7373
array = array.assign(33, 33);
7474
array = array.assign(65, 65);
7575
array = array.assign(97, 97);
76+
assertEquals("[0=0,33=33,65=65,97=97]", array.toString());
77+
assertEquals(33559365, array.hashCode());
7678
array = array.delete(-1);
7779
array = array.delete(33);
7880
array = array.delete(97);
7981
array = array.delete(65);
8082
array = array.delete(0);
83+
assertEquals("[]", array.toString());
84+
assertEquals(0, array.hashCode());
8185
}
8286

8387
public void testRandom()
@@ -111,6 +115,21 @@ public void testRandom()
111115
array.checkInvariants();
112116
}
113117

118+
public void testCollector()
119+
{
120+
Random r = new Random(20L);
121+
JImmutableArray<Integer> array = JImmutableTrieArray.of();
122+
int size = 0;
123+
while (size < 50_000) {
124+
size += r.nextInt(250);
125+
while (array.size() < size) {
126+
final int value = r.nextInt();
127+
array = array.assign(array.size(), value);
128+
}
129+
assertEquals(array, array.values().parallelStream().collect(JImmutableTrieArray.collector()));
130+
}
131+
}
132+
114133
public void testSequential()
115134
{
116135
Map<Integer, Integer> expected = new TreeMap<>();

0 commit comments

Comments
 (0)