use of org.eclipse.collections.impl.math.IntegerSum in project eclipse-collections by eclipse.
the class MutableBagTestCase method forEachWithOccurrences.
@Test
public void forEachWithOccurrences() {
MutableBagIterable<Integer> bag = this.newWith();
bag.addOccurrences(1, 3);
bag.addOccurrences(2, 2);
bag.addOccurrences(3, 1);
IntegerSum sum = new IntegerSum(0);
bag.forEachWithOccurrences((each, index) -> sum.add(each * index));
Assert.assertEquals(10, sum.getIntSum());
bag.removeOccurrences(2, 1);
IntegerSum sum2 = new IntegerSum(0);
bag.forEachWithOccurrences((each, index) -> sum2.add(each * index));
Assert.assertEquals(8, sum2.getIntSum());
bag.removeOccurrences(1, 3);
IntegerSum sum3 = new IntegerSum(0);
bag.forEachWithOccurrences((each, index) -> sum3.add(each * index));
Assert.assertEquals(5, sum3.getIntSum());
}
use of org.eclipse.collections.impl.math.IntegerSum in project eclipse-collections by eclipse.
the class AbstractMutableSortedBagTestCase method forEachWithOccurrences.
@Override
@Test
public void forEachWithOccurrences() {
super.forEachWithOccurrences();
MutableSortedBag<Integer> bag = this.newWith(Collections.reverseOrder(), 3, 3, 3, 2, 2, 1);
MutableList<Integer> actualItems = FastList.newList();
MutableList<Integer> actualIndexes = FastList.newList();
bag.forEachWithOccurrences((each, index) -> {
actualItems.add(each);
actualIndexes.add(index);
});
Assert.assertEquals(FastList.newListWith(3, 2, 1), actualItems);
Assert.assertEquals(FastList.newListWith(3, 2, 1), actualIndexes);
MutableSortedBag<Integer> bag2 = this.newWith();
bag2.addOccurrences(1, 10);
bag2.addOccurrences(2, 10);
bag2.addOccurrences(3, 10);
IntegerSum sum = new IntegerSum(0);
Counter counter = new Counter();
bag2.forEachWithOccurrences((each, occurrences) -> {
counter.increment();
sum.add(each * occurrences * counter.getCount());
});
Assert.assertEquals(140, sum.getIntSum());
bag2.removeOccurrences(2, 1);
IntegerSum sum2 = new IntegerSum(0);
bag2.forEachWithOccurrences((each, occurrences) -> sum2.add(each * occurrences));
Assert.assertEquals(58, sum2.getIntSum());
bag2.removeOccurrences(1, 3);
IntegerSum sum3 = new IntegerSum(0);
bag2.forEachWithOccurrences((each, occurrences) -> sum3.add(each * occurrences));
Assert.assertEquals(55, sum3.getIntSum());
}
use of org.eclipse.collections.impl.math.IntegerSum in project eclipse-collections by eclipse.
the class AbstractMemoryEfficientMutableMapTest method injectInto.
@Test
public void injectInto() {
MutableMap<String, Integer> map = this.newMapWithKeysValues("1", 1, "2", 2, "3", 3);
Integer expectedInteger;
IntegerSum expectedSum;
switch(map.size()) {
case 1:
expectedSum = new IntegerSum(1);
expectedInteger = Integer.valueOf(1);
break;
case 2:
expectedSum = new IntegerSum(3);
expectedInteger = Integer.valueOf(3);
break;
case 3:
expectedSum = new IntegerSum(6);
expectedInteger = Integer.valueOf(6);
break;
default:
expectedSum = new IntegerSum(0);
expectedInteger = Integer.valueOf(0);
break;
}
Integer actual = map.injectInto(0, AddFunction.INTEGER);
Assert.assertEquals(expectedInteger, actual);
Sum sum = map.injectInto(new IntegerSum(0), SumProcedure.number());
Assert.assertEquals(expectedSum, sum);
}
use of org.eclipse.collections.impl.math.IntegerSum in project eclipse-collections by eclipse.
the class UnifiedMapTest 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 UnifiedMapTest 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