|
1 | 1 | package model
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "reflect" |
4 | 6 | "testing"
|
5 | 7 | )
|
6 | 8 |
|
@@ -57,3 +59,173 @@ func TestCompleteGraph(t *testing.T) {
|
57 | 59 | })
|
58 | 60 | }
|
59 | 61 | }
|
| 62 | + |
| 63 | +func TestLadderGraph(t *testing.T) { |
| 64 | + testCases := []struct { |
| 65 | + nodes int |
| 66 | + expectedNodes map[Node]bool |
| 67 | + expectedEdges map[Node][]Node |
| 68 | + }{ |
| 69 | + { |
| 70 | + nodes: 0, |
| 71 | + expectedNodes: map[Node]bool{}, |
| 72 | + expectedEdges: map[Node][]Node{}, |
| 73 | + }, |
| 74 | + { |
| 75 | + nodes: 3, |
| 76 | + expectedNodes: map[Node]bool{ |
| 77 | + 0: true, 1: true, 2: true, 3: true, 4: true, 5: true, |
| 78 | + }, |
| 79 | + expectedEdges: map[Node][]Node{ |
| 80 | + 0: {1, 3}, |
| 81 | + 1: {0, 2, 4}, |
| 82 | + 2: {1, 5}, |
| 83 | + 3: {0, 4}, |
| 84 | + 4: {1, 3, 5}, |
| 85 | + 5: {2, 4}, |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + nodes: 5, |
| 90 | + expectedNodes: map[Node]bool{ |
| 91 | + 0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, |
| 92 | + }, |
| 93 | + expectedEdges: map[Node][]Node{ |
| 94 | + 0: {1}, |
| 95 | + 1: {0, 2}, |
| 96 | + 2: {1, 3}, |
| 97 | + 3: {2, 4}, |
| 98 | + 4: {3, 5}, |
| 99 | + 5: {4, 6}, |
| 100 | + 6: {5, 7}, |
| 101 | + 7: {6, 8}, |
| 102 | + 8: {7, 9}, |
| 103 | + 9: {8, 10}, |
| 104 | + 10: {9}, |
| 105 | + }, |
| 106 | + }, |
| 107 | + { |
| 108 | + nodes: 2, |
| 109 | + expectedNodes: map[Node]bool{ |
| 110 | + 0: true, 1: true, 2: true, |
| 111 | + }, |
| 112 | + expectedEdges: map[Node][]Node{ |
| 113 | + 0: {1}, |
| 114 | + 1: {0, 2}, |
| 115 | + 2: {1}, |
| 116 | + }, |
| 117 | + }, |
| 118 | + { |
| 119 | + nodes: 1, |
| 120 | + expectedNodes: map[Node]bool{ |
| 121 | + 0: true, 1: true, |
| 122 | + }, |
| 123 | + expectedEdges: map[Node][]Node{ |
| 124 | + 0: {1}, |
| 125 | + 1: {0}, |
| 126 | + }, |
| 127 | + }, |
| 128 | + } |
| 129 | + |
| 130 | + for _, tc := range testCases { |
| 131 | + t.Run(fmt.Sprintf("Nodes: %d", tc.nodes), func(t *testing.T) { |
| 132 | + g := LadderGraph(tc.nodes) |
| 133 | + validateGraph(t, g, tc.expectedNodes, tc.expectedEdges) |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestCircularLadderGraph(t *testing.T) { |
| 139 | + // Test case 1: Basic Test |
| 140 | + nodesInSinglePath := 4 |
| 141 | + g := CircularLadderGraph(nodesInSinglePath) |
| 142 | + expectedNodes := map[Node]bool{1: true, 2: true, 3: true, 4: true, 5: true, 6: true} |
| 143 | + expectedEdges := map[Node][]Node{ |
| 144 | + 1: {2, 6}, |
| 145 | + 2: {1, 3}, |
| 146 | + 3: {2, 4}, |
| 147 | + 4: {3, 5}, |
| 148 | + 5: {4, 6}, |
| 149 | + 6: {1, 5}, |
| 150 | + } |
| 151 | + validateGraph(t, g, expectedNodes, expectedEdges) |
| 152 | + |
| 153 | + // Test case 2: Edge Case Test |
| 154 | + nodesInSinglePath = 2 |
| 155 | + g = CircularLadderGraph(nodesInSinglePath) |
| 156 | + expectedNodes = map[Node]bool{1: true, 2: true, 3: true} |
| 157 | + expectedEdges = map[Node][]Node{ |
| 158 | + 1: {2, 3}, |
| 159 | + 2: {1, 3}, |
| 160 | + 3: {1, 2}, |
| 161 | + } |
| 162 | + validateGraph(t, g, expectedNodes, expectedEdges) |
| 163 | + |
| 164 | + // Test case 3: Larger Graph Test |
| 165 | + nodesInSinglePath = 6 |
| 166 | + g = CircularLadderGraph(nodesInSinglePath) |
| 167 | + expectedNodes = map[Node]bool{1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, 12: true} |
| 168 | + expectedEdges = map[Node][]Node{ |
| 169 | + 1: {2, 12}, |
| 170 | + 2: {1, 3}, |
| 171 | + 3: {2, 4}, |
| 172 | + 4: {3, 5}, |
| 173 | + 5: {4, 6}, |
| 174 | + 6: {5, 7}, |
| 175 | + 7: {6, 8}, |
| 176 | + 8: {7, 9}, |
| 177 | + 9: {8, 10}, |
| 178 | + 10: {9, 11}, |
| 179 | + 11: {10, 12}, |
| 180 | + 12: {1, 11}, |
| 181 | + } |
| 182 | + validateGraph(t, g, expectedNodes, expectedEdges) |
| 183 | + |
| 184 | + // Test case 4: Connectivity Test |
| 185 | + // Validate if all nodes are properly connected |
| 186 | + for node, neighbors := range g.Edges { |
| 187 | + for _, neighbor := range neighbors { |
| 188 | + if !contains(g.Edges[neighbor], node) { |
| 189 | + t.Errorf("Test case 4 failed: Nodes %d and %d are not properly connected", node, neighbor) |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + // Test case 5: Node Existence Test |
| 195 | + // Validate if all expected nodes exist |
| 196 | + for node := range expectedNodes { |
| 197 | + if !g.Nodes[node] { |
| 198 | + t.Errorf("Test case 5 failed: Expected node %d does not exist in the generated graph", node) |
| 199 | + } |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +// Helper function to validate the generated graph |
| 204 | +func validateGraph(t *testing.T, g *UndirectedGraph, expectedNodes map[Node]bool, expectedEdges map[Node][]Node) { |
| 205 | + // Validate nodes |
| 206 | + if len(g.Nodes) != len(expectedNodes) { |
| 207 | + t.Errorf("Nodes mismatch, expected: %v, got: %v", expectedNodes, g.Nodes) |
| 208 | + } else { |
| 209 | + for node := range expectedNodes { |
| 210 | + if !g.Nodes[node] { |
| 211 | + t.Errorf("Nodes mismatch, expected: %v, got: %v", expectedNodes, g.Nodes) |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + // Validate edges |
| 216 | + for node, edges := range expectedEdges { |
| 217 | + if !reflect.DeepEqual(g.Edges[node], edges) { |
| 218 | + t.Errorf("Edges mismatch for node %d, expected: %v, got: %v. Full graph: %v", node, edges, g.Edges[node], g) |
| 219 | + } |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +// Helper function to check if a node exists in a slice |
| 224 | +func contains(slice []Node, value Node) bool { |
| 225 | + for _, item := range slice { |
| 226 | + if item == value { |
| 227 | + return true |
| 228 | + } |
| 229 | + } |
| 230 | + return false |
| 231 | +} |
0 commit comments