-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_array_test.go
152 lines (122 loc) · 2.81 KB
/
stack_array_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
package datt_test
import (
"testing"
"github.com/raphadam/datt"
)
func TestStackArrayLen(t *testing.T) {
t.Run("empty stack", func(t *testing.T) {
s := datt.NewStackArray[string](2)
got := s.Len()
AssertEqual(t, got, 0)
})
t.Run("stack with 3 element", func(t *testing.T) {
s := datt.NewStackArray[string](3)
s.Push("1")
s.Push("2")
s.Push("7s")
got := s.Len()
AssertEqual(t, got, 3)
})
}
func TestStackArrayPeek(t *testing.T) {
t.Run("empty stack", func(t *testing.T) {
s := datt.NewStackArray[string](2)
_, ok := s.Peek()
AssertEqual(t, ok, false)
})
t.Run("stack with 3 element must peek the last pushed", func(t *testing.T) {
s := datt.NewStackArray[string](3)
s.Push("sa")
s.Push("henry")
s.Push("doe")
v, ok := s.Peek()
AssertEqual(t, ok, true)
AssertEqual(t, v, "doe")
})
}
func TestStackArrayPush(t *testing.T) {
t.Run("force grow initial cap", func(t *testing.T) {
s := datt.NewStackArray[string](3)
s.Push("sa")
s.Push("henry")
s.Push("doe")
s.Push("bob")
v, ok := s.Peek()
AssertEqual(t, ok, true)
AssertEqual(t, v, "bob")
})
t.Run("push pop push pop", func(t *testing.T) {
s := datt.NewStackArray[string](2)
s.Push("sa")
s.Pop()
s.Push("bob")
v, ok := s.Pop()
AssertEqual(t, ok, true)
AssertEqual(t, v, "bob")
})
}
func TestStackArrayPop(t *testing.T) {
t.Run("empty stack", func(t *testing.T) {
s := datt.NewStackArray[string](2)
_, ok := s.Pop()
AssertEqual(t, ok, false)
})
t.Run("stack with 3 element must pop the last pushed", func(t *testing.T) {
s := datt.NewStackArray[string](2)
s.Push("sa")
s.Push("henry")
s.Push("doe")
v, ok := s.Pop()
AssertEqual(t, ok, true)
AssertEqual(t, v, "doe")
AssertEqual(t, s.Len(), 2)
})
}
func TestStackArrayClear(t *testing.T) {
t.Run("stack with 3 element must empty", func(t *testing.T) {
s := datt.NewStackArray[string](2)
s.Push("henry")
s.Push("doe")
s.Push("sa")
s.Clear()
_, got := s.Peek()
AssertEqual(t, s.Len(), 0)
AssertEqual(t, got, false)
})
t.Run("add one element after clear", func(t *testing.T) {
s := datt.NewStackArray[string](2)
s.Push("henry")
s.Push("doe")
s.Push("sa")
s.Clear()
s.Push("alice")
got, ok := s.Peek()
AssertEqual(t, s.Len(), 1)
AssertEqual(t, got, "alice")
AssertEqual(t, ok, true)
})
}
func TestStackArrayDo(t *testing.T) {
t.Run("empty stack should not iterate", func(t *testing.T) {
s := datt.NewStackArray[int](2)
var iter int
s.Do(func(v int) {
iter++
})
AssertEqual(t, iter, 0)
})
t.Run("stack with elements should iterate on all", func(t *testing.T) {
s := datt.NewStackArray[int](2)
var data [4]int = [4]int{1, 2, 3, 4}
for _, v := range data {
s.Push(v)
}
var got [4]int
var i int = 3
s.Do(func(v int) {
got[i] = v
i--
})
AssertEqual(t, got, data)
})
}