Search in sources :

Example 46 with Sum

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

the class UnifiedMapTest 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>>) UnifiedMap.<Integer, Integer>newMap(100).withKeysValues(null, 10, 1, null, 2, 11, 3, 12).withKeysValues(4, null, 5, null).entrySet();
    for (int i = 0; i < nulls.getBatchCount(7); ++i) {
        nulls.batchForEach(each -> {
            sum4.add(each.getKey() == null ? 1 : each.getKey());
            sum4.add(each.getValue() == null ? 1 : each.getValue());
        }, i, nulls.getBatchCount(7));
    }
    Assert.assertEquals(52, 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) MutableMap(org.eclipse.collections.api.map.MutableMap) Map(java.util.Map) BatchIterable(org.eclipse.collections.impl.parallel.BatchIterable) Test(org.junit.Test)

Example 47 with Sum

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

the class UnifiedMapTest method batchForEachEmptyBatchIterable.

private void batchForEachEmptyBatchIterable(BatchIterable<Integer> batchIterable) {
    // Test batchForEach on empty set, it should simply do nothing and not throw any exceptions
    Sum sum = new IntegerSum(0);
    batchIterable.batchForEach(new SumProcedure<>(sum), 0, batchIterable.getBatchCount(1));
    Assert.assertEquals(0, 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 48 with Sum

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

the class FastListTest method testLazySelectForEach.

/**
 * @deprecated since 3.0. Use {@code asLazy().select(Predicate)} instead.
 */
@Deprecated
@Test
public void testLazySelectForEach() {
    LazyIterable<Integer> select = FastList.newList(Interval.oneTo(5)).asLazy().select(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 49 with Sum

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

the class ParallelArrayIterateTest method parallelForEachPerformanceTwoThreads.

@Test
public void parallelForEachPerformanceTwoThreads() throws InterruptedException {
    Object[] array = Interval.zeroTo(100).toArray();
    Sum parallelSum1 = new LongSum(0);
    Sum parallelSum2 = new LongSum(0);
    this.basicTestParallelSums(array, parallelSum1, parallelSum2);
    Sum linearSum1 = new LongSum(0);
    Sum linearSum2 = new LongSum(0);
    this.basicTestLinearSums(array, linearSum1, linearSum2);
    Assert.assertEquals(parallelSum1, linearSum1);
    Assert.assertEquals(parallelSum2, linearSum2);
}
Also used : LongSum(org.eclipse.collections.impl.math.LongSum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) LongSum(org.eclipse.collections.impl.math.LongSum) Test(org.junit.Test)

Example 50 with Sum

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

the class ParallelArrayIterateTest method parallelForEachPerformanceOneThread.

@Test
public void parallelForEachPerformanceOneThread() {
    Object[] array = Interval.zeroTo(100).toArray();
    Sum parallelSum = new LongSum(0);
    this.parallelSum(array, parallelSum);
    Sum linearSum = new LongSum(0);
    this.linearSum(array, linearSum);
    Assert.assertEquals(parallelSum, linearSum);
}
Also used : LongSum(org.eclipse.collections.impl.math.LongSum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) LongSum(org.eclipse.collections.impl.math.LongSum) 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