3
3
import static org .junit .jupiter .api .Assertions .*;
4
4
5
5
import org .junit .jupiter .api .RepeatedTest ;
6
+ import org .junit .jupiter .api .Test ;
6
7
7
8
/**
8
9
* @author Lewys Davies
9
10
*/
10
11
public class ProbabilityCollectionTest {
11
12
12
- @ RepeatedTest (value = 1000 )
13
+ @ RepeatedTest (value = 10_000 )
13
14
public void test_insert () {
14
15
ProbabilityCollection <String > collection = new ProbabilityCollection <>();
15
16
assertEquals (0 , collection .size ());
@@ -44,12 +45,12 @@ public void test_insert() {
44
45
}
45
46
}
46
47
47
- @ RepeatedTest (value = 1000 )
48
+ @ RepeatedTest (value = 10_000 )
48
49
public void test_remove () {
49
50
ProbabilityCollection <String > collection = new ProbabilityCollection <>();
50
- assertTrue ( collection .size () == 0 );
51
+ assertEquals ( 0 , collection .size ());
51
52
assertTrue (collection .isEmpty ());
52
- assertTrue ( collection .getTotalProbability () == 0 );
53
+ assertEquals ( 0 , collection .getTotalProbability ());
53
54
54
55
String t1 = "Hello" ;
55
56
String t2 = "World" ;
@@ -59,33 +60,36 @@ public void test_remove() {
59
60
collection .add (t2 , 10 );
60
61
collection .add (t3 , 10 );
61
62
62
- assertTrue ( collection .size () == 3 );
63
+ assertEquals ( 3 , collection .size ());
63
64
assertFalse (collection .isEmpty ());
64
- assertTrue ( collection .getTotalProbability () == 30 );
65
+ assertEquals ( 30 , collection .getTotalProbability ());
65
66
66
- collection .remove (t2 );
67
+ // Remove t2
68
+ assertTrue (collection .remove (t2 ));
67
69
68
- assertTrue ( collection .size () == 2 );
70
+ assertEquals ( 2 , collection .size ());
69
71
assertFalse (collection .isEmpty ());
70
- assertTrue ( collection .getTotalProbability () == 20 );
72
+ assertEquals ( 20 , collection .getTotalProbability ());
71
73
72
- collection .remove (t1 );
74
+ // Remove t1
75
+ assertTrue (collection .remove (t1 ));
73
76
74
- assertTrue ( collection .size () == 1 );
77
+ assertEquals ( 1 , collection .size ());
75
78
assertFalse (collection .isEmpty ());
76
- assertTrue ( collection .getTotalProbability () == 10 );
79
+ assertEquals ( 10 , collection .getTotalProbability ());
77
80
78
- collection .remove (t3 );
81
+ //Remove t3
82
+ assertTrue (collection .remove (t3 ));
79
83
80
- assertTrue (collection .size () == 0 );
81
- assertTrue (collection .getTotalProbability () == 0 );
84
+ assertEquals (0 , collection .size ());
82
85
assertTrue (collection .isEmpty ());
86
+ assertEquals (0 , collection .getTotalProbability ());
83
87
}
84
88
85
- @ RepeatedTest (value = 1000 )
89
+ @ RepeatedTest (value = 10_000 )
86
90
public void test_remove_duplicates () {
87
91
ProbabilityCollection <String > collection = new ProbabilityCollection <>();
88
- assertTrue ( collection .size () == 0 );
92
+ assertEquals ( 0 , collection .size ());
89
93
assertTrue (collection .isEmpty ());
90
94
91
95
String t1 = "Hello" ;
@@ -104,27 +108,30 @@ public void test_remove_duplicates() {
104
108
collection .add (t3 , 10 );
105
109
}
106
110
107
- assertTrue ( collection .size () == 30 );
111
+ assertEquals ( 30 , collection .size ());
108
112
assertFalse (collection .isEmpty ());
109
- assertTrue ( collection .getTotalProbability () == 300 );
113
+ assertEquals ( 300 , collection .getTotalProbability ());
110
114
111
- collection .remove (t2 );
115
+ //Remove t2
116
+ assertTrue (collection .remove (t2 ));
112
117
113
- assertTrue ( collection .size () == 20 );
118
+ assertEquals ( 20 , collection .size ());
114
119
assertFalse (collection .isEmpty ());
115
- assertTrue ( collection .getTotalProbability () == 200 );
120
+ assertEquals ( 200 , collection .getTotalProbability ());
116
121
117
- collection .remove (t1 );
122
+ // Remove t1
123
+ assertTrue (collection .remove (t1 ));
118
124
119
- assertTrue ( collection .size () == 10 );
125
+ assertEquals ( 10 , collection .size ());
120
126
assertFalse (collection .isEmpty ());
127
+ assertEquals (100 , collection .getTotalProbability ());
121
128
122
- assertTrue ( collection . getTotalProbability () == 100 );
123
- collection .remove (t3 );
129
+ //Remove t3
130
+ assertTrue ( collection .remove (t3 ) );
124
131
125
- assertTrue ( collection .size () == 0 );
132
+ assertEquals ( 0 , collection .size ());
126
133
assertTrue (collection .isEmpty ());
127
- assertTrue ( collection .getTotalProbability () == 0 );
134
+ assertEquals ( 0 , collection .getTotalProbability ());
128
135
}
129
136
130
137
@ RepeatedTest (1_000_000 )
@@ -159,10 +166,82 @@ public void test_probability() {
159
166
double bResult = b / (double ) totalGets * 100 ;
160
167
double cResult = c / (double ) totalGets * 100 ;
161
168
162
- double acceptableDeviation = 1 ;
169
+ double acceptableDeviation = 1 ; // %
163
170
164
171
assertTrue (Math .abs (aProb - aResult ) <= acceptableDeviation );
165
172
assertTrue (Math .abs (bProb - bResult ) <= acceptableDeviation );
166
173
assertTrue (Math .abs (cProb - cResult ) <= acceptableDeviation );
167
174
}
175
+
176
+ @ RepeatedTest (1_000_000 )
177
+ public void test_get_never_null () {
178
+ ProbabilityCollection <String > collection = new ProbabilityCollection <>();
179
+ // Tests get will never return null
180
+ // Just one smallest element get, must not return null
181
+ collection .add ("A" , 1 );
182
+ assertNotNull (collection .get ());
183
+
184
+ // Reset state
185
+ collection .remove ("A" );
186
+ assertEquals (0 , collection .size ());
187
+ assertTrue (collection .isEmpty ());
188
+
189
+ // Just one large element, must not return null
190
+ collection .add ("A" , 5_000_000 );
191
+ assertNotNull (collection .get ());
192
+ }
193
+
194
+ @ Test
195
+ public void test_Errors () {
196
+ ProbabilityCollection <String > collection = new ProbabilityCollection <>();
197
+
198
+ assertEquals (0 , collection .size ());
199
+ assertTrue (collection .isEmpty ());
200
+ assertEquals (0 , collection .getTotalProbability ());
201
+
202
+ // Cannot get from empty collection
203
+ assertThrows (IllegalStateException .class , () -> {
204
+ collection .get ();
205
+ });
206
+
207
+ assertEquals (0 , collection .size ());
208
+ assertTrue (collection .isEmpty ());
209
+ assertEquals (0 , collection .getTotalProbability ());
210
+
211
+ // Cannot add null object
212
+ assertThrows (IllegalArgumentException .class , () -> {
213
+ collection .add (null , 1 );
214
+ });
215
+
216
+ assertEquals (0 , collection .size ());
217
+ assertTrue (collection .isEmpty ());
218
+ assertEquals (0 , collection .getTotalProbability ());
219
+
220
+ // Cannot add prob 0
221
+ assertThrows (IllegalArgumentException .class , () -> {
222
+ collection .add ("A" , 0 );
223
+ });
224
+
225
+ assertEquals (0 , collection .size ());
226
+ assertTrue (collection .isEmpty ());
227
+ assertEquals (0 , collection .getTotalProbability ());
228
+
229
+ // Cannot remove null
230
+ assertThrows (IllegalArgumentException .class , () -> {
231
+ collection .remove (null );
232
+ });
233
+
234
+ assertEquals (0 , collection .size ());
235
+ assertTrue (collection .isEmpty ());
236
+ assertEquals (0 , collection .getTotalProbability ());
237
+
238
+ // Cannot contains null
239
+ assertThrows (IllegalArgumentException .class , () -> {
240
+ collection .contains (null );
241
+ });
242
+
243
+ assertEquals (0 , collection .size ());
244
+ assertTrue (collection .isEmpty ());
245
+ assertEquals (0 , collection .getTotalProbability ());
246
+ }
168
247
}
0 commit comments