NoIssue: Added some tests for random ints

This commit is contained in:
Minimons
2025-11-28 17:17:22 +01:00
parent 0938645cd0
commit 959bd7dd66
3 changed files with 117 additions and 2 deletions

View File

@@ -29,8 +29,8 @@ repositories {
dependencies {
// The test classes are compiled with these:
implementation("com.r35157.libs:random-api:0.0.0")
implementation("com.r35157.libs:random-impl-ref:0.0.0")
implementation("com.r35157.libs:random-api:0.1-dev")
implementation("com.r35157.libs:random-impl-ref:0.1-dev")
implementation("org.jetbrains:annotations:26.0.1")
// The JUnit platform will not be included in the JAR file but are needed for running the tests:

View File

@@ -0,0 +1,81 @@
package com.r35157.libs.random.tests.happy;
import com.r35157.libs.random.RandomValueGeneratorInt;
import com.r35157.libs.random.impl.ref.RandomValueGeneratorIntImpl;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.HashSet;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
@DisplayName("Happy Random Ints Tests")
public class CorrectRandomIntsTests {
@DisplayName("Extreme narrow band works - always only one value possible")
@Test
void testThatEqualMinMaxBoundsWorks() {
// Arrange
int minBound = 7;
int maxBound = 7;
int expected = 7;
RandomValueGeneratorInt sut = new RandomValueGeneratorIntImpl();
// Act
int actual = sut.getSomeInt(minBound, maxBound);
// Assert
assertEquals(expected, actual);
}
@DisplayName("Produces different values over multiple calls")
@Test
void producesDifferentValuesOverMultipleCalls() {
// Arrange
int iterations = 5;
RandomValueGeneratorInt sut = new RandomValueGeneratorIntImpl();
Set<Integer> seen = new HashSet<>();
// Act
for (int i = 0; i < iterations; i++) {
seen.add(sut.getSomeInt());
}
// Arrange
assertTrue(seen.size() > 1, "Int generator returned the same value every time");
}
@DisplayName("Will never throw")
@Test
void noArgsMethodNeverThrows() {
// Arrange
int iterations = 10_000;
RandomValueGeneratorIntImpl sut = new RandomValueGeneratorIntImpl();
// Act + Assert
for (int i = 0; i < iterations; i++) {
assertDoesNotThrow(() -> sut.getSomeInt());
}
}
@Test
void returnsBothPositiveAndNegative() {
// Arrange
int iterations = 1_000_000;
boolean seenPos = false, seenNeg = false;
RandomValueGeneratorIntImpl sut = new RandomValueGeneratorIntImpl();
// Act
for (int i = 0; i < iterations; i++) {
int v = sut.getSomeInt();
if (v > 0) seenPos = true;
if (v < 0) seenNeg = true;
if (seenPos && seenNeg) break;
}
// Assert
assertTrue(seenPos, "Never saw positive number");
assertTrue(seenNeg, "Never saw negative number");
}
}

View File

@@ -0,0 +1,34 @@
package com.r35157.libs.random.tests.sad;
import com.r35157.libs.random.RandomValueGeneratorInt;
import com.r35157.libs.random.impl.ref.RandomValueGeneratorIntImpl;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DisplayName("Sad Random Ints Tests")
public class IncorrectRandomIntsTests {
@DisplayName("RandomInts.getSomeInts()")
@Test
void testThatMinBoundIsGreaterThanMaxBoundThrows() {
// Arrange
int minBound = 10;
int maxBound = 5;
String expectedErrorMsg = String.format("'minInclusive' (%d) must be less than or equal to 'maxInclusive' (%d)",
minBound, maxBound);
RandomValueGeneratorInt sut = new RandomValueGeneratorIntImpl();
// Act
IllegalArgumentException thrown = assertThrows(
IllegalArgumentException.class,
() -> sut.getSomeInt(minBound, maxBound)
);
// Assert
String actualErrorMsg = thrown.getMessage();
assertEquals(expectedErrorMsg, actualErrorMsg);
}
}