use of org.eclipse.collections.impl.math.Sum in project eclipse-collections by eclipse.
the class LazyIterateTest method selectIterator.
@Test
public void selectIterator() {
LazyIterable<Integer> select = LazyIterate.select(Interval.oneTo(5), Predicates.lessThan(5));
Sum sum = new IntegerSum(0);
for (Integer each : select) {
sum.add(each);
}
Assert.assertEquals(10, sum.getValue().intValue());
}
use of org.eclipse.collections.impl.math.Sum in project eclipse-collections by eclipse.
the class IterableIterateTest method forEachWith.
@Test
public void forEachWith() {
Sum result = new IntegerSum(0);
Iterable<Integer> integers = new IterableAdapter<>(Interval.oneTo(5));
Iterate.forEachWith(integers, (each, parm) -> result.add(each.intValue() * parm.intValue()), 2);
Assert.assertEquals(30, result.getValue().intValue());
}
use of org.eclipse.collections.impl.math.Sum in project eclipse-collections by eclipse.
the class IterableIterateTest method injectIntoWith.
@Test
public void injectIntoWith() {
Sum result = new IntegerSum(0);
Iterable<Integer> iterable = new IterableAdapter<>(Interval.oneTo(5));
Function3<Sum, Integer, Integer, Sum> function = (sum, element, withValue) -> sum.add(element.intValue() * withValue.intValue());
Sum sumOfDoubledValues = Iterate.injectIntoWith(result, iterable, function, 2);
Assert.assertEquals(30, sumOfDoubledValues.getValue().intValue());
}
use of org.eclipse.collections.impl.math.Sum in project eclipse-collections by eclipse.
the class ImmutableUnifiedSetWithHashingStrategyTest method batchForEach.
@Test
public void batchForEach() {
Sum sum = new IntegerSum(0);
BatchIterable<Integer> integerBatchIterable = (BatchIterable<Integer>) this.newSet(1, 2, 3, 4, 5);
integerBatchIterable.batchForEach(new SumProcedure<>(sum), 0, 1);
Assert.assertEquals(15, sum.getValue());
}
use of org.eclipse.collections.impl.math.Sum in project eclipse-collections by eclipse.
the class UnifiedSetTest method batchForEach.
@Test
public void batchForEach() {
// Testing batch size of 1 to 16 with no chains
UnifiedSet<Integer> set = UnifiedSet.<Integer>newSet(10).with(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
for (int sectionCount = 1; sectionCount <= 16; ++sectionCount) {
Sum sum = new IntegerSum(0);
for (int sectionIndex = 0; sectionIndex < sectionCount; ++sectionIndex) {
set.batchForEach(new SumProcedure<>(sum), sectionIndex, sectionCount);
}
Assert.assertEquals(55, sum.getValue());
}
// Testing 1 batch with chains
Sum sum2 = new IntegerSum(0);
UnifiedSet<Integer> set2 = UnifiedSet.<Integer>newSet(3).with(COLLISION_1, COLLISION_2, COLLISION_3, 1, 2);
int numBatches = set2.getBatchCount(100);
for (int i = 0; i < numBatches; ++i) {
set2.batchForEach(new SumProcedure<>(sum2), i, numBatches);
}
Assert.assertEquals(1, numBatches);
Assert.assertEquals(54, sum2.getValue());
// Testing batch size of 3 with chains and uneven last batch
Sum sum3 = new IntegerSum(0);
UnifiedSet<Integer> set3 = UnifiedSet.<Integer>newSet(4, 1.0F).with(COLLISION_1, COLLISION_2, 1, 2, 3, 4, 5);
int numBatches2 = set3.getBatchCount(3);
for (int i = 0; i < numBatches2; ++i) {
set3.batchForEach(new SumProcedure<>(sum3), i, numBatches2);
}
Assert.assertEquals(32, sum3.getValue());
// Test batchForEach on empty set, it should simply do nothing and not throw any exceptions
Sum sum4 = new IntegerSum(0);
UnifiedSet<Integer> set4 = UnifiedSet.newSet();
set4.batchForEach(new SumProcedure<>(sum4), 0, set4.getBatchCount(1));
Assert.assertEquals(0, sum4.getValue());
}
Aggregations