Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
Using SplittableRandom with a seed for reproducibility.
  • Loading branch information
fabiolimace committed Jun 9, 2024
1 parent 561757b commit 76dac16
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 158 deletions.
31 changes: 16 additions & 15 deletions src/test/java/com/github/f4b6a3/uuid/alt/GUIDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Random;
import java.util.SplittableRandom;
import java.util.UUID;
import java.util.function.Function;
import java.util.regex.Pattern;
Expand All @@ -26,7 +27,7 @@ public class GUIDTest {

@Test
public void testConstructorGUID() {
Random random = new Random();
SplittableRandom random = new SplittableRandom(1);
for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
final long msb = random.nextLong();
final long lsb = random.nextLong();
Expand All @@ -38,7 +39,7 @@ public void testConstructorGUID() {

@Test
public void testConstructorLongs() {
Random random = new Random();
SplittableRandom random = new SplittableRandom(1);
for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
final long msb = random.nextLong();
final long lsb = random.nextLong();
Expand All @@ -50,7 +51,7 @@ public void testConstructorLongs() {

@Test
public void testConstructorJdkUUID() {
Random random = new Random();
SplittableRandom random = new SplittableRandom(1);
for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
final long msb = random.nextLong();
final long lsb = random.nextLong();
Expand All @@ -62,7 +63,7 @@ public void testConstructorJdkUUID() {

@Test
public void testConstructorString() {
Random random = new Random();
SplittableRandom random = new SplittableRandom(1);
for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
final long msb = random.nextLong();
final long lsb = random.nextLong();
Expand All @@ -76,10 +77,10 @@ public void testConstructorString() {

@Test
public void testConstructorBytes() {
Random random = new Random();
SplittableRandom seeder = new SplittableRandom(1);
for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
byte[] bytes = new byte[GUID.GUID_BYTES];
random.nextBytes(bytes);
(new Random(seeder.nextLong())).nextBytes(bytes);
GUID guid = new GUID(bytes);
assertEquals(Arrays.toString(bytes), Arrays.toString(guid.toBytes()));
}
Expand Down Expand Up @@ -249,7 +250,7 @@ public void testV7() {

@Test
public void testToUUID() {
Random random = new Random();
SplittableRandom random = new SplittableRandom(1);
for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
final long msb = random.nextLong();
final long lsb = random.nextLong();
Expand All @@ -270,11 +271,11 @@ public void testToString() {

@Test
public void testToBytes() {
Random random = new Random();
SplittableRandom seeder = new SplittableRandom(1);
for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {

byte[] bytes1 = new byte[16];
random.nextBytes(bytes1);
(new Random(seeder.nextLong())).nextBytes(bytes1);
GUID guid0 = new GUID(bytes1);

byte[] bytes2 = guid0.toBytes();
Expand All @@ -287,19 +288,19 @@ public void testToBytes() {
@Test
public void testHashCode() {

Random random = new Random();
SplittableRandom seeder = new SplittableRandom(1);
byte[] bytes = new byte[GUID.GUID_BYTES];

// invoked on the same object
for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
random.nextBytes(bytes);
(new Random(seeder.nextLong())).nextBytes(bytes);
GUID guid1 = new GUID(bytes);
assertEquals(guid1.hashCode(), guid1.hashCode());
}

// invoked on two equal objects
for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
random.nextBytes(bytes);
(new Random(seeder.nextLong())).nextBytes(bytes);
GUID guid1 = new GUID(bytes);
GUID guid2 = new GUID(bytes);
assertEquals(guid1.hashCode(), guid2.hashCode());
Expand All @@ -309,12 +310,12 @@ public void testHashCode() {
@Test
public void testEquals() {

Random random = new Random();
SplittableRandom seeder = new SplittableRandom(1);
byte[] bytes = new byte[GUID.GUID_BYTES];

for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {

random.nextBytes(bytes);
(new Random(seeder.nextLong())).nextBytes(bytes);
GUID guid1 = new GUID(bytes);
GUID guid2 = new GUID(bytes);
assertEquals(guid1, guid2);
Expand All @@ -336,7 +337,7 @@ public void testEquals() {
public void testCompareTo() {

final long zero = 0L;
Random random = new Random();
SplittableRandom random = new SplittableRandom(1);
byte[] bytes = new byte[GUID.GUID_BYTES];

for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Random;
import java.util.SplittableRandom;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

import org.junit.Test;

Expand All @@ -30,11 +31,12 @@ public void testDecode() {
@Test
public void testMultiply() {

SplittableRandom seeder = new SplittableRandom(1);
for (int i = 0; i < 1000; i++) {
byte[] bytes = new byte[UUID_BYTES];
ThreadLocalRandom.current().nextBytes(bytes);
long multiplier = ThreadLocalRandom.current().nextInt() & 0x7fffffff; // positive
long addend = ThreadLocalRandom.current().nextInt() & 0x7fffffff; // positive
(new Random(seeder.nextLong())).nextBytes(bytes);
long multiplier = seeder.nextInt() & 0x7fffffff; // positive
long addend = seeder.nextInt() & 0x7fffffff; // positive

BigInteger number1 = new BigInteger(1, bytes);
BigInteger product1 = number1.multiply(BigInteger.valueOf(multiplier)).add(BigInteger.valueOf(addend));
Expand Down Expand Up @@ -77,11 +79,12 @@ protected static byte[] fromLongs(long[] longs) {

private String getRandomString(BaseN base) {

SplittableRandom random = new SplittableRandom(1);
char[] chars = new char[base.getLength()];

chars[0] = base.getPadding(); // to avoid overflow
for (int i = 1; i < chars.length; i++) {
chars[i] = base.getAlphabet().get(ThreadLocalRandom.current().nextInt(base.getRadix()));
chars[i] = base.getAlphabet().get(random.nextInt(base.getRadix()));
}

return new String(chars);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import java.math.BigInteger;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.Random;
import java.util.SplittableRandom;

import org.junit.Test;

Expand All @@ -22,10 +23,10 @@ public class BaseNRemainderEncoderTest {

@Test
public void testEncode() {

SplittableRandom seeder = new SplittableRandom(1);
for (int i = 0; i < 1000; i++) {
byte[] bytes = new byte[UUID_BYTES];
ThreadLocalRandom.current().nextBytes(bytes);
(new Random(seeder.nextLong())).nextBytes(bytes);
UUID uuid = BinaryCodec.INSTANCE.decode(bytes);
String string = Base62Codec.INSTANCE.encode(uuid);
assertEquals(encode(Base62Codec.INSTANCE.getBase(), bytes), string);
Expand All @@ -34,11 +35,11 @@ public void testEncode() {

@Test
public void testDivide() {

SplittableRandom seeder = new SplittableRandom(1);
for (int i = 0; i < 1000; i++) {
byte[] bytes = new byte[UUID_BYTES];
ThreadLocalRandom.current().nextBytes(bytes);
int divisor = ThreadLocalRandom.current().nextInt() & 0x7fffffff; // positive divisor
(new Random(seeder.nextLong())).nextBytes(bytes);
int divisor = seeder.nextInt() & 0x7fffffff; // positive divisor

CustomDivider divider = x -> new long[] { x / divisor, x % divisor };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.SplittableRandom;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;
Expand All @@ -25,10 +25,10 @@ public class AbstRandomBasedFactoryTest extends UuidFactoryTest {

@Test
public void testByteRandomNextLong() {

SplittableRandom seeder = new SplittableRandom(1);
for (int i = 0; i < 10; i++) {
byte[] bytes = new byte[Long.BYTES];
(new Random()).nextBytes(bytes);
(new Random(seeder.nextLong())).nextBytes(bytes);
long number = ByteBuffer.wrap(bytes).getLong();
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.SafeRandom((x) -> bytes);
assertEquals(number, random.nextLong());
Expand All @@ -40,7 +40,7 @@ public void testByteRandomNextLong() {
int size = Long.BYTES * longs;

byte[] bytes = new byte[size];
(new Random()).nextBytes(bytes);
(new Random(seeder.nextLong())).nextBytes(bytes);
ByteBuffer buffer1 = ByteBuffer.wrap(bytes);
ByteBuffer buffer2 = ByteBuffer.wrap(bytes);

Expand All @@ -58,10 +58,10 @@ public void testByteRandomNextLong() {

@Test
public void testByteRandomNextBytes() {

SplittableRandom seeder = new SplittableRandom(1);
for (int i = 0; i < 10; i++) {
byte[] bytes = new byte[Long.BYTES];
(new Random()).nextBytes(bytes);
(new Random(seeder.nextLong())).nextBytes(bytes);
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.SafeRandom((x) -> bytes);
assertEquals(Arrays.toString(bytes), Arrays.toString(random.nextBytes(Long.BYTES)));
}
Expand All @@ -72,7 +72,7 @@ public void testByteRandomNextBytes() {
int size = Long.BYTES * ints;

byte[] bytes = new byte[size];
(new Random()).nextBytes(bytes);
(new Random(seeder.nextLong())).nextBytes(bytes);
ByteBuffer buffer1 = ByteBuffer.wrap(bytes);
ByteBuffer buffer2 = ByteBuffer.wrap(bytes);

Expand All @@ -92,10 +92,10 @@ public void testByteRandomNextBytes() {

@Test
public void testLogRandomNextLong() {

SplittableRandom seeder = new SplittableRandom(1);
for (int i = 0; i < 10; i++) {
byte[] bytes = new byte[Long.BYTES];
(new Random()).nextBytes(bytes);
(new Random(seeder.nextLong())).nextBytes(bytes);
long number = ByteBuffer.wrap(bytes).getLong();
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.FastRandom(() -> number);
assertEquals(number, random.nextLong());
Expand All @@ -107,7 +107,7 @@ public void testLogRandomNextLong() {
int size = Long.BYTES * ints;

byte[] bytes = new byte[size];
(new Random()).nextBytes(bytes);
(new Random(seeder.nextLong())).nextBytes(bytes);
ByteBuffer buffer1 = ByteBuffer.wrap(bytes);
ByteBuffer buffer2 = ByteBuffer.wrap(bytes);

Expand All @@ -122,10 +122,10 @@ public void testLogRandomNextLong() {

@Test
public void testLogRandomNextBytes() {

SplittableRandom seeder = new SplittableRandom(1);
for (int i = 0; i < 10; i++) {
byte[] bytes = new byte[Long.BYTES];
(new Random()).nextBytes(bytes);
(new Random(seeder.nextLong())).nextBytes(bytes);
long number = ByteBuffer.wrap(bytes).getLong();
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.FastRandom(() -> number);
assertEquals(Arrays.toString(bytes), Arrays.toString(random.nextBytes(Long.BYTES)));
Expand All @@ -137,7 +137,7 @@ public void testLogRandomNextBytes() {
int size = Long.BYTES * ints;

byte[] bytes = new byte[size];
(new Random()).nextBytes(bytes);
(new Random(seeder.nextLong())).nextBytes(bytes);
ByteBuffer buffer1 = ByteBuffer.wrap(bytes);
ByteBuffer buffer2 = ByteBuffer.wrap(bytes);

Expand All @@ -163,7 +163,7 @@ public void testLongRandom() {
}

{
long nextLong = ThreadLocalRandom.current().nextLong();
long nextLong = new SplittableRandom(1).nextLong();
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.FastRandom(() -> nextLong);
byte[] bytes = random.nextBytes(Long.BYTES);
ByteBuffer buffer = ByteBuffer.wrap(bytes);
Expand Down Expand Up @@ -198,11 +198,12 @@ public void testLongRandom() {
}

{
SplittableRandom entropy = new SplittableRandom(1);
long[] nextLong = { //
ThreadLocalRandom.current().nextLong(), //
ThreadLocalRandom.current().nextLong(), //
ThreadLocalRandom.current().nextLong(), //
ThreadLocalRandom.current().nextLong() ///
entropy.nextLong(), //
entropy.nextLong(), //
entropy.nextLong(), //
entropy.nextLong() ///
};

int octects = nextLong.length * Byte.SIZE;
Expand Down Expand Up @@ -237,9 +238,10 @@ public void testLongRandomWithFactory() {

{
// RandomBasedFactory
SplittableRandom random = new SplittableRandom(1);
List<Long> list = new ArrayList<>();
list.add(ThreadLocalRandom.current().nextLong());
list.add(ThreadLocalRandom.current().nextLong());
list.add(random.nextLong());
list.add(random.nextLong());
UUID uuid1 = UuidUtil.setVersion(new UUID(list.get(0), list.get(1)), 4);
RandomBasedFactory factory = new RandomBasedFactory(() -> list.remove(0));
UUID uuid2 = factory.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.concurrent.ThreadLocalRandom;
import java.util.SplittableRandom;

import org.junit.Test;

Expand Down Expand Up @@ -39,7 +39,7 @@ public void testSelectNodeIdentifierStrategy() {
fail("It should use Random node identifier supplier");
}

Long number = ThreadLocalRandom.current().nextLong() & 0x0000ffffffffffffL;
Long number = new SplittableRandom(1).nextLong() & 0x0000ffffffffffffL;
SettingsUtil.setProperty(SettingsUtil.PROPERTY_NODE, number.toString());
supplier = AbstTimeBasedFactory.selectNodeIdFunction();
assertEquals(number.longValue(), supplier.getAsLong());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import static org.junit.Assert.*;
import java.util.HashSet;
import java.util.concurrent.ThreadLocalRandom;
import java.util.SplittableRandom;

import com.github.f4b6a3.uuid.factory.function.impl.DefaultClockSeqFunction.ClockSeqPool;

Expand Down Expand Up @@ -125,6 +125,7 @@ public void testClockSequencePool5() throws InterruptedException {
private static class TestThread extends Thread {

private int index;
private static SplittableRandom random = new SplittableRandom(1);
public static int[] list = new int[CLOCK_SEQUENCE_MAX];

private ClockSeqPool pool;
Expand All @@ -136,7 +137,7 @@ public TestThread(ClockSeqPool pool, int index) {

@Override
public void run() {
int take = ThreadLocalRandom.current().nextInt(CLOCK_SEQUENCE_MAX);
int take = random.nextInt(CLOCK_SEQUENCE_MAX);
list[index] = pool.take(take);
}
}
Expand Down
Loading

0 comments on commit 76dac16

Please sign in to comment.