use of org.eclipse.collections.impl.list.Interval in project eclipse-collections by eclipse.
the class ParallelIterateTest method sumByDouble.
@Test
public void sumByDouble() {
Interval interval = Interval.oneTo(100000);
ObjectDoubleMap<String> sumByCount = ParallelIterate.sumByDouble(interval, EVEN_OR_ODD, i -> 1.0d);
Assert.assertEquals(50000.0, sumByCount.get("Even"), 0.0);
Assert.assertEquals(50000.0, sumByCount.get("Odd"), 0.0);
ObjectDoubleMap<String> sumByValue = ParallelIterate.sumByDouble(interval, EVEN_OR_ODD, Integer::doubleValue);
Assert.assertEquals(interval.sumByDouble(EVEN_OR_ODD, Integer::doubleValue), sumByValue);
ObjectDoubleMap<Integer> sumByValue2 = ParallelIterate.sumByDouble(interval, i -> i % 1000, Integer::doubleValue);
Assert.assertEquals(interval.sumByDouble(i -> i % 1000, Integer::doubleValue), sumByValue2);
Interval interval2 = Interval.oneTo(UNEVEN_COUNT_FOR_SUMBY);
ObjectDoubleMap<String> sumByValue3 = ParallelIterate.sumByDouble(interval2, EVEN_OR_ODD, Integer::doubleValue);
Assert.assertEquals(interval2.sumByDouble(EVEN_OR_ODD, Integer::doubleValue), sumByValue3);
ObjectDoubleMap<Integer> sumByValue4 = ParallelIterate.sumByDouble(interval2, i -> i % 1000, Integer::doubleValue);
Assert.assertEquals(interval2.sumByDouble(i -> i % 1000, Integer::doubleValue), sumByValue4);
Interval small = Interval.oneTo(11);
ObjectDoubleMap<String> smallSumByCount = ParallelIterate.sumByDouble(small, EVEN_OR_ODD, i -> 1.0d);
Assert.assertEquals(5.0, smallSumByCount.get("Even"), 0.0);
Assert.assertEquals(6.0, smallSumByCount.get("Odd"), 0.0);
}
use of org.eclipse.collections.impl.list.Interval in project eclipse-collections by eclipse.
the class ParallelIterateTest method sumByInt.
@Test
public void sumByInt() {
Interval interval = Interval.oneTo(100000);
ObjectLongMap<String> sumByCount = ParallelIterate.sumByInt(interval, EVEN_OR_ODD, i -> 1);
Assert.assertEquals(50000, sumByCount.get("Even"));
Assert.assertEquals(50000, sumByCount.get("Odd"));
ObjectLongMap<String> sumByValue = ParallelIterate.sumByInt(interval, EVEN_OR_ODD, Integer::intValue);
Assert.assertEquals(interval.sumByInt(EVEN_OR_ODD, Integer::intValue), sumByValue);
ObjectLongMap<Integer> sumByValue2 = ParallelIterate.sumByInt(interval, i -> i % 1000, Integer::intValue);
Assert.assertEquals(interval.sumByInt(i -> i % 1000, Integer::intValue), sumByValue2);
Interval interval2 = Interval.oneTo(UNEVEN_COUNT_FOR_SUMBY);
ObjectLongMap<String> sumByValue3 = ParallelIterate.sumByInt(interval2, EVEN_OR_ODD, Integer::intValue);
Assert.assertEquals(interval2.sumByInt(EVEN_OR_ODD, Integer::intValue), sumByValue3);
ObjectLongMap<Integer> sumByValue4 = ParallelIterate.sumByInt(interval2, i -> i % 1000, Integer::intValue);
Assert.assertEquals(interval2.sumByInt(i -> i % 1000, Integer::intValue), sumByValue4);
Interval small = Interval.oneTo(11);
ObjectLongMap<String> smallSumByCount = ParallelIterate.sumByInt(small, EVEN_OR_ODD, i -> 1);
Assert.assertEquals(5.0, smallSumByCount.get("Even"), 0.0);
Assert.assertEquals(6.0, smallSumByCount.get("Odd"), 0.0);
}
use of org.eclipse.collections.impl.list.Interval in project eclipse-collections by eclipse.
the class ParallelIterateTest method sumByFloat.
@Test
public void sumByFloat() {
Interval interval = Interval.oneTo(100000);
ObjectDoubleMap<String> sumByCount = ParallelIterate.sumByFloat(interval, EVEN_OR_ODD, i -> 1.0f);
Assert.assertEquals(50000.0, sumByCount.get("Even"), 0.0);
Assert.assertEquals(50000.0, sumByCount.get("Odd"), 0.0);
ObjectDoubleMap<String> sumByValue = ParallelIterate.sumByFloat(interval, EVEN_OR_ODD, Integer::floatValue);
Assert.assertEquals(interval.sumByFloat(EVEN_OR_ODD, Integer::floatValue), sumByValue);
ObjectDoubleMap<Integer> sumByValue2 = ParallelIterate.sumByFloat(interval, i -> i % 1000, Integer::floatValue);
Assert.assertEquals(interval.sumByDouble(i -> i % 1000, Integer::doubleValue), sumByValue2);
Interval interval2 = Interval.oneTo(UNEVEN_COUNT_FOR_SUMBY);
ObjectDoubleMap<String> sumByValue3 = ParallelIterate.sumByFloat(interval2, EVEN_OR_ODD, Integer::floatValue);
Assert.assertEquals(interval2.sumByFloat(EVEN_OR_ODD, Integer::floatValue), sumByValue3);
ObjectDoubleMap<Integer> sumByValue4 = ParallelIterate.sumByFloat(interval2, i -> i % 1000, Integer::floatValue);
Assert.assertEquals(interval2.sumByFloat(i -> i % 1000, Integer::floatValue), sumByValue4);
Interval small = Interval.oneTo(11);
ObjectDoubleMap<String> smallSumByCount = ParallelIterate.sumByFloat(small, EVEN_OR_ODD, i -> 1.0f);
Assert.assertEquals(5.0, smallSumByCount.get("Even"), 0.0);
Assert.assertEquals(6.0, smallSumByCount.get("Odd"), 0.0);
}
use of org.eclipse.collections.impl.list.Interval in project eclipse-collections by eclipse.
the class ParallelIterateTest method sumByBigInteger.
@Test
public void sumByBigInteger() {
MutableList<BigInteger> list = Interval.oneTo(100000).collect(Object::toString).collect(BigInteger::new).toList().shuffleThis();
MutableMap<String, BigInteger> sumByCount = ParallelIterate.sumByBigInteger(list, EVEN_OR_ODD_BI, bi -> BigInteger.valueOf(1L));
Assert.assertEquals(BigInteger.valueOf(50000L), sumByCount.get("Even"));
Assert.assertEquals(BigInteger.valueOf(50000L), sumByCount.get("Odd"));
MutableMap<String, BigInteger> sumByValue = ParallelIterate.sumByBigInteger(list, EVEN_OR_ODD_BI, bi -> bi);
Assert.assertEquals(Iterate.sumByBigInteger(list, EVEN_OR_ODD_BI, bi -> bi), sumByValue);
MutableMap<Integer, BigInteger> sumByValue2 = ParallelIterate.sumByBigInteger(list, bi -> bi.intValue() % 1000, bi -> bi);
Assert.assertEquals(Iterate.sumByBigInteger(list, bi -> bi.intValue() % 1000, bi -> bi), sumByValue2);
MutableList<BigInteger> list2 = Interval.oneTo(UNEVEN_COUNT_FOR_SUMBY).collect(Object::toString).collect(BigInteger::new).toList();
MutableMap<String, BigInteger> sumByValue3 = ParallelIterate.sumByBigInteger(list2, EVEN_OR_ODD_BI, bi -> bi);
Assert.assertEquals(Iterate.sumByBigInteger(list2, EVEN_OR_ODD_BI, bi -> bi), sumByValue3);
MutableMap<Integer, BigInteger> sumByValue4 = ParallelIterate.sumByBigInteger(list2, bi -> bi.intValue() % 1000, bi -> bi);
Assert.assertEquals(Iterate.sumByBigInteger(list2, bi -> bi.intValue() % 1000, bi -> bi), sumByValue4);
Interval small = Interval.oneTo(11);
MutableMap<String, BigInteger> smallSumByCount = ParallelIterate.sumByBigInteger(small, EVEN_OR_ODD, i -> BigInteger.valueOf(1L));
Assert.assertEquals(new BigInteger("5"), smallSumByCount.get("Even"));
Assert.assertEquals(new BigInteger("6"), smallSumByCount.get("Odd"));
}
use of org.eclipse.collections.impl.list.Interval in project eclipse-collections by eclipse.
the class UnifiedSetWithHashingStrategyTest method trimToSize.
@Test
public void trimToSize() {
UnifiedSetWithHashingStrategy<String> set = UnifiedSetWithHashingStrategy.newSet(HashingStrategies.defaultStrategy());
MutableSet<String> expected = Sets.mutable.empty();
Interval integers = Interval.fromTo(0, 250);
integers.each(each -> {
set.add(each.toString());
expected.add(each.toString());
});
ArrayIterate.forEach(FREQUENT_COLLISIONS, each -> {
set.add(each);
expected.add(each);
});
Assert.assertEquals(expected, set);
Assert.assertEquals(261, set.size());
MutableList<Integer> toRemove = Lists.mutable.withAll(Interval.evensFromTo(0, 20));
toRemove.addAll(Interval.oddsFromTo(35, 55));
toRemove.each(each -> {
set.remove(each.toString());
expected.remove(each.toString());
});
// First assertion to verify that trim does not happen since, the table is already at the smallest required power of 2.
Assert.assertFalse(set.trimToSize());
Assert.assertEquals(239, set.size());
Assert.assertEquals(expected, set);
Interval.evensFromTo(0, 250).each(each -> {
set.remove(each.toString());
expected.remove(each.toString());
});
// Second assertion to verify that trim happens since, the table length is less than smallest required power of 2.
Assert.assertTrue(set.trimToSize());
Assert.assertFalse(set.trimToSize());
Assert.assertEquals(expected, set);
Assert.assertEquals(124, set.size());
expected.each(each -> Assert.assertEquals(each, set.get(each)));
integers.each(each -> {
set.remove(each.toString());
expected.remove(each.toString());
});
Assert.assertTrue(set.trimToSize());
Assert.assertFalse(set.trimToSize());
Assert.assertEquals(expected, set);
expected.each(each -> Assert.assertEquals(each, set.get(each)));
set.clear();
Assert.assertTrue(set.trimToSize());
Interval.oneTo(4).each(each -> set.add(each.toString()));
// Assert that trim does not happen after puts
Assert.assertFalse(set.trimToSize());
set.remove("1");
set.remove("2");
Assert.assertTrue(set.trimToSize());
set.add("1");
// Assert that the resized table due to put is the required size and no need to trim that.
Assert.assertFalse(set.trimToSize());
Interval.zeroTo(4).each(each -> set.add(each.toString()));
Interval.oneTo(3).each(each -> set.remove(each.toString()));
Assert.assertTrue(set.trimToSize());
Assert.assertEquals(2, set.size());
}
Aggregations