-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
60 lines (50 loc) · 1.27 KB
/
main_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
package main
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestPutGet(t *testing.T) {
lru := Constructor[int, int](3)
lru.Put(1, 11)
val, err := lru.Get(1)
require.Equal(t, 11, val)
require.NoError(t, err)
}
func TestEvictMechanism(t *testing.T) {
t.Run("should evict LRU item", func(t *testing.T) {
lru := Constructor[int, int](3)
lru.Put(1, 11)
lru.Put(2, 12)
lru.Put(3, 13)
// cache capacity reached - Put bellow should evict LRU item
lru.Put(4, 14)
val, err := lru.Get(1)
require.Equal(t, 0, val)
require.EqualError(t, err, ErrNotFound.Error())
})
t.Run("should retain new item", func(t *testing.T) {
lru := Constructor[int, int](3)
lru.Put(1, 11)
lru.Put(2, 12)
lru.Put(3, 13)
// cache capacity reached - Put bellow should add new item
lru.Put(4, 14)
val, err := lru.Get(4)
require.Equal(t, 14, val)
require.NoError(t, err)
})
t.Run("should keep items up to capacity amount", func(t *testing.T) {
lru := Constructor[int, int](3)
lru.Put(1, 11)
lru.Put(2, 12)
lru.Put(3, 13)
// cache capacity reached - Put bellow should add new item
lru.Put(4, 14)
val, err := lru.Get(2)
require.NoError(t, err)
require.Equal(t, 12, val)
val, err = lru.Get(3)
require.NoError(t, err)
require.Equal(t, 13, val)
})
}