-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlru_cache_test.go
More file actions
159 lines (127 loc) · 3.51 KB
/
lru_cache_test.go
File metadata and controls
159 lines (127 loc) · 3.51 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package drain3
import (
"testing"
)
func TestLogClusterCacheNonDestructiveGet(t *testing.T) {
cache := NewLogClusterCache(3)
c1 := NewLogCluster([]string{"a"}, 1)
c2 := NewLogCluster([]string{"b"}, 2)
c3 := NewLogCluster([]string{"c"}, 3)
cache.Put(c1) // order: 1
cache.Put(c2) // order: 2, 1
cache.Put(c3) // order: 3, 2, 1
// Get c1 — should NOT move it to front (non-destructive)
got := cache.Get(1)
if got != c1 {
t.Fatalf("expected c1, got %v", got)
}
// Now insert c4 — should evict c1 (LRU), NOT c2
c4 := NewLogCluster([]string{"d"}, 4)
evicted := cache.Put(c4)
if evicted == nil || evicted.ClusterID != 1 {
t.Fatalf("expected eviction of cluster 1, got %v", evicted)
}
if cache.Get(1) != nil {
t.Fatal("cluster 1 should have been evicted")
}
}
func TestLogClusterCacheTouch(t *testing.T) {
cache := NewLogClusterCache(3)
c1 := NewLogCluster([]string{"a"}, 1)
c2 := NewLogCluster([]string{"b"}, 2)
c3 := NewLogCluster([]string{"c"}, 3)
cache.Put(c1) // order: 1
cache.Put(c2) // order: 2, 1
cache.Put(c3) // order: 3, 2, 1
// Touch c1 — should move it to front
cache.Touch(1)
// Now order should be: 1, 3, 2
c4 := NewLogCluster([]string{"d"}, 4)
evicted := cache.Put(c4)
// c2 should be evicted (LRU after touch)
if evicted == nil || evicted.ClusterID != 2 {
t.Fatalf("expected eviction of cluster 2, got %v", evicted)
}
}
func TestLogClusterCacheOverwrite(t *testing.T) {
cache := NewLogClusterCache(3)
c1 := NewLogCluster([]string{"a"}, 1)
cache.Put(c1)
// Overwrite with updated cluster
c1Updated := NewLogCluster([]string{"a", "b"}, 1)
c1Updated.Size = 5
evicted := cache.Put(c1Updated)
if evicted != nil {
t.Fatalf("no eviction expected on overwrite, got %v", evicted)
}
got := cache.Get(1)
if got.Size != 5 {
t.Fatalf("expected size 5, got %d", got.Size)
}
if cache.Len() != 1 {
t.Fatalf("expected len 1, got %d", cache.Len())
}
}
func TestLogClusterCacheEviction(t *testing.T) {
cache := NewLogClusterCache(2)
c1 := NewLogCluster([]string{"a"}, 1)
c2 := NewLogCluster([]string{"b"}, 2)
c3 := NewLogCluster([]string{"c"}, 3)
cache.Put(c1)
cache.Put(c2)
evicted := cache.Put(c3)
if evicted == nil || evicted.ClusterID != 1 {
t.Fatalf("expected eviction of cluster 1, got %v", evicted)
}
if cache.Len() != 2 {
t.Fatalf("expected len 2, got %d", cache.Len())
}
}
func TestLogClusterCacheValues(t *testing.T) {
cache := NewLogClusterCache(5)
for i := 1; i <= 3; i++ {
cache.Put(NewLogCluster([]string{"x"}, i))
}
vals := cache.Values()
if len(vals) != 3 {
t.Fatalf("expected 3 values, got %d", len(vals))
}
// Most recently used first
if vals[0].ClusterID != 3 || vals[1].ClusterID != 2 || vals[2].ClusterID != 1 {
t.Fatalf("unexpected order: %v", vals)
}
}
func TestLogClusterCacheRemove(t *testing.T) {
cache := NewLogClusterCache(5)
c1 := NewLogCluster([]string{"a"}, 1)
cache.Put(c1)
cache.Remove(1)
if cache.Get(1) != nil {
t.Fatal("expected nil after removal")
}
if cache.Len() != 0 {
t.Fatalf("expected len 0, got %d", cache.Len())
}
}
func TestMapStore(t *testing.T) {
store := newMapStore()
c1 := NewLogCluster([]string{"a"}, 1)
c2 := NewLogCluster([]string{"b"}, 2)
store.Put(c1)
store.Put(c2)
if store.Len() != 2 {
t.Fatalf("expected 2, got %d", store.Len())
}
got := store.Get(1)
if got != c1 {
t.Fatalf("expected c1, got %v", got)
}
store.Remove(1)
if store.Get(1) != nil {
t.Fatal("expected nil after removal")
}
vals := store.Values()
if len(vals) != 1 {
t.Fatalf("expected 1 value, got %d", len(vals))
}
}