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