-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlru_test.go
105 lines (80 loc) · 1.9 KB
/
lru_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
package lru
import (
"testing"
)
func TestAddGet(t *testing.T) {
lruCache := NewLruCache(1)
key := "1"
expectedValue := "1"
lruCache.Add(key, expectedValue)
if v, ok := lruCache.Get(key); ok {
s, ok := v.(string)
if !ok {
t.Errorf("expected string, got %v", v)
}
if s != expectedValue {
t.Errorf("got %s, wanted %s", s, expectedValue)
}
} else {
t.Error("get failed, could not find value")
}
}
func TestRemove(t *testing.T) {
lruCache := NewLruCache(1)
key := "1"
expectedValue := "1"
lruCache.Add(key, expectedValue)
if v, ok := lruCache.Get(key); ok {
s, ok := v.(string)
if !ok {
t.Errorf("expected string, got %v", v)
}
if s != expectedValue {
t.Errorf("got %s, wanted %s", s, expectedValue)
}
} else {
t.Error("get failed, could not find value")
}
// remove now
lruCache.Remove(key)
if _, ok := lruCache.Get(key); ok {
t.Error("should not have found element")
}
}
func TestRemoveOldest(t *testing.T) {
lruCache := NewLruCache(2)
keyOldest := "1"
expectedValueOldest := "1"
keyNewest := "2"
expectedValueNewest := "2"
lruCache.Add(keyOldest, expectedValueOldest)
lruCache.Add(keyNewest, expectedValueNewest)
lruCache.RemoveOldest()
if v, ok := lruCache.Get(keyNewest); ok {
s, ok := v.(string)
if !ok {
t.Errorf("expected string, got %v", v)
}
if s != expectedValueNewest {
t.Errorf("got %s, wanted %s", s, expectedValueNewest)
}
} else {
t.Error("get failed, could not find value")
}
// remove now
if _, ok := lruCache.Get(expectedValueOldest); ok {
t.Error("should not have found element")
}
}
func TestLen(t *testing.T) {
lruCache := NewLruCache(2)
keyOldest := "1"
expectedValueOldest := "1"
keyNewest := "2"
expectedValueNewest := "2"
lruCache.Add(keyOldest, expectedValueOldest)
lruCache.Add(keyNewest, expectedValueNewest)
if lruCache.Len() != 2 {
t.Errorf("got %d, expected %d", lruCache.Len(), 2)
}
}