forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_test.go
154 lines (130 loc) · 3.48 KB
/
node_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
package gorgonia
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNodeBasics(t *testing.T) {
var n *Node
var c0, c1 *Node
g := NewGraph()
// withGraph
n = newNode(In(g))
if n.g == nil {
t.Error("Expected *Node to be constructed with a graph")
}
returnNode(n)
// withType
n = newNode(In(g), WithType(Float64))
if !n.t.Eq(Float64) {
t.Error("Expected *Node to be constructed with Float64")
}
returnNode(n)
// withOp
n = newNode(In(g), WithOp(newEBOByType(addOpType, Float64, Float64)))
if op, ok := n.op.(elemBinOp); ok {
if op.binOpType() != addOpType {
t.Error("expected addOpType")
}
} else {
t.Error("Expected *Node to be constructed with an addOp")
}
returnNode(n)
// withOp - statement op
n = newNode(In(g), WithOp(letOp{}))
if _, ok := n.op.(letOp); ok {
if !n.isStmt {
t.Errorf("Expected *Node.isStmt to be true when a statement op is passed in")
}
} else {
t.Error("Expected *Node to be constructed with a letOp")
}
returnNode(n)
// WithName
n = newNode(In(g), WithName("n"))
if n.name != "n" {
t.Error("Expected *Node to be constructed with a name \"n\"")
}
returnNode(n)
// withChildren
c0 = newNode(In(g), WithName("C0"))
c1 = newNode(In(g), WithName("C1"))
n = newNode(In(g), WithChildren(Nodes{c0, c1}))
if len(n.children) == 2 {
if !n.children.Contains(c0) || !n.children.Contains(c1) {
t.Error("Expected *Node to contain those two children")
}
} else {
t.Error("Expected *Node to be constructed with 2 children")
}
if !n.isRoot() {
t.Error("n is supposed to be root")
}
returnNode(n)
returnNode(c0)
returnNode(c1)
// withChildren but they're constants
c0 = NewConstant(3.14)
n = newNode(In(g), WithChildren(Nodes{c0}))
if len(n.children) != 1 {
t.Error("Expected *Node to have 1 child")
}
returnNode(n)
returnNode(c0)
// WithValue but no type
n = newNode(In(g), WithValue(F64(3.14)))
if !n.t.Eq(Float64) {
t.Error("Expected a *Node to be constructed WithValue to get its type from the value if none exists")
}
if !ValueEq(n.boundTo, newF64(3.14)) {
t.Error("Expected *Node to be bound to the correct value. Something has gone really wrong here")
}
returnNode(n)
// WithValue but with existing type that is the same
n = newNode(In(g), WithType(Float64), WithValue(F64(3.14)))
if !ValueEq(n.boundTo, newF64(3.14)) {
t.Error("Expected *Node to be bound to the correct value. Something has gone really wrong here")
}
returnNode(n)
// bad stuff
var f func()
// no graph
f = func() {
n = newNode(WithType(Float64))
}
assert.Panics(t, f)
// conflicting types, types were set first
f = func() {
n = newNode(In(g), WithType(Float32), WithValue(F64(1)))
}
assert.Panics(t, f)
// type mismatch - values were set first
f = func() {
n = newNode(In(g), WithValue(F64(1)), WithType(Float32))
}
assert.Panics(t, f)
// shape type mismatch
f = func() {
n = newNode(In(g), WithType(newTensorType(1, Float64)), WithShape(2, 1))
}
assert.Panics(t, f)
}
func TestNewUniqueNodes(t *testing.T) {
var n *Node
var c0, c1 *Node
g := NewGraph()
// withChildren but they're constants
c0 = NewConstant(3.14)
c1 = newNode(In(g), WithValue(5.0))
n = NewUniqueNode(In(g), WithChildren(Nodes{c0, c1}))
if n.children[0].g == nil {
t.Error("Expected a cloned constant child to have graph g")
}
returnNode(n)
}
func TestCloneTo(t *testing.T) {
g := NewGraph()
g2 := NewGraph()
n := NewUniqueNode(WithName("n"), WithType(Float64), In(g))
n.CloneTo(g2)
assert.True(t, nodeEq(g2.AllNodes()[0], n))
}