-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncache_test.go
More file actions
106 lines (97 loc) · 2.63 KB
/
funcache_test.go
File metadata and controls
106 lines (97 loc) · 2.63 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package funcache
import "testing"
func fib(in int) int {
if in <= 1 {
return 1
}
return fib(in-2) + fib(in-1)
}
func TestCacheFunWithLru(t *testing.T) {
wrapped := func(in []interface{}) ([]interface{}, error) {
res := []interface{}{
fib(in[0].(int)),
}
return res, nil
}
wrapper := CacheFunWithLru(32, wrapped, 3)
if out, err := wrapper([]interface{}{40}); out[0].(int) != fib(40) || err != nil {
t.Errorf("expect %d got %d with %v", fib(40), out[0].(int), err)
}
}
func TestCacheFunWithLfu(t *testing.T) {
wrapped := func(in []interface{}) ([]interface{}, error) {
res := []interface{}{
fib(in[0].(int)),
}
return res, nil
}
wrapper := CacheFunWithLfu(32, wrapped, 3)
if out, err := wrapper([]interface{}{40}); out[0].(int) != fib(40) || err != nil {
t.Errorf("expect %d got %d with %v", fib(40), out[0].(int), err)
}
}
func TestCacheFunWithArc(t *testing.T) {
wrapped := func(in []interface{}) ([]interface{}, error) {
res := []interface{}{
fib(in[0].(int)),
}
return res, nil
}
wrapper := CacheFunWithArc(32, wrapped, 3)
if out, err := wrapper([]interface{}{40}); out[0].(int) != fib(40) || err != nil {
t.Errorf("expect %d got %d with %v", fib(40), out[0].(int), err)
}
}
func BenchmarkRawFib40(b *testing.B) {
for i := 0; i < b.N; i++ {
fib(40)
}
}
func BenchmarkCacheFunWithLruFib40(b *testing.B) {
b.StopTimer()
wrapped := func(in []interface{}) ([]interface{}, error) {
res := []interface{}{
fib(in[0].(int)),
}
return res, nil
}
wrapperFib := CacheFunWithLru(64, wrapped, 3)
b.StartTimer()
for i := 0; i < b.N; i++ {
if out, err := wrapperFib([]interface{}{40}); out[0].(int) != 165580141 || err != nil {
b.Errorf("expect %d got %d with %v", 165580141, out[0].(int), err)
}
}
}
func BenchmarkCacheFunWithLfuFib40(b *testing.B) {
b.StopTimer()
wrapped := func(in []interface{}) ([]interface{}, error) {
res := []interface{}{
fib(in[0].(int)),
}
return res, nil
}
wrapperFib := CacheFunWithLfu(64, wrapped, 3)
b.StartTimer()
for i := 0; i < b.N; i++ {
if out, err := wrapperFib([]interface{}{40}); out[0].(int) != 165580141 || err != nil {
b.Errorf("expect %d got %d with %v", 165580141, out[0].(int), err)
}
}
}
func BenchmarkCacheFunWithArcFib16(b *testing.B) {
b.StopTimer()
wrapped := func(in []interface{}) ([]interface{}, error) {
res := []interface{}{
fib(in[0].(int)),
}
return res, nil
}
wrapperFib := CacheFunWithArc(64, wrapped, 3)
b.StartTimer()
for i := 0; i < b.N; i++ {
if out, err := wrapperFib([]interface{}{40}); out[0].(int) != 165580141 || err != nil {
b.Errorf("expect %d got %d with %v", 165580141, out[0].(int), err)
}
}
}