Skip to content

Commit 0bfaa40

Browse files
author
Trevor Sears
committed
Added more unit tests and bumped package number to v1.0.0!
1 parent aa80f8e commit 0bfaa40

File tree

4 files changed

+185
-52
lines changed

4 files changed

+185
-52
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jsdsl/stack",
3-
"version": "0.2.0",
3+
"version": "1.0.0",
44
"description": "A stack (LIFO) implementation written in TypeScript.",
55
"publishConfig": {
66
"access": "public"

ts/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
/**
2424
* NPM main class used for exporting this package's contents.
25-
*
26-
* @author Trevor Sears <trevorsears.main@gmail.com>
27-
* @version v0.2.0
25+
*
26+
* @author Trevor Sears <trevor@trevorsears.com> (https://trevorsears.com/)
27+
* @version v1.0.0
2828
* @since v0.1.0
2929
*/
3030

ts/stack.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@
2020
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2121
*/
2222

23-
import JSDSL from "@jsdsl/iterator";
23+
import {
24+
Iterable as JSDSLIterable,
25+
Iterator as JSDSLIterator,
26+
AbstractIterator as JSDSLAbstractIterator
27+
} from "@jsdsl/iterator";
2428

2529
/**
26-
* A stack (LIFO) implementation written in JavaScript/TypeScript.
27-
*
28-
* @author Trevor Sears <trevorsears.main@gmail.com>
29-
* @version v0.2.0
30+
* A stack (LIFO) implementation written in TypeScript.
31+
*
32+
* @author Trevor Sears <trevor@trevorsears.com> (https://trevorsears.com/)
33+
* @version v1.0.0
3034
* @since v0.1.0
3135
*/
32-
export class Stack<E> implements JSDSL.Iterable<E> {
36+
export class Stack<E> implements JSDSLIterable<E> {
3337

3438
/**
3539
* The internal raw array used to maintain this stack data structure.
@@ -44,18 +48,28 @@ export class Stack<E> implements JSDSL.Iterable<E> {
4448
*/
4549
public constructor(...elements: E[]) {
4650

47-
this.internalStack = [];
51+
this.internalStack = elements;
4852

4953
}
5054

5155
/**
5256
* Pushes the specified element onto the top of this stack.
5357
*
54-
* @param {E} element The element to push onto this stack.
58+
* Elements are pushed onto the stack in the order they are provided. This entails the following:
59+
*
60+
* <pre>
61+
* let stack: Stack<number> = new Stack();
62+
* stack.push(1, 3, 5);
63+
* stack.pop(); //=> 5
64+
* stack.pop(); //=> 3
65+
* stack.pop(); //=> 1
66+
* </pre>
67+
*
68+
* @param {E[]} elements A list of elements to push onto this stack.
5569
*/
56-
public push(element: E): void {
70+
public push(...elements: E[]): void {
5771

58-
this.internalStack.push(element);
72+
this.internalStack.push(...elements);
5973

6074
}
6175

@@ -106,9 +120,9 @@ export class Stack<E> implements JSDSL.Iterable<E> {
106120
*
107121
* @returns {IIterator<E>} An iterator over the elements of this stack, in LIFO (last in, first out) order.
108122
*/
109-
public iterator(): JSDSL.Iterator<E> {
123+
public iterator(): JSDSLIterator<E> {
110124

111-
return new class extends JSDSL.AbstractIterator<E> {
125+
return new class extends JSDSLAbstractIterator<E> {
112126

113127
protected elements: E[];
114128

ts/tests/stack.test.ts

Lines changed: 155 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
import { 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

1919
describe("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

Comments
 (0)