1
1
package com .thealgorithms .datastructures .caches ;
2
2
3
- import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
4
- import static org .junit .jupiter .api .Assertions .assertEquals ;
5
- import static org .junit .jupiter .api .Assertions .assertFalse ;
6
- import static org .junit .jupiter .api .Assertions .assertNull ;
7
- import static org .junit .jupiter .api .Assertions .assertThrows ;
8
- import static org .junit .jupiter .api .Assertions .assertTrue ;
9
-
10
3
import java .util .ArrayList ;
11
4
import java .util .HashSet ;
12
5
import java .util .List ;
13
6
import java .util .Random ;
14
7
import java .util .Set ;
8
+ import org .junit .jupiter .api .Assertions ;
15
9
import org .junit .jupiter .api .BeforeEach ;
16
10
import org .junit .jupiter .api .Test ;
17
11
import org .junit .jupiter .api .function .Executable ;
@@ -40,22 +34,22 @@ void setUp() {
40
34
@ Test
41
35
void testPutAndGet () {
42
36
cache .put ("a" , "apple" );
43
- assertEquals ("apple" , cache .get ("a" ));
37
+ Assertions . assertEquals ("apple" , cache .get ("a" ));
44
38
}
45
39
46
40
@ Test
47
41
void testOverwriteValue () {
48
42
cache .put ("a" , "apple" );
49
43
cache .put ("a" , "avocado" );
50
- assertEquals ("avocado" , cache .get ("a" ));
44
+ Assertions . assertEquals ("avocado" , cache .get ("a" ));
51
45
}
52
46
53
47
@ Test
54
48
void testExpiration () throws InterruptedException {
55
49
cache .put ("temp" , "value" , 100 ); // short TTL
56
50
Thread .sleep (200 );
57
- assertNull (cache .get ("temp" ));
58
- assertTrue (evictedKeys .contains ("temp" ));
51
+ Assertions . assertNull (cache .get ("temp" ));
52
+ Assertions . assertTrue (evictedKeys .contains ("temp" ));
59
53
}
60
54
61
55
@ Test
@@ -66,9 +60,9 @@ void testEvictionOnCapacity() {
66
60
cache .put ("d" , "delta" ); // triggers eviction
67
61
68
62
int size = cache .size ();
69
- assertEquals (3 , size );
70
- assertEquals (1 , evictedKeys .size ());
71
- assertEquals (1 , evictedValues .size ());
63
+ Assertions . assertEquals (3 , size );
64
+ Assertions . assertEquals (1 , evictedKeys .size ());
65
+ Assertions . assertEquals (1 , evictedValues .size ());
72
66
}
73
67
74
68
@ Test
@@ -78,17 +72,17 @@ void testEvictionListener() {
78
72
cache .put ("z" , "three" );
79
73
cache .put ("w" , "four" ); // one of x, y, z will be evicted
80
74
81
- assertFalse (evictedKeys .isEmpty ());
82
- assertFalse (evictedValues .isEmpty ());
75
+ Assertions . assertFalse (evictedKeys .isEmpty ());
76
+ Assertions . assertFalse (evictedValues .isEmpty ());
83
77
}
84
78
85
79
@ Test
86
80
void testHitsAndMisses () {
87
81
cache .put ("a" , "apple" );
88
- assertEquals ("apple" , cache .get ("a" ));
89
- assertNull (cache .get ("b" ));
90
- assertEquals (1 , cache .getHits ());
91
- assertEquals (1 , cache .getMisses ());
82
+ Assertions . assertEquals ("apple" , cache .get ("a" ));
83
+ Assertions . assertNull (cache .get ("b" ));
84
+ Assertions . assertEquals (1 , cache .getHits ());
85
+ Assertions . assertEquals (1 , cache .getMisses ());
92
86
}
93
87
94
88
@ Test
@@ -97,7 +91,7 @@ void testSizeExcludesExpired() throws InterruptedException {
97
91
cache .put ("b" , "b" , 100 );
98
92
cache .put ("c" , "c" , 100 );
99
93
Thread .sleep (150 );
100
- assertEquals (0 , cache .size ());
94
+ Assertions . assertEquals (0 , cache .size ());
101
95
}
102
96
103
97
@ Test
@@ -106,45 +100,45 @@ void testToStringDoesNotExposeExpired() throws InterruptedException {
106
100
cache .put ("dead" , "gone" , 100 );
107
101
Thread .sleep (150 );
108
102
String result = cache .toString ();
109
- assertTrue (result .contains ("live" ));
110
- assertFalse (result .contains ("dead" ));
103
+ Assertions . assertTrue (result .contains ("live" ));
104
+ Assertions . assertFalse (result .contains ("dead" ));
111
105
}
112
106
113
107
@ Test
114
108
void testNullKeyGetThrows () {
115
- assertThrows (IllegalArgumentException .class , () -> cache .get (null ));
109
+ Assertions . assertThrows (IllegalArgumentException .class , () -> cache .get (null ));
116
110
}
117
111
118
112
@ Test
119
113
void testPutNullKeyThrows () {
120
- assertThrows (IllegalArgumentException .class , () -> cache .put (null , "v" ));
114
+ Assertions . assertThrows (IllegalArgumentException .class , () -> cache .put (null , "v" ));
121
115
}
122
116
123
117
@ Test
124
118
void testPutNullValueThrows () {
125
- assertThrows (IllegalArgumentException .class , () -> cache .put ("k" , null ));
119
+ Assertions . assertThrows (IllegalArgumentException .class , () -> cache .put ("k" , null ));
126
120
}
127
121
128
122
@ Test
129
123
void testPutNegativeTTLThrows () {
130
- assertThrows (IllegalArgumentException .class , () -> cache .put ("k" , "v" , -1 ));
124
+ Assertions . assertThrows (IllegalArgumentException .class , () -> cache .put ("k" , "v" , -1 ));
131
125
}
132
126
133
127
@ Test
134
128
void testBuilderNegativeCapacityThrows () {
135
- assertThrows (IllegalArgumentException .class , () -> new RRCache .Builder <>(0 ));
129
+ Assertions . assertThrows (IllegalArgumentException .class , () -> new RRCache .Builder <>(0 ));
136
130
}
137
131
138
132
@ Test
139
133
void testBuilderNullRandomThrows () {
140
134
RRCache .Builder <String , String > builder = new RRCache .Builder <>(1 );
141
- assertThrows (IllegalArgumentException .class , () -> builder .random (null ));
135
+ Assertions . assertThrows (IllegalArgumentException .class , () -> builder .random (null ));
142
136
}
143
137
144
138
@ Test
145
139
void testBuilderNullEvictionListenerThrows () {
146
140
RRCache .Builder <String , String > builder = new RRCache .Builder <>(1 );
147
- assertThrows (IllegalArgumentException .class , () -> builder .evictionListener (null ));
141
+ Assertions . assertThrows (IllegalArgumentException .class , () -> builder .evictionListener (null ));
148
142
}
149
143
150
144
@ Test
@@ -153,12 +147,12 @@ void testEvictionListenerExceptionDoesNotCrash() {
153
147
154
148
listenerCache .put ("a" , "a" );
155
149
listenerCache .put ("b" , "b" ); // causes eviction but should not crash
156
- assertDoesNotThrow (() -> listenerCache .get ("a" ));
150
+ Assertions . assertDoesNotThrow (() -> listenerCache .get ("a" ));
157
151
}
158
152
159
153
@ Test
160
154
void testTtlZeroThrowsIllegalArgumentException () {
161
155
Executable exec = () -> new RRCache .Builder <String , String >(3 ).defaultTTL (-1 ).build ();
162
- assertThrows (IllegalArgumentException .class , exec );
156
+ Assertions . assertThrows (IllegalArgumentException .class , exec );
163
157
}
164
158
}
0 commit comments