|
| 1 | +package singly |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestAppend(t *testing.T) { |
| 8 | + ll := NewLinkedList() |
| 9 | + ll.Append(1, "Node 1") |
| 10 | + ll.Append(2, "Node 2") |
| 11 | + ll.Append(3, "Node 3") |
| 12 | + |
| 13 | + if ll.Size() != 3 { |
| 14 | + t.Errorf("Expected size 3, got %d", ll.Size()) |
| 15 | + } |
| 16 | + |
| 17 | + if ll.Find(1).data != "Node 1" || ll.Find(2).data != "Node 2" || ll.Find(3).data != "Node 3" { |
| 18 | + t.Errorf("Data mismatch") |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func TestPrepend(t *testing.T) { |
| 23 | + ll := NewLinkedList() |
| 24 | + ll.Prepend(1, "Node 1") |
| 25 | + ll.Prepend(2, "Node 2") |
| 26 | + ll.Prepend(3, "Node 3") |
| 27 | + |
| 28 | + if ll.Size() != 3 { |
| 29 | + t.Errorf("Expected size 3, got %d", ll.Size()) |
| 30 | + } |
| 31 | + |
| 32 | + if ll.head.data != "Node 3" { |
| 33 | + t.Errorf("Expected head data 'Node 3', got '%v'", ll.head.data) |
| 34 | + } |
| 35 | + if ll.tail.data != "Node 1" { |
| 36 | + t.Errorf("Expected tail data 'Node 1', got '%v'", ll.tail.data) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestDeleteHead(t *testing.T) { |
| 41 | + ll := NewLinkedList() |
| 42 | + ll.Append(1, "Node 1") |
| 43 | + ll.Append(2, "Node 2") |
| 44 | + ll.Append(3, "Node 3") |
| 45 | + |
| 46 | + ll.DeleteHead() |
| 47 | + if ll.Size() != 2 { |
| 48 | + t.Errorf("Expected size 2, got %d", ll.Size()) |
| 49 | + } |
| 50 | + if ll.head.data != "Node 2" { |
| 51 | + t.Errorf("Expected head data 'Node 2', got '%v'", ll.head.data) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestDeleteTail(t *testing.T) { |
| 56 | + ll := NewLinkedList() |
| 57 | + ll.Append(1, "Node 1") |
| 58 | + ll.Append(2, "Node 2") |
| 59 | + ll.Append(3, "Node 3") |
| 60 | + |
| 61 | + ll.DeleteTail() |
| 62 | + if ll.Size() != 2 { |
| 63 | + t.Errorf("Expected size 2, got %d", ll.Size()) |
| 64 | + } |
| 65 | + if ll.tail.data != "Node 2" { |
| 66 | + t.Errorf("Expected tail data 'Node 2', got '%v'", ll.tail.data) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestDeleteAny(t *testing.T) { |
| 71 | + ll := NewLinkedList() |
| 72 | + ll.Append(1, "Node 1") |
| 73 | + ll.Append(2, "Node 2") |
| 74 | + ll.Append(3, "Node 3") |
| 75 | + |
| 76 | + ll.DeleteAny(2) |
| 77 | + if ll.Size() != 2 { |
| 78 | + t.Errorf("Expected size 2, got %d", ll.Size()) |
| 79 | + } |
| 80 | + if ll.Find(2) != nil { |
| 81 | + t.Errorf("Expected nil, got %v", ll.Find(2)) |
| 82 | + } |
| 83 | + if ll.head.next.data != "Node 3" { |
| 84 | + t.Errorf("Expected next node to be 'Node 3', got '%v'", ll.head.next.data) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestInsertBetween(t *testing.T) { |
| 89 | + ll := NewLinkedList() |
| 90 | + ll.Append(1, "Node 1") |
| 91 | + ll.Append(3, "Node 3") |
| 92 | + ll.InsertBetween(2, "Node 2", "Node 3", "Node 1") |
| 93 | + |
| 94 | + if ll.Size() != 3 { |
| 95 | + t.Errorf("Expected size 3, got %d", ll.Size()) |
| 96 | + } |
| 97 | + if ll.head.next.data != "Node 2" { |
| 98 | + t.Errorf("Expected next node to be 'Node 2', got '%v'", ll.head.next.data) |
| 99 | + } |
| 100 | + if ll.head.next.next.data != "Node 3" { |
| 101 | + t.Errorf("Expected next next node to be 'Node 3', got '%v'", ll.head.next.next.data) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestReverseList(t *testing.T) { |
| 106 | + ll := NewLinkedList() |
| 107 | + ll.Append(1, "Node 1") |
| 108 | + ll.Append(2, "Node 2") |
| 109 | + ll.Append(3, "Node 3") |
| 110 | + |
| 111 | + ll.ReverseList() |
| 112 | + |
| 113 | + if ll.head.data != "Node 3" { |
| 114 | + t.Errorf("Expected head data 'Node 3', got '%v'", ll.head.data) |
| 115 | + } |
| 116 | + if ll.head.next.data != "Node 2" { |
| 117 | + t.Errorf("Expected next node to be 'Node 2', got '%v'", ll.head.next.data) |
| 118 | + } |
| 119 | + if ll.head.next.next.data != "Node 1" { |
| 120 | + t.Errorf("Expected next next node to be 'Node 1', got '%v'", ll.head.next.next.data) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestClear(t *testing.T) { |
| 125 | + ll := NewLinkedList() |
| 126 | + ll.Append(1, "Node 1") |
| 127 | + ll.Append(2, "Node 2") |
| 128 | + ll.Append(3, "Node 3") |
| 129 | + |
| 130 | + ll.Clear() |
| 131 | + if ll.Size() != 0 { |
| 132 | + t.Errorf("Expected size 0, got %d", ll.Size()) |
| 133 | + } |
| 134 | + if ll.head != nil || ll.tail != nil { |
| 135 | + t.Errorf("Expected head and tail to be nil") |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestFind(t *testing.T) { |
| 140 | + ll := NewLinkedList() |
| 141 | + ll.Append(1, "Node 1") |
| 142 | + ll.Append(2, "Node 2") |
| 143 | + |
| 144 | + node := ll.Find(1) |
| 145 | + if node == nil { |
| 146 | + t.Errorf("Expected node with id 1, got nil") |
| 147 | + } |
| 148 | + if node.data != "Node 1" { |
| 149 | + t.Errorf("Expected node data 'Node 1', got '%v'", node.data) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func TestSize(t *testing.T) { |
| 154 | + ll := NewLinkedList() |
| 155 | + ll.Append(1, "Node 1") |
| 156 | + ll.Append(2, "Node 2") |
| 157 | + |
| 158 | + if ll.Size() != 2 { |
| 159 | + t.Errorf("Expected size 2, got %d", ll.Size()) |
| 160 | + } |
| 161 | +} |
0 commit comments