-
-
Notifications
You must be signed in to change notification settings - Fork 686
Expand file tree
/
Copy pathgosec_cache_test.go
More file actions
85 lines (62 loc) · 1.74 KB
/
gosec_cache_test.go
File metadata and controls
85 lines (62 loc) · 1.74 KB
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
package gosec
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestLRUCache_AddGet(t *testing.T) {
cache := NewLRUCache[string, int](2)
cache.Add("one", 1)
val, ok := cache.Get("one")
assert.True(t, ok)
assert.Equal(t, 1, val)
cache.Add("two", 2)
val, ok = cache.Get("two")
assert.True(t, ok)
assert.Equal(t, 2, val)
}
func TestLRUCache_Miss(t *testing.T) {
cache := NewLRUCache[string, int](2)
val, ok := cache.Get("missing")
assert.False(t, ok)
assert.Equal(t, 0, val)
}
func TestLRUCache_Eviction(t *testing.T) {
cache := NewLRUCache[string, int](2)
cache.Add("one", 1)
cache.Add("two", 2)
// Cache is full: [two, one]
// Access "one" to make it most recently used
// Cache: [one, two]
_, ok := cache.Get("one")
assert.True(t, ok)
// Add "three", should evict "two" (LRU)
cache.Add("three", 3)
// Cache: [three, one]
val, ok := cache.Get("two")
assert.False(t, ok, "Expected 'two' to be evicted")
assert.Equal(t, 0, val)
val, ok = cache.Get("one")
assert.True(t, ok, "Expected 'one' to remain")
assert.Equal(t, 1, val)
val, ok = cache.Get("three")
assert.True(t, ok, "Expected 'three' to exist")
assert.Equal(t, 3, val)
}
func TestLRUCache_UpdateExisting(t *testing.T) {
cache := NewLRUCache[string, int](2)
cache.Add("one", 1)
cache.Add("two", 2)
// Update "one"
cache.Add("one", 10)
val, ok := cache.Get("one")
assert.True(t, ok)
assert.Equal(t, 10, val)
// Ensure updating didn't change size unexpectedly or eviction order incorrectly
// Cache should be: [one, two] (because "one" was just added/updated)
// Add "three", should evict "two"
cache.Add("three", 3)
_, ok = cache.Get("two")
assert.False(t, ok, "Expected 'two' to be evicted")
_, ok = cache.Get("one")
assert.True(t, ok)
}