-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterator_test.go
146 lines (138 loc) · 3.23 KB
/
iterator_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package xirho
import (
"testing"
"time"
"github.com/zephyrtronium/xirho/xmath"
)
type givef struct{}
func (givef) Calc(in Pt, rng *xmath.RNG) Pt {
return Pt{}
}
func (givef) Prep() {}
func TestIteratorPrep(t *testing.T) {
cases := map[string]System{
// iterator.prep has code to handle zero functions, but System.Check
// errs in that case, so we don't test it.
"one": {
Nodes: []Node{
{Func: givef{}, Opacity: 1, Weight: 1},
},
},
"four": {
Nodes: []Node{
{Func: givef{}, Opacity: 1, Weight: 1},
{Func: givef{}, Opacity: 1, Weight: 1},
{Func: givef{}, Opacity: 1, Weight: 1},
{Func: givef{}, Opacity: 1, Weight: 1},
},
},
"zero": {
Nodes: []Node{
{Func: givef{}, Opacity: 1, Weight: 0},
{Func: givef{}, Opacity: 1, Weight: 0},
{Func: givef{}, Opacity: 1, Weight: 0},
{Func: givef{}, Opacity: 1, Weight: 0},
},
},
}
for name, s := range cases {
t.Run(name, func(t *testing.T) {
it := iterator{rng: xmath.NewRNG()}
it.prep(s, nil) // TODO: test palettes
for i := 0; i < it.n; i++ {
v := it.opat(i)
if v > 1<<53 {
t.Error("opacity", i, "too large:", v, ">", 1<<53)
}
}
ng := 0
for i := 0; i < it.n; i++ {
w := it.wrow(i)
for j := 0; j < it.n; j++ {
v := *w
if v >= 1<<53 {
ng++
}
// It is invalid in Go to hold a pointer outside an
// allocated region, even when using one like this.
if j < it.n-1 {
w = nextw(w)
}
}
}
if ng == 0 {
t.Errorf("no weights guaranteeing selection: graph is %x", it.w)
}
})
}
}
func TestIteratorNext(t *testing.T) {
// This is a Las Vegas algorithm with theoretically infinite running time
// if our RNG is good enough (which it isn't). Skip if short.
if testing.Short() {
t.SkipNow()
}
s := System{
Nodes: []Node{
{Func: givef{}, Weight: 1e4},
{Func: givef{}, Weight: 1},
{Func: givef{}, Weight: 1e-4},
{Func: givef{}, Weight: 1e-4},
{Func: givef{}, Weight: 1e-4},
{Func: givef{}, Weight: 1e-4},
},
}
it := iterator{rng: xmath.NewRNG()}
it.prep(s, nil)
w := it.wrow(0)
for i := 0; i < it.n-2; i++ {
if *w == *nextw(w) {
t.Error("weight", i, "equals its predecessor")
}
w = nextw(w)
}
if t.Failed() {
t.Fatalf("weight graph was %x", it.w)
}
m := make([][]bool, 0, len(s.Nodes))
for range s.Nodes {
m = append(m, make([]bool, len(s.Nodes)))
}
n := 0
k := 0
// The failure case here is that this loop is infinite, so the test will
// time out. To have an explicit condition, we will instead run for 30
// seconds; on my PC, it generally takes between 0.2 and 0.5 seconds.
start := time.Now()
for n < len(m) {
j := it.next(k)
if !m[k][j] {
n++
m[k][j] = true
}
k = j
if time.Since(start) > 30*time.Second {
t.Fatal("took too long; selections were", m)
}
}
}
func TestIteratorFinal(t *testing.T) {
s := System{
Nodes: []Node{
{Func: givef{}, Weight: 1},
},
}
it := iterator{rng: xmath.NewRNG()}
it.prep(s, nil)
p := Pt{1, 1, 1, 1}
fp := it.doFinal(p)
if fp != p {
t.Error("point modified by missing final: want", p, "have", fp)
}
s.Final = givef{}
it.prep(s, nil)
fp = it.doFinal(p)
if fp != (Pt{}) {
t.Error("point not modified by final: want", Pt{}, "have", fp, "with input", p)
}
}