Search in sources :

Example 41 with IntegerSum

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());
}
Also used : IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Test(org.junit.Test)

Example 42 with IntegerSum

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Counter(org.eclipse.collections.impl.Counter) Test(org.junit.Test)

Example 43 with IntegerSum

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);
}
Also used : IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Test(org.junit.Test)

Example 44 with IntegerSum

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());
}
Also used : IntegerSum(org.eclipse.collections.impl.math.IntegerSum)

Example 45 with IntegerSum

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());
    }
}
Also used : IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum)

Aggregations

IntegerSum (org.eclipse.collections.impl.math.IntegerSum)93 Sum (org.eclipse.collections.impl.math.Sum)85 Test (org.junit.Test)79 BatchIterable (org.eclipse.collections.impl.parallel.BatchIterable)20 Map (java.util.Map)16 MutableMap (org.eclipse.collections.api.map.MutableMap)16 ObjectIntProcedure (org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure)10 FastList (org.eclipse.collections.impl.list.mutable.FastList)10 Assert (org.junit.Assert)10 Interval (org.eclipse.collections.impl.list.Interval)9 UnifiedMap (org.eclipse.collections.impl.map.mutable.UnifiedMap)9 HashMap (java.util.HashMap)8 ImmutableEntry (org.eclipse.collections.impl.tuple.ImmutableEntry)8 LazyIterable (org.eclipse.collections.api.LazyIterable)7 Predicates (org.eclipse.collections.impl.block.factory.Predicates)7 BigInteger (java.math.BigInteger)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Procedure2 (org.eclipse.collections.api.block.procedure.Procedure2)6 Verify (org.eclipse.collections.impl.test.Verify)6 ArrayList (java.util.ArrayList)4