eclipse-collections
eclipse-collections copied to clipboard
Inner class <name>SumProcedure in abstractLazyPrimitiveIterable.stg should widen sum type for int, short, etc.
Today, the generated code for lazy int returns the following, which will overflow silently for large sums.
private static final class IntSumProcedure implements IntProcedure
{
private int sum = 0;
@Override
public void value(int each)
{
this.sum += each;
}
public int getValue()
{
return this.sum;
}
}
https://github.com/eclipse/eclipse-collections/blob/master/eclipse-collections-code-generator/src/main/resources/impl/lazy/abstractLazyPrimitiveIterable.stg#L451
This was a Project Loom code example in a test that I wrote that failed (using Java 20):
@Test
public void usingLoomWithEclipseCollectionsLazyAsParallel()
{
long sum = Interval.oneTo(2_000_000)
.toList()
.asParallel(Executors.newVirtualThreadPerTaskExecutor(), 2)
.sumOfInt(each -> each * 2);
long expected = IntInterval.oneTo(2_000_000).asLazy().collectInt(each -> each * 2).sum();
Assertions.assertEquals(expected, sum);
}
Expected :1387447424 Actual :4000002000000
A similar test could be written in Java 8 without using asParallel.
Hi, can you assign this issue to me?
Thanks for volunteering @emilie-robichaud ! I have assigned the issue to you.