forked from TheAlgorithms/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.test.ts
More file actions
68 lines (51 loc) · 1.6 KB
/
stack.test.ts
File metadata and controls
68 lines (51 loc) · 1.6 KB
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
import { Stack } from '../stack'
describe('Testing Stack data structure', () => {
it('push should add a new element to the stack', () => {
const stack = new Stack<number>()
stack.push(2)
expect(stack.length()).toBe(1)
})
it('push should throw error on reach limit', () => {
const stack = new Stack<number>(2)
stack.push(2)
stack.push(3)
expect(() => stack.push(4)).toThrow('Stack Overflow')
})
it('isEmpty should return true on empty stack', () => {
const stack = new Stack<number>()
expect(stack.isEmpty()).toBeTruthy()
})
it('isEmpty should return false on not empty stack', () => {
const stack = new Stack<number>()
stack.push(2)
expect(stack.isEmpty()).toBeFalsy()
})
it('top should return the last value', () => {
const stack = new Stack<number>()
stack.push(2)
expect(stack.top()).toBe(2)
})
it('top should return null when the stack is empty', () => {
const stack = new Stack<number>()
expect(stack.top()).toBe(null)
})
it('length should return the number of elements in the stack', () => {
const stack = new Stack<number>()
stack.push(2)
stack.push(2)
stack.push(2)
expect(stack.length()).toBe(3)
})
it('pop should remove the last element and return it', () => {
const stack = new Stack<number>()
stack.push(1)
stack.push(2)
stack.push(3)
expect(stack.pop()).toBe(3)
expect(stack.length()).toBe(2)
})
it('pop should throw an exception if the stack is empty', () => {
const stack = new Stack<number>()
expect(() => stack.pop()).toThrow('Stack Underflow')
})
})