暂无描述

graph_test.go 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package graph
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func edgeDebugString(edge *Edge) string {
  7. debug := ""
  8. debug += fmt.Sprintf("\t\tSrc: %p\n", edge.Src)
  9. debug += fmt.Sprintf("\t\tDest: %p\n", edge.Dest)
  10. debug += fmt.Sprintf("\t\tWeight: %d\n", edge.Weight)
  11. debug += fmt.Sprintf("\t\tResidual: %t\n", edge.Residual)
  12. debug += fmt.Sprintf("\t\tInline: %t\n", edge.Inline)
  13. return debug
  14. }
  15. func edgeMapsDebugString(in, out EdgeMap) string {
  16. debug := ""
  17. debug += "In Edges:\n"
  18. for parent, edge := range in {
  19. debug += fmt.Sprintf("\tParent: %p\n", parent)
  20. debug += edgeDebugString(edge)
  21. }
  22. debug += "Out Edges:\n"
  23. for child, edge := range out {
  24. debug += fmt.Sprintf("\tChild: %p\n", child)
  25. debug += edgeDebugString(edge)
  26. }
  27. return debug
  28. }
  29. func graphDebugString(graph *Graph) string {
  30. debug := ""
  31. for i, node := range graph.Nodes {
  32. debug += fmt.Sprintf("Node %d: %p\n", i, node)
  33. }
  34. for i, node := range graph.Nodes {
  35. debug += "\n"
  36. debug += fmt.Sprintf("=== Node %d: %p ===\n", i, node)
  37. debug += edgeMapsDebugString(node.In, node.Out)
  38. }
  39. return debug
  40. }
  41. func expectedNodesDebugString(Expected []ExpectedNode) string {
  42. debug := ""
  43. for i, node := range Expected {
  44. debug += fmt.Sprintf("Node %d: %p\n", i, node.Node)
  45. }
  46. for i, node := range Expected {
  47. debug += "\n"
  48. debug += fmt.Sprintf("=== Node %d: %p ===\n", i, node.Node)
  49. debug += edgeMapsDebugString(node.In, node.Out)
  50. }
  51. return debug
  52. }
  53. // Checks if all the edges in this equal all the edges in that.
  54. func edgeMapsEqual(this, that EdgeMap) bool {
  55. if len(this) != len(that) {
  56. return false
  57. }
  58. for node, thisEdge := range this {
  59. if *thisEdge != *that[node] {
  60. return false
  61. }
  62. }
  63. return true
  64. }
  65. // Check if node is equal to Expected
  66. func nodesEqual(node *Node, Expected ExpectedNode) bool {
  67. return node == Expected.Node && edgeMapsEqual(node.In, Expected.In) &&
  68. edgeMapsEqual(node.Out, Expected.Out)
  69. }
  70. // Check if the graph equals the one templated by Expected.
  71. func graphsEqual(graph *Graph, Expected []ExpectedNode) bool {
  72. if len(graph.Nodes) != len(Expected) {
  73. return false
  74. }
  75. ExpectedSet := make(map[*Node]ExpectedNode)
  76. for i := range Expected {
  77. ExpectedSet[Expected[i].Node] = Expected[i]
  78. }
  79. for _, node := range graph.Nodes {
  80. ExpectedNode, found := ExpectedSet[node]
  81. if !found || !nodesEqual(node, ExpectedNode) {
  82. return false
  83. }
  84. }
  85. return true
  86. }
  87. type ExpectedNode struct {
  88. Node *Node
  89. In, Out EdgeMap
  90. }
  91. type TrimTreeTestCase struct {
  92. Initial *Graph
  93. Expected []ExpectedNode
  94. Keep NodePtrSet
  95. }
  96. // Makes the edge from parent to child residual
  97. func makeExpectedEdgeResidual(parent, child ExpectedNode) {
  98. parent.Out[child.Node].Residual = true
  99. child.In[parent.Node].Residual = true
  100. }
  101. func makeEdgeInline(edgeMap EdgeMap, node *Node) {
  102. edgeMap[node].Inline = true
  103. }
  104. func setEdgeWeight(edgeMap EdgeMap, node *Node, weight int64) {
  105. edgeMap[node].Weight = weight
  106. }
  107. // Creates a directed edges from the parent to each of the children
  108. func createEdges(parent *Node, children ...*Node) {
  109. for _, child := range children {
  110. edge := &Edge{
  111. Src: parent,
  112. Dest: child,
  113. }
  114. parent.Out[child] = edge
  115. child.In[parent] = edge
  116. }
  117. }
  118. // Creates a node without any edges
  119. func createEmptyNode() *Node {
  120. return &Node{
  121. In: make(EdgeMap),
  122. Out: make(EdgeMap),
  123. }
  124. }
  125. // Creates an array of ExpectedNodes from nodes.
  126. func createExpectedNodes(nodes ...*Node) ([]ExpectedNode, NodePtrSet) {
  127. Expected := make([]ExpectedNode, len(nodes))
  128. Keep := make(NodePtrSet, len(nodes))
  129. for i, node := range nodes {
  130. Expected[i] = ExpectedNode{
  131. Node: node,
  132. In: make(EdgeMap),
  133. Out: make(EdgeMap),
  134. }
  135. Keep[node] = true
  136. }
  137. return Expected, Keep
  138. }
  139. // Creates a directed edges from the parent to each of the children
  140. func createExpectedEdges(parent ExpectedNode, children ...ExpectedNode) {
  141. for _, child := range children {
  142. edge := &Edge{
  143. Src: parent.Node,
  144. Dest: child.Node,
  145. }
  146. parent.Out[child.Node] = edge
  147. child.In[parent.Node] = edge
  148. }
  149. }
  150. // The first test case looks like:
  151. // 0
  152. // |(5)
  153. // 1
  154. // (3)/ \(4)
  155. // 2 3
  156. //
  157. // After Keeping 0, 2, 3. We should see:
  158. // 0
  159. // (3)/ \(4)
  160. // 2 3
  161. func createTestCase1() TrimTreeTestCase {
  162. // Create Initial graph
  163. graph := &Graph{make(Nodes, 4)}
  164. nodes := graph.Nodes
  165. for i := range nodes {
  166. nodes[i] = createEmptyNode()
  167. }
  168. createEdges(nodes[0], nodes[1])
  169. createEdges(nodes[1], nodes[2], nodes[3])
  170. makeEdgeInline(nodes[0].Out, nodes[1])
  171. makeEdgeInline(nodes[1].Out, nodes[2])
  172. setEdgeWeight(nodes[0].Out, nodes[1], 5)
  173. setEdgeWeight(nodes[1].Out, nodes[2], 3)
  174. setEdgeWeight(nodes[1].Out, nodes[3], 4)
  175. // Create Expected graph
  176. Expected, Keep := createExpectedNodes(nodes[0], nodes[2], nodes[3])
  177. createExpectedEdges(Expected[0], Expected[1], Expected[2])
  178. makeEdgeInline(Expected[0].Out, Expected[1].Node)
  179. makeExpectedEdgeResidual(Expected[0], Expected[1])
  180. makeExpectedEdgeResidual(Expected[0], Expected[2])
  181. setEdgeWeight(Expected[0].Out, Expected[1].Node, 3)
  182. setEdgeWeight(Expected[0].Out, Expected[2].Node, 4)
  183. return TrimTreeTestCase{
  184. Initial: graph,
  185. Expected: Expected,
  186. Keep: Keep,
  187. }
  188. }
  189. // This test case looks like:
  190. // 3
  191. // | (12)
  192. // 1
  193. // | (8)
  194. // 2
  195. // | (15)
  196. // 0
  197. // | (10)
  198. // 4
  199. //
  200. // After Keeping 3 and 4. We should see:
  201. // 3
  202. // | (10)
  203. // 4
  204. func createTestCase2() TrimTreeTestCase {
  205. // Create Initial graph
  206. graph := &Graph{make(Nodes, 5)}
  207. nodes := graph.Nodes
  208. for i := range nodes {
  209. nodes[i] = createEmptyNode()
  210. }
  211. createEdges(nodes[3], nodes[1])
  212. createEdges(nodes[1], nodes[2])
  213. createEdges(nodes[2], nodes[0])
  214. createEdges(nodes[0], nodes[4])
  215. setEdgeWeight(nodes[3].Out, nodes[1], 12)
  216. setEdgeWeight(nodes[1].Out, nodes[2], 8)
  217. setEdgeWeight(nodes[2].Out, nodes[0], 15)
  218. setEdgeWeight(nodes[0].Out, nodes[4], 10)
  219. // Create Expected graph
  220. Expected, Keep := createExpectedNodes(nodes[3], nodes[4])
  221. createExpectedEdges(Expected[0], Expected[1])
  222. makeExpectedEdgeResidual(Expected[0], Expected[1])
  223. setEdgeWeight(Expected[0].Out, Expected[1].Node, 10)
  224. return TrimTreeTestCase{
  225. Initial: graph,
  226. Expected: Expected,
  227. Keep: Keep,
  228. }
  229. }
  230. // If we trim an empty graph it should still be empty afterwards
  231. func createTestCase3() TrimTreeTestCase {
  232. graph := &Graph{make(Nodes, 0)}
  233. Expected, Keep := createExpectedNodes()
  234. return TrimTreeTestCase{
  235. Initial: graph,
  236. Expected: Expected,
  237. Keep: Keep,
  238. }
  239. }
  240. // This test case looks like:
  241. // 0
  242. //
  243. // After Keeping 0. We should see:
  244. // 0
  245. func createTestCase4() TrimTreeTestCase {
  246. graph := &Graph{make(Nodes, 1)}
  247. nodes := graph.Nodes
  248. for i := range nodes {
  249. nodes[i] = createEmptyNode()
  250. }
  251. Expected, Keep := createExpectedNodes(nodes[0])
  252. return TrimTreeTestCase{
  253. Initial: graph,
  254. Expected: Expected,
  255. Keep: Keep,
  256. }
  257. }
  258. func createTrimTreeTestCases() []TrimTreeTestCase {
  259. caseGenerators := []func() TrimTreeTestCase{
  260. createTestCase1,
  261. createTestCase2,
  262. createTestCase3,
  263. createTestCase4,
  264. }
  265. cases := make([]TrimTreeTestCase, len(caseGenerators))
  266. for i, gen := range caseGenerators {
  267. cases[i] = gen()
  268. }
  269. return cases
  270. }
  271. func TestTrimTree(t *testing.T) {
  272. tests := createTrimTreeTestCases()
  273. for _, test := range tests {
  274. graph := test.Initial
  275. graph.TrimTree(test.Keep)
  276. if !graphsEqual(graph, test.Expected) {
  277. t.Fatalf("Graphs do not match.\nExpected: %s\nFound: %s\n",
  278. expectedNodesDebugString(test.Expected),
  279. graphDebugString(graph))
  280. }
  281. }
  282. }