-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlru_cache_test.go
47 lines (40 loc) · 1.15 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
package ldclient
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestLRUCache(t *testing.T) {
t.Run("add returns false for never-seen value", func(t *testing.T) {
cache := newLruCache(10)
assert.False(t, cache.add("a"))
})
t.Run("add returns true for already-seen value", func(t *testing.T) {
cache := newLruCache(10)
cache.add("a")
assert.True(t, cache.add("a"))
})
t.Run("oldest value is discarded when capacity is exceeded", func(t *testing.T) {
cache := newLruCache(2)
cache.add("a")
cache.add("b")
cache.add("c")
assert.True(t, cache.add("c"))
assert.True(t, cache.add("b"))
assert.False(t, cache.add("a"))
})
t.Run("re-adding an existing value makes it new again", func(t *testing.T) {
cache := newLruCache(2)
cache.add("a")
cache.add("b")
cache.add("a")
cache.add("c")
assert.True(t, cache.add("c"))
assert.True(t, cache.add("a"))
assert.False(t, cache.add("b"))
})
t.Run("zero-length cache treats values as new", func(t *testing.T) {
cache := newLruCache(0)
assert.False(t, cache.add("a"))
assert.False(t, cache.add("a"))
})
}