forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru_cache_test.go
171 lines (141 loc) · 4.62 KB
/
lru_cache_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
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
160
161
162
163
164
165
166
167
168
169
170
171
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package cache
import (
"testing"
"github.com/ava-labs/gecko/ids"
)
func TestLRU(t *testing.T) {
cache := LRU{Size: 1}
id1 := ids.NewID([32]byte{1})
if _, found := cache.Get(id1); found {
t.Fatalf("Retrieved value when none exists")
}
expectedValue1 := 1
cache.Put(id1, expectedValue1)
if value, found := cache.Get(id1); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if value != expectedValue1 {
t.Fatalf("Failed to retrieve correct value when one exists")
}
cache.Put(id1, expectedValue1)
if value, found := cache.Get(id1); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if value != expectedValue1 {
t.Fatalf("Failed to retrieve correct value when one exists")
}
cache.Put(id1, expectedValue1)
if value, found := cache.Get(id1); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if value != expectedValue1 {
t.Fatalf("Failed to retrieve correct value when one exists")
}
id2 := ids.NewID([32]byte{2})
expectedValue2 := 2
cache.Put(id2, expectedValue2)
if _, found := cache.Get(id1); found {
t.Fatalf("Retrieved value when none exists")
}
if value, found := cache.Get(id2); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if value != expectedValue2 {
t.Fatalf("Failed to retrieve correct value when one exists")
}
}
func TestLRUEviction(t *testing.T) {
cache := LRU{Size: 2}
id1 := ids.NewID([32]byte{1})
id2 := ids.NewID([32]byte{2})
id3 := ids.NewID([32]byte{3})
cache.Put(id1, 1)
cache.Put(id2, 2)
if val, found := cache.Get(id1); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 1 {
t.Fatalf("Retrieved wrong value")
} else if val, found := cache.Get(id2); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 2 {
t.Fatalf("Retrieved wrong value")
} else if _, found := cache.Get(id3); found {
t.Fatalf("Retrieve value when none exists")
}
cache.Put(id3, 3)
if _, found := cache.Get(id1); found {
t.Fatalf("Retrieve value when none exists")
} else if val, found := cache.Get(id2); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 2 {
t.Fatalf("Retrieved wrong value")
} else if val, found := cache.Get(id3); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 3 {
t.Fatalf("Retrieved wrong value")
}
cache.Get(id2)
cache.Put(id1, 1)
if val, found := cache.Get(id1); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 1 {
t.Fatalf("Retrieved wrong value")
} else if val, found := cache.Get(id2); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 2 {
t.Fatalf("Retrieved wrong value")
} else if _, found := cache.Get(id3); found {
t.Fatalf("Retrieved value when none exists")
}
cache.Evict(id2)
cache.Put(id3, 3)
if val, found := cache.Get(id1); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 1 {
t.Fatalf("Retrieved wrong value")
} else if _, found := cache.Get(id2); found {
t.Fatalf("Retrieved value when none exists")
} else if val, found := cache.Get(id3); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 3 {
t.Fatalf("Retrieved wrong value")
}
cache.Flush()
if _, found := cache.Get(id1); found {
t.Fatalf("Retrieved value when none exists")
} else if _, found := cache.Get(id2); found {
t.Fatalf("Retrieved value when none exists")
} else if _, found := cache.Get(id3); found {
t.Fatalf("Retrieved value when none exists")
}
}
func TestLRUResize(t *testing.T) {
cache := LRU{Size: 2}
id1 := ids.NewID([32]byte{1})
id2 := ids.NewID([32]byte{2})
cache.Put(id1, 1)
cache.Put(id2, 2)
if val, found := cache.Get(id1); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 1 {
t.Fatalf("Retrieved wrong value")
} else if val, found := cache.Get(id2); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 2 {
t.Fatalf("Retrieved wrong value")
}
cache.Size = 1
if _, found := cache.Get(id1); found {
t.Fatalf("Retrieve value when none exists")
} else if val, found := cache.Get(id2); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 2 {
t.Fatalf("Retrieved wrong value")
}
cache.Size = 0
if _, found := cache.Get(id1); found {
t.Fatalf("Retrieve value when none exists")
} else if val, found := cache.Get(id2); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 2 {
t.Fatalf("Retrieved wrong value")
}
}