cache - cache_test.go
1 package cache
2
3 import (
4 "testing"
5 )
6
7 func TestLRUCache(t *testing.T) {
8 c := NewLRU[string, string](5)
9
10 var _ Cache[string, string] = c
11
12 if v, ok := c.Get("a"); ok {
13 t.Error("unexpectedly got ok = true")
14 } else if v != "" {
15 t.Errorf("expecting empty string, got %q", v)
16 }
17
18 if !c.Set("a", "1") {
19 t.Error("unexpectedly got set = false")
20 } else if v, ok := c.Get("a"); !ok {
21 t.Error("unexpectedly got ok = false")
22 } else if v != "1" {
23 t.Errorf("expecting %q, got %q", "1", v)
24 } else if len(c.data) != 1 {
25 t.Errorf("expecting cache length to be 1, got %d", len(c.data))
26 }
27
28 if c.Set("a", "1") {
29 t.Error("unexpectedly got set = true")
30 } else if v, ok := c.Get("a"); !ok {
31 t.Error("unexpectedly got ok = false")
32 } else if v != "1" {
33 t.Errorf("expecting %q, got %q", "1", v)
34 } else if len(c.data) != 1 {
35 t.Errorf("expecting cache length to be 1, got %d", len(c.data))
36 }
37
38 c.Set("b", "2")
39
40 if v, ok := c.Get("b"); !ok {
41 t.Error("unexpectedly got ok = false")
42 } else if v != "2" {
43 t.Errorf("expecting %q, got %q", "2", v)
44 } else if len(c.data) != 2 {
45 t.Errorf("expecting cache length to be 2, got %d", len(c.data))
46 }
47
48 c.Set("c", "3")
49 c.Set("d", "4")
50 c.Set("e", "5")
51
52 if len(c.data) != 5 {
53 t.Errorf("expecting cache length to be 5, got %d", len(c.data))
54 }
55
56 c.Set("f", "6")
57
58 if v, ok := c.Get("a"); ok {
59 t.Error("unexpectedly got ok = true")
60 } else if v != "" {
61 t.Errorf("expecting empty string, got %q", v)
62 } else if len(c.data) != 5 {
63 t.Errorf("expecting cache length to be 5, got %d", len(c.data))
64 }
65
66 if v, ok := c.Get("b"); !ok {
67 t.Error("unexpectedly got ok = false")
68 } else if v != "2" {
69 t.Errorf("expecting %q, got %q", "2", v)
70 } else if len(c.data) != 5 {
71 t.Errorf("expecting cache length to be 5, got %d", len(c.data))
72 }
73
74 c.Set("g", "7")
75
76 if v, ok := c.Get("c"); ok {
77 t.Error("unexpectedly got ok = true")
78 } else if v != "" {
79 t.Errorf("expecting empty string, got %q", v)
80 } else if len(c.data) != 5 {
81 t.Errorf("expecting cache length to be 5, got %d", len(c.data))
82 }
83
84 if c.Remove("a") {
85 t.Error("unexpectedly got removed = true")
86 }
87
88 if !c.Remove("d") {
89 t.Error("unexpectedly got removed = false")
90 } else if _, ok := c.Get("d"); ok {
91 t.Error("unexpectedly got ok = true")
92 } else if len(c.data) != 4 {
93 t.Errorf("expecting cache length to be 4, got %d", len(c.data))
94 }
95 }
96
97 func TestMRUCache(t *testing.T) {
98 c := NewMRU[string, string](5)
99
100 var _ Cache[string, string] = c
101
102 if v, ok := c.Get("a"); ok {
103 t.Error("unexpectedly got ok = true")
104 } else if v != "" {
105 t.Errorf("expecting empty string, got %q", v)
106 }
107
108 if !c.Set("a", "1") {
109 t.Error("unexpectedly got set = false")
110 } else if v, ok := c.Get("a"); !ok {
111 t.Error("unexpectedly got ok = false")
112 } else if v != "1" {
113 t.Errorf("expecting %q, got %q", "1", v)
114 } else if len(c.data) != 1 {
115 t.Errorf("expecting cache length to be 1, got %d", len(c.data))
116 }
117
118 if c.Set("a", "1") {
119 t.Error("unexpectedly got set = true")
120 } else if v, ok := c.Get("a"); !ok {
121 t.Error("unexpectedly got ok = false")
122 } else if v != "1" {
123 t.Errorf("expecting %q, got %q", "1", v)
124 } else if len(c.data) != 1 {
125 t.Errorf("expecting cache length to be 1, got %d", len(c.data))
126 }
127
128 c.Set("b", "2")
129
130 if v, ok := c.Get("b"); !ok {
131 t.Error("unexpectedly got ok = false")
132 } else if v != "2" {
133 t.Errorf("expecting %q, got %q", "2", v)
134 } else if len(c.data) != 2 {
135 t.Errorf("expecting cache length to be 2, got %d", len(c.data))
136 }
137
138 c.Set("c", "3")
139 c.Set("d", "4")
140 c.Set("e", "5")
141
142 if len(c.data) != 5 {
143 t.Errorf("expecting cache length to be 5, got %d", len(c.data))
144 }
145
146 c.Set("f", "6")
147
148 if v, ok := c.Get("e"); ok {
149 t.Error("unexpectedly got ok = true")
150 } else if v != "" {
151 t.Errorf("expecting empty string, got %q", v)
152 } else if len(c.data) != 5 {
153 t.Errorf("expecting cache length to be 5, got %d", len(c.data))
154 }
155
156 if v, ok := c.Get("a"); !ok {
157 t.Error("unexpectedly got ok = false")
158 } else if v != "1" {
159 t.Errorf("expecting %q, got %q", "2", v)
160 } else if len(c.data) != 5 {
161 t.Errorf("expecting cache length to be 5, got %d", len(c.data))
162 }
163
164 c.Set("g", "7")
165
166 if v, ok := c.Get("a"); ok {
167 t.Error("unexpectedly got ok = true")
168 } else if v != "" {
169 t.Errorf("expecting empty string, got %q", v)
170 } else if len(c.data) != 5 {
171 t.Errorf("expecting cache length to be 5, got %d", len(c.data))
172 }
173
174 if c.Remove("a") {
175 t.Error("unexpectedly got removed = true")
176 }
177
178 if !c.Remove("d") {
179 t.Error("unexpectedly got removed = false")
180 } else if _, ok := c.Get("d"); ok {
181 t.Error("unexpectedly got ok = true")
182 } else if len(c.data) != 4 {
183 t.Errorf("expecting cache length to be 4, got %d", len(c.data))
184 }
185 }
186
187 func TestCounts(t *testing.T) {
188 c := NewLRU[string, string](5)
189
190 if count := c.Count(); count != 0 {
191 t.Errorf("expecting count %d, got %d", 0, count)
192 }
193
194 c.Set("", "")
195
196 if count := c.Count(); count != 1 {
197 t.Errorf("expecting count %d, got %d", 1, count)
198 }
199
200 c.Set("", "")
201
202 if count := c.Count(); count != 1 {
203 t.Errorf("expecting count %d, got %d", 1, count)
204 }
205
206 c.Set("1", "1")
207 c.Set("2", "2")
208 c.Set("3", "3")
209 c.Set("4", "4")
210 c.Set("5", "5")
211
212 if count := c.Count(); count != 5 {
213 t.Errorf("expecting count %d, got %d", 5, count)
214 }
215 }
216
217 func TestClear(t *testing.T) {
218 c := NewLRU[string, string](5)
219
220 c.Set("a", "1")
221 c.Set("b", "2")
222 c.Set("c", "3")
223 c.Set("d", "4")
224 c.Set("e", "5")
225
226 if count := c.Count(); count != 5 {
227 t.Errorf("expecting count %d, got %d", 5, count)
228 }
229
230 c.Clear()
231
232 if count := c.Count(); count != 0 {
233 t.Errorf("expecting count %d, got %d", 0, count)
234 }
235
236 if v, ok := c.Get("b"); ok {
237 t.Error("unexpectedly got ok = true")
238 } else if v != "" {
239 t.Errorf("expecting %q, got %q", "", v)
240 }
241 }
242