@Test public void valuesCollection_toArray_withLargeTarget() { MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two"); String[] target = new String[3]; target[2] = "yow!"; String[] values = map.values().toArray(target); ArrayIterate.sort( values, values.length, Comparators.safeNullsHigh(Comparators.<String>naturalOrder())); Assert.assertArrayEquals(new String[] {"One", "Two", null}, values); }
@Test public void keySet_ToArray_withLargeTarget() { MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three", 4, "Four"); Integer[] target = new Integer[6]; // deliberately large to force the extra to be set to null target[4] = 42; target[5] = 42; Integer[] result = map.keySet().toArray(target); ArrayIterate.sort( result, result.length, Comparators.safeNullsHigh(Comparators.<Integer>naturalOrder())); Assert.assertArrayEquals(new Integer[] {1, 2, 3, 4, 42, null}, result); }
@Override @Test public void toArray() { super.toArray(); int size = COLLISIONS.size(); for (int i = 1; i < size; i++) { MutableSet<Integer> set = UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, SIZE) .withAll(COLLISIONS.subList(0, i)); Object[] objects = set.toArray(); Assert.assertEquals(set, UnifiedSet.newSetWith(objects)); } MutableSet<Integer> deepChain = UnifiedSetWithHashingStrategy.newSetWith( INTEGER_HASHING_STRATEGY, COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4, COLLISION_5, COLLISION_6); Assert.assertArrayEquals( new Integer[] { COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4, COLLISION_5, COLLISION_6 }, deepChain.toArray()); MutableSet<Integer> minimumChain = UnifiedSetWithHashingStrategy.newSetWith( INTEGER_HASHING_STRATEGY, COLLISION_1, COLLISION_2); minimumChain.remove(COLLISION_2); Assert.assertArrayEquals(new Integer[] {COLLISION_1}, minimumChain.toArray()); MutableSet<Integer> set = UnifiedSetWithHashingStrategy.newSetWith( INTEGER_HASHING_STRATEGY, COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4); Integer[] target = { Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1) }; Integer[] actual = set.toArray(target); ArrayIterate.sort(actual, actual.length, Comparators.safeNullsHigh(Integer::compareTo)); Assert.assertArrayEquals( new Integer[] {COLLISION_1, 1, COLLISION_2, COLLISION_3, COLLISION_4, null}, actual); }
@Test public void keySet_copyKeys() { // a map with a null key MutableMap<Integer, Integer> map1 = this.newMapWithKeyValue(null, 0); Assert.assertArrayEquals(new Object[] {null}, map1.keySet().toArray()); // a map with a chain containing empty slots MutableMap<Integer, Integer> map2 = this.mapWithCollisionsOfSize(5); Assert.assertArrayEquals(new Object[] {0, 17, 34, 51, 68}, map2.keySet().toArray()); // a map with a chain containing empty slots and null key MutableMap<Integer, Integer> map3 = this.mapWithCollisionsOfSize(5); map3.put(null, 42); Integer[] array = map3.keySet().toArray(new Integer[map3.size()]); ArrayIterate.sort( array, array.length, Comparators.safeNullsHigh(Comparators.<Integer>naturalOrder())); Assert.assertArrayEquals(new Object[] {0, 17, 34, 51, 68, null}, array); }