|
| 1 | +import { Stack } from '../../Stack'; |
| 2 | + |
| 3 | +describe('Testing Stack data structure', () => { |
| 4 | + it('push should add a new element to the stack', () => { |
| 5 | + const stack = new Stack<number>(); |
| 6 | + stack.push(2); |
| 7 | + |
| 8 | + expect(stack.length()).toBe(1); |
| 9 | + }); |
| 10 | + |
| 11 | + it('push should throw error on reach limit', () => { |
| 12 | + const stack = new Stack<number>(2); |
| 13 | + stack.push(2); |
| 14 | + stack.push(3); |
| 15 | + |
| 16 | + expect(() => stack.push(4)).toThrow('Stack Overflow'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('isEmpty should return true on empty stack', () => { |
| 20 | + const stack = new Stack<number>(); |
| 21 | + expect(stack.isEmpty()).toBeTruthy(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('isEmpty should return false on not empty stack', () => { |
| 25 | + const stack = new Stack<number>(); |
| 26 | + stack.push(2); |
| 27 | + |
| 28 | + expect(stack.isEmpty()).toBeFalsy(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('top should return the last value', () => { |
| 32 | + const stack = new Stack<number>(); |
| 33 | + stack.push(2); |
| 34 | + |
| 35 | + expect(stack.top()).toBe(2); |
| 36 | + }); |
| 37 | + |
| 38 | + it('top should return null when the stack is empty', () => { |
| 39 | + const stack = new Stack<number>(); |
| 40 | + |
| 41 | + expect(stack.top()).toBe(null); |
| 42 | + }); |
| 43 | + |
| 44 | + it('length should return the number of elements in the stack', () => { |
| 45 | + const stack = new Stack<number>(); |
| 46 | + stack.push(2); |
| 47 | + stack.push(2); |
| 48 | + stack.push(2); |
| 49 | + |
| 50 | + expect(stack.length()).toBe(3); |
| 51 | + }); |
| 52 | + |
| 53 | + it('pop should remove the last element and return it', () => { |
| 54 | + const stack = new Stack<number>(); |
| 55 | + stack.push(1); |
| 56 | + stack.push(2); |
| 57 | + stack.push(3); |
| 58 | + |
| 59 | + expect(stack.pop()).toBe(3); |
| 60 | + expect(stack.length()).toBe(2); |
| 61 | + }); |
| 62 | + |
| 63 | + it('pop should throw an exception if the stack is empty', () => { |
| 64 | + const stack = new Stack<number>(); |
| 65 | + |
| 66 | + expect(() => stack.pop()).toThrow('Stack Underflow'); |
| 67 | + }); |
| 68 | +}); |
0 commit comments