This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-rng.git
commit c80b7a4e094c80acf214eaeee3932efadfc686b9 Author: Alex Herbert <aherb...@apache.org> AuthorDate: Wed Jul 3 16:17:28 2024 +0100 Add tests for bad probability in the constructor --- .../DiscreteProbabilityCollectionSamplerTest.java | 25 ++++++++++++---------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/DiscreteProbabilityCollectionSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/DiscreteProbabilityCollectionSamplerTest.java index 2ad92481..59d2c77f 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/DiscreteProbabilityCollectionSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/DiscreteProbabilityCollectionSamplerTest.java @@ -19,12 +19,15 @@ package org.apache.commons.rng.sampling; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.apache.commons.rng.UniformRandomProvider; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; /** * Test class for {@link DiscreteProbabilityCollectionSampler}. @@ -66,26 +69,26 @@ class DiscreteProbabilityCollectionSamplerTest { probabilities)); } - @Test - void testPrecondition4() { - // NaN probability + @ParameterizedTest + @ValueSource(doubles = {-1, Double.POSITIVE_INFINITY, Double.NaN}) + void testPrecondition4(double p) { final List<Double> collection = Arrays.asList(1d, 2d); - final double[] probabilities = {0, Double.NaN}; + final double[] probabilities = {0, p}; Assertions.assertThrows(IllegalArgumentException.class, () -> new DiscreteProbabilityCollectionSampler<>(rng, collection, probabilities)); } - @Test - void testPrecondition5() { - // Infinite probability - final List<Double> collection = Arrays.asList(1d, 2d); - final double[] probabilities = {0, Double.POSITIVE_INFINITY}; + @ParameterizedTest + @ValueSource(doubles = {-1, Double.POSITIVE_INFINITY, Double.NaN}) + void testPrecondition5(double p) { + final Map<String, Double> collection = new HashMap<>(); + collection.put("one", 0.0); + collection.put("two", p); Assertions.assertThrows(IllegalArgumentException.class, () -> new DiscreteProbabilityCollectionSampler<>(rng, - collection, - probabilities)); + collection)); } @Test