-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_linked_list_test.go
237 lines (211 loc) · 5.35 KB
/
simple_linked_list_test.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package linkedlist
import (
"reflect"
"testing"
)
var array1To10 = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
func TestEmptyListHasSizeZero(t *testing.T) {
list := New([]int{})
if size := list.Size(); size != 0 {
t.Errorf("Size of empty list: %d, expected: %d", size, 0)
}
list = New(nil)
if size := list.Size(); size != 0 {
t.Errorf("Size of empty list: %d, expected: %d", size, 0)
}
}
func TestSingletonListHasSizeOne(t *testing.T) {
list := New([]int{1})
if size := list.Size(); size != 1 {
t.Errorf("Size of singleton list: %d, expected: %d", size, 1)
}
}
func TestNonEmptyListHasCorrectSize(t *testing.T) {
list := New([]int{1, 2, 3})
if size := list.Size(); size != 3 {
t.Errorf("Size of list from [1, 2, 3]: %d, expected: %d", size, 3)
}
}
func TestListHasCorrectSizeAfterPop(t *testing.T) {
list := New([]int{1, 2, 3})
_, _ = list.Pop()
_, _ = list.Pop()
_, _ = list.Pop()
if size := list.Size(); size != 0 {
t.Errorf("Size of list from [1, 2, 3] after 3 calls to pop(): got %d, expected: %d", size, 0)
}
}
func TestListHasCorrectSizeAfterPush(t *testing.T) {
list := New([]int{})
list.Push(1)
list.Push(2)
list.Push(3)
if size := list.Size(); size != 3 {
t.Errorf("Size of list from [] after 3 calls to push(): got %d, expected: %d", size, 3)
}
}
func TestEmptyListToEmptyArray(t *testing.T) {
list := New([]int{})
if array := list.Array(); len(array) != 0 {
t.Errorf("Test empty list to array: %v, want empty array", array)
}
list = New(nil)
if array := list.Array(); len(array) != 0 {
t.Errorf("Test empty list to array: %v, want empty array", array)
}
}
func TestNonEmptyListToArray(t *testing.T) {
expected := []int{1, 2, 3}
list := New(expected)
array := list.Array()
if !reflect.DeepEqual(array, expected) {
t.Errorf("Test non empty list to array: %v, want %v", array, expected)
}
}
func TestPopFromEmptyList(t *testing.T) {
list := New([]int{})
if _, err := list.Pop(); err == nil {
t.Errorf("Pop from empty list: expected error but there was not")
}
list = New(nil)
if _, err := list.Pop(); err == nil {
t.Errorf("Pop from empty list: expected error but there was not")
}
}
func TestPopFromNonEmptyList(t *testing.T) {
list := New([]int{1, 2, 3})
elem, err := list.Pop()
if err != nil {
t.Errorf("Pop from non empty list: unexpected error %v", err)
}
if elem != 3 {
t.Errorf("Pop from non empty list: %d, want %d", elem, 3)
}
actual := list.Array()
expected := []int{1, 2}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Pop from non empty list: %v, want %v", actual, expected)
}
}
func TestPushToEmptyList(t *testing.T) {
list := New([]int{})
list.Push(1)
actual := list.Array()
expected := []int{1}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Push to empty list: %v, want %v", actual, expected)
}
list = New(nil)
list.Push(1)
actual = list.Array()
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Push to empty list: %v, want %v", actual, expected)
}
}
func TestPushToNonEmptyList(t *testing.T) {
list := New([]int{1, 2, 3})
list.Push(4)
actual := list.Array()
expected := []int{1, 2, 3, 4}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Push to non empty list: %v, want %v", actual, expected)
}
}
func TestPushAndPop(t *testing.T) {
list := New([]int{1, 2, 3})
list.Pop()
list.Push(4)
list.Push(5)
list.Pop()
list.Push(6)
actual := list.Array()
expected := []int{1, 2, 4, 6}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Test push and pop: %v, want %v", actual, expected)
}
}
func TestReverseEmptyList(t *testing.T) {
list := New([]int{})
if reversed := list.Reverse().Array(); len(reversed) != 0 {
t.Errorf("Reverse empty list: %v, want empty list", reversed)
}
list = New(nil)
if reversed := list.Reverse().Array(); len(reversed) != 0 {
t.Errorf("Reverse empty list: %v, want empty list", reversed)
}
}
func TestReverseNonEmptyList(t *testing.T) {
list := New([]int{1, 2, 3})
actual := list.Reverse().Array()
expected := []int{3, 2, 1}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Reverse non empty list: %v, want %v", actual, expected)
}
}
func BenchmarkNewList(b *testing.B) {
if testing.Short() {
b.Skip("skipping benchmark in short mode.")
}
for i := 0; i < b.N; i++ {
_ = New(array1To10)
}
}
func BenchmarkListSize(b *testing.B) {
if testing.Short() {
b.Skip("skipping benchmark in short mode.")
}
list := New(array1To10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = list.Size()
}
}
func BenchmarkListPush(b *testing.B) {
if testing.Short() {
b.Skip("skipping benchmark in short mode.")
}
for i := 0; i < b.N; i++ {
b.StopTimer()
list := New([]int{})
b.StartTimer()
for k := 0; k < 1000; k++ {
list.Push(k)
}
}
}
func BenchmarkListPop(b *testing.B) {
if testing.Short() {
b.Skip("skipping benchmark in short mode.")
}
for i := 0; i < b.N; i++ {
b.StopTimer()
list := New([]int{})
for k := 0; k < 1000; k++ {
list.Push(k)
}
b.StartTimer()
for k := 0; k < 1000; k++ {
_, _ = list.Pop()
}
}
}
func BenchmarkListToArray(b *testing.B) {
if testing.Short() {
b.Skip("skipping benchmark in short mode.")
}
list := New(array1To10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = list.Array()
}
}
func BenchmarkListReverse(b *testing.B) {
if testing.Short() {
b.Skip("skipping benchmark in short mode.")
}
list := New(array1To10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = list.Reverse()
}
}