Debian Codegraph @

stacks/linkedliststack/linkedliststack_test.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package linkedliststack
6
7 import (
8 "encoding/json"
9 "strings"
10 "testing"
11
12 "github.com/emirpasic/gods/v2/testutils"
13 )
14
15 func TestStackPush(t *testing.T) {
16 stack := New[int]()
17 if actualValue := stack.Empty(); actualValue != true {
18 t.Errorf("Got %v expected %v", actualValue, true)
19 }
20 stack.Push(1)
21 stack.Push(2)
22 stack.Push(3)
23
24 if actualValue := stack.Values(); actualValue[0] != 3 || actualValue[1] != 2 || actualValue[2] != 1 {
25 t.Errorf("Got %v expected %v", actualValue, "[3,2,1]")
26 }
27 if actualValue := stack.Empty(); actualValue != false {
28 t.Errorf("Got %v expected %v", actualValue, false)
29 }
30 if actualValue := stack.Size(); actualValue != 3 {
31 t.Errorf("Got %v expected %v", actualValue, 3)
32 }
33 if actualValue, ok := stack.Peek(); actualValue != 3 || !ok {
34 t.Errorf("Got %v expected %v", actualValue, 3)
35 }
36 }
37
38 func TestStackPeek(t *testing.T) {
39 stack := New[int]()
40 if actualValue, ok := stack.Peek(); actualValue != 0 || ok {
41 t.Errorf("Got %v expected %v", actualValue, nil)
42 }
43 stack.Push(1)
44 stack.Push(2)
45 stack.Push(3)
46 if actualValue, ok := stack.Peek(); actualValue != 3 || !ok {
47 t.Errorf("Got %v expected %v", actualValue, 3)
48 }
49 }
50
51 func TestStackPop(t *testing.T) {
52 stack := New[int]()
53 stack.Push(1)
54 stack.Push(2)
55 stack.Push(3)
56 stack.Pop()
57 if actualValue, ok := stack.Peek(); actualValue != 2 || !ok {
58 t.Errorf("Got %v expected %v", actualValue, 2)
59 }
60 if actualValue, ok := stack.Pop(); actualValue != 2 || !ok {
61 t.Errorf("Got %v expected %v", actualValue, 2)
62 }
63 if actualValue, ok := stack.Pop(); actualValue != 1 || !ok {
64 t.Errorf("Got %v expected %v", actualValue, 1)
65 }
66 if actualValue, ok := stack.Pop(); actualValue != 0 || ok {
67 t.Errorf("Got %v expected %v", actualValue, nil)
68 }
69 if actualValue := stack.Empty(); actualValue != true {
70 t.Errorf("Got %v expected %v", actualValue, true)
71 }
72 if actualValue := stack.Values(); len(actualValue) != 0 {
73 t.Errorf("Got %v expected %v", actualValue, "[]")
74 }
75 }
76
77 func TestStackIterator(t *testing.T) {
78 stack := New[string]()
79 stack.Push("a")
80 stack.Push("b")
81 stack.Push("c")
82
83 // Iterator
84 it := stack.Iterator()
85 count := 0
86 for it.Next() {
87 count++
88 index := it.Index()
89 value := it.Value()
90 switch index {
91 case 0:
92 if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
93 t.Errorf("Got %v expected %v", actualValue, expectedValue)
94 }
95 case 1:
96 if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
97 t.Errorf("Got %v expected %v", actualValue, expectedValue)
98 }
99 case 2:
100 if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
101 t.Errorf("Got %v expected %v", actualValue, expectedValue)
102 }
103 default:
104 t.Errorf("Too many")
105 }
106 if actualValue, expectedValue := index, count-1; actualValue != expectedValue {
107 t.Errorf("Got %v expected %v", actualValue, expectedValue)
108 }
109 }
110 if actualValue, expectedValue := count, 3; actualValue != expectedValue {
111 t.Errorf("Got %v expected %v", actualValue, expectedValue)
112 }
113
114 stack.Clear()
115 it = stack.Iterator()
116 for it.Next() {
117 t.Errorf("Shouldn't iterate on empty stack")
118 }
119 }
120
121 func TestStackIteratorBegin(t *testing.T) {
122 stack := New[string]()
123 it := stack.Iterator()
124 it.Begin()
125 stack.Push("a")
126 stack.Push("b")
127 stack.Push("c")
128 for it.Next() {
129 }
130 it.Begin()
131 it.Next()
132 if index, value := it.Index(), it.Value(); index != 0 || value != "c" {
133 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "c")
134 }
135 }
136
137 func TestStackIteratorFirst(t *testing.T) {
138 stack := New[string]()
139 it := stack.Iterator()
140 if actualValue, expectedValue := it.First(), false; actualValue != expectedValue {
141 t.Errorf("Got %v expected %v", actualValue, expectedValue)
142 }
143 stack.Push("a")
144 stack.Push("b")
145 stack.Push("c")
146 if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
147 t.Errorf("Got %v expected %v", actualValue, expectedValue)
148 }
149 if index, value := it.Index(), it.Value(); index != 0 || value != "c" {
150 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "c")
151 }
152 }
153
154 func TestStackIteratorNextTo(t *testing.T) {
155 // Sample seek function, i.e. string starting with "b"
156 seek := func(index int, value string) bool {
157 return strings.HasSuffix(value, "b")
158 }
159
160 // NextTo (empty)
161 {
162 stack := New[string]()
163 it := stack.Iterator()
164 for it.NextTo(seek) {
165 t.Errorf("Shouldn't iterate on empty stack")
166 }
167 }
168
169 // NextTo (not found)
170 {
171 stack := New[string]()
172 stack.Push("xx")
173 stack.Push("yy")
174 it := stack.Iterator()
175 for it.NextTo(seek) {
176 t.Errorf("Shouldn't iterate on empty stack")
177 }
178 }
179
180 // NextTo (found)
181 {
182 stack := New[string]()
183 stack.Push("aa")
184 stack.Push("bb")
185 stack.Push("cc")
186 it := stack.Iterator()
187 it.Begin()
188 if !it.NextTo(seek) {
189 t.Errorf("Shouldn't iterate on empty stack")
190 }
191 if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
192 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
193 }
194 if !it.Next() {
195 t.Errorf("Should go to first element")
196 }
197 if index, value := it.Index(), it.Value(); index != 2 || value != "aa" {
198 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "aa")
199 }
200 if it.Next() {
201 t.Errorf("Should not go past last element")
202 }
203 }
204 }
205
206 func TestStackSerialization(t *testing.T) {
207 stack := New[string]()
208 stack.Push("a")
209 stack.Push("b")
210 stack.Push("c")
211
212 var err error
213 assert := func() {
214 testutils.SameElements(t, stack.Values(), []string{"c", "b", "a"})
215 if actualValue, expectedValue := stack.Size(), 3; actualValue != expectedValue {
216 t.Errorf("Got %v expected %v", actualValue, expectedValue)
217 }
218 if err != nil {
219 t.Errorf("Got error %v", err)
220 }
221 }
222
223 assert()
224
225 bytes, err := stack.ToJSON()
226 assert()
227
228 err = stack.FromJSON(bytes)
229 assert()
230
231 bytes, err = json.Marshal([]interface{}{"a", "b", "c", stack})
232 if err != nil {
233 t.Errorf("Got error %v", err)
234 }
235
236 err = json.Unmarshal([]byte(`["a","b","c"]`), &stack)
237 if err != nil {
238 t.Errorf("Got error %v", err)
239 }
240 assert()
241 }
242
243 func TestStackString(t *testing.T) {
244 c := New[int]()
245 c.Push(1)
246 if !strings.HasPrefix(c.String(), "LinkedListStack") {
247 t.Errorf("String should start with container name")
248 }
249 }
250
251 func benchmarkPush(b *testing.B, stack *Stack[int], size int) {
252 for i := 0; i < b.N; i++ {
253 for n := 0; n < size; n++ {
254 stack.Push(n)
255 }
256 }
257 }
258
259 func benchmarkPop(b *testing.B, stack *Stack[int], size int) {
260 for i := 0; i < b.N; i++ {
261 for n := 0; n < size; n++ {
262 stack.Pop()
263 }
264 }
265 }
266
267 func BenchmarkLinkedListStackPop100(b *testing.B) {
268 b.StopTimer()
269 size := 100
270 stack := New[int]()
271 for n := 0; n < size; n++ {
272 stack.Push(n)
273 }
274 b.StartTimer()
275 benchmarkPop(b, stack, size)
276 }
277
278 func BenchmarkLinkedListStackPop1000(b *testing.B) {
279 b.StopTimer()
280 size := 1000
281 stack := New[int]()
282 for n := 0; n < size; n++ {
283 stack.Push(n)
284 }
285 b.StartTimer()
286 benchmarkPop(b, stack, size)
287 }
288
289 func BenchmarkLinkedListStackPop10000(b *testing.B) {
290 b.StopTimer()
291 size := 10000
292 stack := New[int]()
293 for n := 0; n < size; n++ {
294 stack.Push(n)
295 }
296 b.StartTimer()
297 benchmarkPop(b, stack, size)
298 }
299
300 func BenchmarkLinkedListStackPop100000(b *testing.B) {
301 b.StopTimer()
302 size := 100000
303 stack := New[int]()
304 for n := 0; n < size; n++ {
305 stack.Push(n)
306 }
307 b.StartTimer()
308 benchmarkPop(b, stack, size)
309 }
310
311 func BenchmarkLinkedListStackPush100(b *testing.B) {
312 b.StopTimer()
313 size := 100
314 stack := New[int]()
315 b.StartTimer()
316 benchmarkPush(b, stack, size)
317 }
318
319 func BenchmarkLinkedListStackPush1000(b *testing.B) {
320 b.StopTimer()
321 size := 1000
322 stack := New[int]()
323 for n := 0; n < size; n++ {
324 stack.Push(n)
325 }
326 b.StartTimer()
327 benchmarkPush(b, stack, size)
328 }
329
330 func BenchmarkLinkedListStackPush10000(b *testing.B) {
331 b.StopTimer()
332 size := 10000
333 stack := New[int]()
334 for n := 0; n < size; n++ {
335 stack.Push(n)
336 }
337 b.StartTimer()
338 benchmarkPush(b, stack, size)
339 }
340
341 func BenchmarkLinkedListStackPush100000(b *testing.B) {
342 b.StopTimer()
343 size := 100000
344 stack := New[int]()
345 for n := 0; n < size; n++ {
346 stack.Push(n)
347 }
348 b.StartTimer()
349 benchmarkPush(b, stack, size)
350 }