-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_stack_test.go
303 lines (254 loc) · 7.4 KB
/
min_stack_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package stacks
import (
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
// ------------------------------------------------ ---------------------------------------------------------------------
func TestMinStack_Clear(t *testing.T) {
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)
assert.Equal(t, 1, stack.Size())
stack.Clear()
assert.Equal(t, 0, stack.Size())
}
func ExampleMinStack_Clear() {
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.Size())
stack.Clear()
fmt.Println(stack.Size())
// Output:
// 1
// 0
}
// ------------------------------------------------ ---------------------------------------------------------------------
func TestMinStack_IsEmpty(t *testing.T) {
stack := NewMinStack[int](func(a, b int) int { return a - b })
assert.Equal(t, true, stack.IsEmpty())
stack.Push(1)
assert.Equal(t, false, stack.IsEmpty())
}
func ExampleMinStack_IsEmpty() {
stack := NewMinStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.IsEmpty())
stack.Push(1)
fmt.Println(stack.IsEmpty())
// Output:
// true
// false
}
// ------------------------------------------------ ---------------------------------------------------------------------
func TestMinStack_IsNotEmpty(t *testing.T) {
stack := NewMinStack[int](func(a, b int) int { return a - b })
assert.Equal(t, false, stack.IsNotEmpty())
stack.Push(1)
assert.Equal(t, true, stack.IsNotEmpty())
}
func ExampleMinStack_IsNotEmpty() {
stack := NewMinStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.IsNotEmpty())
stack.Push(1)
fmt.Println(stack.IsNotEmpty())
// Output:
// false
// true
}
// ------------------------------------------------ ---------------------------------------------------------------------
func TestMinStack_Peek(t *testing.T) {
type User struct {
}
stack := NewMinStack[*User](func(a, b *User) int { return 0 })
assert.Nil(t, stack.Peek())
u := &User{}
stack.Push(u)
assert.Equal(t, u, stack.Peek())
}
func ExampleMinStack_Peek() {
type User struct {
}
stack := NewMinStack[*User](func(a, b *User) int { return 0 })
fmt.Println(stack.Peek())
u := &User{}
stack.Push(u)
fmt.Println(stack.Peek())
// Output:
// <nil>
// &{}
}
// ------------------------------------------------ ---------------------------------------------------------------------
func TestMinStack_PeekE(t *testing.T) {
stack := NewMinStack[int](func(a, b int) int { return a - b })
element, err := stack.PeekE()
assert.Equal(t, 0, element)
assert.ErrorIs(t, ErrStackEmpty, err)
stack.Push(1)
element, err = stack.PeekE()
assert.Nil(t, err)
assert.Equal(t, 1, element)
}
func ExampleMinStack_PeekE() {
stack := NewMinStack[int](func(a, b int) int { return a - b })
element, err := stack.PeekE()
if errors.Is(err, ErrStackEmpty) {
fmt.Println("stack empty!")
}
stack.Push(1)
element, err = stack.PeekE()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(element)
// Output:
// stack empty!
// 1
}
// ------------------------------------------------ ---------------------------------------------------------------------
func TestMinStack_Pop(t *testing.T) {
type User struct {
}
stack := NewMinStack[*User](func(a, b *User) int { return 0 })
assert.Nil(t, stack.Pop())
u := &User{}
stack.Push(u)
assert.Equal(t, u, stack.Pop())
}
func ExampleMinStack_Pop() {
type User struct {
}
stack := NewMinStack[*User](func(a, b *User) int { return 0 })
fmt.Println(stack.Pop())
u := &User{}
stack.Push(u)
fmt.Println(stack.Pop())
// Output:
// <nil>
// &{}
}
// ------------------------------------------------ ---------------------------------------------------------------------
func TestMinStack_PopE(t *testing.T) {
stack := NewMinStack[int](func(a, b int) int { return a - b })
element, err := stack.PopE()
assert.Equal(t, 0, element)
assert.ErrorIs(t, ErrStackEmpty, err)
stack.Push(1)
element, err = stack.PopE()
assert.Nil(t, err)
assert.Equal(t, 1, element)
}
func ExampleMinStack_PopE() {
stack := NewMinStack[int](func(a, b int) int { return a - b })
element, err := stack.PopE()
if errors.Is(err, ErrStackEmpty) {
fmt.Println("stack empty!")
}
stack.Push(1)
element, err = stack.PopE()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(element)
// Output:
// stack empty!
// 1
}
// ------------------------------------------------ ---------------------------------------------------------------------
func TestMinStack_Push(t *testing.T) {
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)
}
func ExampleMinStack_Push() {
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)
// Output:
//
}
// ------------------------------------------------ ---------------------------------------------------------------------
func TestMinStack_Size(t *testing.T) {
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)
assert.Equal(t, 1, stack.Size())
}
func ExampleMinStack_Size() {
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.Size())
// Output:
// 1
}
// ------------------------------------------------ ---------------------------------------------------------------------
func TestMinStack_String(t *testing.T) {
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)
assert.Equal(t, "[&stack.MinNode[int]{value:1, min:1}]", stack.String())
}
func ExampleMinStack_String() {
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.String())
// Output:
// [&stack.MinNode[int]{value:1, min:1}]
}
// ------------------------------------------------ ---------------------------------------------------------------------
func TestNewMinStack(t *testing.T) {
stack := NewMinStack[int](func(a, b int) int { return a - b })
assert.NotNil(t, stack)
}
func ExampleNewMinStack() {
stack := NewMinStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.String())
// Output:
// []
}
// ------------------------------------------------ ---------------------------------------------------------------------
func TestMinStack_GetMin(t *testing.T) {
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(10)
stack.Push(7)
stack.Push(9)
assert.Equal(t, 7, stack.GetMin())
}
func ExampleMinStack_GetMin() {
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(10)
stack.Push(7)
stack.Push(9)
fmt.Println(stack.GetMin())
// Output:
// 7
}
// ------------------------------------------------ ---------------------------------------------------------------------
func TestMinStack_GetMinE(t *testing.T) {
stack := NewMinStack[int](func(a, b int) int { return a - b })
_, err := stack.GetMinE()
assert.ErrorIs(t, err, ErrStackEmpty)
stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMinE()
assert.Nil(t, err)
assert.Equal(t, 7, element)
}
func ExampleMinStack_GetMinE() {
stack := NewMinStack[int](func(a, b int) int { return a - b })
_, err := stack.GetMinE()
if errors.Is(err, ErrStackEmpty) {
fmt.Println("stack empty!")
}
stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMinE()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(element)
// Output:
// stack empty!
// 7
}
// ------------------------------------------------ ---------------------------------------------------------------------