Geen omschrijving

graph_test.go 6.8KB

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