-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
280 lines (235 loc) · 6 KB
/
main_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package incr
import (
"bytes"
"context"
"fmt"
"math"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/wcharczuk/go-incr/testutil"
)
// testContext returns a test context.
func testContext() context.Context {
ctx := context.Background()
ctx = testutil.WithBlueDye(ctx)
if os.Getenv("INCR_DEBUG_TRACING") != "" {
ctx = WithTracing(ctx)
}
return ctx
}
func epsilon(delta float64) func(float64, float64) bool {
return func(v0, v1 float64) bool {
return math.Abs(v1-v0) <= delta
}
}
func epsilonContext(t *testing.T, delta float64) func(context.Context, float64, float64) (bool, error) {
t.Helper()
return func(ctx context.Context, v0, v1 float64) (bool, error) {
t.Helper()
testutil.BlueDye(ctx, t)
return math.Abs(v1-v0) <= delta, nil
}
}
type mathTypes interface {
~int | ~float64
}
func epsilonFn[A, B mathTypes](eps A, oldv, newv B) bool {
if oldv > newv {
return oldv-newv <= B(eps)
}
return newv-oldv <= B(eps)
}
func concat(a, b string) string {
return a + b
}
func mapAppend(suffix string) func(string) string {
return func(v string) string {
return v + suffix
}
}
type addable interface {
~int | ~float64
}
func add[T addable](v0, v1 T) T {
return v0 + v1
}
func ident[T any](v T) T {
return v
}
func identMany[T any](v ...T) (out T) {
if len(v) > 0 {
out = v[0]
}
return
}
var _ Incr[any] = (*mockBareNode)(nil)
func mockObserver(scope Scope) IObserver {
return WithinScope(scope, &observeIncr[any]{
n: NewNode("mock_observer"),
})
}
func newMockBareNodeWithHeight(scope Scope, height int) *mockBareNode {
mbn := WithinScope(scope, &mockBareNode{
n: NewNode("bare_node"),
})
mbn.n.height = height
return mbn
}
func newMockBareNode(scope Scope) *mockBareNode {
o := WithinScope(scope, &mockBareNode{
n: NewNode("bare_node"),
})
o.Node().height = 0
return o
}
type mockBareNode struct {
n *Node
parents []INode
}
func (mn *mockBareNode) String() string { return mn.n.String() }
func (mn *mockBareNode) Parents() []INode { return mn.parents }
func (mn *mockBareNode) Node() *Node {
return mn.n
}
func (mn *mockBareNode) Value() any {
return nil
}
func newHeightIncr(scope Scope, height int) *heightIncr {
return WithinScope(scope, &heightIncr{
n: &Node{
id: NewIdentifier(),
height: height,
},
})
}
type heightIncr struct {
Incr[struct{}]
n *Node
}
func (hi *heightIncr) String() string { return hi.n.String() }
func (hi *heightIncr) Parents() []INode { return nil }
func (hi heightIncr) Node() *Node {
return hi.n
}
func newList(items ...INode) *recomputeHeapList {
l := new(recomputeHeapList)
for _, i := range items {
i.Node().heightInRecomputeHeap = i.Node().height
l.push(i)
}
return l
}
func createDynamicMaps(scope Scope, label string) Incr[string] {
mapVar0 := Var(scope, fmt.Sprintf("%s-0", label))
mapVar0.Node().SetLabel(fmt.Sprintf("%sv-0", label))
mapVar1 := Var(scope, fmt.Sprintf("%s-1", label))
mapVar1.Node().SetLabel(fmt.Sprintf("%sv-1", label))
m := Map2(scope, mapVar0, mapVar1, func(a, b string) string {
return a + "+" + b
})
m.Node().SetLabel(label)
return m
}
func createDynamicBind(scope Scope, label string, a, b Incr[string]) (VarIncr[string], BindIncr[string]) {
bindVar := Var(scope, "a")
bindVar.Node().SetLabel(fmt.Sprintf("bind - %s - var", label))
bind := Bind(scope, bindVar, func(scope Scope, which string) Incr[string] {
if which == "a" {
m := Map(scope, a, func(v string) string {
return v + "->" + label
})
m.Node().SetLabel(fmt.Sprintf("bind - %s - %s - map", label, which))
return m
}
if which == "b" {
m := Map(scope, b, func(v string) string {
return v + "->" + label
})
m.Node().SetLabel(fmt.Sprintf("bind - %s - %s - map", label, which))
return m
}
return nil
})
bind.Node().SetLabel(fmt.Sprintf("bind - %s", label))
return bindVar, bind
}
func homedir(filename string) string {
var rootDir string
if rootDir = os.Getenv("INCR_DEBUG_DOT_ROOT"); rootDir == "" {
rootDir = os.ExpandEnv("$HOME/Desktop")
}
return filepath.Join(rootDir, filename)
}
func dumpDot(g *Graph, path string) error {
if os.Getenv("INCR_DEBUG_DOT") != "true" {
return nil
}
dotContents := new(bytes.Buffer)
if err := Dot(dotContents, g); err != nil {
return err
}
dotOutput, err := os.Create(os.ExpandEnv(path))
if err != nil {
return err
}
defer func() { _ = dotOutput.Close() }()
dotFullPath, err := exec.LookPath("dot")
if err != nil {
return fmt.Errorf("there was an issue finding `dot` in your path; you may need to install the `graphviz` package or similar on your platform: %w", err)
}
errOut := new(bytes.Buffer)
cmd := exec.Command(dotFullPath, "-Tpng")
cmd.Stdin = dotContents
cmd.Stdout = dotOutput
cmd.Stderr = errOut
if err := cmd.Run(); err != nil {
return fmt.Errorf("%v; %w", errOut.String(), err)
}
return nil
}
func hasKey[A INode](nodes []A, id Identifier) bool {
for _, n := range nodes {
if n.Node().id == id {
return true
}
}
return false
}
func cutoffAlways[A, B any](scope Scope, input Incr[A], cutoff func(context.Context, A) (bool, error), fn func(context.Context, A) (B, error)) Incr[B] {
return WithinScope(scope, &ccutoffAlwaysIncr[A, B]{
n: NewNode("cutoff-always"),
input: input,
cutoff: cutoff,
fn: fn,
})
}
type ccutoffAlwaysIncr[A, B any] struct {
n *Node
input Incr[A]
cutoff func(context.Context, A) (bool, error)
fn func(context.Context, A) (B, error)
value B
}
func (n *ccutoffAlwaysIncr[A, B]) Parents() []INode {
return []INode{n.input}
}
func (n *ccutoffAlwaysIncr[A, B]) Node() *Node { return n.n }
func (n *ccutoffAlwaysIncr[A, B]) Value() B { return n.value }
func (n *ccutoffAlwaysIncr[A, B]) Always() {}
func (n *ccutoffAlwaysIncr[A, B]) Cutoff(ctx context.Context) (bool, error) {
return n.cutoff(ctx, n.input.Value())
}
func (n *ccutoffAlwaysIncr[A, B]) Stabilize(ctx context.Context) (err error) {
var value B
value, err = n.fn(ctx, n.input.Value())
if err != nil {
return
}
n.value = value
return
}
func (n *ccutoffAlwaysIncr[A, B]) String() string {
return n.n.String()
}