1
1
package com .thealgorithms .datastructures .caches ;
2
2
3
- import org .junit .jupiter .api .BeforeEach ;
4
- import org .junit .jupiter .api .Test ;
5
- import org .junit .jupiter .api .function .Executable ;
6
-
7
- import java .util .ArrayList ;
8
- import java .util .List ;
9
- import java .util .Random ;
10
-
11
3
import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
12
4
import static org .junit .jupiter .api .Assertions .assertEquals ;
13
5
import static org .junit .jupiter .api .Assertions .assertFalse ;
14
6
import static org .junit .jupiter .api .Assertions .assertNull ;
15
7
import static org .junit .jupiter .api .Assertions .assertThrows ;
16
8
import static org .junit .jupiter .api .Assertions .assertTrue ;
17
9
10
+ import java .util .ArrayList ;
11
+ import java .util .HashSet ;
12
+ import java .util .List ;
13
+ import java .util .Random ;
14
+ import java .util .Set ;
15
+ import org .junit .jupiter .api .BeforeEach ;
16
+ import org .junit .jupiter .api .Test ;
17
+ import org .junit .jupiter .api .function .Executable ;
18
+
18
19
class RRCacheTest {
19
20
20
21
private RRCache <String , String > cache ;
21
- private List <String > evictedKeys ;
22
+ private Set <String > evictedKeys ;
22
23
private List <String > evictedValues ;
23
24
24
25
@ BeforeEach
25
26
void setUp () {
26
- evictedKeys = new ArrayList <>();
27
+ evictedKeys = new HashSet <>();
27
28
evictedValues = new ArrayList <>();
28
29
29
30
cache = new RRCache .Builder <String , String >(3 )
30
- .defaultTTL (1000 )
31
- .random (new Random (0 ))
32
- .evictionListener ((k , v ) -> {
33
- evictedKeys .add (k );
34
- evictedValues .add (v );
35
- })
36
- .build ();
31
+ .defaultTTL (1000 )
32
+ .random (new Random (0 ))
33
+ .evictionListener ((k , v ) -> {
34
+ evictedKeys .add (k );
35
+ evictedValues .add (v );
36
+ })
37
+ .build ();
37
38
}
38
39
39
40
@ Test
@@ -148,11 +149,7 @@ void testBuilderNullEvictionListenerThrows() {
148
149
149
150
@ Test
150
151
void testEvictionListenerExceptionDoesNotCrash () {
151
- RRCache <String , String > listenerCache = new RRCache .Builder <String , String >(1 )
152
- .evictionListener ((k , v ) -> {
153
- throw new RuntimeException ("Exception" );
154
- })
155
- .build ();
152
+ RRCache <String , String > listenerCache = new RRCache .Builder <String , String >(1 ).evictionListener ((k , v ) -> { throw new RuntimeException ("Exception" ); }).build ();
156
153
157
154
listenerCache .put ("a" , "a" );
158
155
listenerCache .put ("b" , "b" ); // causes eviction but should not crash
@@ -161,9 +158,7 @@ void testEvictionListenerExceptionDoesNotCrash() {
161
158
162
159
@ Test
163
160
void testTtlZeroThrowsIllegalArgumentException () {
164
- Executable exec = () -> new RRCache .Builder <String , String >(3 )
165
- .defaultTTL (-1 )
166
- .build ();
161
+ Executable exec = () -> new RRCache .Builder <String , String >(3 ).defaultTTL (-1 ).build ();
167
162
assertThrows (IllegalArgumentException .class , exec );
168
163
}
169
164
}
0 commit comments