Debian Codegraph @

stacks/arraystack/arraystack_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 arraystack
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 TestStackIteratorOnEmpty(t *testing.T) {
78 stack := New[string]()
79 it := stack.Iterator()
80 for it.Next() {
81 t.Errorf("Shouldn't iterate on empty stack")
82 }
83 }
84
85 func TestStackIteratorNext(t *testing.T) {
86 stack := New[string]()
87 stack.Push("a")
88 stack.Push("b")
89 stack.Push("c")
90
91 it := stack.Iterator()
92 count := 0
93 for it.Next() {
94 count++
95 index := it.Index()
96 value := it.Value()
97 switch index {
98 case 0:
99 if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
100 t.Errorf("Got %v expected %v", actualValue, expectedValue)
101 }
102 case 1:
103 if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
104 t.Errorf("Got %v expected %v", actualValue, expectedValue)
105 }
106 case 2:
107 if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
108 t.Errorf("Got %v expected %v", actualValue, expectedValue)
109 }
110 default:
111 t.Errorf("Too many")
112 }
113 if actualValue, expectedValue := index, count-1; actualValue != expectedValue {
114 t.Errorf("Got %v expected %v", actualValue, expectedValue)
115 }
116 }
117 if actualValue, expectedValue := count, 3; actualValue != expectedValue {
118 t.Errorf("Got %v expected %v", actualValue, expectedValue)
119 }
120
121 stack.Clear()
122 it = stack.Iterator()
123 for it.Next() {
124 t.Errorf("Shouldn't iterate on empty stack")
125 }
126 }
127
128 func TestStackIteratorPrev(t *testing.T) {
129 stack := New[string]()
130 stack.Push("a")
131 stack.Push("b")
132 stack.Push("c")
133
134 it := stack.Iterator()
135 for it.Next() {
136 }
137 count := 0
138 for it.Prev() {
139 count++
140 index := it.Index()
141 value := it.Value()
142 switch index {
143 case 0:
144 if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
145 t.Errorf("Got %v expected %v", actualValue, expectedValue)
146 }
147 case 1:
148 if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
149 t.Errorf("Got %v expected %v", actualValue, expectedValue)
150 }
151 case 2:
152 if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
153 t.Errorf("Got %v expected %v", actualValue, expectedValue)
154 }
155 default:
156 t.Errorf("Too many")
157 }
158 if actualValue, expectedValue := index, 3-count; actualValue != expectedValue {
159 t.Errorf("Got %v expected %v", actualValue, expectedValue)
160 }
161 }
162 if actualValue, expectedValue := count, 3; actualValue != expectedValue {
163 t.Errorf("Got %v expected %v", actualValue, expectedValue)
164 }
165 }
166
167 func TestStackIteratorBegin(t *testing.T) {
168 stack := New[string]()
169 it := stack.Iterator()
170 it.Begin()
171 stack.Push("a")
172 stack.Push("b")
173 stack.Push("c")
174 for it.Next() {
175 }
176 it.Begin()
177 it.Next()
178 if index, value := it.Index(), it.Value(); index != 0 || value != "c" {
179 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "c")
180 }
181 }
182
183 func TestStackIteratorEnd(t *testing.T) {
184 stack := New[string]()
185 it := stack.Iterator()
186
187 if index := it.Index(); index != -1 {
188 t.Errorf("Got %v expected %v", index, -1)
189 }
190
191 it.End()
192 if index := it.Index(); index != 0 {
193 t.Errorf("Got %v expected %v", index, 0)
194 }
195
196 stack.Push("a")
197 stack.Push("b")
198 stack.Push("c")
199 it.End()
200 if index := it.Index(); index != stack.Size() {
201 t.Errorf("Got %v expected %v", index, stack.Size())
202 }
203
204 it.Prev()
205 if index, value := it.Index(), it.Value(); index != stack.Size()-1 || value != "a" {
206 t.Errorf("Got %v,%v expected %v,%v", index, value, stack.Size()-1, "a")
207 }
208 }
209
210 func TestStackIteratorFirst(t *testing.T) {
211 stack := New[string]()
212 it := stack.Iterator()
213 if actualValue, expectedValue := it.First(), false; actualValue != expectedValue {
214 t.Errorf("Got %v expected %v", actualValue, expectedValue)
215 }
216 stack.Push("a")
217 stack.Push("b")
218 stack.Push("c")
219 if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
220 t.Errorf("Got %v expected %v", actualValue, expectedValue)
221 }
222 if index, value := it.Index(), it.Value(); index != 0 || value != "c" {
223 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "c")
224 }
225 }
226
227 func TestStackIteratorLast(t *testing.T) {
228 stack := New[string]()
229 it := stack.Iterator()
230 if actualValue, expectedValue := it.Last(), false; actualValue != expectedValue {
231 t.Errorf("Got %v expected %v", actualValue, expectedValue)
232 }
233 stack.Push("a")
234 stack.Push("b")
235 stack.Push("c")
236 if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
237 t.Errorf("Got %v expected %v", actualValue, expectedValue)
238 }
239 if index, value := it.Index(), it.Value(); index != 2 || value != "a" {
240 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "a")
241 }
242 }
243
244 func TestStackIteratorNextTo(t *testing.T) {
245 // Sample seek function, i.e. string starting with "b"
246 seek := func(index int, value string) bool {
247 return strings.HasSuffix(value, "b")
248 }
249
250 // NextTo (empty)
251 {
252 stack := New[string]()
253 it := stack.Iterator()
254 for it.NextTo(seek) {
255 t.Errorf("Shouldn't iterate on empty stack")
256 }
257 }
258
259 // NextTo (not found)
260 {
261 stack := New[string]()
262 stack.Push("xx")
263 stack.Push("yy")
264 it := stack.Iterator()
265 for it.NextTo(seek) {
266 t.Errorf("Shouldn't iterate on empty stack")
267 }
268 }
269
270 // NextTo (found)
271 {
272 stack := New[string]()
273 stack.Push("aa")
274 stack.Push("bb")
275 stack.Push("cc")
276 it := stack.Iterator()
277 it.Begin()
278 if !it.NextTo(seek) {
279 t.Errorf("Shouldn't iterate on empty stack")
280 }
281 if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
282 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
283 }
284 if !it.Next() {
285 t.Errorf("Should go to first element")
286 }
287 if index, value := it.Index(), it.Value(); index != 2 || value != "aa" {
288 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "aa")
289 }
290 if it.Next() {
291 t.Errorf("Should not go past last element")
292 }
293 }
294 }
295
296 func TestStackIteratorPrevTo(t *testing.T) {
297 // Sample seek function, i.e. string starting with "b"
298 seek := func(index int, value string) bool {
299 return strings.HasSuffix(value, "b")
300 }
301
302 // PrevTo (empty)
303 {
304 stack := New[string]()
305 it := stack.Iterator()
306 it.End()
307 for it.PrevTo(seek) {
308 t.Errorf("Shouldn't iterate on empty stack")
309 }
310 }
311
312 // PrevTo (not found)
313 {
314 stack := New[string]()
315 stack.Push("xx")
316 stack.Push("yy")
317 it := stack.Iterator()
318 it.End()
319 for it.PrevTo(seek) {
320 t.Errorf("Shouldn't iterate on empty stack")
321 }
322 }
323
324 // PrevTo (found)
325 {
326 stack := New[string]()
327 stack.Push("aa")
328 stack.Push("bb")
329 stack.Push("cc")
330 it := stack.Iterator()
331 it.End()
332 if !it.PrevTo(seek) {
333 t.Errorf("Shouldn't iterate on empty stack")
334 }
335 if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
336 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
337 }
338 if !it.Prev() {
339 t.Errorf("Should go to first element")
340 }
341 if index, value := it.Index(), it.Value(); index != 0 || value != "cc" {
342 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "cc")
343 }
344 if it.Prev() {
345 t.Errorf("Should not go before first element")
346 }
347 }
348 }
349
350 func TestStackSerialization(t *testing.T) {
351 stack := New[string]()
352 stack.Push("a")
353 stack.Push("b")
354 stack.Push("c")
355
356 var err error
357 assert := func() {
358 testutils.SameElements(t, stack.Values(), []string{"c", "b", "a"})
359 if actualValue, expectedValue := stack.Size(), 3; actualValue != expectedValue {
360 t.Errorf("Got %v expected %v", actualValue, expectedValue)
361 }
362 if err != nil {
363 t.Errorf("Got error %v", err)
364 }
365 }
366
367 assert()
368
369 bytes, err := stack.ToJSON()
370 assert()
371
372 err = stack.FromJSON(bytes)
373 assert()
374
375 bytes, err = json.Marshal([]interface{}{"a", "b", "c", stack})
376 if err != nil {
377 t.Errorf("Got error %v", err)
378 }
379
380 err = json.Unmarshal([]byte(`["a","b","c"]`), &stack)
381 if err != nil {
382 t.Errorf("Got error %v", err)
383 }
384 assert()
385 }
386
387 func TestStackString(t *testing.T) {
388 c := New[int]()
389 c.Push(1)
390 if !strings.HasPrefix(c.String(), "ArrayStack") {
391 t.Errorf("String should start with container name")
392 }
393 }
394
395 func benchmarkPush(b *testing.B, stack *Stack[int], size int) {
396 for i := 0; i < b.N; i++ {
397 for n := 0; n < size; n++ {
398 stack.Push(n)
399 }
400 }
401 }
402
403 func benchmarkPop(b *testing.B, stack *Stack[int], size int) {
404 for i := 0; i < b.N; i++ {
405 for n := 0; n < size; n++ {
406 stack.Pop()
407 }
408 }
409 }
410
411 func BenchmarkArrayStackPop100(b *testing.B) {
412 b.StopTimer()
413 size := 100
414 stack := New[int]()
415 for n := 0; n < size; n++ {
416 stack.Push(n)
417 }
418 b.StartTimer()
419 benchmarkPop(b, stack, size)
420 }
421
422 func BenchmarkArrayStackPop1000(b *testing.B) {
423 b.StopTimer()
424 size := 1000
425 stack := New[int]()
426 for n := 0; n < size; n++ {
427 stack.Push(n)
428 }
429 b.StartTimer()
430 benchmarkPop(b, stack, size)
431 }
432
433 func BenchmarkArrayStackPop10000(b *testing.B) {
434 b.StopTimer()
435 size := 10000
436 stack := New[int]()
437 for n := 0; n < size; n++ {
438 stack.Push(n)
439 }
440 b.StartTimer()
441 benchmarkPop(b, stack, size)
442 }
443
444 func BenchmarkArrayStackPop100000(b *testing.B) {
445 b.StopTimer()
446 size := 100000
447 stack := New[int]()
448 for n := 0; n < size; n++ {
449 stack.Push(n)
450 }
451 b.StartTimer()
452 benchmarkPop(b, stack, size)
453 }
454
455 func BenchmarkArrayStackPush100(b *testing.B) {
456 b.StopTimer()
457 size := 100
458 stack := New[int]()
459 b.StartTimer()
460 benchmarkPush(b, stack, size)
461 }
462
463 func BenchmarkArrayStackPush1000(b *testing.B) {
464 b.StopTimer()
465 size := 1000
466 stack := New[int]()
467 for n := 0; n < size; n++ {
468 stack.Push(n)
469 }
470 b.StartTimer()
471 benchmarkPush(b, stack, size)
472 }
473
474 func BenchmarkArrayStackPush10000(b *testing.B) {
475 b.StopTimer()
476 size := 10000
477 stack := New[int]()
478 for n := 0; n < size; n++ {
479 stack.Push(n)
480 }
481 b.StartTimer()
482 benchmarkPush(b, stack, size)
483 }
484
485 func BenchmarkArrayStackPush100000(b *testing.B) {
486 b.StopTimer()
487 size := 100000
488 stack := New[int]()
489 for n := 0; n < size; n++ {
490 stack.Push(n)
491 }
492 b.StartTimer()
493 benchmarkPush(b, stack, size)
494 }