瀏覽代碼

Adds Weight checking to graph_test

Wade Simba Khadder 8 年之前
父節點
當前提交
9def89fcf4
共有 1 個檔案被更改,包括 26 行新增17 行删除
  1. 26
    17
      internal/graph/graph_test.go

+ 26
- 17
internal/graph/graph_test.go 查看文件

9
 	debug := ""
9
 	debug := ""
10
 	debug += fmt.Sprintf("\t\tSrc: %p\n", edge.Src)
10
 	debug += fmt.Sprintf("\t\tSrc: %p\n", edge.Src)
11
 	debug += fmt.Sprintf("\t\tDest: %p\n", edge.Dest)
11
 	debug += fmt.Sprintf("\t\tDest: %p\n", edge.Dest)
12
+	debug += fmt.Sprintf("\t\tWeight: %d\n", edge.Weight)
12
 	debug += fmt.Sprintf("\t\tResidual: %t\n", edge.Residual)
13
 	debug += fmt.Sprintf("\t\tResidual: %t\n", edge.Residual)
13
 	debug += fmt.Sprintf("\t\tInline: %t\n", edge.Inline)
14
 	debug += fmt.Sprintf("\t\tInline: %t\n", edge.Inline)
14
 	return debug
15
 	return debug
57
 	return debug
58
 	return debug
58
 }
59
 }
59
 
60
 
60
-// Checks if two edges are equal
61
-func edgesEqual(this, that *Edge) bool {
62
-	return this.Src == that.Src && this.Dest == that.Dest &&
63
-		this.Residual == that.Residual && this.Inline == that.Inline
64
-}
65
-
66
 // Checks if all the edges in this equal all the edges in that.
61
 // Checks if all the edges in this equal all the edges in that.
67
 func edgeMapsEqual(this, that EdgeMap) bool {
62
 func edgeMapsEqual(this, that EdgeMap) bool {
68
 	if len(this) != len(that) {
63
 	if len(this) != len(that) {
69
 		return false
64
 		return false
70
 	}
65
 	}
71
 	for node, thisEdge := range this {
66
 	for node, thisEdge := range this {
72
-		if !edgesEqual(thisEdge, that[node]) {
67
+		if *thisEdge != *that[node] {
73
 			return false
68
 			return false
74
 		}
69
 		}
75
 	}
70
 	}
122
 	edgeMap[node].Inline = true
117
 	edgeMap[node].Inline = true
123
 }
118
 }
124
 
119
 
120
+func setEdgeWeight(edgeMap EdgeMap, node *Node, weight int64) {
121
+	edgeMap[node].Weight = weight
122
+}
123
+
125
 // Creates a directed edges from the parent to each of the children
124
 // Creates a directed edges from the parent to each of the children
