@Benchmark public void gsc() { MutableList<Integer> result = FastList.newList(); for (int i = 0; i < 1000; i++) { result.addAll(this.integersGSC); } }
@Test public void keySet_retainAll() { // a map with a null key MutableMap<Integer, Integer> map = this.newMapWithKeyValue(null, 0); MutableList<Object> retained = Lists.mutable.of(); retained.add(null); Assert.assertFalse(map.keySet().retainAll(retained)); Verify.assertContains(null, map.keySet()); // a map with a chain containing empty slots MutableMap<Integer, Integer> map2 = this.mapWithCollisionsOfSize(5); Assert.assertFalse(map2.keySet().retainAll(FastList.<Integer>newListWith(0, 17, 34, 51, 68))); Verify.assertContainsAll(map2.keySet(), 0, 17, 34, 51, 68); // a map with no chaining, nothing retained MutableMap<Integer, String> map3 = this.newMapWithKeyValue(1, "One"); Assert.assertTrue(map3.keySet().retainAll(FastList.<Integer>newListWith(9))); Verify.assertEmpty(map3); Set<Integer> keys = this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three", 4, "Four").keySet(); Assert.assertTrue(keys.retainAll(FastList.<Integer>newListWith(1, 2, 3))); Verify.assertContainsAll(keys, 1, 2, 3); }
@Test public void testContainsAll() { MutableList<String> list = new CompositeFastList<String>(); list.addAll(FastList.newListWith("1", "2", "3", "4")); list.addAll(FastList.newListWith("3", "B", "3", "B")); Assert.assertTrue(list.containsAll(FastList.newList().with("2", "B"))); }
private static void runUnifiedSetRetainAllFromList(int shift) { MultiReaderUnifiedSet<CollidingInt> set = MultiReaderUnifiedSet.newSet(); MutableList<CollidingInt> toRetain = Lists.mutable.of(); int size = 100000; for (int i = 0; i < size; i++) { set.add(new CollidingInt(i, shift)); if (i % 2 == 0) { toRetain.add(new CollidingInt(i, shift)); } } Verify.assertSize(size, set); Assert.assertTrue(set.containsAll(toRetain)); Assert.assertTrue(set.retainAll(toRetain)); Assert.assertTrue(set.containsAll(toRetain)); Assert.assertFalse(set.retainAll(toRetain)); // a second call should not modify the set Verify.assertSize(size / 2, set); for (int i = 0; i < size; i += 2) { Verify.assertContains(new CollidingInt(i, shift), set); } }
private void executeParallelIterate(int level, ExecutorService executorService) { MutableList<Integer> items = Lists.mutable.of(); for (int i = 0; i < 20000; i++) { items.add(i % 1000 == 0 ? level : 0); } ParallelIterate.forEach(items, new RecursiveProcedure(), executorService); }
@Test public void testDefaultConstructor() { MutableList<String> list = new CompositeFastList<String>(); list.add("1"); list.add("2"); Verify.assertSize(2, list); Verify.assertContains("1", list); }
@Test public void toArray() { ImmutableCollection<Integer> integers = this.classUnderTest(); MutableList<Integer> copy = FastList.newList(integers); Assert.assertArrayEquals(integers.toArray(), copy.toArray()); Assert.assertArrayEquals( integers.toArray(new Integer[integers.size()]), copy.toArray(new Integer[integers.size()])); }
public <V> MutableList<V> collect(BooleanToObjectFunction<? extends V> function) { MutableList<V> results = FastList.newList(BooleanArrayList.this.size); BooleanIterator iterator = this.booleanIterator(); while (iterator.hasNext()) { results.add(function.valueOf(iterator.next())); } return results; }
@Override @Test public void newListWithSize() { super.newListWithSize(); MutableList<Integer> objects = ArrayListAdapter.<Integer>newList(4).with(1, 2, 3); Assert.assertEquals(1, objects.indexOf(2)); }
@Test public void toSortedList() { ImmutableBag<String> strings = this.newBag(); MutableList<String> copy = FastList.newList(strings); MutableList<String> list = strings.toSortedList(Collections.<String>reverseOrder()); Assert.assertEquals(copy.sortThis(Collections.<String>reverseOrder()), list); MutableList<String> list2 = strings.toSortedList(); Assert.assertEquals(copy.sortThis(), list2); }
@Test public void toSortedList() { ImmutableCollection<Integer> integers = this.classUnderTest(); MutableList<Integer> copy = FastList.newList(integers); MutableList<Integer> list = integers.toSortedList(Collections.<Integer>reverseOrder()); Assert.assertEquals(copy.sortThis(Collections.<Integer>reverseOrder()), list); MutableList<Integer> list2 = integers.toSortedList(); Assert.assertEquals(copy.sortThis(), list2); }
@Override @Test public void testClone() { super.testClone(); MutableList<Integer> list = this.newWith(1, 2, 3); MutableList<Integer> list2 = list.clone(); Verify.assertListsEqual(list, list2); }
@Override @Test public void retainAll() { super.retainAll(); MutableList<String> list = new CompositeFastList<String>(); list.addAll(FastList.newListWith("1", "2", "3", "4")); list.addAll(FastList.newListWith("3", "B", "3", "B")); list.retainAll(FastList.newList().with("2", "B")); Verify.assertSize(3, list); }
protected static MutableList<String> generateCollisions() { MutableList<String> collisions = FastList.newList(); ObjectBooleanHashMap<String> hashMap = new ObjectBooleanHashMap<String>(); for (int each = 3; collisions.size() <= 10; each++) { if (hashMap.index(String.valueOf(each)) == hashMap.index(String.valueOf(3))) { collisions.add(String.valueOf(each)); } } return collisions; }
@Override @Test public void removeIf() { super.removeIf(); MutableList<Integer> objects = this.newWith(1, 2, 3, null); objects.removeIf(Predicates.isNull()); Verify.assertSize(3, objects); Verify.assertContainsAll(objects, 1, 2, 3); }
@Test @Override public void subList() { // Not serializable MutableList<String> list = this.newWith("A", "B", "C", "D"); MutableList<String> sublist = list.subList(1, 3); Verify.assertSize(2, sublist); Verify.assertContainsAll(sublist, "B", "C"); sublist.add("X"); Verify.assertSize(3, sublist); Verify.assertContainsAll(sublist, "B", "C", "X"); Verify.assertSize(5, list); Verify.assertContainsAll(list, "A", "B", "C", "X", "D"); sublist.remove("X"); Verify.assertContainsAll(sublist, "B", "C"); Verify.assertContainsAll(list, "A", "B", "C", "D"); Assert.assertEquals("C", sublist.set(1, "R")); Verify.assertContainsAll(sublist, "B", "R"); Verify.assertContainsAll(list, "A", "B", "R", "D"); sublist.addAll(Arrays.asList("W", "G")); Verify.assertContainsAll(sublist, "B", "R", "W", "G"); Verify.assertContainsAll(list, "A", "B", "R", "W", "G", "D"); sublist.clear(); Verify.assertEmpty(sublist); Assert.assertFalse(sublist.remove("X")); Verify.assertEmpty(sublist); Verify.assertContainsAll(list, "A", "D"); }
// find element in sorted list private static void testBinarySearch() { List<String> list = Lists.newArrayList("2", "4", "13", "31", "43"); MutableList<String> mutableList = FastList.newListWith("2", "4", "13", "31", "43"); // find element in sorted list int jdk = Collections.binarySearch(list, "13"); int guava = Ordering.natural().binarySearch(list, "13"); int gs = mutableList.binarySearch("13"); System.out.println("find = " + jdk + ":" + guava + ":" + gs); // print find = 2:2:2 }
@Test public void flatten_value() { MutableMap<String, String> map = this.newMapWithKeysValues("1", "One", "2", "Two"); Function<String, Iterable<Character>> function = object -> { MutableList<Character> result = Lists.mutable.of(); if (object != null) { char[] chars = object.toCharArray(); for (char aChar : chars) { result.add(Character.valueOf(aChar)); } } return result; }; RichIterable<Character> blob = map.flatCollect(function); RichIterable<Character> blobFromTarget = map.flatCollect(function, FastList.<Character>newList()); switch (map.size()) { case 1: Assert.assertTrue( blob.containsAllArguments( Character.valueOf('O'), Character.valueOf('n'), Character.valueOf('e'))); Assert.assertTrue( blobFromTarget.containsAllArguments( Character.valueOf('O'), Character.valueOf('n'), Character.valueOf('e'))); break; case 2: case 3: Assert.assertTrue( blob.containsAllArguments( Character.valueOf('O'), Character.valueOf('n'), Character.valueOf('e'), Character.valueOf('T'), Character.valueOf('w'), Character.valueOf('o'))); Assert.assertTrue( blobFromTarget.containsAllArguments( Character.valueOf('O'), Character.valueOf('n'), Character.valueOf('e'), Character.valueOf('T'), Character.valueOf('w'), Character.valueOf('o'))); break; default: Assert.assertEquals(0, blob.size()); Assert.assertEquals(0, blobFromTarget.size()); break; } }
@Test public void remove() { for (int i = 1; i < COLLISIONS.size(); i++) { MutableMap<Integer, Integer> map = this.mapWithCollisionsOfSize(i); Assert.assertNull(map.put(null, null)); Assert.assertNull(map.remove(null)); Assert.assertNull(map.remove(COLLISION_10)); Integer biggestValue = COLLISIONS.get(i - 1); Assert.assertEquals(biggestValue, map.remove(biggestValue)); } }
@Override @Test public void lastIndexOf() { super.lastIndexOf(); MutableList<String> list = new CompositeFastList<String>(); list.addAll(FastList.newListWith("1", "2", "3", "4")); list.addAll(FastList.newListWith("3", "B", "3", "B")); Assert.assertEquals(6, list.lastIndexOf("3")); Assert.assertEquals(3, list.lastIndexOf("4")); Assert.assertEquals(-1, list.lastIndexOf("missing")); }
@Override @Test public void clear() { super.clear(); MutableList<String> list = new CompositeFastList<String>(); list.addAll(FastList.newListWith("1", "2", "3", "4")); list.addAll(FastList.newListWith("3", "B", "3", "B")); list.clear(); Assert.assertTrue(list.isEmpty()); Assert.assertEquals(0, list.size()); }
@Test public void forEachWithIndex() { int[] indexSum = new int[1]; MutableList<String> result = Lists.mutable.of(); MutableSet<String> source = Sets.fixedSize.of("1", "2", "3", "4"); source.forEachWithIndex( (each, index) -> { result.add(each); indexSum[0] += index; }); Assert.assertEquals(FastList.newListWith("1", "2", "3", "4"), result); Assert.assertEquals(6, indexSum[0]); }
@Override @Test public void forEachWithIndex() { super.forEachWithIndex(); MutableList<Integer> list = FastList.newList(); CompositeFastList<Integer> iterables = new CompositeFastList<Integer>(); iterables.addComposited(Interval.fromTo(6, 10).toList()); iterables.addComposited(Interval.oneTo(5).toList()); iterables.forEachWithIndex((each, index) -> list.add(index, each)); Verify.assertSize(10, list); Verify.assertAllSatisfy(list, Predicates.greaterThan(0).and(Predicates.lessThan(11))); Verify.assertStartsWith(list, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5); }
@Override @Test public void forEachWith() { super.forEachWith(); MutableList<Integer> list = FastList.newList(); CompositeFastList<Integer> iterables = new CompositeFastList<Integer>(); iterables.addComposited(Interval.fromTo(6, 10).toList()); iterables.addComposited(Interval.oneTo(5).toList()); iterables.forEachWith((each, parameter) -> list.add(parameter.intValue(), each), 0); Verify.assertSize(10, list); Verify.assertAllSatisfy(list, Predicates.greaterThan(0).and(Predicates.lessThan(11))); Verify.assertStartsWith(list, 5, 4, 3, 2, 1, 10, 9, 8, 7, 6); }
// find element in unsorted collection private static void testSearch() { Collection<String> collection = Lists.newArrayList("2", "14", "3", "13", "43"); MutableList<String> mutableList = FastList.newListWith("2", "14", "3", "13", "43"); Iterable<String> iterable = collection; // find element in unsorted collection String jdk = collection.stream().filter("13"::equals).findFirst().get(); String guava = Iterables.find(iterable, "13"::equals); String apache = CollectionUtils.find(iterable, "13"::equals); String gs = mutableList.select("13"::equals).get(0); System.out.println( "find = " + jdk + ":" + guava + ":" + apache + ":" + gs); // print find = 13:13:13:13 }
@Override @Test public void retainAllFromEntrySet() { super.retainAllFromEntrySet(); for (int i = 1; i < COLLISIONS.size(); i++) { MutableMap<Integer, Integer> map = this.mapWithCollisionsOfSize(i); Assert.assertFalse( map.entrySet() .retainAll( FastList.newList(map.entrySet()) .with(ImmutableEntry.of(COLLISION_10, COLLISION_10)))); Assert.assertTrue(map.entrySet().retainAll(this.mapWithCollisionsOfSize(i - 1).entrySet())); Assert.assertEquals(this.mapWithCollisionsOfSize(i - 1), map); } // simple map, collection to retain contains non-entry element MutableMap<Integer, String> map4 = this.newMapWithKeysValues(1, "One", 2, "Two"); FastList<Object> toRetain = FastList.<Object>newListWith( ImmutableEntry.of(1, "One"), "explosion!", ImmutableEntry.of(2, "Two")); Assert.assertFalse(map4.entrySet().retainAll(toRetain)); }
@Test public void contains_key_and_value() { for (int i = 1; i < COLLISIONS.size(); i++) { MutableMap<Integer, Integer> map = this.mapWithCollisionsOfSize(i); Assert.assertTrue(map.containsKey(COLLISIONS.get(i - 1))); Assert.assertTrue(map.containsValue(COLLISIONS.get(i - 1))); Assert.assertFalse(map.containsKey(COLLISION_10)); Assert.assertFalse(map.containsValue(COLLISION_10)); Assert.assertFalse(map.containsKey(null)); Assert.assertFalse(map.containsValue(null)); map.put(null, null); Assert.assertTrue(map.containsKey(null)); Assert.assertTrue(map.containsValue(null)); } }
@Override @Test public void removeFromEntrySet() { super.removeFromEntrySet(); for (int i = 1; i < COLLISIONS.size(); i++) { MutableMap<Integer, Integer> map = this.mapWithCollisionsOfSize(i); Integer biggestValue = COLLISIONS.get(i - 1); Assert.assertTrue(map.entrySet().remove(ImmutableEntry.of(biggestValue, biggestValue))); Assert.assertEquals(this.mapWithCollisionsOfSize(i - 1), map); Assert.assertFalse(map.entrySet().remove(ImmutableEntry.of(COLLISION_10, COLLISION_10))); Assert.assertEquals(this.mapWithCollisionsOfSize(i - 1), map); Assert.assertFalse(map.entrySet().remove(null)); } }
@Test public void testAddWithIndex() { MutableList<String> list = new CompositeFastList<String>(); list.addAll(FastList.newListWith("1", "2", "3", "4")); list.addAll(FastList.newListWith("A", "B", "C", "B")); list.add(3, "NEW"); Verify.assertSize(9, list); Assert.assertEquals("NEW", list.get(3)); Assert.assertEquals("4", list.get(4)); list.add(0, "START"); Verify.assertSize(10, list); Assert.assertEquals("START", list.getFirst()); list.add(10, "END"); Verify.assertSize(11, list); Assert.assertEquals("END", list.getLast()); }
private static void assertUnifiedSetForEachWith(int shift) { MultiReaderUnifiedSet<CollidingInt> set = MultiReaderUnifiedSet.newSet(); int size = 100000; for (int i = 0; i < size; i++) { Assert.assertTrue(set.add(new CollidingInt(i, shift))); } MutableList<CollidingInt> keys = FastList.newList(size); set.forEachWith( (key, s) -> { Assert.assertEquals("foo", s); keys.add(key); }, "foo"); Verify.assertSize(size, keys); Collections.sort(keys); for (int i = 0; i < size; i++) { Verify.assertItemAtIndex(new CollidingInt(i, shift), i, keys); } }