forked from loiane/javascript-datastructures-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.spec.ts
163 lines (129 loc) · 3.78 KB
/
stack.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import 'mocha';
import { expect } from 'chai';
import Stack from '../../../src/ts/data-structures/stack';
describe('Stack', () => {
let stack: Stack<number>;
beforeEach(() => {
stack = new Stack<number>();
});
it('starts empty', () => {
expect(stack.size()).to.equal(0);
expect(stack.isEmpty()).to.equal(true);
});
it('pushes elements', () => {
stack.push(1);
expect(stack.size()).to.equal(1);
stack.push(2);
expect(stack.size()).to.equal(2);
stack.push(3);
expect(stack.size()).to.equal(3);
expect(stack.isEmpty()).to.equal(false);
});
it('pops elements', () => {
stack.push(1);
stack.push(2);
stack.push(3);
expect(stack.pop()).to.equal(3);
expect(stack.pop()).to.equal(2);
expect(stack.pop()).to.equal(1);
expect(stack.pop()).to.equal(undefined);
});
it('implements LIFO logic', () => {
stack.push(1);
stack.push(2);
stack.push(3);
expect(stack.pop()).to.equal(3);
expect(stack.pop()).to.equal(2);
expect(stack.pop()).to.equal(1);
expect(stack.pop()).to.equal(undefined);
});
it('allows to peek at the top element in the stack without popping it', () => {
expect(stack.peek()).to.equal(undefined);
stack.push(1);
expect(stack.peek()).to.equal(1);
stack.push(2);
expect(stack.peek()).to.equal(2);
stack.pop();
expect(stack.peek()).to.equal(1);
});
it('returns the correct size', () => {
expect(stack.size()).to.equal(0);
stack.push(1);
expect(stack.size()).to.equal(1);
stack.push(2);
expect(stack.size()).to.equal(2);
stack.push(3);
expect(stack.size()).to.equal(3);
stack.clear();
expect(stack.isEmpty()).to.equal(true);
stack.push(1);
stack.push(2);
stack.push(3);
stack.pop();
expect(stack.size()).to.equal(2);
stack.pop();
expect(stack.size()).to.equal(1);
stack.pop();
expect(stack.size()).to.equal(0);
stack.pop();
expect(stack.size()).to.equal(0);
});
it('returns if it is empty', () => {
expect(stack.isEmpty()).to.equal(true);
stack.push(1);
expect(stack.isEmpty()).to.equal(false);
stack.push(2);
expect(stack.isEmpty()).to.equal(false);
stack.push(3);
expect(stack.isEmpty()).to.equal(false);
stack.clear();
expect(stack.isEmpty()).to.equal(true);
stack.push(1);
stack.push(2);
stack.push(3);
stack.pop();
expect(stack.isEmpty()).to.equal(false);
stack.pop();
expect(stack.isEmpty()).to.equal(false);
stack.pop();
expect(stack.isEmpty()).to.equal(true);
stack.pop();
expect(stack.isEmpty()).to.equal(true);
});
it('clears the stack', () => {
stack.clear();
expect(stack.isEmpty()).to.equal(true);
stack.push(1);
stack.push(2);
stack.clear();
expect(stack.isEmpty()).to.equal(true);
});
it('returns toString primitive types', () => {
expect(stack.toString()).to.equal('');
stack.push(1);
expect(stack.toString()).to.equal('1');
stack.push(2);
expect(stack.toString()).to.equal('1,2');
stack.clear();
expect(stack.toString()).to.equal('');
const stackString = new Stack<string>();
stackString.push('el1');
expect(stackString.toString()).to.equal('el1');
stackString.push('el2');
expect(stackString.toString()).to.equal('el1,el2');
});
it('returns toString objects', () => {
class MyObj {
constructor(public el1: any, public el2: any) { }
toString() {
return `${this.el1.toString()}|${this.el2.toString()}`;
}
}
const stackMyObj = new Stack<MyObj>();
expect(stackMyObj.toString()).to.equal('');
stackMyObj.push(new MyObj(1, 2));
expect(stackMyObj.toString()).to.equal('1|2');
stackMyObj.push(new MyObj(3, 4));
expect(stackMyObj.toString()).to.equal('1|2,3|4');
});
});