|
| 1 | +import 'mocha'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { CircularLinkedList } from '../../../src/ts/index'; |
| 4 | +import MyObj from './my-obj'; |
| 5 | + |
| 6 | +describe('CircularLinkedList', () => { |
| 7 | + let list: CircularLinkedList<number>; |
| 8 | + let min: number; |
| 9 | + let max: number; |
| 10 | + |
| 11 | + beforeEach(function() { |
| 12 | + list = new CircularLinkedList<number>(); |
| 13 | + min = 1; |
| 14 | + max = 3; |
| 15 | + }); |
| 16 | + |
| 17 | + function pushesElements() { |
| 18 | + for (let i = min; i <= max; i++) { |
| 19 | + list.push(i); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + function verifyList() { |
| 24 | + let current = list.getHead(); |
| 25 | + for (let i = min; i <= max && current; i++) { |
| 26 | + expect(current).to.not.be.an('undefined'); |
| 27 | + if (current) { |
| 28 | + // TS strictNullChecks |
| 29 | + expect(current.element).to.not.be.an('undefined'); |
| 30 | + expect(current.element).to.equal(i); |
| 31 | + if (i < max) { |
| 32 | + expect(current.next).to.not.be.an('undefined'); |
| 33 | + if (current.next) { |
| 34 | + // TS strictNullChecks |
| 35 | + expect(current.next.element).to.equal(i + 1); |
| 36 | + } |
| 37 | + } else { |
| 38 | + // circular list |
| 39 | + expect(current.next).to.not.be.an('undefined'); |
| 40 | + expect(current.next).to.equal(list.getHead()); |
| 41 | + if (current.next) { |
| 42 | + expect(current.next.element).to.equal(min); |
| 43 | + } |
| 44 | + } |
| 45 | + current = current.next; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + it('starts empty', () => { |
| 51 | + expect(list.size()).to.equal(0); |
| 52 | + expect(list.isEmpty()).to.equal(true); |
| 53 | + expect(list.getHead()).to.be.an('undefined'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('pushes elements', () => { |
| 57 | + pushesElements(); |
| 58 | + verifyList(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns element at specific index: invalid position', () => { |
| 62 | + // list is empty |
| 63 | + expect(list.getElementAt(3)).to.be.an('undefined'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns element at specific index', () => { |
| 67 | + let node; |
| 68 | + |
| 69 | + pushesElements(); |
| 70 | + |
| 71 | + for (let i = min; i <= max; i++) { |
| 72 | + node = list.getElementAt(i - 1); |
| 73 | + expect(node).to.not.be.an('undefined'); |
| 74 | + if (node) { |
| 75 | + expect(node.element).to.equal(i); |
| 76 | + } |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + it('inserts elements first position empty list', () => { |
| 81 | + const element = 1; |
| 82 | + max = element; |
| 83 | + expect(list.insert(0, element)).to.equal(true); |
| 84 | + verifyList(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('inserts elements first position not empty list', () => { |
| 88 | + max = 2; |
| 89 | + expect(list.insert(0, max)).to.equal(true); |
| 90 | + |
| 91 | + expect(list.insert(0, min)).to.equal(true); |
| 92 | + |
| 93 | + verifyList(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('inserts elements invalid position empty list', () => { |
| 97 | + expect(list.insert(1, 1)).to.equal(false); |
| 98 | + }); |
| 99 | + |
| 100 | + it('inserts elements invalid position not empty list', () => { |
| 101 | + const element = 1; |
| 102 | + expect(list.insert(0, element)).to.equal(true); |
| 103 | + expect(list.insert(2, element)).to.equal(false); |
| 104 | + }); |
| 105 | + |
| 106 | + it('inserts elements in the middle of list', () => { |
| 107 | + expect(list.insert(0, 3)).to.equal(true); |
| 108 | + expect(list.insert(0, 1)).to.equal(true); |
| 109 | + expect(list.insert(1, 2)).to.equal(true); |
| 110 | + verifyList(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('inserts elements at the end of list', () => { |
| 114 | + max = 5; |
| 115 | + |
| 116 | + for (let i = min; i <= max; i++) { |
| 117 | + expect(list.insert(i - 1, i)).to.equal(true); |
| 118 | + } |
| 119 | + |
| 120 | + verifyList(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('returns index of elements', () => { |
| 124 | + let index; |
| 125 | + |
| 126 | + pushesElements(); |
| 127 | + |
| 128 | + for (let i = min; i <= max; i++) { |
| 129 | + index = list.indexOf(i); |
| 130 | + expect(index).to.equal(i - 1); |
| 131 | + } |
| 132 | + |
| 133 | + expect(list.indexOf(max + 2)).to.equal(-1); |
| 134 | + }); |
| 135 | + |
| 136 | + it('removes valid elements', () => { |
| 137 | + let element; |
| 138 | + |
| 139 | + pushesElements(); |
| 140 | + |
| 141 | + const minIndex = min; |
| 142 | + for (let i = minIndex; i <= max; i++) { |
| 143 | + element = list.remove(i); |
| 144 | + expect(element).to.not.be.an('undefined'); |
| 145 | + expect(element).to.equal(i); |
| 146 | + min++; |
| 147 | + verifyList(); |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | + it('removes invalid elements', () => { |
| 152 | + let element; |
| 153 | + |
| 154 | + pushesElements(); |
| 155 | + |
| 156 | + for (let i = max + 2; i <= max + 4; i++) { |
| 157 | + element = list.remove(i); |
| 158 | + expect(element).to.be.an('undefined'); |
| 159 | + } |
| 160 | + }); |
| 161 | + |
| 162 | + it('removes element invalid position empty list', () => { |
| 163 | + let element; |
| 164 | + |
| 165 | + for (let i = min; i <= max; i++) { |
| 166 | + element = list.removeAt(i - 1); |
| 167 | + expect(element).to.be.an('undefined'); |
| 168 | + } |
| 169 | + }); |
| 170 | + |
| 171 | + it('removes element invalid position not empty list', () => { |
| 172 | + let element; |
| 173 | + |
| 174 | + pushesElements(); |
| 175 | + |
| 176 | + for (let i = max + 2; i <= max + 4; i++) { |
| 177 | + element = list.removeAt(i); |
| 178 | + expect(element).to.be.an('undefined'); |
| 179 | + } |
| 180 | + }); |
| 181 | + |
| 182 | + it('removes first element list single element', () => { |
| 183 | + const value = 1; |
| 184 | + list.push(value); |
| 185 | + |
| 186 | + const element = list.removeAt(0); |
| 187 | + expect(element).to.not.be.an('undefined'); |
| 188 | + expect(element).to.equal(value); |
| 189 | + |
| 190 | + expect(list.getHead()).to.be.an('undefined'); |
| 191 | + expect(list.isEmpty()).to.equal(true); |
| 192 | + }); |
| 193 | + |
| 194 | + it('removes first element list multiple elements', () => { |
| 195 | + pushesElements(); |
| 196 | + |
| 197 | + const element = list.removeAt(0); |
| 198 | + expect(element).to.not.be.an('undefined'); |
| 199 | + expect(element).to.equal(min); |
| 200 | + |
| 201 | + min = 2; |
| 202 | + verifyList(); |
| 203 | + }); |
| 204 | + |
| 205 | + it('removes element from middle of list', () => { |
| 206 | + pushesElements(); // 1, 2, 3 |
| 207 | + |
| 208 | + const element = list.removeAt(1); // element 2 |
| 209 | + expect(element).to.not.be.an('undefined'); |
| 210 | + expect(element).to.equal(2); |
| 211 | + |
| 212 | + // list needs to be [1, 3] |
| 213 | + let current = list.getHead(); |
| 214 | + |
| 215 | + // element 1 |
| 216 | + expect(current).to.not.be.an('undefined'); |
| 217 | + if (current) { |
| 218 | + expect(current.element).to.not.be.an('undefined'); |
| 219 | + expect(current.element).to.equal(min); |
| 220 | + expect(current.next).to.not.be.an('undefined'); |
| 221 | + if (current.next) { |
| 222 | + expect(current.next.element).to.equal(max); |
| 223 | + current = current.next; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + // element 3 |
| 228 | + expect(current).to.not.be.an('undefined'); |
| 229 | + if (current) { |
| 230 | + expect(current.element).to.not.be.an('undefined'); |
| 231 | + expect(current.element).to.equal(max); |
| 232 | + expect(current.next).to.not.be.an('undefined'); |
| 233 | + expect(current.next).to.equal(list.getHead()); |
| 234 | + if (current.next) { |
| 235 | + expect(current.next.element).to.equal(min); |
| 236 | + } |
| 237 | + } |
| 238 | + }); |
| 239 | + |
| 240 | + it('removes element from end of list', () => { |
| 241 | + let element; |
| 242 | + |
| 243 | + pushesElements(); |
| 244 | + |
| 245 | + const maxIndex = max; |
| 246 | + for (let i = max; i >= min; i--) { |
| 247 | + element = list.removeAt(i - 1); |
| 248 | + expect(element).to.not.be.an('undefined'); |
| 249 | + expect(element).to.equal(i); |
| 250 | + max--; |
| 251 | + verifyList(); |
| 252 | + } |
| 253 | + }); |
| 254 | + |
| 255 | + it('returns the head of the list', () => { |
| 256 | + expect(list.getHead()).to.be.an('undefined'); |
| 257 | + |
| 258 | + list.push(1); |
| 259 | + expect(list.getHead()).to.not.be.an('undefined'); |
| 260 | + }); |
| 261 | + |
| 262 | + it('returns the correct size', () => { |
| 263 | + expect(list.size()).to.equal(0); |
| 264 | + |
| 265 | + for (let i = min; i <= max; i++) { |
| 266 | + list.push(i); |
| 267 | + expect(list.size()).to.equal(i); |
| 268 | + } |
| 269 | + |
| 270 | + const size = max; |
| 271 | + for (let i = min; i <= max; i++) { |
| 272 | + list.remove(i); |
| 273 | + expect(list.size()).to.equal(size - i); |
| 274 | + } |
| 275 | + |
| 276 | + expect(list.size()).to.equal(0); |
| 277 | + }); |
| 278 | + |
| 279 | + it('returns if it is empty', () => { |
| 280 | + expect(list.isEmpty()).to.equal(true); |
| 281 | + for (let i = min; i <= max; i++) { |
| 282 | + list.push(i); |
| 283 | + expect(list.isEmpty()).to.equal(false); |
| 284 | + } |
| 285 | + |
| 286 | + for (let i = min; i < max; i++) { |
| 287 | + list.remove(i); |
| 288 | + expect(list.isEmpty()).to.equal(false); |
| 289 | + } |
| 290 | + list.remove(max); |
| 291 | + expect(list.isEmpty()).to.equal(true); |
| 292 | + |
| 293 | + pushesElements(); |
| 294 | + expect(list.isEmpty()).to.equal(false); |
| 295 | + |
| 296 | + list.clear(); |
| 297 | + expect(list.isEmpty()).to.equal(true); |
| 298 | + }); |
| 299 | + |
| 300 | + it('clears the list', () => { |
| 301 | + expect(list.size()).to.equal(0); |
| 302 | + list.clear(); |
| 303 | + expect(list.size()).to.equal(0); |
| 304 | + pushesElements(); |
| 305 | + expect(list.size()).to.greaterThan(0); |
| 306 | + list.clear(); |
| 307 | + expect(list.size()).to.equal(0); |
| 308 | + }); |
| 309 | + |
| 310 | + it('returns toString primitive types', () => { |
| 311 | + expect(list.toString()).to.equal(''); |
| 312 | + |
| 313 | + list.push(1); |
| 314 | + expect(list.toString()).to.equal('1'); |
| 315 | + |
| 316 | + list.push(2); |
| 317 | + expect(list.toString()).to.equal('1,2'); |
| 318 | + |
| 319 | + list.clear(); |
| 320 | + expect(list.toString()).to.equal(''); |
| 321 | + }); |
| 322 | + |
| 323 | + it('returns toString primitive types: string', () => { |
| 324 | + const ds = new CircularLinkedList<string>(); |
| 325 | + ds.push('el1'); |
| 326 | + expect(ds.toString()).to.equal('el1'); |
| 327 | + |
| 328 | + ds.push('el2'); |
| 329 | + expect(ds.toString()).to.equal('el1,el2'); |
| 330 | + }); |
| 331 | + |
| 332 | + it('returns toString objects', () => { |
| 333 | + const ds = new CircularLinkedList<MyObj>(); |
| 334 | + expect(ds.toString()).to.equal(''); |
| 335 | + |
| 336 | + ds.push(new MyObj(1, 2)); |
| 337 | + expect(ds.toString()).to.equal('1|2'); |
| 338 | + |
| 339 | + ds.push(new MyObj(3, 4)); |
| 340 | + expect(ds.toString()).to.equal('1|2,3|4'); |
| 341 | + }); |
| 342 | +}); |
0 commit comments