eclipse / eclipse-collections

Eclipse Collections is a collections framework for Java with optimized data structures and a rich, functional and fluent API.
https://eclipse.dev/collections/
2.44k stars 615 forks source link

Fix flaky test ParallelIterableTestCase#sumOfFloatConsistentRounding. #1706

Open motlin opened 1 month ago

motlin commented 1 month ago

This test sometimes fails with a delta greater than the allowed delta.

[ERROR]   ParallelCollectListIterableTest>ParallelIterableTestCase.sumOfFloatConsistentRounding:894 Batch size: 1000 ==> expected: <5.0000000000000995> but was: <5.000000000000101>

The test:

    @Test
    public void sumOfFloatConsistentRounding()
    {
        FloatFunction<Integer> roundingSensitiveElementFunction = i -> (i <= 99995) ? 1.0e-18f : 1.0f;

        MutableList<Integer> list = Interval.oneTo(100_000).toList().shuffleThis();
        double baseline = this.getExpectedWith(list.toArray(new Integer[]{}))
                .sumOfFloat(roundingSensitiveElementFunction);

        for (Integer batchSize : BATCH_SIZES)
        {
            this.batchSize = batchSize;

            ParallelIterable<Integer> testCollection = this.newWith(list.toArray(new Integer[]{}));
            assertEquals(
                    baseline,
                    testCollection.sumOfFloat(roundingSensitiveElementFunction),
                    1.0e-15,
                    "Batch size: " + this.batchSize);
        }
    }
IamLRBA commented 1 week ago

@motlin , I think the test failure you're encountering in the ParallelIterableTestCase#sumOfFloatConsistentRounding is related to floating-point precision. The error message shows that the expected result is <5.0000000000000995>, but the actual result was <5.000000000000101>. This difference is very small and falls within a "flaky test" range because it depends on how floating-point arithmetic is handled internally, which can vary slightly across platforms or when using parallel processing.

IamLRBA commented 1 week ago

Please assign me to this issue so that i can try and solve it. Thanks!

motlin commented 1 week ago

Assigned to you @IamLRBA !

IamLRBA commented 1 week ago

Thank you @motlin