Search in sources :

Example 61 with IntegerSum

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

the class UnifiedMapWithHashingStrategyTest 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>>) UnifiedMapWithHashingStrategy.<Integer, Integer>newMap(INTEGER_HASHING_STRATEGY, 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 : ImmutableEntry(org.eclipse.collections.impl.tuple.ImmutableEntry) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) HashMap(java.util.HashMap) MutableMap(org.eclipse.collections.api.map.MutableMap) Map(java.util.Map) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) BatchIterable(org.eclipse.collections.impl.parallel.BatchIterable) Test(org.junit.Test)

Example 62 with IntegerSum

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

the class UnifiedMapWithHashingStrategyTest method batchForEachEntry.

@Test
public void batchForEachEntry() {
    // Testing batch size of 1 to 16 with no chains
    BatchIterable<Map.Entry<Integer, Integer>> entries = (BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMapWithHashingStrategy.newWithKeysValues(INTEGER_HASHING_STRATEGY, 1, 1, 2, 2, 3, 3, 4, 4).entrySet();
    for (int sectionCount = 1; sectionCount <= 16; ++sectionCount) {
        Sum sum = new IntegerSum(0);
        for (int sectionIndex = 0; sectionIndex < sectionCount; ++sectionIndex) {
            entries.batchForEach(new EntrySumProcedure(sum), sectionIndex, sectionCount);
        }
        Assert.assertEquals(20, sum.getValue());
    }
}
Also used : ImmutableEntry(org.eclipse.collections.impl.tuple.ImmutableEntry) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) HashMap(java.util.HashMap) MutableMap(org.eclipse.collections.api.map.MutableMap) Map(java.util.Map) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) BatchIterable(org.eclipse.collections.impl.parallel.BatchIterable) Test(org.junit.Test)

Example 63 with IntegerSum

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

the class UnifiedMapWithHashingStrategyTest method batchForEachEntry_null_handling.

@Test
public void batchForEachEntry_null_handling() {
    // Testing batchForEach handling null keys and null values
    Sum sum4 = new IntegerSum(0);
    BatchIterable<Map.Entry<Integer, Integer>> nulls = (BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMapWithHashingStrategy.<Integer, Integer>newMap(INTEGER_HASHING_STRATEGY, 100).withKeysValues(null, 10, 1, null, 2, 11, 3, 12).withKeysValues(4, null, 5, null).entrySet();
    int numBatches = nulls.getBatchCount(7);
    for (int i = 0; i < numBatches; ++i) {
        nulls.batchForEach(each -> {
            sum4.add(each.getKey() == null ? 1 : each.getKey());
            sum4.add(each.getValue() == null ? 1 : each.getValue());
        }, i, numBatches);
    }
    Assert.assertEquals(52, sum4.getValue());
}
Also used : ImmutableEntry(org.eclipse.collections.impl.tuple.ImmutableEntry) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) HashMap(java.util.HashMap) MutableMap(org.eclipse.collections.api.map.MutableMap) Map(java.util.Map) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) BatchIterable(org.eclipse.collections.impl.parallel.BatchIterable) Test(org.junit.Test)

Example 64 with IntegerSum

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

the class UnifiedSetWithHashingStrategyTest method lazyRejectForEach.

/**
 * @deprecated since 3.0.
 */
@Deprecated
@Test
public void lazyRejectForEach() {
    UnifiedSetWithHashingStrategy<Integer> integers = UnifiedSetWithHashingStrategy.newSetWith(INTEGER_HASHING_STRATEGY, 1, 2, 3, 4, 5);
    LazyIterable<Integer> select = integers.lazyReject(Predicates.lessThan(5));
    Sum sum = new IntegerSum(0);
    select.forEach(new SumProcedure<>(sum));
    Assert.assertEquals(5L, sum.getValue().intValue());
}
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 65 with IntegerSum

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

the class UnifiedSetWithHashingStrategyTest 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);
    UnifiedSetWithHashingStrategy<Integer> set2 = UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, 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);
    UnifiedSetWithHashingStrategy<Integer> set3 = UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, 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);
    UnifiedSetWithHashingStrategy<Integer> set4 = UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY);
    set4.batchForEach(new SumProcedure<>(sum4), 0, set4.getBatchCount(1));
    Assert.assertEquals(0, sum4.getValue());
}
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)

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