-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsystem_test.go
96 lines (84 loc) · 1.87 KB
/
lsystem_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
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"testing"
)
func equals(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, va := range a {
if va != b[i] {
return false
}
}
return true
}
func TestFunction(t *testing.T) {
testLindenmayer(t, lindenmayer)
}
func TestChannel(t *testing.T) {
testLindenmayer(t, coliner)
}
func testLindenmayer(t *testing.T, f func([]string, map[string][]string, int) []string) {
tests := []struct {
Rules map[string][]string
Start []string
Iterations int
Expected []string
}{
{ // simple in/out test
map[string][]string{
"A": {"A", "B"},
"B": {"A"},
},
[]string{"A"}, 0,
[]string{"A"},
},
{ // 4 gen tree
map[string][]string{
"A": {"A", "B"},
"B": {"A"},
},
[]string{"A"}, 4,
[]string{"A", "B", "A", "A", "B", "A", "B", "A"},
},
{ // dragon curve
map[string][]string{
"X": {"X", "+", "Y", "F"},
"Y": {"F", "X", "-", "Y"},
},
[]string{"F", "X"}, 2,
[]string{"F", "X", "+", "Y", "F", "+", "F", "X", "-", "Y", "F"},
},
{ // sierpinski triangle
map[string][]string{
"A": {"B", "-", "A", "-", "B"},
"B": {"A", "+", "B", "+", "A"},
},
[]string{"A"}, 2,
[]string{"A", "+", "B", "+", "A", "-", "B", "-", "A", "-", "B", "-", "A", "+", "B", "+", "A"},
},
}
for _, c := range tests {
if r := f(c.Start, c.Rules, c.Iterations); !equals(r, c.Expected) {
t.Errorf("f(%v, %v, %v) != %v (got %v)", c.Start, c.Rules, c.Iterations, c.Expected, r)
}
}
}
func BenchmarkFunction(b *testing.B) {
benchmarkLindenmayer(b, lindenmayer)
}
func BenchmarkChannel(b *testing.B) {
benchmarkLindenmayer(b, coliner)
}
func benchmarkLindenmayer(b *testing.B, f func([]string, map[string][]string, int) []string) {
rules := map[string][]string{
"A": {"A", "B"},
"B": {"A"},
}
start := []string{"A"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
f(start, rules, 10)
}
}