@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 testForEachUsingMap() {
    // Test the default batch size calculations
    IntegerSum sum1 = new IntegerSum(0);
    MutableMap<String, Integer> map1 =
        Interval.fromTo(1, 10000).toMap(String::valueOf, Functions.getIntegerPassThru());
    ParallelIterate.forEach(map1, new SumProcedure(sum1), new SumCombiner(sum1));
    Assert.assertEquals(50005000, sum1.getSum());

    // Testing batch size 1
    IntegerSum sum2 = new IntegerSum(0);
    UnifiedMap<String, Integer> map2 =
        (UnifiedMap<String, Integer>)
            Interval.fromTo(1, 100).toMap(String::valueOf, Functions.getIntegerPassThru());
    ParallelIterate.forEach(
        map2, new SumProcedure(sum2), new SumCombiner(sum2), 1, map2.getBatchCount(map2.size()));
    Assert.assertEquals(5050, sum2.getSum());

    // Testing an uneven batch size
    IntegerSum sum3 = new IntegerSum(0);
    UnifiedMap<String, Integer> set3 =
        (UnifiedMap<String, Integer>)
            Interval.fromTo(1, 100).toMap(String::valueOf, Functions.getIntegerPassThru());
    ParallelIterate.forEach(
        set3, new SumProcedure(sum3), new SumCombiner(sum3), 1, set3.getBatchCount(13));
    Assert.assertEquals(5050, sum3.getSum());
  }
  @Test
  public void testForEachUsingSet() {
    // Tests the default batch size calculations
    IntegerSum sum = new IntegerSum(0);
    MutableSet<Integer> set = Interval.toSet(1, 10000);
    ParallelIterate.forEach(set, new SumProcedure(sum), new SumCombiner(sum));
    Assert.assertEquals(50005000, sum.getSum());

    // Testing batch size 1
    IntegerSum sum2 = new IntegerSum(0);
    UnifiedSet<Integer> set2 = UnifiedSet.newSet(Interval.oneTo(100));
    ParallelIterate.forEach(
        set2, new SumProcedure(sum2), new SumCombiner(sum2), 1, set2.getBatchCount(set2.size()));
    Assert.assertEquals(5050, sum2.getSum());

    // Testing an uneven batch size
    IntegerSum sum3 = new IntegerSum(0);
    UnifiedSet<Integer> set3 = UnifiedSet.newSet(Interval.oneTo(100));
    ParallelIterate.forEach(
        set3, new SumProcedure(sum3), new SumCombiner(sum3), 1, set3.getBatchCount(13));
    Assert.assertEquals(5050, sum3.getSum());

    // Testing divideByZero exception by passing 1 as batchSize
    IntegerSum sum4 = new IntegerSum(0);
    UnifiedSet<Integer> set4 = UnifiedSet.newSet(Interval.oneTo(100));
    ParallelIterate.forEach(set4, new SumProcedure(sum4), new SumCombiner(sum4), 1);
    Assert.assertEquals(5050, sum4.getSum());
  }
 @Test
 public void updateValue() {
   MutableMapIterable<Integer, Integer> map = this.newMap();
   Iterate.forEach(
       Interval.oneTo(1000), each -> map.updateValue(each % 10, () -> 0, integer -> integer + 1));
   Assert.assertEquals(Interval.zeroTo(9).toSet(), map.keySet());
   Assert.assertEquals(
       FastList.newList(Collections.nCopies(10, 100)), FastList.newList(map.values()));
 }
 @Before
 public void setUp() {
   this.dropIterable = new DropIterable<>(Interval.oneTo(5), 2);
   this.emptyListDropIterable = new DropIterable<>(FastList.<Integer>newList(), 2);
   this.zeroCountDropIterable = new DropIterable<>(Interval.oneTo(5), 0);
   this.nearCountDropIterable = new DropIterable<>(Interval.oneTo(5), 4);
   this.sameCountDropIterable = new DropIterable<>(Interval.oneTo(5), 5);
   this.higherCountDropIterable = new DropIterable<>(Interval.oneTo(5), 6);
 }
 @Test
 public void updateValue_collisions() {
   MutableMapIterable<Integer, Integer> map = this.newMap();
   MutableList<Integer> list = Interval.oneTo(2000).toList();
   Collections.shuffle(list);
   Iterate.forEach(list, each -> map.updateValue(each % 1000, () -> 0, integer -> integer + 1));
   Assert.assertEquals(Interval.zeroTo(999).toSet(), map.keySet());
   Assert.assertEquals(
       HashBag.newBag(map.values()).toStringOfItemToCount(),
       FastList.newList(Collections.nCopies(1000, 2)),
       FastList.newList(map.values()));
 }
 @Override
 @Test
 public void forEach() {
   super.forEach();
   MutableList<Integer> list = FastList.newList();
   CompositeFastList<Integer> iterables = new CompositeFastList<Integer>();
   iterables.addComposited(Interval.oneTo(5).toList());
   iterables.addComposited(Interval.fromTo(6, 10).toList());
   iterables.forEach(CollectionAddProcedure.on(list));
   Verify.assertSize(10, list);
   Verify.assertAllSatisfy(list, Predicates.greaterThan(0).and(Predicates.lessThan(11)));
 }
 @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);
 }
  @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 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 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 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 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 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 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 detectIndexWithSmallList() {
   ArrayList<Integer> list = new ArrayList<Integer>(Interval.toReverseList(1, 5));
   Assert.assertEquals(4, Iterate.detectIndexWith(list, Predicates2.equal(), 1));
   Assert.assertEquals(0, Iterate.detectIndexWith(list, Predicates2.equal(), 5));
   Assert.assertEquals(-1, Iterate.detectIndexWith(list, Predicates2.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 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 groupByEach() {
    MutableMap<String, Integer> map = this.newMapWithKeysValues("1", 1, "2", 2, "3", 3);

    NegativeIntervalFunction function = new NegativeIntervalFunction();
    MutableMultimap<Integer, Integer> expected = FastListMultimap.newMultimap();
    for (int i = 1; i < map.size(); i++) {
      expected.putAll(-i, Interval.fromTo(i, map.size()));
    }

    Multimap<Integer, Integer> actual = map.groupByEach(function);
    expected.forEachKey(
        each -> {
          Assert.assertTrue(actual.containsKey(each));
          MutableList<Integer> values = actual.get(each).toList();
          Verify.assertNotEmpty(values);
          Assert.assertTrue(expected.get(each).containsAllIterable(values));
        });

    Multimap<Integer, Integer> actualFromTarget =
        map.groupByEach(function, FastListMultimap.<Integer, Integer>newMultimap());
    expected.forEachKey(
        each -> {
          Assert.assertTrue(actualFromTarget.containsKey(each));
          MutableList<Integer> values = actualFromTarget.get(each).toList();
          Verify.assertNotEmpty(values);
          Assert.assertTrue(expected.get(each).containsAllIterable(values));
        });
  }
 @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));
 }
