-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap_n_test.go
161 lines (120 loc) · 3.09 KB
/
map_n_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
package incr
import (
"testing"
"github.com/wcharczuk/go-incr/testutil"
)
func Test_MapN_AddInput_beforeObservationStabilization(t *testing.T) {
ctx := testContext()
g := New()
r0 := Return(g, 1)
r1 := Return(g, 2)
mn := MapN(g, sum, r0, r1)
r2 := Return(g, 3)
err := mn.AddInput(r2)
testutil.NoError(t, err)
om := MustObserve(g, mn)
err = g.Stabilize(ctx)
testutil.NoError(t, err)
testutil.Equal(t, 6, om.Value())
}
func Test_MapN_AddInput_afterObservatino(t *testing.T) {
ctx := testContext()
g := New()
r0 := Return(g, 1)
r1 := Return(g, 2)
mn := MapN(g, sum, r0, r1)
om := MustObserve(g, mn)
r2 := Return(g, 3)
err := mn.AddInput(r2)
testutil.NoError(t, err)
err = g.Stabilize(ctx)
testutil.NoError(t, err)
testutil.Equal(t, 6, om.Value())
}
func Test_MapN_AddInput_afterStabilization(t *testing.T) {
ctx := testContext()
g := New()
r0 := Return(g, 1)
r1 := Return(g, 2)
r2 := Return(g, 3)
mn := MapN(g, sum, r0, r1, r2)
om := MustObserve(g, mn)
err := g.Stabilize(ctx)
testutil.NoError(t, err)
testutil.Equal(t, 6, om.Value())
r3 := Return(g, 4)
err = mn.AddInput(r3)
testutil.NoError(t, err)
err = g.Stabilize(ctx)
testutil.NoError(t, err)
testutil.Equal(t, 10, om.Value())
}
func sum[A ~int | ~float64](values ...A) (out A) {
for _, v := range values {
out += v
}
return
}
func Test_MapN_RemoveInput(t *testing.T) {
ctx := testContext()
g := New()
r0 := Return(g, 1)
r1 := Return(g, 2)
mn := MapN(g, sum, r0, r1)
om := MustObserve(g, mn)
r2 := Return(g, 3)
err := mn.AddInput(r2)
testutil.NoError(t, err)
err = g.Stabilize(ctx)
testutil.NoError(t, err)
testutil.Equal(t, 6, om.Value())
err = mn.RemoveInput(r1.Node().ID())
testutil.NoError(t, err)
testutil.Equal(t, 2, len(mn.Node().parents))
err = g.Stabilize(ctx)
testutil.NoError(t, err)
testutil.Equal(t, 4, om.Value())
hasR1 := g.Has(r1)
testutil.Equal(t, false, hasR1)
}
func Test_MapN_RemoveInput_onlyInput(t *testing.T) {
ctx := testContext()
g := New()
mn := MapN[int](g, sum)
om := MustObserve(g, mn)
r2 := Return(g, 3)
err := mn.AddInput(r2)
testutil.NoError(t, err)
err = g.Stabilize(ctx)
testutil.NoError(t, err)
testutil.Equal(t, 3, om.Value())
err = mn.RemoveInput(r2.Node().ID())
testutil.NoError(t, err)
testutil.Equal(t, 0, len(mn.Node().parents))
err = g.Stabilize(ctx)
testutil.NoError(t, err)
testutil.Equal(t, 0, om.Value())
hasR2 := g.Has(r2)
testutil.Equal(t, false, hasR2)
}
func Test_MapN_RemoveInput_heightUpdates(t *testing.T) {
ctx := testContext()
g := New()
r0 := Return(g, 2)
m0 := Map[int](g, r0, ident)
r1 := Return(g, 1)
mn := MapN[int](g, sum, r1)
om := MustObserve(g, mn)
err := mn.AddInput(m0)
testutil.NoError(t, err)
err = g.Stabilize(ctx)
testutil.NoError(t, err)
testutil.Equal(t, 3, om.Value())
testutil.Equal(t, 2, mn.Node().height)
err = mn.RemoveInput(m0.Node().ID())
testutil.NoError(t, err)
err = g.Stabilize(ctx)
testutil.NoError(t, err)
testutil.Equal(t, 1, om.Value())
testutil.Equal(t, 2, mn.Node().height, "the height should stay the same as strictly it shouldn't get smaller, but staying higher is fine")
}