|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.BDDAssertions.then; |
4 | 4 |
|
| 5 | +import java.util.Random; |
| 6 | + |
| 7 | +import net.jqwik.api.ForAll; |
5 | 8 | import net.jqwik.api.Property; |
| 9 | +import net.jqwik.api.constraints.LongRange; |
6 | 10 |
|
7 | 11 | class DeterministicRandomTest { |
8 | 12 |
|
9 | 13 | @Property |
10 | | - void basicTest() { |
11 | | - DeterministicRandom deterministicRandom = new DeterministicRandom(12345L); |
12 | | - then(deterministicRandom).isNotNull(); |
| 14 | + void nextLongCreatesRandomInstanceInCache(@ForAll @LongRange(min = 1L) long initialSeed) { |
| 15 | + // given |
| 16 | + DeterministicRandom.getSeedCache().clear(); |
| 17 | + DeterministicRandom deterministicRandom = new DeterministicRandom(initialSeed); |
| 18 | + |
| 19 | + // when |
| 20 | + long seed = deterministicRandom.nextLong(); |
| 21 | + |
| 22 | + // then |
| 23 | + then(DeterministicRandom.getSeedCache()).containsKey(seed); |
| 24 | + then(DeterministicRandom.getSeedCache().get(seed)).isNotNull(); |
| 25 | + } |
| 26 | + |
| 27 | + @Property |
| 28 | + void getRandomInstanceRetrievesCachedInstance(@ForAll @LongRange(min = 1L) long initialSeed) { |
| 29 | + // given |
| 30 | + DeterministicRandom.getSeedCache().clear(); |
| 31 | + DeterministicRandom deterministicRandom = new DeterministicRandom(initialSeed); |
| 32 | + long seed = deterministicRandom.nextLong(); |
| 33 | + |
| 34 | + // when |
| 35 | + Random cachedRandom = deterministicRandom.getRandomInstance(seed); |
| 36 | + |
| 37 | + // then |
| 38 | + then(cachedRandom).isNotNull(); |
| 39 | + then(cachedRandom).isSameAs(DeterministicRandom.getSeedCache().get(seed)); |
| 40 | + } |
| 41 | + |
| 42 | + @Property |
| 43 | + void getCurrentSeedRandomReturnsCurrentSeedRandom(@ForAll @LongRange(min = 1L) long initialSeed) { |
| 44 | + // given |
| 45 | + DeterministicRandom.getSeedCache().clear(); |
| 46 | + DeterministicRandom deterministicRandom = new DeterministicRandom(initialSeed); |
| 47 | + |
| 48 | + // when |
| 49 | + long firstSeed = deterministicRandom.nextLong(); |
| 50 | + Random firstRandom = deterministicRandom.getCurrentSeedRandom(); |
| 51 | + |
| 52 | + long secondSeed = deterministicRandom.nextLong(); |
| 53 | + Random secondRandom = deterministicRandom.getCurrentSeedRandom(); |
| 54 | + |
| 55 | + // then |
| 56 | + then(firstRandom).isSameAs(deterministicRandom.getRandomInstance(firstSeed)); |
| 57 | + then(secondRandom).isSameAs(deterministicRandom.getRandomInstance(secondSeed)); |
| 58 | + then(firstRandom).isNotSameAs(secondRandom); |
| 59 | + } |
| 60 | + |
| 61 | + @Property |
| 62 | + void cacheSizeIsLimitedTo32(@ForAll @LongRange(min = 1L) long initialSeed) { |
| 63 | + // given |
| 64 | + DeterministicRandom.getSeedCache().clear(); |
| 65 | + DeterministicRandom deterministicRandom = new DeterministicRandom(initialSeed); |
| 66 | + |
| 67 | + // when |
| 68 | + for (int i = 0; i < 35; i++) { |
| 69 | + deterministicRandom.nextLong(); |
| 70 | + } |
| 71 | + |
| 72 | + // then |
| 73 | + then(DeterministicRandom.getSeedCache().size()).isLessThanOrEqualTo(32); |
| 74 | + } |
| 75 | + |
| 76 | + @Property |
| 77 | + void sameInitialSeedProducesSameLongSequence(@ForAll @LongRange(min = 1L) long initialSeed) { |
| 78 | + // given |
| 79 | + DeterministicRandom.getSeedCache().clear(); |
| 80 | + DeterministicRandom random1 = new DeterministicRandom(initialSeed); |
| 81 | + DeterministicRandom random2 = new DeterministicRandom(initialSeed); |
| 82 | + |
| 83 | + // when |
| 84 | + long value1 = random1.nextLong(); |
| 85 | + long value2 = random2.nextLong(); |
| 86 | + |
| 87 | + // then |
| 88 | + then(value1).isEqualTo(value2); |
| 89 | + } |
| 90 | + |
| 91 | + @Property |
| 92 | + void cachedRandomInstanceProducesDeterministicValues(@ForAll @LongRange(min = 1L) long initialSeed) { |
| 93 | + // given |
| 94 | + DeterministicRandom.getSeedCache().clear(); |
| 95 | + DeterministicRandom deterministicRandom = new DeterministicRandom(initialSeed); |
| 96 | + long seed = deterministicRandom.nextLong(); |
| 97 | + |
| 98 | + // when |
| 99 | + Random cached1 = deterministicRandom.getRandomInstance(seed); |
| 100 | + Random cached2 = deterministicRandom.getRandomInstance(seed); |
| 101 | + |
| 102 | + // then |
| 103 | + then(cached1).isSameAs(cached2); |
| 104 | + int value1 = cached1.nextInt(); |
| 105 | + int value2 = cached2.nextInt(); |
| 106 | + then(value1).isNotEqualTo(value2); // Same instance, so state progresses |
| 107 | + } |
| 108 | + |
| 109 | + @Property |
| 110 | + void multipleSeedsAreStoredIndependently(@ForAll @LongRange(min = 1L) long initialSeed) { |
| 111 | + // given |
| 112 | + DeterministicRandom.getSeedCache().clear(); |
| 113 | + DeterministicRandom deterministicRandom = new DeterministicRandom(initialSeed); |
| 114 | + |
| 115 | + // when |
| 116 | + long seed1 = deterministicRandom.nextLong(); |
| 117 | + long seed2 = deterministicRandom.nextLong(); |
| 118 | + long seed3 = deterministicRandom.nextLong(); |
| 119 | + |
| 120 | + // then |
| 121 | + then(DeterministicRandom.getSeedCache()).containsKeys(seed1, seed2, seed3); |
| 122 | + then(deterministicRandom.getRandomInstance(seed1)) |
| 123 | + .isNotSameAs(deterministicRandom.getRandomInstance(seed2)); |
| 124 | + then(deterministicRandom.getRandomInstance(seed2)) |
| 125 | + .isNotSameAs(deterministicRandom.getRandomInstance(seed3)); |
13 | 126 | } |
14 | 127 | } |
0 commit comments