|
2 | 2 |
|
3 | 3 | import static org.junit.Assert.*;
|
4 | 4 |
|
| 5 | +import java.util.HashSet; |
5 | 6 | import java.util.Set;
|
6 | 7 | import java.util.TreeSet;
|
7 | 8 |
|
8 | 9 | import org.junit.After;
|
9 | 10 | import org.junit.Before;
|
10 | 11 | import org.junit.Test;
|
| 12 | +import org.thejavaguy.prng.generators.util.IntRange; |
11 | 13 |
|
12 | 14 | public abstract class CommonTests {
|
13 | 15 | private PRNG.Smart sut;
|
@@ -37,119 +39,145 @@ public void nextDouble_IsAlwaysGEQ0andL1() {
|
37 | 39 | @Test
|
38 | 40 | public void nextInt_WithUpperLimit_IsAlwaysWithinLimit() {
|
39 | 41 | int reps = 10_000_000;
|
| 42 | + IntRange range = new IntRange(0, 10); |
40 | 43 | Set<Integer> results = new TreeSet<>();
|
41 | 44 | for (int i = 0; i < reps; ++i) {
|
42 | 45 | int actual = sut.nextInt(10);
|
43 | 46 | results.add(actual);
|
44 |
| - assertTrue(actual >= 0); |
45 |
| - assertTrue(actual <= 10); |
| 47 | + assertTrue(range.contains(actual)); |
46 | 48 | }
|
47 |
| - assertEquals(11, results.size()); |
| 49 | + assertEquals(range.size(), results.size()); |
48 | 50 | }
|
49 | 51 |
|
50 | 52 | @Test
|
51 | 53 | public void nextInt_WithUpperLimit_IsAlwaysWithinLimit2() {
|
52 | 54 | int reps = 10_000_000;
|
| 55 | + IntRange range = new IntRange(0, 1); |
53 | 56 | for (int i = 0; i < reps; ++i) {
|
54 | 57 | int actual = sut.nextInt(1);
|
55 |
| - assertTrue(actual >= 0); |
56 |
| - assertTrue(actual <= 1); |
| 58 | + assertTrue(range.contains(actual)); |
57 | 59 | }
|
58 | 60 | }
|
59 | 61 |
|
60 | 62 | @Test
|
61 | 63 | public void nextInt_WithNegativeLimits_IsAlwaysWithinLimits() {
|
62 | 64 | int reps = 10_000_000;
|
| 65 | + IntRange range = new IntRange(-10, -1); |
63 | 66 | Set<Integer> results = new TreeSet<>();
|
64 | 67 | for (int i = 0; i < reps; ++i) {
|
65 |
| - int actual = sut.nextInt(-10, -1); |
| 68 | + int actual = sut.nextInt(range); |
66 | 69 | results.add(Integer.valueOf(actual));
|
67 |
| - assertTrue(actual >= -10); |
68 |
| - assertTrue(actual <= -1); |
| 70 | + assertTrue(range.contains(actual)); |
69 | 71 | }
|
70 |
| - assertEquals(10, results.size()); |
| 72 | + assertEquals(range.size(), results.size()); |
71 | 73 | }
|
72 | 74 |
|
73 | 75 | @Test
|
74 | 76 | public void nextByte_IsAlwaysWithinLimits() {
|
75 | 77 | int reps = 10_000_000;
|
| 78 | + IntRange range = new IntRange(Byte.MIN_VALUE, Byte.MAX_VALUE); |
76 | 79 | Set<Integer> results = new TreeSet<>();
|
77 | 80 | for (int i = 0; i < reps; ++i) {
|
78 | 81 | byte actual = sut.nextByte();
|
79 | 82 | results.add(Integer.valueOf(actual));
|
80 |
| - assertTrue(actual >= -128); |
81 |
| - assertTrue(actual <= 127); |
| 83 | + assertTrue(range.contains(actual)); |
| 84 | + } |
| 85 | + assertEquals(range.size(), results.size()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void nextShort_IsAlwaysWithinLimits() { |
| 90 | + int reps = 10_000_000; |
| 91 | + IntRange range = new IntRange(Short.MIN_VALUE, Short.MAX_VALUE); |
| 92 | + Set<Short> results = new HashSet<>(); |
| 93 | + for (int i = 0; i < reps; ++i) { |
| 94 | + short actual = sut.nextShort(); |
| 95 | + results.add(Short.valueOf(actual)); |
| 96 | + assertTrue(range.contains(actual)); |
82 | 97 | }
|
83 |
| - assertEquals(256, results.size()); |
| 98 | + assertEquals(range.size(), results.size()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void nextChar_IsAlwaysWithinLimits() { |
| 103 | + int reps = 10_000_000; |
| 104 | + IntRange range = new IntRange(Character.MIN_VALUE, Character.MAX_VALUE); |
| 105 | + Set<Character> results = new HashSet<>(); |
| 106 | + for (int i = 0; i < reps; ++i) { |
| 107 | + char actual = sut.nextChar(); |
| 108 | + results.add(Character.valueOf(actual)); |
| 109 | + assertTrue(range.contains(actual)); |
| 110 | + } |
| 111 | + assertEquals(range.size(), results.size()); |
84 | 112 | }
|
85 | 113 |
|
86 | 114 | @Test
|
87 | 115 | public void nextInt_WithNegativeEdgeLimits_IsAlwaysWithinLimits() {
|
88 | 116 | int reps = 10_000_000;
|
| 117 | + IntRange range = new IntRange(Integer.MIN_VALUE, 0); |
89 | 118 | for (int i = 0; i < reps; ++i) {
|
90 |
| - int actual = sut.nextInt(Integer.MIN_VALUE, 0); |
91 |
| - assertTrue(actual >= Integer.MIN_VALUE); |
92 |
| - assertTrue(actual <= 0); |
| 119 | + int actual = sut.nextInt(range); |
| 120 | + assertTrue(range.contains(actual)); |
93 | 121 | }
|
94 | 122 | }
|
95 | 123 |
|
96 | 124 | @Test
|
97 | 125 | public void nextInt_WithPositiveEdgeLimits_IsAlwaysWithinLimits() {
|
98 | 126 | int reps = 10_000_000;
|
| 127 | + IntRange range = new IntRange(0, Integer.MAX_VALUE); |
99 | 128 | for (int i = 0; i < reps; ++i) {
|
100 |
| - int actual = sut.nextInt(0, Integer.MAX_VALUE); |
101 |
| - assertTrue(actual >= 0); |
102 |
| - assertTrue(actual <= Integer.MAX_VALUE); |
| 129 | + int actual = sut.nextInt(range); |
| 130 | + assertTrue(range.contains(actual)); |
103 | 131 | }
|
104 | 132 | }
|
105 | 133 |
|
106 | 134 | @Test
|
107 | 135 | public void nextInt_WithEdgeLimits_IsAlwaysWithinLimits() {
|
108 | 136 | int reps = 10_000_000;
|
| 137 | + IntRange range = new IntRange(Integer.MIN_VALUE, Integer.MAX_VALUE); |
109 | 138 | for (int i = 0; i < reps; ++i) {
|
110 |
| - int actual = sut.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE); |
111 |
| - assertTrue(actual >= Integer.MIN_VALUE); |
112 |
| - assertTrue(actual <= Integer.MAX_VALUE); |
| 139 | + int actual = sut.nextInt(range); |
| 140 | + assertTrue(range.contains(actual)); |
113 | 141 | }
|
114 | 142 | }
|
115 | 143 |
|
116 | 144 | @Test
|
117 | 145 | public void nextInt_WithWorstNegativeEdgeLimits_IsAlwaysWithinLimits() {
|
118 | 146 | int reps = 10_000_000;
|
| 147 | + IntRange range = new IntRange(Integer.MIN_VALUE, Integer.MIN_VALUE + 1); |
119 | 148 | for (int i = 0; i < reps; ++i) {
|
120 |
| - int actual = sut.nextInt(Integer.MIN_VALUE, Integer.MIN_VALUE + 1); |
121 |
| - assertTrue(actual >= Integer.MIN_VALUE); |
122 |
| - assertTrue(actual <= Integer.MIN_VALUE + 1); |
| 149 | + int actual = sut.nextInt(range); |
| 150 | + assertTrue(range.contains(actual)); |
123 | 151 | }
|
124 | 152 | }
|
125 | 153 |
|
126 | 154 | @Test
|
127 | 155 | public void nextInt_WithWorstPositiveEdgeLimits_IsAlwaysWithinLimits() {
|
128 | 156 | int reps = 10_000_000;
|
| 157 | + IntRange range = new IntRange(Integer.MAX_VALUE - 1, Integer.MAX_VALUE); |
129 | 158 | for (int i = 0; i < reps; ++i) {
|
130 |
| - int actual = sut.nextInt(Integer.MAX_VALUE - 1, Integer.MAX_VALUE); |
131 |
| - assertTrue(actual >= Integer.MAX_VALUE - 1); |
132 |
| - assertTrue(actual <= Integer.MAX_VALUE); |
| 159 | + int actual = sut.nextInt(range); |
| 160 | + assertTrue(range.contains(actual)); |
133 | 161 | }
|
134 | 162 | }
|
135 | 163 |
|
136 | 164 | @Test
|
137 | 165 | public void nextInt_WithPossibleNegativeOverflow_IsAlwaysWithinLimits() {
|
138 | 166 | int reps = 10_000_000;
|
| 167 | + IntRange range = new IntRange(Integer.MIN_VALUE, 1); |
139 | 168 | for (int i = 0; i < reps; ++i) {
|
140 |
| - int actual = sut.nextInt(Integer.MIN_VALUE, 1); |
141 |
| - assertTrue(actual >= Integer.MIN_VALUE); |
142 |
| - assertTrue(actual <= 1); |
| 169 | + int actual = sut.nextInt(range); |
| 170 | + assertTrue(range.contains(actual)); |
143 | 171 | }
|
144 | 172 | }
|
145 | 173 |
|
146 | 174 | @Test
|
147 | 175 | public void nextInt_WithPossiblePositiveOverflow_IsAlwaysWithinLimits() {
|
148 | 176 | int reps = 10_000_000;
|
| 177 | + IntRange range = new IntRange(-1, Integer.MAX_VALUE); |
149 | 178 | for (int i = 0; i < reps; ++i) {
|
150 |
| - int actual = sut.nextInt(-1, Integer.MAX_VALUE); |
151 |
| - assertTrue(actual >= -1); |
152 |
| - assertTrue(actual <= Integer.MAX_VALUE); |
| 179 | + int actual = sut.nextInt(range); |
| 180 | + assertTrue(range.contains(actual)); |
153 | 181 | }
|
154 | 182 | }
|
155 | 183 | }
|
0 commit comments