7
7
import { Stack } from "../stack" ;
8
8
9
9
/**
10
- *
11
- *
12
- * @author Trevor Sears <trevorsears.main@gmail. com>
13
- * @version v0.1 .0
10
+ * Unit tests for the stack data structure implemented by this package.
11
+ *
12
+ * @author Trevor Sears <trevor@ trevorsears.com> (https://trevorsears.com/)
13
+ * @version v1.0 .0
14
14
* @since v0.1.0
15
15
*/
16
16
17
- let stack : Stack < number > ;
17
+ let stack : Stack < any > ;
18
18
19
19
describe ( "Initialization" , ( ) => {
20
20
@@ -23,6 +23,8 @@ describe("Initialization", () => {
23
23
stack = new Stack < number > ( ) ;
24
24
25
25
expect ( stack ) . toBeDefined ( ) ;
26
+ expect ( stack . pop ( ) ) . toBeUndefined ( ) ;
27
+ expect ( stack . peek ( ) ) . toBeUndefined ( ) ;
26
28
27
29
} ) ;
28
30
@@ -36,6 +38,7 @@ describe("Initialization", () => {
36
38
expect ( stack . pop ( ) ) . toBe ( 3 ) ;
37
39
expect ( stack . pop ( ) ) . toBe ( 1 ) ;
38
40
expect ( stack . pop ( ) ) . toBeUndefined ( ) ;
41
+ expect ( stack . peek ( ) ) . toBeUndefined ( ) ;
39
42
40
43
} ) ;
41
44
@@ -45,7 +48,7 @@ describe("Per-method tests.", () => {
45
48
46
49
beforeEach ( ( ) => {
47
50
48
- stack = new Stack < number > ( ) ;
51
+ stack = new Stack < any > ( ) ;
49
52
50
53
} ) ;
51
54
@@ -56,6 +59,42 @@ describe("Per-method tests.", () => {
56
59
stack . push ( 4 ) ;
57
60
58
61
expect ( stack . toArray ( ) ) . toStrictEqual ( [ 4 ] )
62
+ expect ( stack . peek ( ) ) . toBe ( 4 ) ;
63
+ expect ( stack . pop ( ) ) . toBe ( 4 ) ;
64
+
65
+ } ) ;
66
+
67
+ test ( "Pushing undefined works as expected." , ( ) : void => {
68
+
69
+ stack . push ( undefined ) ;
70
+
71
+ expect ( stack . toArray ( ) ) . toStrictEqual ( [ undefined ] )
72
+ expect ( stack . peek ( ) ) . toBe ( undefined ) ;
73
+ expect ( stack . pop ( ) ) . toBe ( undefined ) ;
74
+
75
+ } ) ;
76
+
77
+ test ( "Pushing null works as expected." , ( ) : void => {
78
+
79
+ stack . push ( null ) ;
80
+
81
+ expect ( stack . toArray ( ) ) . toStrictEqual ( [ null ] )
82
+ expect ( stack . peek ( ) ) . toBe ( null ) ;
83
+ expect ( stack . pop ( ) ) . toBe ( null ) ;
84
+
85
+ } ) ;
86
+
87
+ test ( "Pushing multiple values occurs in the expected order." , ( ) : void => {
88
+
89
+ stack . push ( 1 , 2 ) ;
90
+ stack . push ( 3 , 4 , 5 ) ;
91
+
92
+ expect ( stack . toArray ( ) ) . toStrictEqual ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
93
+ expect ( stack . pop ( ) ) . toBe ( 5 ) ;
94
+ expect ( stack . pop ( ) ) . toBe ( 4 ) ;
95
+ expect ( stack . pop ( ) ) . toBe ( 3 ) ;
96
+ expect ( stack . pop ( ) ) . toBe ( 2 ) ;
97
+ expect ( stack . pop ( ) ) . toBe ( 1 ) ;
59
98
60
99
} ) ;
61
100
@@ -103,60 +142,140 @@ describe("Per-method tests.", () => {
103
142
104
143
} ) ;
105
144
106
- } ) ;
107
-
108
- describe ( "#add" , ( ) => {
109
-
110
-
111
-
112
145
} ) ;
113
146
114
147
describe ( "#clear" , ( ) => {
115
148
116
-
117
-
118
- } ) ;
119
-
120
- describe ( "#contains" , ( ) => {
121
-
122
-
123
-
124
- } ) ;
125
-
126
- describe ( "#get" , ( ) => {
127
-
128
-
149
+ test ( "#clear on empty stack does nothing" , ( ) : void => {
150
+
151
+ let beforeClear : any [ ] = [ ...stack . toArray ( ) ] ;
152
+
153
+ stack . clear ( ) ;
154
+
155
+ let afterClear : any [ ] = [ ...stack . toArray ( ) ] ;
156
+
157
+ expect ( beforeClear ) . toStrictEqual ( afterClear ) ;
158
+
159
+ } ) ;
160
+
161
+ test ( "#clear empties a populated stack" , ( ) : void => {
162
+
163
+ stack . push ( 1 , 2 , 3 , 5 , 8 ) ;
164
+ stack . clear ( ) ;
165
+
166
+ expect ( stack . isEmpty ( ) ) . toBeTruthy ( ) ;
167
+
168
+ } ) ;
129
169
130
170
} ) ;
131
171
132
172
describe ( "#isEmpty" , ( ) => {
133
173
134
-
174
+ test ( "#isEmpty returns true for newly initialized blank stack" , ( ) : void => {
175
+
176
+ expect ( stack . isEmpty ( ) ) . toBeTruthy ( ) ;
177
+
178
+ } ) ;
179
+
180
+ test ( "#isEmpty returns false for populated stack" , ( ) : void => {
181
+
182
+ stack . push ( 5 , 10 , 15 ) ;
183
+
184
+ expect ( stack . isEmpty ( ) ) . toBeFalsy ( ) ;
185
+
186
+ } ) ;
187
+
188
+ test ( "#isEmpty correctly changes result after a stack is emptied" , ( ) : void => {
189
+
190
+ expect ( stack . isEmpty ( ) ) . toBeTruthy ( ) ;
191
+
192
+ stack . push ( 1 , 2 , 4 , 8 , 16 ) ;
193
+
194
+ expect ( stack . isEmpty ( ) ) . toBeFalsy ( ) ;
195
+
196
+ stack . pop ( ) ;
197
+
198
+ expect ( stack . isEmpty ( ) ) . toBeFalsy ( ) ;
199
+
200
+ stack . pop ( ) ;
201
+
202
+ expect ( stack . isEmpty ( ) ) . toBeFalsy ( ) ;
203
+
204
+ stack . pop ( ) ;
205
+
206
+ expect ( stack . isEmpty ( ) ) . toBeFalsy ( ) ;
207
+
208
+ stack . pop ( ) ;
209
+
210
+ expect ( stack . isEmpty ( ) ) . toBeFalsy ( ) ;
211
+
212
+ stack . pop ( ) ;
213
+
214
+ expect ( stack . isEmpty ( ) ) . toBeTruthy ( ) ;
215
+
216
+ } ) ;
135
217
136
218
} ) ;
137
219
138
220
describe ( "#iterator" , ( ) => {
139
221
140
-
141
-
142
- } ) ;
143
-
144
- describe ( "#remove" , ( ) => {
145
-
146
-
222
+ test ( "#iterator iterates over content in stack #pop order" , ( ) : void => {
223
+
224
+ stack . push ( 2 , 4 , 6 , 8 ) ;
225
+
226
+ let expectedElements : number [ ] = [ 8 , 6 , 4 , 2 ] ;
227
+ let index : number = 0 ;
228
+
229
+ for ( let element of stack ) expect ( element ) . toBe ( expectedElements [ index ++ ] ) ;
230
+
231
+ } ) ;
147
232
148
233
} ) ;
149
234
150
235
describe ( "#size" , ( ) => {
151
236
152
-
237
+ test ( "#size returns 0 for empty stacks" , ( ) : void => {
238
+
239
+ expect ( stack . size ( ) ) . toBe ( 0 ) ;
240
+
241
+ } ) ;
242
+
243
+ test ( "#size returns the correct number of contained elements" , ( ) : void => {
244
+
245
+ expect ( stack . size ( ) ) . toBe ( 0 ) ;
246
+
247
+ stack . push ( 0 ) ;
248
+
249
+ expect ( stack . size ( ) ) . toBe ( 1 ) ;
250
+
251
+ stack . push ( 0 ) ;
252
+
253
+ expect ( stack . size ( ) ) . toBe ( 2 ) ;
254
+
255
+ stack . push ( 0 ) ;
256
+
257
+ expect ( stack . size ( ) ) . toBe ( 3 ) ;
258
+
259
+ } ) ;
153
260
154
261
} ) ;
155
262
156
263
describe ( "#toArray" , ( ) => {
264
+
265
+ test ( "#toArray returns an empty array for empty stacks" , ( ) : void => {
266
+
267
+ expect ( stack . toArray ( ) ) . toStrictEqual ( [ ] ) ;
268
+
269
+ } ) ;
157
270
158
-
271
+ test ( "#toArray returns elements in the expected order" , ( ) : void => {
272
+
273
+ stack . push ( 3 , 2 , 1 ) ;
274
+
275
+ expect ( stack . toArray ( ) ) . toStrictEqual ( [ 3 , 2 , 1 ] ) ;
276
+
277
+ } ) ;
159
278
160
279
} ) ;
161
280
162
- } ) ;
281
+ } ) ;
0 commit comments