use of org.eclipse.collections.impl.math.IntegerSum 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());
}
use of org.eclipse.collections.impl.math.IntegerSum in project eclipse-collections by eclipse.
the class UnifiedSetWithHashingStrategyTest method lazySelectForEach.
/**
* @deprecated since 3.0.
*/
@Deprecated
@Test
public void lazySelectForEach() {
UnifiedSetWithHashingStrategy<Integer> integers = UnifiedSetWithHashingStrategy.newSetWith(INTEGER_HASHING_STRATEGY, 1, 2, 3, 4, 5);
LazyIterable<Integer> select = integers.lazySelect(Predicates.lessThan(5));
Sum sum = new IntegerSum(0);
select.forEach(new SumProcedure<>(sum));
Assert.assertEquals(10, sum.getValue().intValue());
}
use of org.eclipse.collections.impl.math.IntegerSum in project eclipse-collections by eclipse.
the class UnifiedMapWithHashingStrategyTest method batchIterable_forEachNullHandling.
private void batchIterable_forEachNullHandling(BatchIterable<Integer> batchIterable, int expectedValue) {
// Testing forEach handling null keys and null values
Sum sum = new IntegerSum(0);
batchIterable.forEach(each -> sum.add(each == null ? 0 : each));
Assert.assertEquals(expectedValue, sum.getValue());
}
use of org.eclipse.collections.impl.math.IntegerSum in project eclipse-collections by eclipse.
the class UnifiedMapWithHashingStrategyTest method batchIterable_forEach.
private void batchIterable_forEach(BatchIterable<Integer> batchIterable, int expectedValue) {
IntegerSum sum = new IntegerSum(0);
batchIterable.forEach(new SumProcedure<>(sum));
Assert.assertEquals(expectedValue, sum.getValue());
}
use of org.eclipse.collections.impl.math.IntegerSum in project eclipse-collections by eclipse.
the class UnifiedMapWithHashingStrategyTest method batchForEachTestCases.
private void batchForEachTestCases(BatchIterable<Integer> batchIterable, int expectedValue) {
// Testing batch size of 1 to 16 with no chains
for (int sectionCount = 1; sectionCount <= 16; ++sectionCount) {
Sum sum = new IntegerSum(0);
for (int sectionIndex = 0; sectionIndex < sectionCount; ++sectionIndex) {
batchIterable.batchForEach(new SumProcedure<>(sum), sectionIndex, sectionCount);
}
Assert.assertEquals(expectedValue, sum.getValue());
}
}
Aggregations