Enhance docs, add more tests in MRUCache (#5951)

This commit is contained in:
Hardik Pawar
2024-10-23 16:05:26 +05:30
committed by GitHub
parent 8db9d10748
commit d868982a72
2 changed files with 153 additions and 25 deletions

View File

@@ -11,27 +11,27 @@ public class MRUCacheTest {
@Test
public void putAndGetIntegerValues() {
MRUCache<Integer, Integer> lruCache = new MRUCache<>(SIZE);
MRUCache<Integer, Integer> mruCache = new MRUCache<>(SIZE);
for (int i = 0; i < SIZE; i++) {
lruCache.put(i, i);
mruCache.put(i, i);
}
for (int i = 0; i < SIZE; i++) {
assertEquals(i, lruCache.get(i));
assertEquals(i, mruCache.get(i));
}
}
@Test
public void putAndGetStringValues() {
MRUCache<String, String> lruCache = new MRUCache<>(SIZE);
MRUCache<String, String> mruCache = new MRUCache<>(SIZE);
for (int i = 0; i < SIZE; i++) {
lruCache.put("key" + i, "value" + i);
mruCache.put("key" + i, "value" + i);
}
for (int i = 0; i < SIZE; i++) {
assertEquals("value" + i, lruCache.get("key" + i));
assertEquals("value" + i, mruCache.get("key" + i));
}
}
@@ -53,6 +53,73 @@ public class MRUCacheTest {
mruCache.put(i, i);
}
assertEquals(9, mruCache.get(9));
// After inserting 10 items, the cache should have evicted the least recently used ones.
assertEquals(9, mruCache.get(9)); // Most recently used
assertEquals(0, mruCache.get(0)); // Least recently used, should be evicted
}
@Test
public void overwriteExistingKey() {
MRUCache<Integer, String> mruCache = new MRUCache<>(SIZE);
mruCache.put(1, "one");
mruCache.put(1, "uno"); // Overwriting the value for key 1
assertEquals("uno", mruCache.get(1));
assertNull(mruCache.get(2)); // Ensure other keys are unaffected
}
@Test
public void evictionOrder() {
MRUCache<Integer, Integer> mruCache = new MRUCache<>(SIZE);
for (int i = 0; i < SIZE; i++) {
mruCache.put(i, i);
}
// Access a key to make it most recently used
mruCache.get(2);
// Add new items to trigger eviction
mruCache.put(5, 5);
mruCache.put(6, 6);
// Key 3 should be evicted since 2 is the most recently used
assertEquals(3, mruCache.get(3));
assertEquals(4, mruCache.get(4)); // Key 4 should still be available
assertEquals(6, mruCache.get(6)); // Key 6 should be available
}
@Test
public void cacheHandlesLargeValues() {
MRUCache<String, String> mruCache = new MRUCache<>(SIZE);
for (int i = 0; i < SIZE; i++) {
mruCache.put("key" + i, "value" + i);
}
// Verify values
for (int i = 0; i < SIZE; i++) {
assertEquals("value" + i, mruCache.get("key" + i));
}
// Add large value
mruCache.put("largeKey", "largeValue");
// Verify eviction of the least recently used (key 0 should be evicted)
assertEquals("value0", mruCache.get("key0"));
assertEquals("largeValue", mruCache.get("largeKey"));
}
@Test
public void testEmptyCacheBehavior() {
MRUCache<Integer, Integer> mruCache = new MRUCache<>(SIZE);
// Verify that accessing any key returns null
assertNull(mruCache.get(1));
assertNull(mruCache.get(100));
// Adding to cache and checking again
mruCache.put(1, 10);
assertEquals(10, mruCache.get(1));
}
}