@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 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());
  }
 @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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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());
 }
Exemplo n.º 19
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 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()));
 }
Exemplo n.º 22
0
  @Test
  public void toSortedMap() {
    MutableSortedMap<Integer, String> map =
        this.newBag().toSortedMap(Integer::valueOf, Functions.<String>getPassThru());

    Verify.assertMapsEqual(
        this.newBag().toMap(Integer::valueOf, Functions.<String>getPassThru()), map);
    Verify.assertListsEqual(Interval.oneTo(this.numKeys()), map.keySet().toList());
  }
  @Test
  public void flatCollect() {
    ImmutableMap<Integer, Integer> map = this.newMapWithKeysValues(1, 1, 2, 2, 3, 3, 4, 4);

    if (map.isEmpty()) {
      Function<Integer, Iterable<Object>> fail =
          each -> {
            throw new AssertionError();
          };
      Assert.assertEquals(Bags.immutable.empty(), map.flatCollect(fail));
      Assert.assertEquals(Bags.immutable.empty(), map.flatCollect(fail, HashBag.newBag()));
    } else {
      MutableBag<Integer> expected =
          Interval.oneTo(map.size()).flatCollect(each -> Interval.oneTo(each)).toBag();
      Assert.assertEquals(expected, map.flatCollect(each -> Interval.oneTo(each)));
      Assert.assertEquals(
          expected, map.flatCollect(each -> Interval.oneTo(each), HashBag.newBag()));
    }
  }
 @Before
 public void setUp() {
   Interval interval = Interval.oneTo(20000);
   this.iterables =
       Lists.immutable.of(
           interval.toList(),
           interval.toList().asUnmodifiable(),
           interval.toList().asSynchronized(),
           interval.toList().toImmutable(),
           interval.toSet(),
           interval.toSet().asUnmodifiable(),
           interval.toSet().asSynchronized(),
           interval.toSet().toImmutable(),
           interval.toBag(),
           interval.toBag().asUnmodifiable(),
           interval.toBag().asSynchronized(),
           interval.toBag().toImmutable(),
           interval.toSortedSet(),
           interval.toSortedSet().asUnmodifiable(),
           interval.toSortedSet().asSynchronized(),
           interval.toSortedSet().toImmutable(),
           interval.toMap(Functions.<Integer>getPassThru(), Functions.<Integer>getPassThru()),
           interval
               .toMap(Functions.<Integer>getPassThru(), Functions.<Integer>getPassThru())
               .asUnmodifiable(),
           interval
               .toMap(Functions.<Integer>getPassThru(), Functions.<Integer>getPassThru())
               .asSynchronized(),
           interval
               .toMap(Functions.<Integer>getPassThru(), Functions.<Integer>getPassThru())
               .toImmutable(),
           ArrayListAdapter.<Integer>newList().withAll(interval),
           ArrayListAdapter.<Integer>newList().withAll(interval).asUnmodifiable(),
           ArrayListAdapter.<Integer>newList().withAll(interval).asSynchronized(),
           new CompositeFastList<Integer>().withAll(interval.toList()),
           new CompositeFastList<Integer>().withAll(interval.toList()).asUnmodifiable(),
           new CompositeFastList<Integer>().withAll(interval.toList()).asSynchronized(),
           new CompositeFastList<Integer>().withAll(interval.toList()).toImmutable(),
           ListAdapter.adapt(new LinkedList<Integer>()).withAll(interval),
           ListAdapter.adapt(new LinkedList<Integer>()).withAll(interval).asUnmodifiable(),
           ListAdapter.adapt(new LinkedList<Integer>()).withAll(interval).asSynchronized(),
           UnifiedSetWithHashingStrategy.<Integer>newSet(HashingStrategies.defaultStrategy())
               .withAll(interval),
           UnifiedSetWithHashingStrategy.<Integer>newSet(HashingStrategies.defaultStrategy())
               .withAll(interval)
               .asUnmodifiable(),
           UnifiedSetWithHashingStrategy.<Integer>newSet(HashingStrategies.defaultStrategy())
               .withAll(interval)
               .asSynchronized(),
           UnifiedSetWithHashingStrategy.<Integer>newSet(HashingStrategies.defaultStrategy())
               .withAll(interval)
               .toImmutable());
 }
 @Test
 public void forEachWithIndexOver100() {
   ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101));
   Iterate.sortThis(list);
   ArrayListIterate.forEachWithIndex(
       list,
       new ObjectIntProcedure<Integer>() {
         public void value(Integer object, int index) {
           Assert.assertEquals(index, object - 1);
         }
       });
 }
 @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 reverseThis() {
   super.reverseThis();
   CompositeFastList<Integer> composite = new CompositeFastList<Integer>();
   composite.addAll(FastList.newListWith(9, 8, 7));
   composite.addAll(FastList.newListWith(6, 5, 4));
   composite.addAll(FastList.newListWith(3, 2, 1));
   CompositeFastList<Integer> reversed = composite.reverseThis();
   Assert.assertSame(composite, reversed);
   Assert.assertEquals(Interval.oneTo(9), reversed);
 }
 @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);
 }
 @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);
 }