Skip to content
Permalink
Browse files
RNG-163: Replace expected thrown exception with assertThrows
Some multiple methods, one per exception, have been merged into a common
method.
  • Loading branch information
aherbert committed Aug 16, 2021
1 parent 07a6dc9 commit 5d4eb724fc174e9a7750d7730a3630fe6926e734
Show file tree
Hide file tree
Showing 46 changed files with 483 additions and 406 deletions.
@@ -29,19 +29,21 @@
* tests too).
*/
public class BaseProviderTest {
@Test(expected = IllegalStateException.class)
@Test
public void testStateSizeTooSmall() {
final DummyGenerator dummy = new DummyGenerator();
final int size = dummy.getStateSize();
Assumptions.assumeTrue(size > 0);
dummy.restoreState(new RandomProviderDefaultState(new byte[size - 1]));
final RandomProviderDefaultState state = new RandomProviderDefaultState(new byte[size - 1]);
Assertions.assertThrows(IllegalStateException.class, () -> dummy.restoreState(state));
}

@Test(expected = IllegalStateException.class)
@Test
public void testStateSizeTooLarge() {
final DummyGenerator dummy = new DummyGenerator();
final int size = dummy.getStateSize();
dummy.restoreState(new RandomProviderDefaultState(new byte[size + 1]));
final RandomProviderDefaultState state = new RandomProviderDefaultState(new byte[size + 1]);
Assertions.assertThrows(IllegalStateException.class, () -> dummy.restoreState(state));
}

@Test
@@ -56,52 +56,28 @@ public static Iterable<RestorableUniformRandomProvider[]> getList() {

// Precondition tests

@Test(expected = IllegalArgumentException.class)
public void testPreconditionNextInt1() {
generator.nextInt(-1);
}

@Test(expected = IllegalArgumentException.class)
public void testPreconditionNextInt2() {
generator.nextInt(0);
}

@Test(expected = IllegalArgumentException.class)
public void testPreconditionNextLong1() {
generator.nextLong(-1);
@Test
public void testPreconditionNextInt() {
Assertions.assertThrows(IllegalArgumentException.class, () -> generator.nextInt(-1));
Assertions.assertThrows(IllegalArgumentException.class, () -> generator.nextInt(0));
}

@Test(expected = IllegalArgumentException.class)
public void testPreconditionNextLong2() {
generator.nextLong(0);
@Test
public void testPreconditionNextLong() {
Assertions.assertThrows(IllegalArgumentException.class, () -> generator.nextLong(-1));
Assertions.assertThrows(IllegalArgumentException.class, () -> generator.nextLong(0));
}

@Test(expected = IndexOutOfBoundsException.class)
public void testPreconditionNextBytes1() {
@Test
public void testPreconditionNextBytes() {
final int size = 10;
final int num = 1;
final byte[] buf = new byte[size];
generator.nextBytes(buf, -1, num);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testPreconditionNextBytes2() {
final int size = 10;
final byte[] buf = new byte[size];
generator.nextBytes(buf, size, 0);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testPreconditionNextBytes3() {
final int size = 10;
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> generator.nextBytes(buf, -1, num));
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> generator.nextBytes(buf, size, 0));
final int offset = 2;
final byte[] buf = new byte[size];
generator.nextBytes(buf, offset, size - offset + 1);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testPreconditionNextBytes4() {
final int size = 10;
final int offset = 1;
final byte[] buf = new byte[size];
generator.nextBytes(buf, offset, -1);
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> generator.nextBytes(buf, offset, size - offset + 1));
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> generator.nextBytes(buf, offset, -1));
}


@@ -245,19 +221,18 @@ public void testStateSettable() {
Assertions.assertEquals(listOrig, listReplay);
}

@Test(expected = IllegalStateException.class)
@Test
public void testStateWrongSize() {
final RandomProviderState state = new DummyGenerator().saveState();
// Try to restore with an invalid state (wrong size).
generator.restoreState(state);
Assertions.assertThrows(IllegalStateException.class, () -> generator.restoreState(state));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testRestoreForeignState() {
generator.restoreState(new RandomProviderState() {});
Assertions.assertThrows(IllegalArgumentException.class, () -> generator.restoreState(new RandomProviderState() {}));
}


///// Support methods below.

/**
@@ -104,7 +104,7 @@ public void testRestoreToNewInstance() {
*
* @throws IOException Signals that an I/O exception has occurred.
*/
@Test(expected = IllegalStateException.class)
@Test
public void testRestoreWithInvalidClass() throws IOException {
// Serialize something
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
@@ -123,7 +123,6 @@ public void testRestoreWithInvalidClass() throws IOException {
final RandomProviderDefaultState dummyState = new RandomProviderDefaultState(sizeAndState);

final JDKRandom rng = new JDKRandom(13L);
// This should throw
rng.restoreState(dummyState);
Assertions.assertThrows(IllegalStateException.class, () -> rng.restoreState(dummyState));
}
}
@@ -110,7 +110,7 @@ public void testSubcycleGeneratorsIndex() {
}
}

@Test(expected = IllegalStateException.class)
@Test
public void testCmresFactoryThrowsWithDuplicateMultiplier() {
ArrayList<Cmres> list = new ArrayList<Cmres>();
final long multiply = 0;
@@ -128,6 +128,6 @@ public void testCmresFactoryThrowsWithDuplicateMultiplier() {

list.add(new Cmres(nextMultiply, rotate, start));
// This should throw as the list now contains the multiply value
Cmres.Factory.checkUnique(list, nextMultiply);
Assertions.assertThrows(IllegalStateException.class, () -> Cmres.Factory.checkUnique(list, nextMultiply));
}
}
@@ -18,6 +18,7 @@

import org.apache.commons.rng.core.RandomAssert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;

public class XoRoShiRo128PlusPlusTest {
/** The size of the array SEED. */
@@ -115,8 +116,8 @@ public void testLongJump() {
* and the abstract nextOutput() method should not be used. This test checks the method
* throws an exception if used.
*/
@Test(expected = UnsupportedOperationException.class)
@Test
public void testNextOutputThrows() {
new XoRoShiRo128PlusPlus(SEED).nextOutput();
Assertions.assertThrows(UnsupportedOperationException.class, () -> new XoRoShiRo128PlusPlus(SEED).nextOutput());
}
}
@@ -107,13 +107,13 @@ private static void assertHexDecodeAndEncode(char[] chars) {
Assertions.assertArrayEquals(text.toLowerCase(Locale.US).toCharArray(), Hex.encodeHex(decoded));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testHexThrowsWithOddNumberOfCharacters() {
Hex.decodeHex("0");
Assertions.assertThrows(IllegalArgumentException.class, () -> Hex.decodeHex("0"));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testHexThrowsWithIllegalHexCharacters() {
Hex.decodeHex("0g");
Assertions.assertThrows(IllegalArgumentException.class, () -> Hex.decodeHex("0g"));
}
}
@@ -49,11 +49,12 @@ public void testSampleTrivial() {
Assertions.fail(word + " not in list");
}

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

/**
@@ -73,44 +73,49 @@ public void testSampleWhenNequalsKIsNotShuffled() {
}
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testKgreaterThanNThrows() {
// Must fail for k > n.
final int n = 2;
final int k = 3;
new CombinationSampler(rng, n, k);
Assertions.assertThrows(IllegalArgumentException.class,
() -> new CombinationSampler(rng, n, k));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testNequalsZeroThrows() {
// Must fail for n = 0.
final int n = 0;
final int k = 3;
new CombinationSampler(rng, n, k);
Assertions.assertThrows(IllegalArgumentException.class,
() -> new CombinationSampler(rng, n, k));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testKequalsZeroThrows() {
// Must fail for k = 0.
final int n = 2;
final int k = 0;
new CombinationSampler(rng, n, k);
Assertions.assertThrows(IllegalArgumentException.class,
() -> new CombinationSampler(rng, n, k));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testNisNegativeThrows() {
// Must fail for n <= 0.
final int n = -1;
final int k = 3;
new CombinationSampler(rng, n, k);
Assertions.assertThrows(IllegalArgumentException.class,
() -> new CombinationSampler(rng, n, k));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testKisNegativeThrows() {
// Must fail for k <= 0.
final int n = 0;
final int k = -1;
new CombinationSampler(rng, n, k);
Assertions.assertThrows(IllegalArgumentException.class,
() -> new CombinationSampler(rng, n, k));
}

/**
@@ -63,23 +63,25 @@ public void testDiscreteProbabilitySampler() {
/**
* Test an empty builder cannot build a sampler.
*/
@Test(expected = IllegalStateException.class)
@Test
public void testEmptyBuilderThrows() {
final UniformRandomProvider rng = RandomSource.SPLIT_MIX_64.create(0L);
final Builder<SharedStateObjectSampler<Integer>> builder = CompositeSamplers
.newSharedStateObjectSamplerBuilder();
Assertions.assertEquals(0, builder.size());
builder.build(rng);
Assertions.assertThrows(IllegalStateException.class,
() -> builder.build(rng));
}

/**
* Test adding null sampler to a builder.
*/
@Test(expected = NullPointerException.class)
@Test
public void testNullSharedStateObjectSamplerThrows() {
final Builder<SharedStateObjectSampler<Integer>> builder = CompositeSamplers
.newSharedStateObjectSamplerBuilder();
builder.add(null, 1.0);
Assertions.assertThrows(NullPointerException.class,
() -> builder.add(null, 1.0));
}

/**

0 comments on commit 5d4eb72

Please sign in to comment.