-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbl_test.go
More file actions
136 lines (102 loc) · 3.03 KB
/
dbl_test.go
File metadata and controls
136 lines (102 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package dbl
import "testing"
func TestDoubleLinkedList_Prepend(t *testing.T) {
dbl := buildList()
dbl.Prepend(-1)
if dbl.Head.Value != -1 {
t.Errorf("expected list's head value to be -1, found %v", dbl.Head.Value)
}
if dbl.Head.next.Value != 0 {
t.Errorf("expected list's second item value to be 0, found %v", dbl.Head.next.Value)
}
}
func TestDoubleLinkedList_Append(t *testing.T) {
dbl := buildList()
if l := dbl.Length(); l != 3 {
t.Errorf("expected list length to be 3, found %d", l)
}
if dbl.Head.Value != 0 {
t.Errorf("expected list's head value to be 0, found %v", dbl.Head.Value)
}
if dbl.Tail.Value != 2 {
t.Errorf("expected list's head value to be 2, found %v", dbl.Head.Value)
}
}
func buildList() *DBL {
dbl := NewList()
dbl.Append(0)
dbl.Append(1)
dbl.Append(2)
return dbl
}
func TestDoubleLinkedList_Get(t *testing.T) {
dbl := buildList()
if v, _ := dbl.Get(0); v.Value != 0 {
t.Errorf("expected Get(0) to return a node of value 0, found %d", v.Value)
}
if v, _ := dbl.Get(1); v.Value != 1 {
t.Errorf("expected Get(1) to return a node of value 1, found %d", v.Value)
}
if v, _ := dbl.Get(2); v.Value != 2 {
t.Errorf("expected Get(2) to return a node of value 2, found %d", v.Value)
}
if _, ok := dbl.Get(4); ok {
t.Errorf("expected Get(4) to return not ok, but ok(true) was returned")
}
}
func TestDoubleLinkedList_Remove(t *testing.T) {
dbl := buildList()
dbl.Remove(1)
if l := dbl.Length(); l != 2 {
t.Errorf("expected list length to be 2, found %d", l)
}
if v, _ := dbl.Get(0); v.Value != 0 {
t.Errorf("expected Get(0) to return a node of value 0, found %d", v.Value)
}
if v, _ := dbl.Get(1); v.Value != 2 {
t.Errorf("expected Get(1) to return a node of value 2, found %d", v.Value)
}
if dbl.Head.next != dbl.Tail {
t.Errorf("expected the list head.next to be equal to the tail")
}
if dbl.Tail.previous != dbl.Head {
t.Errorf("expected the list tail.previous to be equal to the head")
}
}
func TestDoubleLinkedList_RemoveAll(t *testing.T) {
dbl := buildList()
dbl.RemoveAll()
if l := dbl.Length(); l != 0 {
t.Errorf("expected RemoveAll to make the list empty, but found %d items", l)
}
}
func TestDoubleLinkedList_Pop(t *testing.T) {
dbl := buildList()
n := dbl.Pop()
if l := dbl.Length(); l != 2 {
t.Errorf("expected Pop to reduce the list length down to 2, but found %d", l)
}
if n.Value != 2 {
t.Errorf("expected the popped element value to be 2, found %d", n.Value)
}
n = dbl.Pop()
if l := dbl.Length(); l != 1 {
t.Errorf("expected Pop to reduce the list length down to 1, but found %d", l)
}
if n.Value != 1 {
t.Errorf("expected the popped element value to be 1, found %d", n.Value)
}
n = dbl.Pop()
if l := dbl.Length(); l != 0 {
t.Errorf("expected Pop to reduce the list length down to 0, but found %d", l)
}
if n.Value != 0 {
t.Errorf("expected the popped element value to be 0, found %d", n.Value)
}
if dbl.Head != nil {
t.Errorf("expected the list head to be nil")
}
if dbl.Tail != nil {
t.Errorf("expected the list tail to be nil")
}
}