11package com .thealgorithms .datastructures .caches ;
22
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-
103import java .util .ArrayList ;
114import java .util .HashSet ;
125import java .util .List ;
136import java .util .Random ;
147import java .util .Set ;
8+ import org .junit .jupiter .api .Assertions ;
159import org .junit .jupiter .api .BeforeEach ;
1610import org .junit .jupiter .api .Test ;
1711import org .junit .jupiter .api .function .Executable ;
@@ -40,22 +34,22 @@ void setUp() {
4034 @ Test
4135 void testPutAndGet () {
4236 cache .put ("a" , "apple" );
43- assertEquals ("apple" , cache .get ("a" ));
37+ Assertions . assertEquals ("apple" , cache .get ("a" ));
4438 }
4539
4640 @ Test
4741 void testOverwriteValue () {
4842 cache .put ("a" , "apple" );
4943 cache .put ("a" , "avocado" );
50- assertEquals ("avocado" , cache .get ("a" ));
44+ Assertions . assertEquals ("avocado" , cache .get ("a" ));
5145 }
5246
5347 @ Test
5448 void testExpiration () throws InterruptedException {
5549 cache .put ("temp" , "value" , 100 ); // short TTL
5650 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" ));
5953 }
6054
6155 @ Test
@@ -66,9 +60,9 @@ void testEvictionOnCapacity() {
6660 cache .put ("d" , "delta" ); // triggers eviction
6761
6862 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 ());
7266 }
7367
7468 @ Test
@@ -78,17 +72,17 @@ void testEvictionListener() {
7872 cache .put ("z" , "three" );
7973 cache .put ("w" , "four" ); // one of x, y, z will be evicted
8074
81- assertFalse (evictedKeys .isEmpty ());
82- assertFalse (evictedValues .isEmpty ());
75+ Assertions . assertFalse (evictedKeys .isEmpty ());
76+ Assertions . assertFalse (evictedValues .isEmpty ());
8377 }
8478
8579 @ Test
8680 void testHitsAndMisses () {
8781 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 ());
9286 }
9387
9488 @ Test
@@ -97,7 +91,7 @@ void testSizeExcludesExpired() throws InterruptedException {
9791 cache .put ("b" , "b" , 100 );
9892 cache .put ("c" , "c" , 100 );
9993 Thread .sleep (150 );
100- assertEquals (0 , cache .size ());
94+ Assertions . assertEquals (0 , cache .size ());
10195 }
10296
10397 @ Test
@@ -106,45 +100,45 @@ void testToStringDoesNotExposeExpired() throws InterruptedException {
106100 cache .put ("dead" , "gone" , 100 );
107101 Thread .sleep (150 );
108102 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" ));
111105 }
112106
113107 @ Test
114108 void testNullKeyGetThrows () {
115- assertThrows (IllegalArgumentException .class , () -> cache .get (null ));
109+ Assertions . assertThrows (IllegalArgumentException .class , () -> cache .get (null ));
116110 }
117111
118112 @ Test
119113 void testPutNullKeyThrows () {
120- assertThrows (IllegalArgumentException .class , () -> cache .put (null , "v" ));
114+ Assertions . assertThrows (IllegalArgumentException .class , () -> cache .put (null , "v" ));
121115 }
122116
123117 @ Test
124118 void testPutNullValueThrows () {
125- assertThrows (IllegalArgumentException .class , () -> cache .put ("k" , null ));
119+ Assertions . assertThrows (IllegalArgumentException .class , () -> cache .put ("k" , null ));
126120 }
127121
128122 @ Test
129123 void testPutNegativeTTLThrows () {
130- assertThrows (IllegalArgumentException .class , () -> cache .put ("k" , "v" , -1 ));
124+ Assertions . assertThrows (IllegalArgumentException .class , () -> cache .put ("k" , "v" , -1 ));
131125 }
132126
133127 @ Test
134128 void testBuilderNegativeCapacityThrows () {
135- assertThrows (IllegalArgumentException .class , () -> new RRCache .Builder <>(0 ));
129+ Assertions . assertThrows (IllegalArgumentException .class , () -> new RRCache .Builder <>(0 ));
136130 }
137131
138132 @ Test
139133 void testBuilderNullRandomThrows () {
140134 RRCache .Builder <String , String > builder = new RRCache .Builder <>(1 );
141- assertThrows (IllegalArgumentException .class , () -> builder .random (null ));
135+ Assertions . assertThrows (IllegalArgumentException .class , () -> builder .random (null ));
142136 }
143137
144138 @ Test
145139 void testBuilderNullEvictionListenerThrows () {
146140 RRCache .Builder <String , String > builder = new RRCache .Builder <>(1 );
147- assertThrows (IllegalArgumentException .class , () -> builder .evictionListener (null ));
141+ Assertions . assertThrows (IllegalArgumentException .class , () -> builder .evictionListener (null ));
148142 }
149143
150144 @ Test
@@ -153,12 +147,12 @@ void testEvictionListenerExceptionDoesNotCrash() {
153147
154148 listenerCache .put ("a" , "a" );
155149 listenerCache .put ("b" , "b" ); // causes eviction but should not crash
156- assertDoesNotThrow (() -> listenerCache .get ("a" ));
150+ Assertions . assertDoesNotThrow (() -> listenerCache .get ("a" ));
157151 }
158152
159153 @ Test
160154 void testTtlZeroThrowsIllegalArgumentException () {
161155 Executable exec = () -> new RRCache .Builder <String , String >(3 ).defaultTTL (-1 ).build ();
162- assertThrows (IllegalArgumentException .class , exec );
156+ Assertions . assertThrows (IllegalArgumentException .class , exec );
163157 }
164158}
0 commit comments