Search in sources :

Example 31 with Sum

use of org.eclipse.collections.impl.math.Sum 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());
}
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 32 with Sum

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

Example 33 with Sum

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

Example 34 with Sum

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

the class UnifiedMapWithHashingStrategyTest method batchForEachEntry_chains.

@Test
public void batchForEachEntry_chains() {
    BatchIterable<Map.Entry<Integer, Integer>> collisions = (BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMapWithHashingStrategy.<Integer, Integer>newMap(INTEGER_HASHING_STRATEGY, 5).withKeysValues(COLLISION_1, 1, COLLISION_2, 2, COLLISION_3, 3, 1, 4).withKeysValues(2, 5, 3, 6).entrySet();
    // Testing 1 batch with chains
    Sum sum2 = new IntegerSum(0);
    // testing getBatchCount returns 1
    int batchCount = collisions.getBatchCount(100000);
    for (int i = 0; i < batchCount; ++i) {
        collisions.batchForEach(new EntrySumProcedure(sum2), i, batchCount);
    }
    Assert.assertEquals(1, batchCount);
    Assert.assertEquals(78, sum2.getValue());
    // Testing 3 batches with chains and uneven last batch
    Sum sum3 = new IntegerSum(0);
    for (int i = 0; i < 5; ++i) {
        collisions.batchForEach(new EntrySumProcedure(sum3), i, 5);
    }
    Assert.assertEquals(78, sum3.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 35 with Sum

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

the class UnifiedMapWithHashingStrategyTest method batchIterable_forEachEntry_emptySet.

@Test
public void batchIterable_forEachEntry_emptySet() {
    // Test forEach on empty set, it should simply do nothing and not throw any exceptions
    Sum sum = new IntegerSum(0);
    BatchIterable<Map.Entry<Integer, Integer>> empty = (BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMapWithHashingStrategy.newMap(INTEGER_HASHING_STRATEGY).entrySet();
    empty.forEach(new EntrySumProcedure(sum));
    Assert.assertEquals(0, 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)

Aggregations

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