-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_test.go
More file actions
109 lines (74 loc) · 1.94 KB
/
stack_test.go
File metadata and controls
109 lines (74 loc) · 1.94 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
//go:build test && (test_small || test_all)
package stack
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestStack(t *testing.T) {
t.Parallel()
stack := Stack[string]{}
stack.Push("a")
assert.Equal(t, 1, stack.Len())
stack.Push("b")
assert.Equal(t, 2, stack.Len())
assert.Equal(t, "b", stack.Pop())
assert.Equal(t, 1, stack.Len())
stack.Push("c")
assert.Equal(t, 2, stack.Len())
assert.Equal(t, "c", stack.Pop())
assert.Equal(t, 1, stack.Len())
assert.Equal(t, "a", stack.Pop())
assert.Equal(t, 0, stack.Len())
assert.Panics(t, func() { _ = stack.Pop() })
}
func TestStackFrontPop(t *testing.T) {
t.Parallel()
stack := NewStackWithCap[string](10)
stack.Push("a")
assert.Equal(t, 1, stack.Len())
stack.Push("b")
assert.Equal(t, 2, stack.Len())
assert.Equal(t, "a", stack.PopBottom())
assert.Equal(t, 1, stack.Len())
stack.Push("c")
assert.Equal(t, 2, stack.Len())
assert.Equal(t, "b", stack.PopBottom())
assert.Equal(t, 1, stack.Len())
assert.Equal(t, "c", stack.Pop())
assert.Equal(t, 0, stack.Len())
assert.Panics(t, func() { _ = stack.PopBottom() })
}
func TestStackTraverse(t *testing.T) {
t.Parallel()
stack := NewStackWithCap[string](10)
stack.Push("a", "b", "c")
seq := []string{}
stack.Visit(func(_ int, s string) bool {
seq = append(seq, s)
return true
})
assert.Equal(t, []string{"c", "b", "a"}, seq)
seq = []string{}
stack.Visit(func(_ int, s string) bool {
seq = append(seq, s)
return s != "b"
})
assert.Equal(t, []string{"c", "b"}, seq)
}
func TestStackTraverseUpward(t *testing.T) {
t.Parallel()
stack := NewStackWithCap[string](10)
stack.Push("a", "b", "c")
seq := []string{}
stack.VisitUpward(func(_ int, s string) bool {
seq = append(seq, s)
return true
})
assert.Equal(t, []string{"a", "b", "c"}, seq)
seq = []string{}
stack.VisitUpward(func(_ int, s string) bool {
seq = append(seq, s)
return s != "b"
})
assert.Equal(t, []string{"a", "b"}, seq)
}