forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_test.go
209 lines (169 loc) · 4.79 KB
/
graph_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
package gorgonia
import (
"testing"
"github.com/gonum/graph"
"github.com/gonum/graph/topo"
"github.com/stretchr/testify/assert"
)
func TestGraphBasics(t *testing.T) {
assert := assert.New(t)
g, x, y, xy := simpleEqn()
// basic stuff
assert.Equal(g, xy.g)
assert.Contains(g.AllNodes(), x)
assert.Contains(g.AllNodes(), y)
assert.Contains(g.AllNodes(), xy)
assert.Equal(Nodes{x, y}, g.leaves)
// Node/addressing stuff
xid := x.ID()
xFromID := g.Node(xid)
assert.Equal(x, xFromID)
var correctTo Nodes
correctTo = Nodes{xy}
assert.Equal(correctTo, g.to[x])
assert.Equal(correctTo, g.to[y])
// test Uniquifying ability of ExprGraph
newX := g.AddNode(x)
assert.Equal(x, newX)
newY := g.AddNode(y)
assert.Equal(y, newY)
newXY := Must(Add(x, y))
correctTo = append(correctTo, xy) // note this is correct. .Set() will be called when graph.To() is called
assert.Equal(xy, newXY)
assert.Equal(correctTo, g.to[y])
assert.Equal(correctTo, g.to[x])
correctTo = Nodes{xy}
assert.Equal(correctTo, graphNodeToNode(g.To(y)))
assert.Equal(correctTo, graphNodeToNode(g.To(x)))
assert.Equal(3, len(g.Nodes()))
// Now, time to deal with constants
xy1 := Must(Add(xy, onef64))
assert.Nil(onef64.g)
assert.Equal(g, xy1.g)
var containsOne bool
for _, node := range g.Nodes() {
n := node.(*Node)
if n.Hashcode() == onef64.Hashcode() {
containsOne = true
break
}
}
if !containsOne {
t.Errorf("graph does not contain a clone of onef64: %v", g.Nodes())
}
// duplicate constants
one := NewConstant(1.0)
newOne := g.AddNode(one)
if one == newOne {
t.Error("one should not have been added to the graph")
}
assert.NotNil(newOne.g)
assert.NotEqual(one, newOne)
}
// This test is added to make sure I'm sane when dealing with sorted graphs
// because sometimes Eobard Thawne is needed
func TestGraphSort(t *testing.T) {
assert := assert.New(t)
g, _, _, z := simpleVecEqn()
WithName("z")(z)
var sortedNodes []graph.Node
var err error
// stability tests
for i := 0; i < 100; i++ {
if sortedNodes, err = topo.Sort(g); err != nil {
t.Error(err)
}
// expected := Nodes{z, y, x} // the old version of ExprGraph was stable with topo.Sort, but the new version ain't
// assert.Equal(expected, sortedNodes)
assert.Equal(z, sortedNodes[0])
}
// this is to remind myself how this thing sorts:
t.Logf("%v", graphNodeToNode(sortedNodes))
}
// test that collisions are handled correctly
func TestGraphCollisions(t *testing.T) {
assert := assert.New(t)
g, _, _, xy := simpleEqn()
delete(g.byHash, xy.hash)
g.byHash[0xdeadbeef] = xy
xy.hash = 0xdeadbeef
xy.name = "original"
t.Logf("original: %p, hash %x", xy, xy.Hashcode())
col := new(Node)
col.name = "COLIN THE COLLISION"
col.hash = 0xdeadbeef
col.hashed = true
col2 := g.AddNode(col)
assert.Equal(col, col2)
assert.Equal(4, len(g.AllNodes()), "%v", g.AllNodes())
assert.True(g.Has(col))
colleen := new(Node)
colleen.name = "COLLEEN THE COLLISION"
colleen.hash = 0xdeadbeef
colleen.hashed = true
colleen2 := g.AddNode(colleen)
assert.Equal(colleen, colleen2)
assert.Equal(5, len(g.AllNodes()), "%v", g.AllNodes())
assert.True(g.Has(colleen))
}
func TestGraphEquality(t *testing.T) {
_, x, y, z := simpleVecEqn()
xh1 := x.Hashcode()
yh1 := y.Hashcode()
if xh1 == yh1 {
t.Error("Different nodes, should have different hashes")
}
_, x2, y2, z2 := simpleVecEqn()
if x.Hashcode() != x2.Hashcode() {
t.Error("They should have the same hash")
}
if y.Hashcode() != y2.Hashcode() {
t.Error("They should have the same hash")
}
if z.Hashcode() != z2.Hashcode() {
t.Error("They should have the same hash")
}
}
func TestGraphSubgraph(t *testing.T) {
var err error
var sortedNodes Nodes
assert := assert.New(t)
g, x, y, z := simpleVecEqn()
sub := Nodes{x, y}
g2 := g.subgraph(sub)
t.Logf("%v", g2.AllNodes())
if sortedNodes, err = Sort(g2); err != nil {
t.Fatal(err)
}
assert.NotContains(sortedNodes, z)
assert.Contains(g2.roots, x)
assert.Contains(g2.roots, y)
assert.Equal(2, len(g2.roots))
}
func TestGraphSubgraphRoots(t *testing.T) {
assert := assert.New(t)
g, x, y, z := simpleVecEqn()
sz := Must(Sum(z))
a := NewVector(g, Float64, WithName("a"), WithShape(2))
b := NewVector(g, Float64, WithName("b"), WithShape(2))
c := Must(Add(a, b))
sc := Must(Sum(c))
var szVal, scVal Value
readSZ := Read(sz, &szVal)
readSC := Read(sc, &scVal)
// check that stmt nodes aren't included in the roots
sg := g.SubgraphRoots(readSZ, readSC)
assert.Contains(sg.roots, sz)
assert.Contains(sg.roots, sc)
assert.Equal(2, len(sg.roots))
// check that subgrapphing actually works
sg = g.SubgraphRoots(c)
ns := sg.AllNodes()
assert.NotContains(ns, sc)
assert.NotContains(ns, readSC)
assert.NotContains(ns, x)
assert.NotContains(ns, y)
assert.NotContains(ns, z)
assert.NotContains(ns, sz)
assert.NotContains(ns, readSZ)
}