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