|
@@ -44,13 +44,13 @@ func graphDebugString(graph *Graph) string {
|
44
|
44
|
return debug
|
45
|
45
|
}
|
46
|
46
|
|
47
|
|
-func expectedNodesDebugString(Expected []ExpectedNode) string {
|
|
47
|
+func expectedNodesDebugString(expected []ExpectedNode) string {
|
48
|
48
|
debug := ""
|
49
|
|
- for i, node := range Expected {
|
|
49
|
+ for i, node := range expected {
|
50
|
50
|
debug += fmt.Sprintf("Node %d: %p\n", i, node.Node)
|
51
|
51
|
}
|
52
|
52
|
|
53
|
|
- for i, node := range Expected {
|
|
53
|
+ for i, node := range expected {
|
54
|
54
|
debug += "\n"
|
55
|
55
|
debug += fmt.Sprintf("=== Node %d: %p ===\n", i, node.Node)
|
56
|
56
|
debug += edgeMapsDebugString(node.In, node.Out)
|
|
@@ -58,7 +58,7 @@ func expectedNodesDebugString(Expected []ExpectedNode) string {
|
58
|
58
|
return debug
|
59
|
59
|
}
|
60
|
60
|
|
61
|
|
-// Checks if all the edges in this equal all the edges in that.
|
|
61
|
+// edgeMapsEqual checks if all the edges in this equal all the edges in that.
|
62
|
62
|
func edgeMapsEqual(this, that EdgeMap) bool {
|
63
|
63
|
if len(this) != len(that) {
|
64
|
64
|
return false
|
|
@@ -71,25 +71,25 @@ func edgeMapsEqual(this, that EdgeMap) bool {
|
71
|
71
|
return true
|
72
|
72
|
}
|
73
|
73
|
|
74
|
|
-// Check if node is equal to Expected
|
75
|
|
-func nodesEqual(node *Node, Expected ExpectedNode) bool {
|
76
|
|
- return node == Expected.Node && edgeMapsEqual(node.In, Expected.In) &&
|
77
|
|
- edgeMapsEqual(node.Out, Expected.Out)
|
|
74
|
+// nodesEqual checks if node is equal to expected.
|
|
75
|
+func nodesEqual(node *Node, expected ExpectedNode) bool {
|
|
76
|
+ return node == expected.Node && edgeMapsEqual(node.In, expected.In) &&
|
|
77
|
+ edgeMapsEqual(node.Out, expected.Out)
|
78
|
78
|
}
|
79
|
79
|
|
80
|
|
-// Check if the graph equals the one templated by Expected.
|
81
|
|
-func graphsEqual(graph *Graph, Expected []ExpectedNode) bool {
|
82
|
|
- if len(graph.Nodes) != len(Expected) {
|
|
80
|
+// graphsEqual checks if graph is equivalent to the graph templated by expected.
|
|
81
|
+func graphsEqual(graph *Graph, expected []ExpectedNode) bool {
|
|
82
|
+ if len(graph.Nodes) != len(expected) {
|
83
|
83
|
return false
|
84
|
84
|
}
|
85
|
|
- ExpectedSet := make(map[*Node]ExpectedNode)
|
86
|
|
- for i := range Expected {
|
87
|
|
- ExpectedSet[Expected[i].Node] = Expected[i]
|
|
85
|
+ expectedSet := make(map[*Node]ExpectedNode)
|
|
86
|
+ for i := range expected {
|
|
87
|
+ expectedSet[expected[i].Node] = expected[i]
|
88
|
88
|
}
|
89
|
89
|
|
90
|
90
|
for _, node := range graph.Nodes {
|
91
|
|
- ExpectedNode, found := ExpectedSet[node]
|
92
|
|
- if !found || !nodesEqual(node, ExpectedNode) {
|
|
91
|
+ expectedNode, found := expectedSet[node]
|
|
92
|
+ if !found || !nodesEqual(node, expectedNode) {
|
93
|
93
|
return false
|
94
|
94
|
}
|
95
|
95
|
}
|
|
@@ -107,7 +107,7 @@ type TrimTreeTestCase struct {
|
107
|
107
|
Keep NodePtrSet
|
108
|
108
|
}
|
109
|
109
|
|
110
|
|
-// Makes the edge from parent to child residual
|
|
110
|
+// makeExpectedEdgeResidual makes the edge from parent to child residual.
|
111
|
111
|
func makeExpectedEdgeResidual(parent, child ExpectedNode) {
|
112
|
112
|
parent.Out[child.Node].Residual = true
|
113
|
113
|
child.In[parent.Node].Residual = true
|
|
@@ -121,7 +121,7 @@ func setEdgeWeight(edgeMap EdgeMap, node *Node, weight int64) {
|
121
|
121
|
edgeMap[node].Weight = weight
|
122
|
122
|
}
|
123
|
123
|
|
124
|
|
-// Creates a directed edges from the parent to each of the children
|
|
124
|
+// createEdges creates directed edges from the parent to each of the children.
|
125
|
125
|
func createEdges(parent *Node, children ...*Node) {
|
126
|
126
|
for _, child := range children {
|
127
|
127
|
edge := &Edge{
|
|
@@ -133,7 +133,7 @@ func createEdges(parent *Node, children ...*Node) {
|
133
|
133
|
}
|
134
|
134
|
}
|
135
|
135
|
|
136
|
|
-// Creates a node without any edges
|
|
136
|
+// createEmptyNode creates a node without any edges.
|
137
|
137
|
func createEmptyNode() *Node {
|
138
|
138
|
return &Node{
|
139
|
139
|
In: make(EdgeMap),
|
|
@@ -141,24 +141,25 @@ func createEmptyNode() *Node {
|
141
|
141
|
}
|
142
|
142
|
}
|
143
|
143
|
|
144
|
|
-// Creates an array of ExpectedNodes from nodes.
|
|
144
|
+// createExpectedNodes creates a slice of ExpectedNodes from nodes.
|
145
|
145
|
func createExpectedNodes(nodes ...*Node) ([]ExpectedNode, NodePtrSet) {
|
146
|
|
- Expected := make([]ExpectedNode, len(nodes))
|
147
|
|
- Keep := make(NodePtrSet, len(nodes))
|
|
146
|
+ expected := make([]ExpectedNode, len(nodes))
|
|
147
|
+ keep := make(NodePtrSet, len(nodes))
|
148
|
148
|
|
149
|
149
|
for i, node := range nodes {
|
150
|
|
- Expected[i] = ExpectedNode{
|
|
150
|
+ expected[i] = ExpectedNode{
|
151
|
151
|
Node: node,
|
152
|
152
|
In: make(EdgeMap),
|
153
|
153
|
Out: make(EdgeMap),
|
154
|
154
|
}
|
155
|
|
- Keep[node] = true
|
|
155
|
+ keep[node] = true
|
156
|
156
|
}
|
157
|
157
|
|
158
|
|
- return Expected, Keep
|
|
158
|
+ return expected, keep
|
159
|
159
|
}
|
160
|
160
|
|
161
|
|
-// Creates a directed edges from the parent to each of the children
|
|
161
|
+// createExpectedEdges creates directed edges from the parent to each of the
|
|
162
|
+// children.
|
162
|
163
|
func createExpectedEdges(parent ExpectedNode, children ...ExpectedNode) {
|
163
|
164
|
for _, child := range children {
|
164
|
165
|
edge := &Edge{
|
|
@@ -170,17 +171,17 @@ func createExpectedEdges(parent ExpectedNode, children ...ExpectedNode) {
|
170
|
171
|
}
|
171
|
172
|
}
|
172
|
173
|
|
173
|
|
-// The first test case looks like:
|
|
174
|
+// createTestCase1 creates a test case that initally looks like:
|
174
|
175
|
// 0
|
175
|
176
|
// |(5)
|
176
|
177
|
// 1
|
177
|
178
|
// (3)/ \(4)
|
178
|
|
-// 2 3
|
|
179
|
+// 2 3.
|
179
|
180
|
//
|
180
|
|
-// After Keeping 0, 2, 3. We should see:
|
|
181
|
+// After keeping 0, 2, and 3, it expects the graph:
|
181
|
182
|
// 0
|
182
|
183
|
// (3)/ \(4)
|
183
|
|
-// 2 3
|
|
184
|
+// 2 3.
|
184
|
185
|
func createTestCase1() TrimTreeTestCase {
|
185
|
186
|
// Create Initial graph
|
186
|
187
|
graph := &Graph{make(Nodes, 4)}
|
|
@@ -197,21 +198,21 @@ func createTestCase1() TrimTreeTestCase {
|
197
|
198
|
setEdgeWeight(nodes[1].Out, nodes[3], 4)
|
198
|
199
|
|
199
|
200
|
// Create Expected graph
|
200
|
|
- Expected, Keep := createExpectedNodes(nodes[0], nodes[2], nodes[3])
|
201
|
|
- createExpectedEdges(Expected[0], Expected[1], Expected[2])
|
202
|
|
- makeEdgeInline(Expected[0].Out, Expected[1].Node)
|
203
|
|
- makeExpectedEdgeResidual(Expected[0], Expected[1])
|
204
|
|
- makeExpectedEdgeResidual(Expected[0], Expected[2])
|
205
|
|
- setEdgeWeight(Expected[0].Out, Expected[1].Node, 3)
|
206
|
|
- setEdgeWeight(Expected[0].Out, Expected[2].Node, 4)
|
|
201
|
+ expected, keep := createExpectedNodes(nodes[0], nodes[2], nodes[3])
|
|
202
|
+ createExpectedEdges(expected[0], expected[1], expected[2])
|
|
203
|
+ makeEdgeInline(expected[0].Out, expected[1].Node)
|
|
204
|
+ makeExpectedEdgeResidual(expected[0], expected[1])
|
|
205
|
+ makeExpectedEdgeResidual(expected[0], expected[2])
|
|
206
|
+ setEdgeWeight(expected[0].Out, expected[1].Node, 3)
|
|
207
|
+ setEdgeWeight(expected[0].Out, expected[2].Node, 4)
|
207
|
208
|
return TrimTreeTestCase{
|
208
|
209
|
Initial: graph,
|
209
|
|
- Expected: Expected,
|
210
|
|
- Keep: Keep,
|
|
210
|
+ Expected: expected,
|
|
211
|
+ Keep: keep,
|
211
|
212
|
}
|
212
|
213
|
}
|
213
|
214
|
|
214
|
|
-// This test case looks like:
|
|
215
|
+// createTestCase2 creates a test case that initially looks like:
|
215
|
216
|
// 3
|
216
|
217
|
// | (12)
|
217
|
218
|
// 1
|
|
@@ -220,12 +221,12 @@ func createTestCase1() TrimTreeTestCase {
|
220
|
221
|
// | (15)
|
221
|
222
|
// 0
|
222
|
223
|
// | (10)
|
223
|
|
-// 4
|
|
224
|
+// 4.
|
224
|
225
|
//
|
225
|
|
-// After Keeping 3 and 4. We should see:
|
|
226
|
+// After keeping 3 and 4, it expects the graph:
|
226
|
227
|
// 3
|
227
|
228
|
// | (10)
|
228
|
|
-// 4
|
|
229
|
+// 4.
|
229
|
230
|
func createTestCase2() TrimTreeTestCase {
|
230
|
231
|
// Create Initial graph
|
231
|
232
|
graph := &Graph{make(Nodes, 5)}
|
|
@@ -243,44 +244,45 @@ func createTestCase2() TrimTreeTestCase {
|
243
|
244
|
setEdgeWeight(nodes[0].Out, nodes[4], 10)
|
244
|
245
|
|
245
|
246
|
// Create Expected graph
|
246
|
|
- Expected, Keep := createExpectedNodes(nodes[3], nodes[4])
|
247
|
|
- createExpectedEdges(Expected[0], Expected[1])
|
248
|
|
- makeExpectedEdgeResidual(Expected[0], Expected[1])
|
249
|
|
- setEdgeWeight(Expected[0].Out, Expected[1].Node, 10)
|
|
247
|
+ expected, keep := createExpectedNodes(nodes[3], nodes[4])
|
|
248
|
+ createExpectedEdges(expected[0], expected[1])
|
|
249
|
+ makeExpectedEdgeResidual(expected[0], expected[1])
|
|
250
|
+ setEdgeWeight(expected[0].Out, expected[1].Node, 10)
|
250
|
251
|
return TrimTreeTestCase{
|
251
|
252
|
Initial: graph,
|
252
|
|
- Expected: Expected,
|
253
|
|
- Keep: Keep,
|
|
253
|
+ Expected: expected,
|
|
254
|
+ Keep: keep,
|
254
|
255
|
}
|
255
|
256
|
}
|
256
|
257
|
|
257
|
|
-// If we trim an empty graph it should still be empty afterwards
|
|
258
|
+// createTestCase3 creates an initally empty graph and expects an empty graph
|
|
259
|
+// after trimming.
|
258
|
260
|
func createTestCase3() TrimTreeTestCase {
|
259
|
261
|
graph := &Graph{make(Nodes, 0)}
|
260
|
|
- Expected, Keep := createExpectedNodes()
|
|
262
|
+ expected, keep := createExpectedNodes()
|
261
|
263
|
return TrimTreeTestCase{
|
262
|
264
|
Initial: graph,
|
263
|
|
- Expected: Expected,
|
264
|
|
- Keep: Keep,
|
|
265
|
+ Expected: expected,
|
|
266
|
+ Keep: keep,
|
265
|
267
|
}
|
266
|
268
|
}
|
267
|
269
|
|
268
|
|
-// This test case looks like:
|
269
|
|
-// 0
|
|
270
|
+// createTestCase4 creates a test case that initially looks like:
|
|
271
|
+// 0.
|
270
|
272
|
//
|
271
|
|
-// After Keeping 0. We should see:
|
272
|
|
-// 0
|
|
273
|
+// After keeping 0, it expects the graph:
|
|
274
|
+// 0.
|
273
|
275
|
func createTestCase4() TrimTreeTestCase {
|
274
|
276
|
graph := &Graph{make(Nodes, 1)}
|
275
|
277
|
nodes := graph.Nodes
|
276
|
278
|
for i := range nodes {
|
277
|
279
|
nodes[i] = createEmptyNode()
|
278
|
280
|
}
|
279
|
|
- Expected, Keep := createExpectedNodes(nodes[0])
|
|
281
|
+ expected, keep := createExpectedNodes(nodes[0])
|
280
|
282
|
return TrimTreeTestCase{
|
281
|
283
|
Initial: graph,
|
282
|
|
- Expected: Expected,
|
283
|
|
- Keep: Keep,
|
|
284
|
+ Expected: expected,
|
|
285
|
+ Keep: keep,
|
284
|
286
|
}
|
285
|
287
|
}
|
286
|
288
|
|