Пример #22
0
 @Test
 public void toSet() {
   MutableSet<String> expectedSet =
       this.numKeys() == 0
           ? UnifiedSet.<String>newSet()
           : Interval.oneTo(this.numKeys()).collect(String::valueOf).toSet();
   Assert.assertEquals(expectedSet, this.newBag().toSet());
 }
 @Test
 public void forEachOver100() {
   ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101));
   Iterate.sortThis(list);
   FastList<Integer> result = FastList.newList(101);
   ArrayListIterate.forEach(list, CollectionAddProcedure.<Integer>on(result));
   Verify.assertListsEqual(list, result);
 }
 @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());
 }
 @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 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 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 testForEachWithIndexWithFromToWithCommandoPatternOptimization() {
   MutableList<Integer> result2 = Lists.mutable.of();
   // Requires list of 100+ elements to engage commando pattern optimization
   ArrayListAdapter.adapt(new ArrayList<>(Interval.oneTo(200)))
       .forEachWithIndex(99, 199, new AddToList(result2));
   Verify.assertSize(101, result2);
 }
 @Test
 public void testForEachWithIndexToArrayUsingArrayList() {
   Integer[] array = new Integer[200];
   List<Integer> list = new ArrayList<>(Interval.oneTo(200));
   Assert.assertTrue(ArrayIterate.allSatisfy(array, Predicates.isNull()));
   ParallelIterate.forEachWithIndex(list, (each, index) -> array[index] = each, 10, 10);
   Assert.assertArrayEquals(array, list.toArray(new Integer[] {}));
 }
 @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));
 }