126
 func createEdges(parent *Node, children ...*Node) {
125
 func createEdges(parent *Node, children ...*Node) {
127
 	for _, child := range children {
126
 	for _, child := range children {
173
 
172
 
174
 // The first test case looks like:
173
 // The first test case looks like:
175
 //     0
174
 //     0
176
-//     |
175
+//     |(5)
177
 //     1
176
 //     1
178
-//   /   \
179
-//  2     3
177
+// (3)/ \(4)
178
+//   2   3
180
 //
179
 //
181
 // After Keeping 0, 2, 3. We should see:
180
 // After Keeping 0, 2, 3. We should see:
182
 //     0
181
 //     0
183
-//   /   \
184
-//  2     3
182
+// (3)/ \(4)
183
+//   2   3
185
 func createTestCase1() TrimTreeTestCase {
184
 func createTestCase1() TrimTreeTestCase {
186
 	// Create Initial graph
185
 	// Create Initial graph
187
 	graph := &Graph{make(Nodes, 4)}
186
 	graph := &Graph{make(Nodes, 4)}
193
 	createEdges(nodes[1], nodes[2], nodes[3])
192
 	createEdges(nodes[1], nodes[2], nodes[3])
194
 	makeEdgeInline(nodes[0].Out, nodes[1])
193
 	makeEdgeInline(nodes[0].Out, nodes[1])
195
 	makeEdgeInline(nodes[1].Out, nodes[2])
194
 	makeEdgeInline(nodes[1].Out, nodes[2])
195
+	setEdgeWeight(nodes[0].Out, nodes[1], 5)
196
+	setEdgeWeight(nodes[1].Out, nodes[2], 3)
197
+	setEdgeWeight(nodes[1].Out, nodes[3], 4)
196
 
198
 
197
 	// Create Expected graph
199
 	// Create Expected graph
198
 	Expected, Keep := createExpectedNodes(nodes[0], nodes[2], nodes[3])
200
 	Expected, Keep := createExpectedNodes(nodes[0], nodes[2], nodes[3])
200
 	makeEdgeInline(Expected[0].Out, Expected[1].Node)
202
 	makeEdgeInline(Expected[0].Out, Expected[1].Node)
201
 	makeExpectedEdgeResidual(Expected[0], Expected[1])
203
 	makeExpectedEdgeResidual(Expected[0], Expected[1])
202
 	makeExpectedEdgeResidual(Expected[0], Expected[2])
204
 	makeExpectedEdgeResidual(Expected[0], Expected[2])
205
+	setEdgeWeight(Expected[0].Out, Expected[1].Node, 3)
206
+	setEdgeWeight(Expected[0].Out, Expected[2].Node, 4)
203
 	return TrimTreeTestCase{
207
 	return TrimTreeTestCase{
204
 		Initial:  graph,
208
 		Initial:  graph,
205
 		Expected: Expected,
209
 		Expected: Expected,
209
 
213
 
210
 // This test case looks like:
214
 // This test case looks like:
211
 //   3
215
 //   3
212
-//   |
216
+//   | (12)
213
 //   1
217
 //   1
214
-//   |
218
+//   | (8)
215
 //   2
219
 //   2
216
-//   |
220
+//   | (15)
217
 //   0
221
 //   0
218
-//   |
222
+//   | (10)
219
 //   4
223
 //   4
220
 //
224
 //
221
 // After Keeping 3 and 4. We should see:
225
 // After Keeping 3 and 4. We should see:
222
 //   3
226
 //   3
223
-//   |
227
+//   | (10)
224
 //   4
228
 //   4
225
 func createTestCase2() TrimTreeTestCase {
229
 func createTestCase2() TrimTreeTestCase {
226
 	// Create Initial graph
230
 	// Create Initial graph
233
 	createEdges(nodes[1], nodes[2])
237
 	createEdges(nodes[1], nodes[2])
234
 	createEdges(nodes[2], nodes[0])
238
 	createEdges(nodes[2], nodes[0])
235
 	createEdges(nodes[0], nodes[4])
239
 	createEdges(nodes[0], nodes[4])
240
+	setEdgeWeight(nodes[3].Out, nodes[1], 12)
241
+	setEdgeWeight(nodes[1].Out, nodes[2], 8)
242
+	setEdgeWeight(nodes[2].Out, nodes[0], 15)
243
+	setEdgeWeight(nodes[0].Out, nodes[4], 10)
236
 
244
 
237
 	// Create Expected graph
245
 	// Create Expected graph
238
 	Expected, Keep := createExpectedNodes(nodes[3], nodes[4])
246
 	Expected, Keep := createExpectedNodes(nodes[3], nodes[4])
239
 	createExpectedEdges(Expected[0], Expected[1])
247
 	createExpectedEdges(Expected[0], Expected[1])
240
 	makeExpectedEdgeResidual(Expected[0], Expected[1])
248
 	makeExpectedEdgeResidual(Expected[0], Expected[1])
249
+	setEdgeWeight(Expected[0].Out, Expected[1].Node, 10)
241
 	return TrimTreeTestCase{
250
 	return TrimTreeTestCase{
242
 		Initial:  graph,
251
 		Initial:  graph,
243
 		Expected: Expected,
252
 		Expected: Expected,