@Test public void sortOnListWithLessThan10Elements() { ArrayList<Integer> integers = this.newArrayList(2, 3, 4, 1, 5, 7, 6, 9, 8); Verify.assertStartsWith(ArrayListIterate.sortThis(integers), 1, 2, 3, 4, 5, 6, 7, 8, 9); ArrayList<Integer> integers2 = this.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9); Verify.assertStartsWith( ArrayListIterate.sortThis(integers2, Collections.<Integer>reverseOrder()), 9, 8, 7, 6, 5, 4, 3, 2, 1); ArrayList<Integer> integers3 = this.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9); Verify.assertStartsWith(ArrayListIterate.sortThis(integers3), 1, 2, 3, 4, 5, 6, 7, 8, 9); ArrayList<Integer> integers4 = this.newArrayList(9, 8, 7, 6, 5, 4, 3, 2, 1); Verify.assertStartsWith(ArrayListIterate.sortThis(integers4), 1, 2, 3, 4, 5, 6, 7, 8, 9); ThisIsNotAnArrayList<Integer> arrayListThatIsnt = new ThisIsNotAnArrayList<Integer>(FastList.newListWith(9, 8, 7, 6, 5, 4, 3, 2, 1)); Verify.assertStartsWith( ArrayListIterate.sortThis(arrayListThatIsnt), 1, 2, 3, 4, 5, 6, 7, 8, 9); }
@Test public void forEachWithIndexUsingFromToWithOptimisableList() { ArrayList<Integer> optimisableList = Interval.oneTo(105).addAllTo(new ArrayList<Integer>()); final ArrayList<Integer> expected = Interval.oneTo(105).addAllTo(new ArrayList<Integer>()); ArrayList<Integer> results = new ArrayList<Integer>(); ArrayListIterate.forEachWithIndex( optimisableList, 0, 104, ObjectIntProcedures.fromProcedure(CollectionAddProcedure.on(results))); Assert.assertEquals(expected, results); MutableList<Integer> reverseResults = Lists.mutable.of(); final ObjectIntProcedure<Integer> objectIntProcedure = ObjectIntProcedures.fromProcedure(CollectionAddProcedure.on(reverseResults)); ArrayListIterate.forEachWithIndex(expected, 104, 0, objectIntProcedure); Assert.assertEquals(ListIterate.reverseThis(expected), reverseResults); Verify.assertThrows( IndexOutOfBoundsException.class, new Runnable() { public void run() { ArrayListIterate.forEachWithIndex(expected, 104, -1, objectIntProcedure); } }); Verify.assertThrows( IndexOutOfBoundsException.class, new Runnable() { public void run() { ArrayListIterate.forEachWithIndex(expected, -1, 104, objectIntProcedure); } }); }
@Test public void forEachUsingFromTo() { final ArrayList<Integer> integers = Interval.oneTo(5).addAllTo(new ArrayList<Integer>()); ArrayList<Integer> results = new ArrayList<Integer>(); ArrayListIterate.forEach(integers, 0, 4, CollectionAddProcedure.on(results)); Assert.assertEquals(integers, results); MutableList<Integer> reverseResults = Lists.mutable.of(); final CollectionAddProcedure<Integer> procedure = CollectionAddProcedure.on(reverseResults); ArrayListIterate.forEach(integers, 4, 0, procedure); Assert.assertEquals(ListIterate.reverseThis(integers), reverseResults); Verify.assertThrows( IndexOutOfBoundsException.class, new Runnable() { public void run() { ArrayListIterate.forEach(integers, 4, -1, procedure); } }); Verify.assertThrows( IndexOutOfBoundsException.class, new Runnable() { public void run() { ArrayListIterate.forEach(integers, -1, 4, procedure); } }); }
@Test public void countWithOver100() { ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101)); Assert.assertEquals( 101, ArrayListIterate.countWith(list, Predicates2.instanceOf(), Integer.class)); Assert.assertEquals( 0, ArrayListIterate.countWith(list, Predicates2.instanceOf(), Double.class)); }
@Test public void anySatisfyWith() { ArrayList<Integer> list = this.getIntegerList(); Assert.assertTrue( ArrayListIterate.anySatisfyWith(list, Predicates2.instanceOf(), Integer.class)); Assert.assertFalse( ArrayListIterate.anySatisfyWith(list, Predicates2.instanceOf(), Double.class)); }
@Test public void detectWithIfNone() { ArrayList<Integer> list = this.getIntegerList(); Assert.assertEquals( Integer.valueOf(7), ArrayListIterate.detectWithIfNone(list, Predicates2.equal(), 6, 7)); Assert.assertEquals( Integer.valueOf(2), ArrayListIterate.detectWithIfNone(list, Predicates2.equal(), 2, 7)); }
@Test public void anySatisfyWithOver100() { ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101)); Assert.assertTrue( ArrayListIterate.anySatisfyWith(list, Predicates2.instanceOf(), Integer.class)); Assert.assertFalse( ArrayListIterate.anySatisfyWith(list, Predicates2.instanceOf(), Double.class)); }
@Test public void allSatisfyWith() { ArrayList<Integer> list = this.getIntegerList(); Assert.assertTrue( ArrayListIterate.allSatisfyWith(list, Predicates2.instanceOf(), Integer.class)); Predicate2<Integer, Integer> greaterThanPredicate = Predicates2.greaterThan(); Assert.assertFalse(ArrayListIterate.allSatisfyWith(list, greaterThanPredicate, 2)); }
@Test public void countWith() { ArrayList<Integer> list = this.getIntegerList(); Assert.assertEquals( 5, ArrayListIterate.countWith(list, Predicates2.instanceOf(), Integer.class)); Assert.assertEquals( 0, ArrayListIterate.countWith(list, Predicates2.instanceOf(), Double.class)); }
@Test public void noneSatisfyWithOver100() { ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101)); Assert.assertFalse( ArrayListIterate.noneSatisfyWith(list, Predicates2.instanceOf(), Integer.class)); Predicate2<Integer, Integer> greaterThanPredicate = Predicates2.greaterThan(); Assert.assertTrue(ArrayListIterate.noneSatisfyWith(list, greaterThanPredicate, 150)); }
@Test public void detect() { ArrayList<Integer> list = this.getIntegerList(); Assert.assertEquals(Integer.valueOf(1), ArrayListIterate.detect(list, Predicates.equal(1))); //noinspection CachedNumberConstructorCall,UnnecessaryBoxing ArrayList<Integer> list2 = this.newArrayList(1, new Integer(2), 2); // test relies on having a unique instance of "2" Assert.assertSame(list2.get(1), ArrayListIterate.detect(list2, Predicates.equal(2))); }
@Test public void distinct() { ArrayList<Integer> list = new ArrayList<Integer>(); list.addAll(FastList.newListWith(9, 4, 7, 7, 5, 6, 2, 4)); List<Integer> result = ArrayListIterate.distinct(list); Verify.assertListsEqual(FastList.newListWith(9, 4, 7, 5, 6, 2), result); ArrayList<Integer> target = new ArrayList<Integer>(); ArrayListIterate.distinct(list, target); Verify.assertListsEqual(FastList.newListWith(9, 4, 7, 5, 6, 2), target); Verify.assertSize(8, list); }
@Test public void drop() { ArrayList<Integer> list = this.getIntegerList(); ArrayList<Integer> results = ArrayListIterate.drop(list, 2); Assert.assertEquals(FastList.newListWith(3, 2, 1), results); Verify.assertSize(0, ArrayListIterate.drop(list, 5)); Verify.assertSize(0, ArrayListIterate.drop(list, 6)); Verify.assertSize(5, ArrayListIterate.drop(list, 0)); Verify.assertSize(0, ArrayListIterate.drop(new ArrayList<Integer>(), 2)); }
@Test public void copyToArray() { ThisIsNotAnArrayList<Integer> notAnArrayList = this.newNotAnArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Integer[] target1 = {1, 2, null, null}; ArrayListIterate.toArray(notAnArrayList, target1, 2, 2); Assert.assertArrayEquals(target1, new Integer[] {1, 2, 1, 2}); ArrayList<Integer> arrayList = this.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Integer[] target2 = {1, 2, null, null}; ArrayListIterate.toArray(arrayList, target2, 2, 2); Assert.assertArrayEquals(target2, new Integer[] {1, 2, 1, 2}); }
@Test public void take() { ArrayList<Integer> list = this.getIntegerList(); Assert.assertEquals(FastList.newListWith(5, 4), ArrayListIterate.take(list, 2)); Verify.assertSize(0, ArrayListIterate.take(list, 0)); Verify.assertSize(5, ArrayListIterate.take(list, 5)); Verify.assertSize(0, ArrayListIterate.take(new ArrayList<Integer>(), 2)); ArrayList<Integer> list1 = new ArrayList<Integer>(130); list1.addAll(Interval.oneTo(120)); Verify.assertListsEqual(Interval.oneTo(120), ArrayListIterate.take(list1, 125)); }
@Test public void reverseForEach() { ArrayList<Integer> integers = Interval.oneTo(5).addAllTo(new ArrayList<Integer>()); MutableList<Integer> reverseResults = Lists.mutable.of(); ArrayListIterate.reverseForEach(integers, CollectionAddProcedure.on(reverseResults)); Assert.assertEquals(ListIterate.reverseThis(integers), reverseResults); }
@Test public void reverseForEach_emptyList() { ArrayList<Integer> integers = new ArrayList<Integer>(); MutableList<Integer> results = Lists.mutable.of(); ArrayListIterate.reverseForEach(integers, CollectionAddProcedure.on(results)); Assert.assertEquals(integers, results); }
@Test public void collectOver100() { ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101)); ArrayList<Class<?>> newCollection = ArrayListIterate.collect(list, Functions.getToClass()); Verify.assertSize(101, newCollection); Verify.assertContains(Integer.class, newCollection); }
@Test public void select() { ArrayList<Integer> list = this.getIntegerList(); ArrayList<Integer> results = ArrayListIterate.select(list, Predicates.instanceOf(Integer.class)); Verify.assertSize(5, results); }
@Test public void selectOver100() { ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101)); ArrayList<Integer> results = ArrayListIterate.select(list, Predicates.instanceOf(Integer.class)); Verify.assertSize(101, results); }
@Test public void rejectWith() { ArrayList<Integer> list = this.getIntegerList(); ArrayList<Integer> results = ArrayListIterate.rejectWith(list, Predicates2.instanceOf(), Integer.class); Verify.assertEmpty(results); }
@Test public void selectInstancesOfOver100() { ArrayList<Number> list = new ArrayList<Number>(Interval.oneTo(101)); list.add(102.0); MutableList<Double> results = ArrayListIterate.selectInstancesOf(list, Double.class); Assert.assertEquals(iList(102.0), results); }
@Test public void rejectWithOver100() { ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101)); ArrayList<Integer> results = ArrayListIterate.rejectWith(list, Predicates2.instanceOf(), Integer.class); Verify.assertEmpty(results); }
@Test public void sortingWitoutAccessToInternalArray() { ThisIsNotAnArrayList<Integer> arrayListThatIsnt = new ThisIsNotAnArrayList<Integer>(FastList.newListWith(5, 3, 4, 1, 2)); Verify.assertStartsWith( ArrayListIterate.sortThis(arrayListThatIsnt, Comparators.naturalOrder()), 1, 2, 3, 4, 5); }
@Test public void detectIndexSmallList() { ArrayList<Integer> list = new ArrayList<Integer>(Interval.toReverseList(1, 5)); Assert.assertEquals(4, ArrayListIterate.detectIndex(list, Predicates.equal(1))); Assert.assertEquals(0, Iterate.detectIndex(list, Predicates.equal(5))); Assert.assertEquals(-1, Iterate.detectIndex(list, Predicates.equal(10))); }
@Test public void collectIfOver100() { ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101)); ArrayList<Class<?>> result = ArrayListIterate.collectIf(list, Predicates.equal(101), Functions.getToClass()); Assert.assertEquals(FastList.newListWith(Integer.class), result); }
@Test public void detectIndexOver100() { ArrayList<Integer> list = new ArrayList<Integer>(Interval.toReverseList(1, 101)); Assert.assertEquals(100, ArrayListIterate.detectIndex(list, Predicates.equal(1))); Assert.assertEquals(0, Iterate.detectIndex(list, Predicates.equal(101))); Assert.assertEquals(-1, Iterate.detectIndex(list, Predicates.equal(200))); }
@Test public void partitionOver100() { ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101)); PartitionMutableList<Integer> result = ArrayListIterate.partition(list, Predicates.in(Lists.immutable.of(1))); Verify.assertSize(1, result.getSelected()); Verify.assertSize(100, result.getRejected()); }
@Test public void groupByWithOptimisedList() { ArrayList<Integer> list = new ArrayList<Integer>(Interval.toReverseList(1, 105)); MutableMultimap<String, Integer> target = new FastListMultimap<String, Integer>(); MutableMultimap<String, Integer> result = ArrayListIterate.groupBy(list, Functions.getToString(), target); Assert.assertEquals(result.get("105"), FastList.newListWith(105)); }
@Test public void selectAndRejectWithOver100() { ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101)); Twin<MutableList<Integer>> result = ArrayListIterate.selectAndRejectWith(list, Predicates2.in(), Lists.immutable.of(1)); Verify.assertSize(1, result.getOne()); Verify.assertSize(100, result.getTwo()); }