Search in sources :

Example 86 with IntegerSum

use of org.eclipse.collections.impl.math.IntegerSum in project eclipse-collections by eclipse.

the class UnifiedMapTest method batchForEachChains.

private void batchForEachChains(BatchIterable<Integer> batchIterable, int expectedValue) {
    // Testing 1 batch with chains
    Sum sum = new IntegerSum(0);
    // testing getBatchCount returns 1
    int numBatches = batchIterable.getBatchCount(100000);
    for (int i = 0; i < numBatches; ++i) {
        batchIterable.batchForEach(new SumProcedure<>(sum), i, numBatches);
    }
    Assert.assertEquals(1, numBatches);
    Assert.assertEquals(expectedValue, sum.getValue());
    // Testing 3 batches with chains and uneven last batch
    Sum sum2 = new IntegerSum(0);
    for (int i = 0; i < 5; ++i) {
        batchIterable.batchForEach(new SumProcedure<>(sum2), i, 5);
    }
    Assert.assertEquals(expectedValue, sum2.getValue());
}
Also used : IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum)

Example 87 with IntegerSum

use of org.eclipse.collections.impl.math.IntegerSum in project eclipse-collections by eclipse.

the class UnifiedMapTest method batchIterable_forEachEntry_chains.

@Test
public void batchIterable_forEachEntry_chains() {
    BatchIterable<Map.Entry<Integer, Integer>> collisions = (BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMap.<Integer, Integer>newMap(5).withKeysValues(COLLISION_1, 1, COLLISION_2, 2, COLLISION_3, 3, 1, 4).withKeysValues(2, 5, 3, 6).entrySet();
    Sum sum = new IntegerSum(0);
    collisions.forEach(new EntrySumProcedure(sum));
    Assert.assertEquals(78, 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) MutableMap(org.eclipse.collections.api.map.MutableMap) Map(java.util.Map) BatchIterable(org.eclipse.collections.impl.parallel.BatchIterable) Test(org.junit.Test)

Example 88 with IntegerSum

use of org.eclipse.collections.impl.math.IntegerSum in project eclipse-collections by eclipse.

the class UnifiedMapTest method batchIterable_forEachEntry_null_handling.

@Test
public void batchIterable_forEachEntry_null_handling() {
    // Testing batchForEach handling null keys and null values
    Sum sum = new IntegerSum(0);
    BatchIterable<Map.Entry<Integer, Integer>> nulls = (BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMap.<Integer, Integer>newMap(100).withKeysValues(null, 10, 1, null, 2, 11, 3, 12).withKeysValues(4, null, 5, null).entrySet();
    nulls.forEach(each -> {
        sum.add(each.getKey() == null ? 0 : each.getKey());
        sum.add(each.getValue() == null ? 0 : each.getValue());
    });
    Assert.assertEquals(48, 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) MutableMap(org.eclipse.collections.api.map.MutableMap) Map(java.util.Map) BatchIterable(org.eclipse.collections.impl.parallel.BatchIterable) Test(org.junit.Test)

Example 89 with IntegerSum

use of org.eclipse.collections.impl.math.IntegerSum in project eclipse-collections by eclipse.

the class UnifiedMapTest method batchForEachEntry_emptySet.

@Test
public void batchForEachEntry_emptySet() {
    // Test batchForEach on empty set, it should simply do nothing and not throw any exceptions
    Sum sum5 = new IntegerSum(0);
    BatchIterable<Map.Entry<Integer, Integer>> empty = (BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMap.newMap().entrySet();
    empty.batchForEach(new EntrySumProcedure(sum5), 0, empty.getBatchCount(1));
    Assert.assertEquals(0, sum5.getValue());
}
Also used : IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) MutableMap(org.eclipse.collections.api.map.MutableMap) Map(java.util.Map) BatchIterable(org.eclipse.collections.impl.parallel.BatchIterable) Test(org.junit.Test)

Example 90 with IntegerSum

use of org.eclipse.collections.impl.math.IntegerSum in project eclipse-collections by eclipse.

the class UnifiedMapTest method batchIterable_forEachEntry.

@Test
public void batchIterable_forEachEntry() {
    BatchIterable<Map.Entry<Integer, Integer>> entries = (BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMap.newWithKeysValues(1, 1, 2, 2, 3, 3, 4, 4).entrySet();
    Sum sum = new IntegerSum(0);
    entries.forEach(new EntrySumProcedure(sum));
    Assert.assertEquals(20, 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) MutableMap(org.eclipse.collections.api.map.MutableMap) Map(java.util.Map) BatchIterable(org.eclipse.collections.impl.parallel.BatchIterable) Test(org.junit.Test)

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