Skip to content
Permalink
Browse files
Sonar fix: lambdas to have 1 invocation possible throwing an exception
  • Loading branch information
aherbert committed May 4, 2022
1 parent ee10ce3 commit e3ef4499b9de52c8318ce9e2be56d67bc10be6d2
Showing 8 changed files with 45 additions and 23 deletions.
@@ -249,7 +249,8 @@ void testStateWrongSize(RestorableUniformRandomProvider generator) {
@ParameterizedTest
@MethodSource("getList")
void testRestoreForeignState(RestorableUniformRandomProvider generator) {
Assertions.assertThrows(IllegalArgumentException.class, () -> generator.restoreState(new RandomProviderState() {}));
final RandomProviderState state = new RandomProviderState() {};
Assertions.assertThrows(IllegalArgumentException.class, () -> generator.restoreState(state));
}

///// Support methods below.
@@ -118,6 +118,7 @@ void testLongJump() {
*/
@Test
void testNextOutputThrows() {
Assertions.assertThrows(UnsupportedOperationException.class, () -> new XoRoShiRo128PlusPlus(SEED).nextOutput());
final XoRoShiRo128PlusPlus rng = new XoRoShiRo128PlusPlus(SEED);
Assertions.assertThrows(UnsupportedOperationException.class, () -> rng.nextOutput());
}
}
@@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Assertions;
@@ -51,10 +52,11 @@ void testSampleTrivial() {

@Test
void testSamplePrecondition() {
final UniformRandomProvider rng = RandomSource.SPLIT_MIX_64.create(0L);
// Must fail for empty collection.
final List<String> empty = Collections.emptyList();
Assertions.assertThrows(IllegalArgumentException.class,
() -> new CollectionSampler<>(RandomSource.SPLIT_MIX_64.create(0L),
new ArrayList<>()));
() -> new CollectionSampler<>(rng, empty));
}

/**
@@ -19,7 +19,6 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@@ -39,63 +38,76 @@ class DiscreteProbabilityCollectionSamplerTest {
@Test
void testPrecondition1() {
// Size mismatch
final List<Double> collection = Arrays.asList(1d, 2d);
final double[] probabilities = {0};
Assertions.assertThrows(IllegalArgumentException.class,
() -> new DiscreteProbabilityCollectionSampler<>(rng,
Arrays.asList(1d, 2d),
new double[] {0d}));
collection,
probabilities));
}

@Test
void testPrecondition2() {
// Negative probability
final List<Double> collection = Arrays.asList(1d, 2d);
final double[] probabilities = {0, -1};
Assertions.assertThrows(IllegalArgumentException.class,
() -> new DiscreteProbabilityCollectionSampler<>(rng,
Arrays.asList(1d, 2d),
new double[] {0d, -1d}));
collection,
probabilities));
}

@Test
void testPrecondition3() {
// Probabilities do not sum above 0
final List<Double> collection = Arrays.asList(1d, 2d);
final double[] probabilities = {0, 0};
Assertions.assertThrows(IllegalArgumentException.class,
() -> new DiscreteProbabilityCollectionSampler<>(rng,
Arrays.asList(1d, 2d),
new double[] {0d, 0d}));
collection,
probabilities));
}

@Test
void testPrecondition4() {
// NaN probability
final List<Double> collection = Arrays.asList(1d, 2d);
final double[] probabilities = {0, Double.NaN};
Assertions.assertThrows(IllegalArgumentException.class,
() -> new DiscreteProbabilityCollectionSampler<>(rng,
Arrays.asList(1d, 2d),
new double[] {0d, Double.NaN}));
collection,
probabilities));
}

@Test
void testPrecondition5() {
// Infinite probability
final List<Double> collection = Arrays.asList(1d, 2d);
final double[] probabilities = {0, Double.POSITIVE_INFINITY};
Assertions.assertThrows(IllegalArgumentException.class,
() -> new DiscreteProbabilityCollectionSampler<>(rng,
Arrays.asList(1d, 2d),
new double[] {0d, Double.POSITIVE_INFINITY}));
collection,
probabilities));
}

@Test
void testPrecondition6() {
// Empty Map<T, Double> not allowed
final Map<String, Double> collection = Collections.emptyMap();
Assertions.assertThrows(IllegalArgumentException.class,
() -> new DiscreteProbabilityCollectionSampler<>(rng,
new HashMap<>()));
collection));
}

@Test
void testPrecondition7() {
// Empty List<T> not allowed
final List<Double> collection = Collections.emptyList();
final double[] probabilities = {};
Assertions.assertThrows(IllegalArgumentException.class,
() -> new DiscreteProbabilityCollectionSampler<>(rng,
Collections.<Double>emptyList(),
new double[0]));
collection,
probabilities));
}

@Test
@@ -96,13 +96,15 @@ void testFactorialLogCacheExpansion() {

@Test
void testLogFactorialThrowsWhenNegative() {
final FactorialLog factorialLog = FactorialLog.create();
Assertions.assertThrows(IndexOutOfBoundsException.class,
() -> FactorialLog.create().value(-1));
() -> factorialLog.value(-1));
}

@Test
void testLogFactorialWithCacheThrowsWhenNegative() {
final FactorialLog factorialLog = FactorialLog.create();
Assertions.assertThrows(NegativeArraySizeException.class,
() -> FactorialLog.create().withCache(-1));
() -> factorialLog.withCache(-1));
}
}
@@ -189,8 +189,9 @@ void testWithRangeConstructorWhenMaxAboveMin() {
void testWithRangeConstructorThrowsWhenMaxIsLessThanMin() {
final double min = PoissonSampler.PIVOT;
final double max = Math.nextDown(min);
final PoissonSamplerCache cache = createPoissonSamplerCache();
Assertions.assertThrows(IllegalArgumentException.class,
() -> createPoissonSamplerCache().withRange(min, max));
() -> cache.withRange(min, max));
}

/**
@@ -46,8 +46,9 @@ public void nextBytes(byte[] bytes) {}
};

// Infinite loop (in v1.1).
final ZigguratNormalizedGaussianSampler sampler = new ZigguratNormalizedGaussianSampler(bad);
Assertions.assertThrows(StackOverflowError.class,
() -> new ZigguratNormalizedGaussianSampler(bad).sample());
() -> sampler.sample());
}

/**
@@ -54,9 +54,11 @@ private static Iterable<ProvidersList.Data> getProvidersTestData() {
@ParameterizedTest
@MethodSource("getProvidersTestData")
void testUnsupportedSeedType(ProvidersList.Data data) {
final RandomSource originalSource = data.getSource();
final byte seed = 123;
final Object[] originalArgs = data.getArgs();
Assertions.assertThrows(UnsupportedOperationException.class,
() -> data.getSource().create(seed, data.getArgs()));
() -> originalSource.create(seed, originalArgs));
}

/**

0 comments on commit e3ef449

Please sign in to comment.