/
memoize_test.go
64 lines (54 loc) · 1.33 KB
/
memoize_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
package memoize_test
import (
"testing"
"github.com/BenLubar/memoize"
)
func TestPanic(t *testing.T) {
count := 0
f := func(i int) {
count++
if count%2 == 1 {
panic(count)
}
}
f = memoize.Memoize(f).(func(int))
expect := func(p interface{}, i int) {
defer func() {
if r := recover(); p != r {
t.Errorf("for input %d:\nexpected: %v\nactual: %v", i, p, r)
}
}()
f(i)
}
expect(1, 1)
expect(1, 1)
expect(nil, 2)
expect(nil, 2)
expect(1, 1)
expect(3, 100)
}
func TestVariadic(t *testing.T) {
count := 0
var concat func(string, ...string) string
concat = func(s0 string, s1 ...string) string {
count++
if len(s1) == 0 {
return s0
}
return concat(s0+s1[0], s1[1:]...)
}
concat = memoize.Memoize(concat).(func(string, ...string) string)
expect := func(actual, expected string, n int) {
if actual != expected || n != count {
t.Errorf("expected: %q\nactual: %q\nexpected count: %d\nactual count: %d", expected, actual, n, count)
}
}
expect("", "", 0)
expect(concat("string"), "string", 1)
expect(concat("string", "one"), "stringone", 3)
expect(concat("string", "one"), "stringone", 3)
expect(concat("string", "two"), "stringtwo", 5)
expect(concat("string", "one"), "stringone", 5)
expect(concat("stringone", "two"), "stringonetwo", 7)
expect(concat("string", "one", "two"), "stringonetwo", 8)
}