77import { Stack } from "../stack" ;
88
99/**
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
1414 * @since v0.1.0
1515 */
1616
17- let stack : Stack < number > ;
17+ let stack : Stack < any > ;
1818
1919describe ( "Initialization" , ( ) => {
2020
@@ -23,6 +23,8 @@ describe("Initialization", () => {
2323 stack = new Stack < number > ( ) ;
2424
2525 expect ( stack ) . toBeDefined ( ) ;
26+ expect ( stack . pop ( ) ) . toBeUndefined ( ) ;
27+ expect ( stack . peek ( ) ) . toBeUndefined ( ) ;
2628
2729 } ) ;
2830
@@ -36,6 +38,7 @@ describe("Initialization", () => {
3638 expect ( stack . pop ( ) ) . toBe ( 3 ) ;
3739 expect ( stack . pop ( ) ) . toBe ( 1 ) ;
3840 expect ( stack . pop ( ) ) . toBeUndefined ( ) ;
41+ expect ( stack . peek ( ) ) . toBeUndefined ( ) ;
3942
4043 } ) ;
4144
@@ -45,7 +48,7 @@ describe("Per-method tests.", () => {
4548
4649 beforeEach ( ( ) => {
4750
48- stack = new Stack < number > ( ) ;
51+ stack = new Stack < any > ( ) ;
4952
5053 } ) ;
5154
@@ -56,6 +59,42 @@ describe("Per-method tests.", () => {
5659 stack . push ( 4 ) ;
5760
5861 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 ) ;
5998
6099 } ) ;
61100
@@ -103,60 +142,140 @@ describe("Per-method tests.", () => {
103142
104143 } ) ;
105144
106- } ) ;
107-
108- describe ( "#add" , ( ) => {
109-
110-
111-
112145 } ) ;
113146
114147 describe ( "#clear" , ( ) => {
115148
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+ } ) ;
129169
130170 } ) ;
131171
132172 describe ( "#isEmpty" , ( ) => {
133173
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+ } ) ;
135217
136218 } ) ;
137219
138220 describe ( "#iterator" , ( ) => {
139221
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+ } ) ;
147232
148233 } ) ;
149234
150235 describe ( "#size" , ( ) => {
151236
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+ } ) ;
153260
154261 } ) ;
155262
156263 describe ( "#toArray" , ( ) => {
264+
265+ test ( "#toArray returns an empty array for empty stacks" , ( ) : void => {
266+
267+ expect ( stack . toArray ( ) ) . toStrictEqual ( [ ] ) ;
268+
269+ } ) ;
157270
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+ } ) ;
159278
160279 } ) ;
161280
162- } ) ;
281+ } ) ;
0 commit comments