Skip to content

Commit a0c62c7

Browse files
committed
add unit test for ArrayStack
1 parent 37691e1 commit a0c62c7

File tree

2 files changed

+173
-28
lines changed

2 files changed

+173
-28
lines changed

array_stack.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package stack
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// EmptyStackTopIndex 当栈为空的时候栈顶的指针指向的下标位置
9+
const EmptyStackTopIndex = -1
410

511
// ArrayStack 数组实现的栈
612
type ArrayStack[T any] struct {
713

8-
// 栈顶指针
14+
// 栈顶指针,始终指向栈顶元素所在的下标
915
topIndex int
1016

1117
// 栈中的元素,栈底是0,会不断往上弹
@@ -17,18 +23,19 @@ var _ Stack[any] = &ArrayStack[any]{}
1723
func NewArrayStack[T any]() *ArrayStack[T] {
1824
return &ArrayStack[T]{
1925
slice: make([]T, 0),
20-
topIndex: 0,
26+
topIndex: EmptyStackTopIndex,
2127
}
2228
}
2329

2430
func (x *ArrayStack[T]) Push(values ...T) {
2531
for _, value := range values {
26-
if x.topIndex < len(x.slice) {
27-
x.slice[x.topIndex] = value
32+
nextIndex := x.topIndex + 1
33+
if nextIndex < len(x.slice) {
34+
x.slice[nextIndex] = value
2835
} else {
2936
x.slice = append(x.slice, value)
3037
}
31-
x.topIndex++
38+
x.topIndex = nextIndex
3239
}
3340
}
3441

@@ -38,7 +45,7 @@ func (x *ArrayStack[T]) Pop() T {
3845
}
3946

4047
func (x *ArrayStack[T]) PopE() (T, error) {
41-
if x.topIndex == 0 {
48+
if x.topIndex == EmptyStackTopIndex {
4249
var zero T
4350
return zero, ErrStackEmpty
4451
}
@@ -53,30 +60,38 @@ func (x *ArrayStack[T]) Peek() T {
5360
}
5461

5562
func (x *ArrayStack[T]) PeekE() (T, error) {
56-
if x.topIndex == 0 {
63+
if x.topIndex == EmptyStackTopIndex {
5764
var zero T
58-
return zero , ErrStackEmpty
65+
return zero, ErrStackEmpty
5966
}
6067
return x.slice[x.topIndex], nil
6168
}
6269

6370
func (x *ArrayStack[T]) IsEmpty() bool {
64-
return x.topIndex == 0
71+
return x.topIndex == EmptyStackTopIndex
6572
}
6673

6774
func (x *ArrayStack[T]) IsNotEmpty() bool {
68-
return x.topIndex != 0
75+
return x.topIndex != EmptyStackTopIndex
6976
}
7077

7178
func (x *ArrayStack[T]) Size() int {
72-
return x.topIndex
79+
return x.topIndex + 1
7380
}
7481

75-
func (x *ArrayStack[T]) Clear() {
76-
x.topIndex = 0
82+
func (x *ArrayStack[T]) Clear() {
83+
x.topIndex = EmptyStackTopIndex
7784
x.slice = make([]T, 0)
7885
}
7986

8087
func (x *ArrayStack[T]) String() string {
81-
return fmt.Sprintf("%#v", x)
88+
sb := strings.Builder{}
89+
sb.WriteString("[")
90+
currentIndex := x.topIndex
91+
for currentIndex != EmptyStackTopIndex {
92+
sb.WriteString(fmt.Sprintf("%#v", x.slice[currentIndex]))
93+
currentIndex--
94+
}
95+
sb.WriteString("]")
96+
return sb.String()
8297
}

array_stack_test.go

Lines changed: 143 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package stack
22

33
import (
4+
"errors"
45
"fmt"
56
"github.com/stretchr/testify/assert"
67
"testing"
@@ -11,31 +12,29 @@ import (
1112
func TestArrayStack_Clear(t *testing.T) {
1213
stack := NewArrayStack[int]()
1314
stack.Push(1)
14-
stack.Push(2)
15-
assert.Equal(t, 2, stack.Size())
15+
assert.Equal(t, 1, stack.Size())
1616
stack.Clear()
1717
assert.Equal(t, 0, stack.Size())
1818
}
1919

2020
func ExampleArrayStack_Clear() {
2121
stack := NewArrayStack[int]()
2222
stack.Push(1)
23-
stack.Push(2)
2423
fmt.Println(stack.Size())
2524
stack.Clear()
2625
fmt.Println(stack.Size())
2726
// Output:
28-
// 2
27+
// 1
2928
// 0
3029
}
3130

3231
// ------------------------------------------------ ---------------------------------------------------------------------
3332

3433
func TestArrayStack_IsEmpty(t *testing.T) {
3534
stack := NewArrayStack[int]()
36-
assert.Equal(t, stack.IsEmpty(), true)
35+
assert.Equal(t, true, stack.IsEmpty())
3736
stack.Push(1)
38-
assert.Equal(t, stack.IsEmpty(), false)
37+
assert.Equal(t, false, stack.IsEmpty())
3938
}
4039

4140
func ExampleArrayStack_IsEmpty() {
@@ -52,64 +51,195 @@ func ExampleArrayStack_IsEmpty() {
5251

5352
func TestArrayStack_IsNotEmpty(t *testing.T) {
5453
stack := NewArrayStack[int]()
55-
assert.Equal(t, stack.IsNotEmpty(), false)
54+
assert.Equal(t, false, stack.IsNotEmpty())
5655
stack.Push(1)
57-
assert.Equal(t, stack.IsEmpty(), true)
56+
assert.Equal(t, true, stack.IsNotEmpty())
5857
}
5958

6059
func ExampleArrayStack_IsNotEmpty() {
6160
stack := NewArrayStack[int]()
6261
fmt.Println(stack.IsNotEmpty())
6362
stack.Push(1)
6463
fmt.Println(stack.IsNotEmpty())
64+
// Output:
65+
// false
66+
// true
6567
}
6668

6769
// ------------------------------------------------ ---------------------------------------------------------------------
6870

69-
func TestArrayStack_Size(t *testing.T) {
71+
func TestArrayStack_Peek(t *testing.T) {
72+
type User struct {
73+
}
74+
stack := NewArrayStack[*User]()
75+
assert.Nil(t, stack.Peek())
76+
u := &User{}
77+
stack.Push(u)
78+
assert.Equal(t, u, stack.Peek())
79+
}
7080

81+
func ExampleArrayStack_Peek() {
82+
type User struct {
83+
}
84+
stack := NewArrayStack[*User]()
85+
fmt.Println(stack.Peek())
86+
u := &User{}
87+
stack.Push(u)
88+
fmt.Println(stack.Peek())
89+
// Output:
90+
// <nil>
91+
// &{}
7192
}
7293

7394
// ------------------------------------------------ ---------------------------------------------------------------------
7495

75-
func TestArrayStack_Peek(t *testing.T) {
96+
func TestArrayStack_PeekE(t *testing.T) {
97+
stack := NewArrayStack[int]()
98+
element, err := stack.PeekE()
99+
assert.Equal(t, 0, element)
100+
assert.ErrorIs(t, ErrStackEmpty, err)
76101

102+
stack.Push(1)
103+
element, err = stack.PeekE()
104+
assert.Nil(t, err)
105+
assert.Equal(t, 1, element)
77106
}
78107

79-
// ------------------------------------------------ ---------------------------------------------------------------------
80-
81-
func TestArrayStack_PeekE(t *testing.T) {
108+
func ExampleArrayStack_PeekE() {
109+
stack := NewArrayStack[int]()
110+
element, err := stack.PeekE()
111+
if errors.Is(err, ErrStackEmpty) {
112+
fmt.Println("stack empty!")
113+
}
82114

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
83125
}
84126

85127
// ------------------------------------------------ ---------------------------------------------------------------------
86128

87129
func TestArrayStack_Pop(t *testing.T) {
130+
type User struct {
131+
}
132+
stack := NewArrayStack[*User]()
133+
assert.Nil(t, stack.Pop())
134+
u := &User{}
135+
stack.Push(u)
136+
assert.Equal(t, u, stack.Pop())
137+
}
88138

139+
func ExampleArrayStack_Pop() {
140+
type User struct {
141+
}
142+
stack := NewArrayStack[*User]()
143+
fmt.Println(stack.Pop())
144+
u := &User{}
145+
stack.Push(u)
146+
fmt.Println(stack.Pop())
147+
// Output:
148+
// <nil>
149+
// &{}
89150
}
90151

91152
// ------------------------------------------------ ---------------------------------------------------------------------
92153

93154
func TestArrayStack_PopE(t *testing.T) {
155+
stack := NewArrayStack[int]()
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 ExampleArrayStack_PopE() {
167+
stack := NewArrayStack[int]()
168+
element, err := stack.PopE()
169+
if errors.Is(err, ErrStackEmpty) {
170+
fmt.Println("stack empty!")
171+
}
94172

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
95183
}
96184

97185
// ------------------------------------------------ ---------------------------------------------------------------------
98186

99187
func TestArrayStack_Push(t *testing.T) {
188+
stack := NewArrayStack[int]()
189+
stack.Push(1)
190+
}
191+
192+
func ExampleArrayStack_Push() {
193+
stack := NewArrayStack[int]()
194+
stack.Push(1)
195+
// Output:
196+
//
197+
}
198+
199+
// ------------------------------------------------ ---------------------------------------------------------------------
200+
201+
func TestArrayStack_Size(t *testing.T) {
202+
stack := NewArrayStack[int]()
203+
stack.Push(1)
204+
assert.Equal(t, 1, stack.Size())
205+
}
100206

207+
func ExampleArrayStack_Size() {
208+
stack := NewArrayStack[int]()
209+
stack.Push(1)
210+
fmt.Println(stack.Size())
211+
// Output:
212+
// 1
101213
}
102214

103215
// ------------------------------------------------ ---------------------------------------------------------------------
104216

105217
func TestArrayStack_String(t *testing.T) {
218+
stack := NewArrayStack[int]()
219+
stack.Push(1)
220+
assert.Equal(t, "[1]", stack.String())
221+
}
106222

223+
func ExampleArrayStack_String() {
224+
stack := NewArrayStack[int]()
225+
stack.Push(1)
226+
fmt.Println(stack.String())
227+
// Output:
228+
// [1]
107229
}
108230

109231
// ------------------------------------------------ ---------------------------------------------------------------------
110232

111233
func TestNewArrayStack(t *testing.T) {
234+
stack := NewArrayStack[int]()
235+
assert.NotNil(t, stack)
236+
}
112237

238+
func ExampleNewArrayStack() {
239+
stack := NewArrayStack[int]()
240+
fmt.Println(stack.String())
241+
// Output:
242+
// []
113243
}
114244

115245
// ------------------------------------------------ ---------------------------------------------------------------------

0 commit comments

Comments
 (0)