forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlru_cache_test.go
62 lines (47 loc) · 1.32 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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package cache
import (
"testing"
"github.com/ava-labs/avalanchego/ids"
)
func TestLRU(t *testing.T) {
cache := &LRU{Size: 1}
TestBasic(t, cache)
}
func TestLRUEviction(t *testing.T) {
cache := &LRU{Size: 2}
TestEviction(t, cache)
}
func TestLRUResize(t *testing.T) {
cache := LRU{Size: 2}
id1 := ids.ID{1}
id2 := ids.ID{2}
cache.Put(id1, 1)
cache.Put(id2, 2)
if val, found := cache.Get(id1); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 1 {
t.Fatalf("Retrieved wrong value")
} else if val, found := cache.Get(id2); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 2 {
t.Fatalf("Retrieved wrong value")
}
cache.Size = 1
if _, found := cache.Get(id1); found {
t.Fatalf("Retrieve value when none exists")
} else if val, found := cache.Get(id2); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 2 {
t.Fatalf("Retrieved wrong value")
}
cache.Size = 0
if _, found := cache.Get(id1); found {
t.Fatalf("Retrieve value when none exists")
} else if val, found := cache.Get(id2); !found {
t.Fatalf("Failed to retrieve value when one exists")
} else if val != 2 {
t.Fatalf("Retrieved wrong value")
}
}