7
7
// author [Milad](https://github.com/miraddo)
8
8
// see stackarray.go, stacklinkedlist.go, stacklinkedlistwithlist.go
9
9
10
- package stack
10
+ package stack_test
11
11
12
12
import (
13
13
"container/list"
14
+ "github.com/TheAlgorithms/Go/structure/stack"
14
15
"reflect"
15
16
"testing"
16
17
)
17
18
18
19
// TestStackLinkedList for testing Stack with LinkedList
19
20
func TestStackLinkedList (t * testing.T ) {
20
- var newStack Stack
21
+ var newStack stack. Stack
21
22
22
- newStack .push (1 )
23
- newStack .push (2 )
23
+ newStack .Push (1 )
24
+ newStack .Push (2 )
24
25
25
26
t .Run ("Stack Push" , func (t * testing.T ) {
26
- result := newStack .show ()
27
+ result := newStack .Show ()
27
28
expected := []any {2 , 1 }
28
29
for x := range result {
29
30
if result [x ] != expected [x ] {
@@ -33,20 +34,20 @@ func TestStackLinkedList(t *testing.T) {
33
34
})
34
35
35
36
t .Run ("Stack isEmpty" , func (t * testing.T ) {
36
- if newStack .isEmpty () {
37
- t .Error ("Stack isEmpty is returned true but expected false" , newStack .isEmpty ())
37
+ if newStack .IsEmpty () {
38
+ t .Error ("Stack isEmpty is returned true but expected false" , newStack .IsEmpty ())
38
39
}
39
40
40
41
})
41
42
42
43
t .Run ("Stack Length" , func (t * testing.T ) {
43
- if newStack .len () != 2 {
44
- t .Error ("Stack Length should be 2 but got" , newStack .len ())
44
+ if newStack .Length () != 2 {
45
+ t .Error ("Stack Length should be 2 but got" , newStack .Length ())
45
46
}
46
47
})
47
48
48
- newStack .pop ()
49
- pop := newStack .pop ()
49
+ newStack .Pop ()
50
+ pop := newStack .Pop ()
50
51
51
52
t .Run ("Stack Pop" , func (t * testing.T ) {
52
53
if pop != 1 {
@@ -55,74 +56,82 @@ func TestStackLinkedList(t *testing.T) {
55
56
56
57
})
57
58
58
- newStack .push (52 )
59
- newStack .push (23 )
60
- newStack .push (99 )
61
- t .Run ("Stack Peak " , func (t * testing.T ) {
62
- if newStack .peak () != 99 {
63
- t .Error ("Stack Peak should return 99 but got " , newStack .peak ())
59
+ newStack .Push (52 )
60
+ newStack .Push (23 )
61
+ newStack .Push (99 )
62
+ t .Run ("Stack Peek " , func (t * testing.T ) {
63
+ if newStack .Peek () != 99 {
64
+ t .Error ("Stack Peak should return 99 but got " , newStack .Peek ())
64
65
}
65
66
})
66
67
}
67
68
68
69
// TestStackArray for testing Stack with Array
69
70
func TestStackArray (t * testing.T ) {
71
+ newStack := stack .NewStack [int ]()
70
72
t .Run ("Stack With Array" , func (t * testing.T ) {
71
73
72
- stackPush (2 )
73
- stackPush (3 )
74
+ newStack . Push (2 )
75
+ newStack . Push (3 )
74
76
75
77
t .Run ("Stack Push" , func (t * testing.T ) {
76
- if ! reflect .DeepEqual ([]any {3 , 2 }, stackArray ) {
77
- t .Errorf ("Stack Push is not work we expected %v but got %v" , []any {3 , 2 }, stackArray )
78
+ var stackElements []any
79
+ for i := 0 ; i < 2 ; i ++ {
80
+ poppedElement := newStack .Pop ()
81
+ stackElements = append (stackElements , poppedElement )
82
+ }
83
+
84
+ if ! reflect .DeepEqual ([]any {3 , 2 }, stackElements ) {
85
+ t .Errorf ("Stack Push is not work we expected %v but got %v" , []any {3 , 2 }, newStack )
78
86
}
87
+
88
+ newStack .Push (2 )
89
+ newStack .Push (3 )
79
90
})
80
91
81
- pop := stackPop ()
92
+ pop := newStack . Pop ()
82
93
83
94
t .Run ("Stack Pop" , func (t * testing.T ) {
84
-
85
- if stackLength () == 2 && pop != 3 {
95
+ if newStack .Length () == 2 && pop != 3 {
86
96
t .Errorf ("Stack Pop is not work we expected %v but got %v" , 3 , pop )
87
97
}
88
98
})
89
99
90
- stackPush (2 )
91
- stackPush (83 )
100
+ newStack . Push (2 )
101
+ newStack . Push (83 )
92
102
93
103
t .Run ("Stack Peak" , func (t * testing.T ) {
94
-
95
- if stackPeak () != 83 {
96
- t .Errorf ("Stack Peak is not work we expected %v but got %v" , 83 , stackPeak ())
104
+ if newStack .Peek () != 83 {
105
+ t .Errorf ("Stack Peek is not work we expected %v but got %v" , 83 , newStack .Peek ())
97
106
}
98
107
})
99
108
100
109
t .Run ("Stack Length" , func (t * testing.T ) {
101
- if stackLength () != 3 {
102
- t .Errorf ("Stack Length is not work we expected %v but got %v" , 3 , stackLength ())
110
+ if newStack . Length () != 3 {
111
+ t .Errorf ("Stack Length is not work we expected %v but got %v" , 3 , newStack . Length ())
103
112
}
104
113
})
105
114
106
115
t .Run ("Stack Empty" , func (t * testing.T ) {
107
- if stackEmpty () == true {
108
- t .Errorf ("Stack Empty is not work we expected %v but got %v" , false , stackEmpty ())
116
+ if newStack . IsEmpty () == true {
117
+ t .Errorf ("Stack Empty is not work we expected %v but got %v" , false , newStack . IsEmpty ())
109
118
}
110
119
111
- stackPop ()
112
- stackPop ()
113
- stackPop ()
120
+ newStack . Pop ()
121
+ newStack . Pop ()
122
+ newStack . Pop ()
114
123
115
- if stackEmpty () == false {
116
- t .Errorf ("Stack Empty is not work we expected %v but got %v" , true , stackEmpty ())
124
+ if newStack . IsEmpty () == false {
125
+ t .Errorf ("Stack Empty is not work we expected %v but got %v" , true , newStack . IsEmpty ())
117
126
}
118
127
})
119
128
})
120
129
}
121
130
122
131
// TestStackLinkedListWithList for testing Stack with Container/List Library (STL)
123
132
func TestStackLinkedListWithList (t * testing.T ) {
124
- stackList := & SList {
125
- stack : list .New (),
133
+ stackList := & stack. SList {
134
+ Stack : list .New (),
126
135
}
127
136
128
137
t .Run ("Stack Push" , func (t * testing.T ) {
@@ -147,7 +156,7 @@ func TestStackLinkedListWithList(t *testing.T) {
147
156
148
157
stackList .Push (2 )
149
158
stackList .Push (83 )
150
- peak , _ := stackList .Peak ()
159
+ peak , _ := stackList .Peek ()
151
160
if peak != 83 {
152
161
t .Errorf ("Stack Peak is not work we expected %v but got %v" , 83 , peak )
153
162
}
@@ -160,8 +169,8 @@ func TestStackLinkedListWithList(t *testing.T) {
160
169
})
161
170
162
171
t .Run ("Stack Empty" , func (t * testing.T ) {
163
- if stackList .Empty () == true {
164
- t .Errorf ("Stack Empty is not work we expected %v but got %v" , false , stackList .Empty ())
172
+ if stackList .IsEmpty () == true {
173
+ t .Errorf ("Stack Empty is not work we expected %v but got %v" , false , stackList .IsEmpty ())
165
174
}
166
175
167
176
d1 , err := stackList .Pop ()
@@ -172,8 +181,8 @@ func TestStackLinkedListWithList(t *testing.T) {
172
181
t .Errorf ("got an unexpected error %v, pop1: %v, pop2: %v, pop3: %v" , err , d1 , d2 , d3 )
173
182
}
174
183
175
- if stackList .Empty () == false {
176
- t .Errorf ("Stack Empty is not work we expected %v but got %v" , true , stackList .Empty ())
184
+ if stackList .IsEmpty () == false {
185
+ t .Errorf ("Stack Empty is not work we expected %v but got %v" , true , stackList .IsEmpty ())
177
186
}
178
187
})
179
188
}
0 commit comments