Skip to content
Permalink
Browse files
RNG-163: Replace JUnit 4 Assert with JUnit 5 Assertions
Tests for float equality with a delta of 0.0 have been updated to remove the delta.

Replace JUnit 4 Assume with JUnit 5 Assumptions
  • Loading branch information
aherbert committed Aug 16, 2021
1 parent 6663901 commit 07a6dc916a5ab71cc583727c43517cb668c81ad7
Show file tree
Hide file tree
Showing 71 changed files with 961 additions and 961 deletions.
@@ -17,8 +17,8 @@
package org.apache.commons.rng.core;

import org.junit.Test;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;

/**
* Tests for {@link BaseProvider}.
@@ -33,7 +33,7 @@ public class BaseProviderTest {
public void testStateSizeTooSmall() {
final DummyGenerator dummy = new DummyGenerator();
final int size = dummy.getStateSize();
Assume.assumeTrue(size > 0);
Assumptions.assumeTrue(size > 0);
dummy.restoreState(new RandomProviderDefaultState(new byte[size - 1]));
}

@@ -50,15 +50,15 @@ public void testFillStateInt() {
final int[] seed = {1, 2, 3};

for (int i = 0; i < state.length; i++) {
Assert.assertEquals(0, state[i]);
Assertions.assertEquals(0, state[i]);
}

new DummyGenerator().fillState(state, seed);
for (int i = 0; i < seed.length; i++) {
Assert.assertEquals(seed[i], state[i]);
Assertions.assertEquals(seed[i], state[i]);
}
for (int i = seed.length; i < state.length; i++) {
Assert.assertNotEquals(0, state[i]);
Assertions.assertNotEquals(0, state[i]);
}
}

@@ -68,15 +68,15 @@ public void testFillStateLong() {
final long[] seed = {1, 2, 3};

for (int i = 0; i < state.length; i++) {
Assert.assertEquals(0, state[i]);
Assertions.assertEquals(0, state[i]);
}

new DummyGenerator().fillState(state, seed);
for (int i = 0; i < seed.length; i++) {
Assert.assertEquals(seed[i], state[i]);
Assertions.assertEquals(seed[i], state[i]);
}
for (int i = seed.length; i < state.length; i++) {
Assert.assertNotEquals(0, state[i]);
Assertions.assertNotEquals(0, state[i]);
}
}

@@ -16,8 +16,8 @@
*/
package org.apache.commons.rng.core;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -90,7 +90,7 @@ public UniformRandomProvider jump() {
* @return the jump function
*/
private TestJumpFunction getLongJumpFunction() {
Assume.assumeTrue("No long jump function", generator instanceof LongJumpableUniformRandomProvider);
Assumptions.assumeTrue(generator instanceof LongJumpableUniformRandomProvider, "No long jump function");

final LongJumpableUniformRandomProvider rng = (LongJumpableUniformRandomProvider) generator;
return new TestJumpFunction() {
@@ -124,8 +124,8 @@ public void testLongJumpReturnsACopy() {
*/
private void assertJumpReturnsACopy(TestJumpFunction jumpFunction) {
final UniformRandomProvider copy = jumpFunction.jump();
Assert.assertNotSame("The copy instance should be a different object", generator, copy);
Assert.assertEquals("The copy instance should be the same class", generator.getClass(), copy.getClass());
Assertions.assertNotSame(generator, copy, "The copy instance should be a different object");
Assertions.assertEquals(generator.getClass(), copy.getClass(), "The copy instance should be the same class");
}

/**
@@ -165,7 +165,7 @@ public void testLongJumpCopyMatchesPreJumpState() {
* @param jumpFunction Jump function to test.
*/
private void assertCopyMatchesPreJumpState(TestJumpFunction jumpFunction) {
Assume.assumeTrue("Not a restorable RNG", generator instanceof RestorableUniformRandomProvider);
Assumptions.assumeTrue(generator instanceof RestorableUniformRandomProvider, "Not a restorable RNG");

for (int repeats = 0; repeats < 2; repeats++) {
// Exercise the generator.
@@ -176,15 +176,15 @@ private void assertCopyMatchesPreJumpState(TestJumpFunction jumpFunction) {
generator.nextBoolean();

final RandomProviderState preJumpState = ((RestorableUniformRandomProvider) generator).saveState();
Assume.assumeTrue("Not a recognised state", preJumpState instanceof RandomProviderDefaultState);
Assumptions.assumeTrue(preJumpState instanceof RandomProviderDefaultState, "Not a recognised state");

final UniformRandomProvider copy = jumpFunction.jump();

final RandomProviderState copyState = ((RestorableUniformRandomProvider) copy).saveState();
final RandomProviderDefaultState expected = (RandomProviderDefaultState) preJumpState;
final RandomProviderDefaultState actual = (RandomProviderDefaultState) copyState;
Assert.assertArrayEquals("The copy instance state should match the state of the original",
expected.getState(), actual.getState());
Assertions.assertArrayEquals(expected.getState(), actual.getState(),
"The copy instance state should match the state of the original");
}
}

@@ -240,12 +240,13 @@ private void assertJumpResetsDefaultState(TestJumpFunction jumpFunction, int sta
final RandomProviderState postJumpState = ((RestorableUniformRandomProvider) generator).saveState();
final byte[] actual = ((RandomProviderDefaultState) postJumpState).getState();

Assume.assumeTrue("Implementation has removed default state", actual.length >= stateSize);
Assumptions.assumeTrue(actual.length >= stateSize, "Implementation has removed default state");

// The implementation requires that any sub-class state is prepended to the
// state thus the default state is at the end.
final byte[] defaultState = Arrays.copyOfRange(actual, actual.length - stateSize, actual.length);
Assert.assertArrayEquals("The jump should reset the default state to zero", expected, defaultState);
Assertions.assertArrayEquals(expected, defaultState,
"The jump should reset the default state to zero");
}
}

@@ -21,7 +21,7 @@
import java.util.ArrayList;
import java.util.concurrent.Callable;

import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -121,7 +121,7 @@ public void run() {
}
};

Assert.assertTrue(isUniformNextBytes(buffer, 0, size, nextMethod));
Assertions.assertTrue(isUniformNextBytes(buffer, 0, size, nextMethod));
}

@Test
@@ -140,11 +140,11 @@ public void run() {
};

// Test should pass for the part of the buffer where values are put.
Assert.assertTrue(isUniformNextBytes(buffer, offset, offset + size, nextMethod));
Assertions.assertTrue(isUniformNextBytes(buffer, offset, offset + size, nextMethod));

// Test must fail for the parts of the buffer where no values are put.
Assert.assertFalse(isUniformNextBytes(buffer, 0, offset, nextMethod));
Assert.assertFalse(isUniformNextBytes(buffer, offset + size, buffer.length, nextMethod));
Assertions.assertFalse(isUniformNextBytes(buffer, 0, offset, nextMethod));
Assertions.assertFalse(isUniformNextBytes(buffer, offset + size, buffer.length, nextMethod));
}

@Test
@@ -234,15 +234,15 @@ public void testStateSettable() {
final List<Number> listOrig = makeList(n);
// Discard a few more.
final List<Number> listDiscard = makeList(n);
Assert.assertNotEquals(0, listDiscard.size());
Assert.assertNotEquals(listOrig, listDiscard);
Assertions.assertNotEquals(0, listDiscard.size());
Assertions.assertNotEquals(listOrig, listDiscard);
// Reset.
generator.restoreState(state);
// Replay.
final List<Number> listReplay = makeList(n);
Assert.assertNotSame(listOrig, listReplay);
Assertions.assertNotSame(listOrig, listReplay);
// Check that the restored state is the same as the original.
Assert.assertEquals(listOrig, listReplay);
Assertions.assertEquals(listOrig, listReplay);
}

@Test(expected = IllegalStateException.class)
@@ -376,10 +376,9 @@ private void checkRandomWalk(int sampleSize,

final double actual = Math.abs(walk);
final double max = Math.sqrt(sampleSize) * 2.576;
Assert.assertTrue(generator + ": Walked too far astray: " + actual +
" > " + max +
" (test will fail randomly about 1 in 100 times)",
actual < max);
Assertions.assertTrue(actual < max,
() -> generator + ": Walked too far astray: " + actual + " > " + max +
" (test will fail randomly about 1 in 100 times)");
}

/**
@@ -516,7 +515,7 @@ private <T extends Number> void checkNextInRange(T max,
Arrays.fill(observed, 0);
for (int j = 0; j < sampleSize; j++) {
final long value = nextMethod.call().longValue();
Assert.assertTrue("Range", value >= 0 && value < n);
Assertions.assertTrue(value >= 0 && value < n, "Range");

for (int k = 0; k < numBins; k++) {
if (value < binUpperBounds[k]) {
@@ -552,8 +551,8 @@ private <T extends Number> void checkNextInRange(T max,
// 12 0.00190

if (numFailures > 11) { // Test will fail with 0.5% probability
Assert.fail(generator + ": Too many failures for n = " + n +
" (" + numFailures + " out of " + numTests + " tests failed)");
Assertions.fail(generator + ": Too many failures for n = " + n +
" (" + numFailures + " out of " + numTests + " tests failed)");
}
}

@@ -586,8 +585,8 @@ static void checkNextBytesChunks(RestorableUniformRandomProvider rng,
System.arraycopy(b1, b1.length - b3.length, b3, 0, b3.length);

// Sequence of calls must be the same.
Assert.assertArrayEquals("chunkSize=" + chunkSize + " numChunks=" + numChunks,
b2, b3);
Assertions.assertArrayEquals(b2, b3,
() -> "chunkSize=" + chunkSize + " numChunks=" + numChunks);
}

/**

0 comments on commit 07a6dc9

Please sign in to comment.