|
| 1 | +import unittest |
| 2 | +from collections import deque |
| 3 | +from clone import Node, Solution |
| 4 | + |
| 5 | +# Assuming Node and Solution are already defined above |
| 6 | + |
| 7 | +class TestCloneGraph(unittest.TestCase): |
| 8 | + def build_graph(self, adj): |
| 9 | + if not adj: |
| 10 | + return None |
| 11 | + nodes = {i+1: Node(i+1) for i in range(len(adj))} |
| 12 | + for i, neighbors in enumerate(adj, start=1): |
| 13 | + nodes[i].neighbors = [nodes[n] for n in neighbors] |
| 14 | + return nodes[1] |
| 15 | + |
| 16 | + def graph_to_adj(self, node): |
| 17 | + if not node: |
| 18 | + return [] |
| 19 | + adj = {} |
| 20 | + q = deque([node]) |
| 21 | + seen = set() |
| 22 | + while q: |
| 23 | + cur = q.popleft() |
| 24 | + if cur.val in seen: |
| 25 | + continue |
| 26 | + seen.add(cur.val) |
| 27 | + adj[cur.val] = [n.val for n in cur.neighbors] |
| 28 | + for n in cur.neighbors: |
| 29 | + q.append(n) |
| 30 | + return [adj[i] for i in sorted(adj.keys())] |
| 31 | + |
| 32 | + def test_empty_graph(self): |
| 33 | + sol = Solution() |
| 34 | + self.assertIsNone(sol.cloneGraph(None)) |
| 35 | + |
| 36 | + def test_single_node(self): |
| 37 | + start = self.build_graph([[]]) |
| 38 | + sol = Solution() |
| 39 | + cloned = sol.cloneGraph(start) |
| 40 | + self.assertEqual(self.graph_to_adj(cloned), [[]]) |
| 41 | + self.assertIsNot(cloned, start) |
| 42 | + |
| 43 | + def test_two_nodes_connected(self): |
| 44 | + start = self.build_graph([[2], [1]]) |
| 45 | + sol = Solution() |
| 46 | + cloned = sol.cloneGraph(start) |
| 47 | + self.assertEqual(self.graph_to_adj(cloned), [[2], [1]]) |
| 48 | + self.assertIsNot(cloned, start) |
| 49 | + |
| 50 | + def test_four_cycle(self): |
| 51 | + start = self.build_graph([[2,4],[1,3],[2,4],[1,3]]) |
| 52 | + sol = Solution() |
| 53 | + cloned = sol.cloneGraph(start) |
| 54 | + self.assertEqual( |
| 55 | + self.graph_to_adj(cloned), |
| 56 | + [[2,4],[1,3],[2,4],[1,3]] |
| 57 | + ) |
| 58 | + self.assertIsNot(cloned, start) |
| 59 | + |
| 60 | + def test_branch_graph(self): |
| 61 | + start = self.build_graph([[2,3], [4], [4], []]) |
| 62 | + sol = Solution() |
| 63 | + cloned = sol.cloneGraph(start) |
| 64 | + self.assertEqual( |
| 65 | + self.graph_to_adj(cloned), |
| 66 | + [[2,3], [4], [4], []] |
| 67 | + ) |
| 68 | + self.assertIsNot(cloned, start) |
| 69 | + |
| 70 | +class TestCloneGraph1(unittest.TestCase): |
| 71 | + def build_diamond_graph(self): |
| 72 | + # 1-2-3 |
| 73 | + # \ | / |
| 74 | + # 4 |
| 75 | + |
| 76 | + n1 = Node(1) |
| 77 | + n2 = Node(2) |
| 78 | + n3 = Node(3) |
| 79 | + n4 = Node(4) |
| 80 | + |
| 81 | + n1.neighbors = [n2, n3] |
| 82 | + n2.neighbors = [n1, n4, n3] |
| 83 | + n3.neighbors = [n1, n2, n4] |
| 84 | + n4.neighbors = [n2, n3] |
| 85 | + |
| 86 | + return n1 |
| 87 | + |
| 88 | + def collect_edges(self, node): |
| 89 | + # helper to serialize the graph for comparison |
| 90 | + visited = set() |
| 91 | + q = deque([node]) |
| 92 | + edges = {} |
| 93 | + |
| 94 | + while q: |
| 95 | + cur = q.popleft() |
| 96 | + if cur.val in visited: |
| 97 | + continue |
| 98 | + visited.add(cur.val) |
| 99 | + edges[cur.val] = sorted([n.val for n in cur.neighbors]) |
| 100 | + for n in cur.neighbors: |
| 101 | + q.append(n) |
| 102 | + |
| 103 | + return edges |
| 104 | + |
| 105 | + def test_diamond_graph(self): |
| 106 | + sol = Solution() |
| 107 | + original = self.build_diamond_graph() |
| 108 | + cloned = sol.cloneGraph(original) |
| 109 | + |
| 110 | + original_edges = self.collect_edges(original) |
| 111 | + cloned_edges = self.collect_edges(cloned) |
| 112 | + |
| 113 | + self.assertEqual(original_edges, cloned_edges) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + unittest.main() |
0 commit comments