Skip to content

Commit 0659a05

Browse files
committed
Ran clang-format, make tests deterministic
1 parent 3b18922 commit 0659a05

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

src/main/java/com/thealgorithms/datastructures/caches/RRCache.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ private int evictExpired() {
212212
notifyEviction(k, entry.value);
213213
}
214214
}
215-
216215
return expiredCount;
217216
}
218217

@@ -303,6 +302,15 @@ public int size() {
303302
}
304303
}
305304

305+
/**
306+
* Returns the current {@link EvictionStrategy} used by this cache instance.
307+
308+
* @return the eviction strategy currently assigned to this cache
309+
*/
310+
public EvictionStrategy<K, V> getEvictionStrategy() {
311+
return evictionStrategy;
312+
}
313+
306314
/**
307315
* Returns a string representation of the cache, including metadata and current non-expired entries.
308316
*
@@ -355,7 +363,8 @@ public interface EvictionStrategy<K, V> {
355363
* @param <V> the type of values
356364
*/
357365
public static class NoEvictionStrategy<K, V> implements EvictionStrategy<K, V> {
358-
@Override public int onAccess(RRCache<K, V> cache) {
366+
@Override
367+
public int onAccess(RRCache<K, V> cache) {
359368
return cache.evictExpired();
360369
}
361370
}

src/test/java/com/thealgorithms/datastructures/caches/RRCacheTest.java

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,15 @@ void testPeriodicEvictionStrategyEvictsAtInterval() throws InterruptedException
164164
.build();
165165

166166
periodicCache.put("x", "1");
167-
int ev1 = periodicCache.size();
168-
int ev2 = periodicCache.size();
169-
Thread.sleep(50);
170-
int ev3 = periodicCache.size();
171-
172-
Assertions.assertEquals(1, ev1);
173-
Assertions.assertEquals(1, ev2);
174-
Assertions.assertEquals(0, ev3, "Eviction should happen on the 3rd access");
175-
Assertions.assertEquals(0, cache.size());
167+
Thread.sleep(100);
168+
int ev1 = periodicCache.getEvictionStrategy().onAccess(periodicCache);
169+
int ev2 = periodicCache.getEvictionStrategy().onAccess(periodicCache);
170+
int ev3 = periodicCache.getEvictionStrategy().onAccess(periodicCache);
171+
172+
Assertions.assertEquals(0, ev1);
173+
Assertions.assertEquals(0, ev2);
174+
Assertions.assertEquals(1, ev3, "Eviction should happen on the 3rd access");
175+
Assertions.assertEquals(0, periodicCache.size());
176176
}
177177

178178
@Test
@@ -193,10 +193,10 @@ void testNoEvictionStrategyEvictsOnEachCall() throws InterruptedException {
193193
.build();
194194

195195
noEvictionStrategyCache.put("x", "1");
196-
Thread.sleep(50);
197-
int size = noEvictionStrategyCache.size();
196+
Thread.sleep(100);
197+
int evicted = noEvictionStrategyCache.getEvictionStrategy().onAccess(noEvictionStrategyCache);
198198

199-
Assertions.assertEquals(0, size);
199+
Assertions.assertEquals(1, evicted);
200200
}
201201

202202
@Test
@@ -208,4 +208,37 @@ void testBuilderThrowsExceptionIfEvictionStrategyNull() {
208208

209209
Assertions.assertThrows(IllegalArgumentException.class, executable);
210210
}
211+
212+
213+
@Test
214+
void testReturnsCorrectStrategyInstance() {
215+
RRCache.EvictionStrategy<String, String> strategy = new RRCache.NoEvictionStrategy<>();
216+
217+
RRCache<String, String> newCache = new RRCache.Builder<String, String>(10)
218+
.defaultTTL(1000)
219+
.evictionStrategy(strategy)
220+
.build();
221+
222+
Assertions.assertSame(strategy, newCache.getEvictionStrategy(), "Returned strategy should be the same instance");
223+
}
224+
225+
@Test
226+
void testDefaultStrategyIsNoEviction() {
227+
RRCache<String, String> newCache = new RRCache.Builder<String, String>(5)
228+
.defaultTTL(1000)
229+
.build();
230+
231+
Assertions.assertTrue(
232+
newCache.getEvictionStrategy() instanceof RRCache.PeriodicEvictionStrategy<String, String>,
233+
"Default strategy should be NoEvictionStrategy"
234+
);
235+
}
236+
237+
@Test
238+
void testGetEvictionStrategyIsNotNull() {
239+
RRCache<String, String> newCache = new RRCache.Builder<String, String>(5)
240+
.build();
241+
242+
Assertions.assertNotNull(newCache.getEvictionStrategy(), "Eviction strategy should never be null");
243+
}
211244
}

0 commit comments

Comments
 (0)