-
Notifications
You must be signed in to change notification settings - Fork 0
/
lrucache_test.go
55 lines (52 loc) · 1.05 KB
/
lrucache_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
package lrucache
import (
"testing"
)
func TestBasicFunctions(t *testing.T) {
testdata := map[string]interface{}{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
"f": 6,
}
// Check Lrucache initialization with NewLrucache
cache, err := NewLrucache(-1)
if err == nil {
t.Fatal("NewLrucache(-1) does not return error")
}
cache, err = NewLrucache(0)
if err == nil {
t.Fatal("NewLrucache(0) does not return error")
}
cache, err = NewLrucache(len(testdata) - 1)
if err != nil {
t.Fatalf("NewLrucache(%d) returns error: %v", len(testdata)-1, err)
}
for k, v := range testdata {
cache.Set(k, v)
}
if cache.Len() != len(testdata)-1 {
t.Error("Cache displacement does not work")
}
// Check Get()
val, ok := cache.Get("d")
if !ok {
t.Error("Get method is not working")
}
if val != 4 {
t.Error("Get method returns wrong vakue")
}
// Check Del()
cache.Del("d")
_, ok = cache.Get("d")
if ok {
t.Error("Del method is not working")
}
// Check Flush()
cache.Flush()
if cache.Len() != 0 {
t.Error("Flush method is not working")
}
}