|
| 1 | +package copylistwithrandompointer |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestCopyRandomList(t *testing.T) { |
| 6 | + tests := []struct { |
| 7 | + name string |
| 8 | + input [][]interface{} |
| 9 | + }{ |
| 10 | + { |
| 11 | + name: "example 1", |
| 12 | + input: [][]interface{}{{7, nil}, {13, 0}, {11, 4}, {10, 2}, {1, 0}}, |
| 13 | + }, |
| 14 | + { |
| 15 | + name: "example 2", |
| 16 | + input: [][]interface{}{{1, 1}, {2, 1}}, |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "example 3", |
| 20 | + input: [][]interface{}{{3, nil}, {3, 0}, {3, nil}}, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "empty list", |
| 24 | + input: [][]interface{}{}, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "single node no random", |
| 28 | + input: [][]interface{}{{1, nil}}, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "single node self random", |
| 32 | + input: [][]interface{}{{1, 0}}, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "all random null", |
| 36 | + input: [][]interface{}{{1, nil}, {2, nil}, {3, nil}}, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "all random to first", |
| 40 | + input: [][]interface{}{{1, nil}, {2, 0}, {3, 0}, {4, 0}}, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + for _, tt := range tests { |
| 45 | + t.Run(tt.name, func(t *testing.T) { |
| 46 | + head := buildList(tt.input) |
| 47 | + copied := copyRandomList(head) |
| 48 | + |
| 49 | + if !verifyDeepCopy(head, copied, tt.input) { |
| 50 | + t.Errorf("Deep copy verification failed") |
| 51 | + } |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func buildList(data [][]interface{}) *Node { |
| 57 | + if len(data) == 0 { |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + nodes := make([]*Node, len(data)) |
| 62 | + for i, pair := range data { |
| 63 | + nodes[i] = &Node{Val: pair[0].(int)} |
| 64 | + } |
| 65 | + |
| 66 | + for i := 0; i < len(nodes)-1; i++ { |
| 67 | + nodes[i].Next = nodes[i+1] |
| 68 | + } |
| 69 | + |
| 70 | + for i, pair := range data { |
| 71 | + if pair[1] != nil { |
| 72 | + idx := pair[1].(int) |
| 73 | + nodes[i].Random = nodes[idx] |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return nodes[0] |
| 78 | +} |
| 79 | + |
| 80 | +func verifyDeepCopy(original, copied *Node, data [][]interface{}) bool { |
| 81 | + if original == nil && copied == nil { |
| 82 | + return true |
| 83 | + } |
| 84 | + if original == nil || copied == nil { |
| 85 | + return false |
| 86 | + } |
| 87 | + |
| 88 | + origNodes := []*Node{} |
| 89 | + copyNodes := []*Node{} |
| 90 | + |
| 91 | + for cur := original; cur != nil; cur = cur.Next { |
| 92 | + origNodes = append(origNodes, cur) |
| 93 | + } |
| 94 | + |
| 95 | + for cur := copied; cur != nil; cur = cur.Next { |
| 96 | + copyNodes = append(copyNodes, cur) |
| 97 | + } |
| 98 | + |
| 99 | + if len(origNodes) != len(copyNodes) { |
| 100 | + return false |
| 101 | + } |
| 102 | + |
| 103 | + for i := 0; i < len(origNodes); i++ { |
| 104 | + if origNodes[i] == copyNodes[i] { |
| 105 | + return false |
| 106 | + } |
| 107 | + |
| 108 | + if origNodes[i].Val != copyNodes[i].Val { |
| 109 | + return false |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + for i := 0; i < len(origNodes); i++ { |
| 114 | + origRandom := origNodes[i].Random |
| 115 | + copyRandom := copyNodes[i].Random |
| 116 | + |
| 117 | + if origRandom == nil && copyRandom == nil { |
| 118 | + continue |
| 119 | + } |
| 120 | + if origRandom == nil || copyRandom == nil { |
| 121 | + return false |
| 122 | + } |
| 123 | + |
| 124 | + origIdx := -1 |
| 125 | + for j, node := range origNodes { |
| 126 | + if node == origRandom { |
| 127 | + origIdx = j |
| 128 | + break |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if origIdx == -1 { |
| 133 | + return false |
| 134 | + } |
| 135 | + |
| 136 | + if copyRandom != copyNodes[origIdx] { |
| 137 | + return false |
| 138 | + } |
| 139 | + |
| 140 | + if copyRandom == origRandom { |
| 141 | + return false |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return true |
| 146 | +} |
0 commit comments