-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.test.ts
157 lines (131 loc) · 4.21 KB
/
stack.test.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
import Stack from "../../../../src/app/data-structures/Stack/Stack";
import ILinearStorage from "../../../../src/app/types/ILinearStorage";
import CollectionIsEmptyException from "../../../../src/app/exceptions/CollectionIsEmptyException";
import CollectionIsFullException from "../../../../src/app/exceptions/CollectionIsFullException";
describe("stack", () => {
describe("method peek", () => {
it("should correct peek value from top", () => {
const stack: ILinearStorage<number> = new Stack(100);
stack.push(5);
stack.push(10);
expect(stack.peek()).toBe(10);
});
it("should throw when stack is empty", () => {
const stack: ILinearStorage<number> = new Stack(1);
expect(() => {
stack.peek();
}).toThrowError(CollectionIsEmptyException);
});
});
describe("method push", () => {
it("should correct push to top", () => {
const stack: ILinearStorage<number> = new Stack(100);
stack.push(5);
stack.push(10);
expect(stack.peek()).toBe(10);
});
it("should throw when stack is full", () => {
const stack: ILinearStorage<number> = new Stack(1);
stack.push(10);
expect(() => {
stack.push(20);
}).toThrowError(CollectionIsFullException);
});
});
describe("method pop", () => {
describe("should correct pop from top", () => {
const stack: ILinearStorage<number> = new Stack(100);
stack.push(5);
stack.push(10);
const popped = stack.pop();
it("should delete correct", () => {
expect(stack.peek()).toBe(5);
});
it("should return correct value", () => {
expect(popped).toBe(10);
});
});
it("should throw when stack is empty", () => {
const stack: ILinearStorage<number> = new Stack(1);
expect(() => {
stack.pop();
}).toThrowError(CollectionIsEmptyException);
});
});
describe("method has", function () {
const stack = new Stack();
stack.push(5);
it("should return true when value exists", () => {
expect(stack.has(5)).toBe(true);
});
it("should return false when value does not exist", () => {
expect(stack.has(10)).toBe(false);
});
});
describe("method length", function () {
describe("when stack is non empty", () => {
describe("after adding", () => {
it("should return updated length value", () => {
const stack = new Stack();
stack.push(5);
stack.push(15);
stack.push(10);
expect(stack.length()).toBe(3);
});
});
describe("after deleting", () => {
it("should return updated length value", () => {
const stack = new Stack();
stack.push(5);
stack.push(15);
stack.push(10);
stack.pop();
expect(stack.length()).toBe(2);
});
});
});
describe("when stack is empty", () => {
it("should return zero value", () => {
const stack = new Stack();
expect(stack.length()).toBe(0);
});
});
});
describe("method isEmpty", () => {
it("should return true when stack is empty", () => {
const stack: ILinearStorage<number> = new Stack(100);
expect(stack.isEmpty()).toBe(true);
});
});
describe("method isFull", () => {
it("should return false when stack elements length lower than its capacity", () => {
const stack: ILinearStorage<number> = new Stack(100);
stack.push(10);
expect(stack.isFull()).toBe(false);
});
it("should return true when stack elements length same as its capacity", () => {
const stack: ILinearStorage<number> = new Stack(1);
stack.push(10);
expect(stack.isFull()).toBe(true);
});
});
describe("method clear", () => {
it("should correctly clear stack", () => {
const stack = new Stack();
stack.push(10);
stack.push(20);
stack.clear();
expect(stack.isEmpty()).toBe(true);
});
});
describe("method reverse", () => {
it("should correctly reverse stack", () => {
const stack: ILinearStorage<number> = new Stack();
stack.push(5);
stack.push(10);
stack.push(15);
stack.reverse();
expect(stack.peek()).toBe(5);
});
});
});