|
| 1 | +import { expect } from 'chai'; |
| 2 | +import * as helpers from './helpers'; |
| 3 | +import * as funcs from './ch4-q03'; |
| 4 | + |
| 5 | +function toArrayOfArrays(lists) { |
| 6 | + return lists.map(l => l.toArray()); |
| 7 | +} |
| 8 | + |
| 9 | +for (let key in funcs) { |
| 10 | + let func = funcs[key]; |
| 11 | + |
| 12 | + describe('ch4-q03: ' + key, function() { |
| 13 | + |
| 14 | + beforeEach(function() { |
| 15 | + this.tree = new helpers.Tree(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('returns empty list for empty tree', function() { |
| 19 | + expect(toArrayOfArrays(func(this.tree))).to.eql([]); |
| 20 | + }); |
| 21 | + |
| 22 | + it('returns true for single node tree', function() { |
| 23 | + this.tree.add(10); |
| 24 | + expect(toArrayOfArrays(func(this.tree))).to.eql([[10]]); |
| 25 | + }); |
| 26 | + |
| 27 | + it('returns single value lists for left heavy tree', function() { |
| 28 | + [10, 9, 8].forEach(v => this.tree.add(v)); |
| 29 | + expect(toArrayOfArrays(func(this.tree))).to.eql([ |
| 30 | + [10], |
| 31 | + [9], |
| 32 | + [8] |
| 33 | + ]); |
| 34 | + }); |
| 35 | + |
| 36 | + it('returns 2 value lists for upside down V shaped tree', function() { |
| 37 | + [10, 11, 12, 13, 9, 8, 7].forEach(v => this.tree.add(v)); |
| 38 | + expect(toArrayOfArrays(func(this.tree))).to.eql([ |
| 39 | + [10], |
| 40 | + [9, 11], |
| 41 | + [8, 12], |
| 42 | + [7, 13] |
| 43 | + ]); |
| 44 | + }); |
| 45 | + |
| 46 | + it('returns true for larger balanced tree', function() { |
| 47 | + let expected = []; |
| 48 | + this.tree.add(8); |
| 49 | + expected.push([8]); |
| 50 | + expect(toArrayOfArrays(func(this.tree)), 'root').to.be.eql(expected); |
| 51 | + this.tree.add(4); |
| 52 | + this.tree.add(12); |
| 53 | + expected.push([4, 12]); |
| 54 | + expect(toArrayOfArrays(func(this.tree)), 'depth 1').to.eql(expected); |
| 55 | + this.tree.add(2); |
| 56 | + this.tree.add(6); |
| 57 | + this.tree.add(10); |
| 58 | + this.tree.add(14); |
| 59 | + expected.push([2, 6, 10, 14]); |
| 60 | + expect(toArrayOfArrays(func(this.tree)), 'depth 2').to.eql(expected); |
| 61 | + this.tree.add(1); |
| 62 | + this.tree.add(3); |
| 63 | + this.tree.add(5); |
| 64 | + this.tree.add(7); |
| 65 | + expected.push([1, 3, 5, 7]); |
| 66 | + expect(toArrayOfArrays(func(this.tree)), '1/2 depth 3').to.eql(expected); |
| 67 | + this.tree.add(9); |
| 68 | + this.tree.add(11); |
| 69 | + this.tree.add(13); |
| 70 | + this.tree.add(15); |
| 71 | + expected[expected.length - 1].push(9, 11, 13, 15); |
| 72 | + expect(toArrayOfArrays(func(this.tree)), 'depth 3').to.eql(expected); |
| 73 | + this.tree.add(16); |
| 74 | + expected.push([16]); |
| 75 | + expect(toArrayOfArrays(func(this.tree)), '1 depth 4').to.eql(expected); |
| 76 | + }); |
| 77 | + |
| 78 | + }); |
| 79 | + |
| 80 | +} |
0 commit comments