-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
LFUCacheTest.java
138 lines (107 loc) · 4.61 KB
/
LFUCacheTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.thealgorithms.datastructures.caches;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
class LFUCacheTest {
@Test
void testLFUCacheWithIntegerValueShouldPass() {
LFUCache<Integer, Integer> lfuCache = new LFUCache<>(5);
lfuCache.put(1, 10);
lfuCache.put(2, 20);
lfuCache.put(3, 30);
lfuCache.put(4, 40);
lfuCache.put(5, 50);
// get method call will increase frequency of key 1 by 1
assertEquals(10, lfuCache.get(1));
// this operation will remove value with key as 2
lfuCache.put(6, 60);
// will return null as value with key 2 is now evicted
assertNull(lfuCache.get(2));
// should return 60
assertEquals(60, lfuCache.get(6));
// this operation will remove value with key as 3
lfuCache.put(7, 70);
assertNull(lfuCache.get(2));
assertEquals(70, lfuCache.get(7));
}
@Test
void testLFUCacheWithStringValueShouldPass() {
LFUCache<Integer, String> lfuCache = new LFUCache<>(5);
lfuCache.put(1, "Alpha");
lfuCache.put(2, "Beta");
lfuCache.put(3, "Gamma");
lfuCache.put(4, "Delta");
lfuCache.put(5, "Epsilon");
// get method call will increase frequency of key 1 by 1
assertEquals("Alpha", lfuCache.get(1));
// this operation will remove value with key as 2
lfuCache.put(6, "Digamma");
// will return null as value with key 2 is now evicted
assertNull(lfuCache.get(2));
// should return string Digamma
assertEquals("Digamma", lfuCache.get(6));
// this operation will remove value with key as 3
lfuCache.put(7, "Zeta");
assertNull(lfuCache.get(2));
assertEquals("Zeta", lfuCache.get(7));
}
@Test
void testUpdateValueShouldPreserveFrequency() {
LFUCache<Integer, String> lfuCache = new LFUCache<>(3);
lfuCache.put(1, "A");
lfuCache.put(2, "B");
lfuCache.put(3, "C");
assertEquals("A", lfuCache.get(1)); // Accessing key 1
lfuCache.put(4, "D"); // This should evict key 2
assertNull(lfuCache.get(2)); // Key 2 should be evicted
assertEquals("C", lfuCache.get(3)); // Key 3 should still exist
assertEquals("A", lfuCache.get(1)); // Key 1 should still exist
lfuCache.put(1, "Updated A"); // Update the value of key 1
assertEquals("Updated A", lfuCache.get(1)); // Check if the update was successful
}
@Test
void testEvictionPolicyWhenFull() {
LFUCache<Integer, String> lfuCache = new LFUCache<>(2);
lfuCache.put(1, "One");
lfuCache.put(2, "Two");
assertEquals("One", lfuCache.get(1)); // Access key 1
lfuCache.put(3, "Three"); // This should evict key 2 (least frequently used)
assertNull(lfuCache.get(2)); // Key 2 should be evicted
assertEquals("One", lfuCache.get(1)); // Key 1 should still exist
assertEquals("Three", lfuCache.get(3)); // Check if key 3 exists
}
@Test
void testGetFromEmptyCacheShouldReturnNull() {
LFUCache<Integer, String> lfuCache = new LFUCache<>(3);
assertNull(lfuCache.get(1)); // Should return null as the cache is empty
}
@Test
void testPutNullValueShouldStoreNull() {
LFUCache<Integer, String> lfuCache = new LFUCache<>(3);
lfuCache.put(1, null); // Store a null value
assertNull(lfuCache.get(1)); // Should return null
}
@Test
void testInvalidCacheCapacityShouldThrowException() {
assertThrows(IllegalArgumentException.class, () -> new LFUCache<>(0));
assertThrows(IllegalArgumentException.class, () -> new LFUCache<>(-1));
}
@Test
void testMultipleAccessPatterns() {
LFUCache<Integer, String> lfuCache = new LFUCache<>(5);
lfuCache.put(1, "A");
lfuCache.put(2, "B");
lfuCache.put(3, "C");
lfuCache.put(4, "D");
assertEquals("A", lfuCache.get(1)); // Access 1
lfuCache.put(5, "E"); // Should not evict anything yet
lfuCache.put(6, "F"); // Evict B
assertNull(lfuCache.get(2)); // B should be evicted
assertEquals("C", lfuCache.get(3)); // C should still exist
assertEquals("D", lfuCache.get(4)); // D should still exist
assertEquals("A", lfuCache.get(1)); // A should still exist
assertEquals("E", lfuCache.get(5)); // E should exist
assertEquals("F", lfuCache.get(6)); // F should exist
}
}