-
Notifications
You must be signed in to change notification settings - Fork 352
/
check_test.go
59 lines (51 loc) · 1.1 KB
/
check_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
//////////////////////////////////////////////////////////////////////
//
// DO NOT EDIT THIS PART
// Your task is to edit `main.go`
//
package main
import (
"strconv"
"sync"
"testing"
)
func TestMain(t *testing.T) {
cache, db := run(t)
cacheLen := len(cache.cache)
pagesLen := cache.pages.Len()
if cacheLen != CacheSize {
t.Errorf("Incorrect cache size %v", cacheLen)
}
if pagesLen != CacheSize {
t.Errorf("Incorrect pages size %v", pagesLen)
}
if db.Calls > callsPerCycle {
t.Errorf("Too much db uses %v", db.Calls)
}
}
func TestLRU(t *testing.T) {
loader := Loader{
DB: GetMockDB(),
}
cache := New(&loader)
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
value := cache.Get("Test" + strconv.Itoa(i))
if value != "Test" + strconv.Itoa(i) {
t.Errorf("Incorrect db response %v", value)
}
wg.Done()
}(i)
}
wg.Wait()
if len(cache.cache) != 100 {
t.Errorf("cache not 100: %d", len(cache.cache))
}
cache.Get("Test0")
cache.Get("Test101")
if _, ok := cache.cache["Test0"]; !ok {
t.Errorf("0 evicted incorrectly: %v", cache.cache)
}
}