11package stack
22
33import (
4+ "errors"
45 "fmt"
56 "github.com/stretchr/testify/assert"
67 "testing"
@@ -11,31 +12,29 @@ import (
1112func 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
2020func 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
3433func 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
4140func ExampleArrayStack_IsEmpty () {
@@ -52,64 +51,195 @@ func ExampleArrayStack_IsEmpty() {
5251
5352func 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
6059func 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
87129func 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
93154func 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
99187func 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
105217func 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
111233func 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