Skip to content

Commit 6441ed5

Browse files
committed
add unit test for MinStack
1 parent 3d0b095 commit 6441ed5

File tree

1 file changed

+295
-2
lines changed

1 file changed

+295
-2
lines changed

min_stack_test.go

Lines changed: 295 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,303 @@
11
package stack
22

3-
import "testing"
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/stretchr/testify/assert"
7+
"testing"
8+
)
49

5-
func TestMinStack(t *testing.T) {
10+
// ------------------------------------------------ ---------------------------------------------------------------------
611

12+
func TestMinStack_Clear(t *testing.T) {
13+
stack := NewMinStack[int](func(a, b int) int { return a - b })
14+
stack.Push(1)
15+
assert.Equal(t, 1, stack.Size())
16+
stack.Clear()
17+
assert.Equal(t, 0, stack.Size())
18+
}
19+
20+
func ExampleMinStack_Clear() {
21+
stack := NewMinStack[int](func(a, b int) int { return a - b })
22+
stack.Push(1)
23+
fmt.Println(stack.Size())
24+
stack.Clear()
25+
fmt.Println(stack.Size())
26+
// Output:
27+
// 1
28+
// 0
29+
}
30+
31+
// ------------------------------------------------ ---------------------------------------------------------------------
32+
33+
func TestMinStack_IsEmpty(t *testing.T) {
34+
stack := NewMinStack[int](func(a, b int) int { return a - b })
35+
assert.Equal(t, true, stack.IsEmpty())
36+
stack.Push(1)
37+
assert.Equal(t, false, stack.IsEmpty())
38+
}
39+
40+
func ExampleMinStack_IsEmpty() {
41+
stack := NewMinStack[int](func(a, b int) int { return a - b })
42+
fmt.Println(stack.IsEmpty())
43+
stack.Push(1)
44+
fmt.Println(stack.IsEmpty())
45+
// Output:
46+
// true
47+
// false
48+
}
49+
50+
// ------------------------------------------------ ---------------------------------------------------------------------
51+
52+
func TestMinStack_IsNotEmpty(t *testing.T) {
53+
stack := NewMinStack[int](func(a, b int) int { return a - b })
54+
assert.Equal(t, false, stack.IsNotEmpty())
55+
stack.Push(1)
56+
assert.Equal(t, true, stack.IsNotEmpty())
57+
}
58+
59+
func ExampleMinStack_IsNotEmpty() {
60+
stack := NewMinStack[int](func(a, b int) int { return a - b })
61+
fmt.Println(stack.IsNotEmpty())
62+
stack.Push(1)
63+
fmt.Println(stack.IsNotEmpty())
64+
// Output:
65+
// false
66+
// true
67+
}
68+
69+
// ------------------------------------------------ ---------------------------------------------------------------------
70+
71+
func TestMinStack_Peek(t *testing.T) {
72+
type User struct {
73+
}
74+
stack := NewMinStack[*User](func(a, b *User) int { return 0 })
75+
assert.Nil(t, stack.Peek())
76+
u := &User{}
77+
stack.Push(u)
78+
assert.Equal(t, u, stack.Peek())
79+
}
80+
81+
func ExampleMinStack_Peek() {
82+
type User struct {
83+
}
84+
stack := NewMinStack[*User](func(a, b *User) int { return 0 })
85+
fmt.Println(stack.Peek())
86+
u := &User{}
87+
stack.Push(u)
88+
fmt.Println(stack.Peek())
89+
// Output:
90+
// <nil>
91+
// &{}
92+
}
93+
94+
// ------------------------------------------------ ---------------------------------------------------------------------
95+
96+
func TestMinStack_PeekE(t *testing.T) {
97+
stack := NewMinStack[int](func(a, b int) int { return a - b })
98+
element, err := stack.PeekE()
99+
assert.Equal(t, 0, element)
100+
assert.ErrorIs(t, ErrStackEmpty, err)
101+
102+
stack.Push(1)
103+
element, err = stack.PeekE()
104+
assert.Nil(t, err)
105+
assert.Equal(t, 1, element)
106+
}
107+
108+
func ExampleMinStack_PeekE() {
109+
stack := NewMinStack[int](func(a, b int) int { return a - b })
110+
element, err := stack.PeekE()
111+
if errors.Is(err, ErrStackEmpty) {
112+
fmt.Println("stack empty!")
113+
}
114+
115+
stack.Push(1)
116+
element, err = stack.PeekE()
117+
if err != nil {
118+
fmt.Println(err.Error())
119+
return
120+
}
121+
fmt.Println(element)
122+
// Output:
123+
// stack empty!
124+
// 1
125+
}
126+
127+
// ------------------------------------------------ ---------------------------------------------------------------------
128+
129+
func TestMinStack_Pop(t *testing.T) {
130+
type User struct {
131+
}
132+
stack := NewMinStack[*User](func(a, b *User) int { return 0 })
133+
assert.Nil(t, stack.Pop())
134+
u := &User{}
135+
stack.Push(u)
136+
assert.Equal(t, u, stack.Pop())
137+
}
138+
139+
func ExampleMinStack_Pop() {
140+
type User struct {
141+
}
142+
stack := NewMinStack[*User](func(a, b *User) int { return 0 })
143+
fmt.Println(stack.Pop())
144+
u := &User{}
145+
stack.Push(u)
146+
fmt.Println(stack.Pop())
147+
// Output:
148+
// <nil>
149+
// &{}
150+
}
151+
152+
// ------------------------------------------------ ---------------------------------------------------------------------
153+
154+
func TestMinStack_PopE(t *testing.T) {
155+
stack := NewMinStack[int](func(a, b int) int { return a - b })
156+
element, err := stack.PopE()
157+
assert.Equal(t, 0, element)
158+
assert.ErrorIs(t, ErrStackEmpty, err)
159+
160+
stack.Push(1)
161+
element, err = stack.PopE()
162+
assert.Nil(t, err)
163+
assert.Equal(t, 1, element)
164+
}
165+
166+
func ExampleMinStack_PopE() {
167+
stack := NewMinStack[int](func(a, b int) int { return a - b })
168+
element, err := stack.PopE()
169+
if errors.Is(err, ErrStackEmpty) {
170+
fmt.Println("stack empty!")
171+
}
172+
173+
stack.Push(1)
174+
element, err = stack.PopE()
175+
if err != nil {
176+
fmt.Println(err.Error())
177+
return
178+
}
179+
fmt.Println(element)
180+
// Output:
181+
// stack empty!
182+
// 1
183+
}
184+
185+
// ------------------------------------------------ ---------------------------------------------------------------------
186+
187+
func TestMinStack_Push(t *testing.T) {
188+
stack := NewMinStack[int](func(a, b int) int { return a - b })
189+
stack.Push(1)
190+
}
191+
192+
func ExampleMinStack_Push() {
193+
stack := NewMinStack[int](func(a, b int) int { return a - b })
194+
stack.Push(1)
195+
// Output:
196+
//
197+
}
198+
199+
// ------------------------------------------------ ---------------------------------------------------------------------
200+
201+
func TestMinStack_Size(t *testing.T) {
202+
stack := NewMinStack[int](func(a, b int) int { return a - b })
203+
stack.Push(1)
204+
assert.Equal(t, 1, stack.Size())
205+
}
206+
207+
func ExampleMinStack_Size() {
208+
stack := NewMinStack[int](func(a, b int) int { return a - b })
209+
stack.Push(1)
210+
fmt.Println(stack.Size())
211+
// Output:
212+
// 1
213+
}
214+
215+
// ------------------------------------------------ ---------------------------------------------------------------------
216+
217+
func TestMinStack_String(t *testing.T) {
218+
stack := NewMinStack[int](func(a, b int) int { return a - b })
219+
stack.Push(1)
220+
assert.Equal(t, "[&stack.MinNode[int]{value:1, min:1}]", stack.String())
221+
}
222+
223+
func ExampleMinStack_String() {
224+
stack := NewMinStack[int](func(a, b int) int { return a - b })
225+
stack.Push(1)
226+
fmt.Println(stack.String())
227+
// Output:
228+
// [&stack.MinNode[int]{value:1, min:1}]
229+
}
230+
231+
// ------------------------------------------------ ---------------------------------------------------------------------
232+
233+
func TestNewMinStack(t *testing.T) {
234+
stack := NewMinStack[int](func(a, b int) int { return a - b })
235+
assert.NotNil(t, stack)
236+
}
237+
238+
func ExampleNewMinStack() {
239+
stack := NewMinStack[int](func(a, b int) int { return a - b })
240+
fmt.Println(stack.String())
241+
// Output:
242+
// []
243+
}
244+
245+
// ------------------------------------------------ ---------------------------------------------------------------------
246+
247+
func TestMinStack_GetMin(t *testing.T) {
248+
stack := NewMinStack[int](func(a, b int) int { return a - b })
249+
stack.Push(10)
250+
stack.Push(7)
251+
stack.Push(9)
252+
assert.Equal(t, 7, stack.GetMin())
253+
}
254+
255+
func ExampleMinStack_GetMin() {
256+
stack := NewMinStack[int](func(a, b int) int { return a - b })
257+
stack.Push(10)
258+
stack.Push(7)
259+
stack.Push(9)
260+
fmt.Println(stack.GetMin())
261+
// Output:
262+
// 7
263+
}
264+
265+
// ------------------------------------------------ ---------------------------------------------------------------------
266+
267+
func TestMinStack_GetMinE(t *testing.T) {
268+
stack := NewMinStack[int](func(a, b int) int { return a - b })
269+
270+
_, err := stack.GetMinE()
271+
assert.ErrorIs(t, err, ErrStackEmpty)
272+
273+
stack.Push(10)
274+
stack.Push(7)
275+
stack.Push(9)
276+
element, err := stack.GetMinE()
277+
assert.Nil(t, err)
278+
assert.Equal(t, 7, element)
279+
}
280+
281+
func ExampleMinStack_GetMinE() {
282+
stack := NewMinStack[int](func(a, b int) int { return a - b })
7283

284+
_, err := stack.GetMinE()
285+
if errors.Is(err, ErrStackEmpty) {
286+
fmt.Println("stack empty!")
287+
}
8288

289+
stack.Push(10)
290+
stack.Push(7)
291+
stack.Push(9)
292+
element, err := stack.GetMinE()
293+
if err != nil {
294+
fmt.Println(err.Error())
295+
return
296+
}
297+
fmt.Println(element)
298+
// Output:
299+
// stack empty!
300+
// 7
9301
}
10302

303+
// ------------------------------------------------ ---------------------------------------------------------------------

0 commit comments

Comments
 (0)