-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru_test.go
84 lines (79 loc) · 1.43 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
package lru
import (
"encoding/hex"
"math/rand"
"testing"
)
func TestLruAppend(t *testing.T) {
c := New[string, int](4)
c.Set("a", 1)
c.Set("b", 2)
c.Set("c", 3)
c.Set("d", 4)
c.Set("e", 5)
if c.Get("a") != 0 {
t.FailNow()
}
if c.Get("e") != 5 {
t.FailNow()
}
}
func TestLruChange(t *testing.T) {
c := New[string, int](4)
c.Set("a", 1)
c.Set("b", 2)
c.Set("c", 3)
c.Set("d", 4)
c.Set("a", 5)
if c.Get("a") != 5 {
t.FailNow()
}
}
func TestLruDel(t *testing.T) {
c := New[string, int](4)
c.Set("a", 1)
c.Set("b", 2)
c.Set("c", 3)
c.Set("d", 4)
c.Del("b")
if c.List.Size != c.Len() || c.Len() != 3 {
t.FailNow()
}
if c.Get("b") != 0 {
t.FailNow()
}
}
func TestLruSize(t *testing.T) {
b := make([]byte, 4)
c := New[string, int](4)
if c.List.Size != c.Len() || c.Len() != 0 {
t.FailNow()
}
rand.Read(b)
c.Set(hex.EncodeToString(b), rand.Int())
if c.List.Size != c.Len() || c.Len() != 1 {
t.FailNow()
}
rand.Read(b)
c.Set(hex.EncodeToString(b), rand.Int())
if c.List.Size != c.Len() || c.Len() != 2 {
t.FailNow()
}
rand.Read(b)
c.Set(hex.EncodeToString(b), rand.Int())
if c.List.Size != c.Len() || c.Len() != 3 {
t.FailNow()
}
rand.Read(b)
c.Set(hex.EncodeToString(b), rand.Int())
if c.List.Size != c.Len() || c.Len() != 4 {
t.FailNow()
}
for i := 0; i < 65536; i++ {
rand.Read(b)
c.Set(hex.EncodeToString(b), rand.Int())
if c.List.Size != c.Len() || c.Len() != 4 {
t.FailNow()
}
}
}