forked from couchbase/sync_gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlru_cache_test.go
79 lines (66 loc) · 1.58 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
/*
Copyright 2016-Present Couchbase, Inc.
Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/
package base
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type testStruct struct {
x int
y int
}
func keyForTest(i int) string {
return fmt.Sprintf("key-%d", i)
}
func valueForTest(i int) *testStruct {
return &testStruct{
x: i,
y: i * i,
}
}
func verifyValue(t *testing.T, value *testStruct, i int) {
assert.True(t, value != nil)
assert.Equal(t, i, value.x)
assert.Equal(t, i*i, value.y)
}
func TestLRUCache(t *testing.T) {
ids := make([]string, 20)
for i := 0; i < 20; i++ {
ids[i] = fmt.Sprintf("%d", i)
}
cache, err := NewLRUCache(10)
assert.True(t, err == nil)
for i := 0; i < 10; i++ {
key := keyForTest(i)
value := valueForTest(i)
cache.Put(key, value)
}
for i := 0; i < 10; i++ {
value, _ := cache.Get(keyForTest(i))
testValue, ok := value.(*testStruct)
assert.True(t, ok)
verifyValue(t, testValue, i)
}
for i := 10; i < 13; i++ {
key := keyForTest(i)
value := valueForTest(i)
cache.Put(key, value)
}
for i := 0; i < 3; i++ {
value, _ := cache.Get(keyForTest(i))
assert.True(t, value == nil)
}
for i := 3; i < 13; i++ {
value, _ := cache.Get(keyForTest(i))
testValue, ok := value.(*testStruct)
assert.True(t, ok)
verifyValue(t, testValue, i)
}
}