-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
59 lines (46 loc) · 1.47 KB
/
main_test.go
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
package main
import (
"testing"
)
func TestLRUCache(t *testing.T) {
// Create a new LRU cache with a capacity of 3
lru := newLRUCache(3)
// Test 1: Adding and getting values
lru.Update("key1", 1)
lru.Update("key2", 2)
lru.Update("key3", 3)
// Check the length after adding 3 entries
if lru.length != 3 {
t.Errorf("Expected length: 3, Got: %d", lru.length)
}
// Check the values retrieved from the cache
checkValue(t, lru.Get("key1"), 1, "key1")
checkValue(t, lru.Get("key2"), 2, "key2")
checkValue(t, lru.Get("key3"), 3, "key3")
// Test 2: Updating an existing value
lru.Update("key2", 22)
// Check the length after updating an entry
if lru.length != 3 {
t.Errorf("Expected length: 3, Got: %d", lru.length)
}
// Check the updated value
checkValue(t, lru.Get("key2"), 22, "key2")
// Test 3: Adding more values to trigger eviction
lru.Update("key4", 4)
lru.Update("key5", 5)
// Check the length after adding 2 more entries
if lru.length != 3 {
t.Errorf("Expected length: 3, Got: %d", lru.length)
}
// Check that the least recently used entry was evicted
checkValue(t, lru.Get("key1"), nil, "key1")
// Check the values of remaining entries
checkValue(t, lru.Get("key2"), 22, "key2")
checkValue(t, lru.Get("key4"), 4, "key4")
checkValue(t, lru.Get("key5"), 5, "key5")
}
func checkValue(t *testing.T, actual interface{}, expected interface{}, key string) {
if actual != expected {
t.Errorf("Expected value for %s: %v, Got: %v", key, expected, actual)
}
}