From aa6b67625bd56975169d6d0a9b96ecb8ec8e1a30 Mon Sep 17 00:00:00 2001 From: Gabriele Cardosi Date: Tue, 9 Jan 2024 14:44:20 +0100 Subject: [PATCH] [incubator-kie-issues#804] Fix flaky test (checked with 2000000 iterations) (#5644) Co-authored-by: BAMOE CI --- .../core/PMMLRuntimeContextImplTest.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/kie-pmml-trusty/kie-pmml-evaluator/kie-pmml-evaluator-core/src/test/java/org/kie/pmml/evaluator/core/PMMLRuntimeContextImplTest.java b/kie-pmml-trusty/kie-pmml-evaluator/kie-pmml-evaluator-core/src/test/java/org/kie/pmml/evaluator/core/PMMLRuntimeContextImplTest.java index ee55a8b19c7..c8bfeca9e5b 100644 --- a/kie-pmml-trusty/kie-pmml-evaluator/kie-pmml-evaluator-core/src/test/java/org/kie/pmml/evaluator/core/PMMLRuntimeContextImplTest.java +++ b/kie-pmml-trusty/kie-pmml-evaluator/kie-pmml-evaluator-core/src/test/java/org/kie/pmml/evaluator/core/PMMLRuntimeContextImplTest.java @@ -22,9 +22,9 @@ import java.util.List; import java.util.Random; import java.util.concurrent.atomic.AtomicReference; -import java.util.stream.Collectors; import java.util.stream.IntStream; +import org.apache.commons.math3.util.Precision; import org.assertj.core.data.Percentage; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -88,23 +88,27 @@ void getFixedProbabilityMap() { AtomicReference totalReference = new AtomicReference<>(initialTotalProbability); Random rand = new Random(); List doubles = IntStream.range(0, 3).mapToDouble(value -> { - double currentTotal = totalReference.get(); - int nextInt = Math.abs(rand.nextInt((int) (currentTotal * 100))); - double toReturn = (double) nextInt / 100; - totalReference.set(currentTotal - toReturn); - return toReturn; - }).boxed().sorted((f1, f2) -> Double.compare(f2, f1)) - .collect(Collectors.toList()); + double remainingProbability = totalReference.get(); + double toReturn = invalidBound(remainingProbability) ? 0.00 : + Precision.round(rand.nextDouble(remainingProbability), 2); + totalReference.set(Precision.round((remainingProbability - toReturn), 2)); + return toReturn; + }).boxed().sorted((f1, f2) -> Double.compare(f2, f1)).toList(); LinkedHashMap probabilityResultMap = new LinkedHashMap<>(); int counter = 0; for (Double toPut : doubles) { probabilityResultMap.put("Element-" + counter, toPut); counter++; } - double initialProbability = probabilityResultMap.values().stream().mapToDouble(x -> x).sum(); + double initialProbability = Precision.round(probabilityResultMap.values().stream().mapToDouble(x -> x).sum(), + 2); assertThat(initialProbability).isLessThanOrEqualTo(initialTotalProbability); LinkedHashMap retrieved = PMMLRuntimeContextImpl.getFixedProbabilityMap(probabilityResultMap); double totalProbability = retrieved.values().stream().mapToDouble(x -> x).sum(); assertThat(totalProbability).isCloseTo(1.0, Percentage.withPercentage(0.01)); } + + private static boolean invalidBound(double bound) { + return (!(0.0 < bound && bound < Double.POSITIVE_INFINITY)); + } } \ No newline at end of file