|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { Tree } from './helpers'; |
| 3 | +import * as funcs from './ch4-q12'; |
| 4 | + |
| 5 | +for (let key in funcs) { |
| 6 | + let func = funcs[key]; |
| 7 | + |
| 8 | + describe('ch4-q12: ' + key, function() { |
| 9 | + |
| 10 | + beforeEach(function() { |
| 11 | + this.tree = new Tree(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('throws an error if tree is null or empty', function() { |
| 15 | + expect(() => func(null, 10)).to.throw('tree must be valid and non-empty'); |
| 16 | + expect(() => func(this.tree)).to.throw('tree must be valid and non-empty'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('returns 0 when no paths sum to value', function() { |
| 20 | + [10, 9, 11, 8, 7, 6].forEach(v => this.tree.add(v)); |
| 21 | + expect(func(this.tree, 50)).to.eql(0); |
| 22 | + expect(func(this.tree, 5)).to.eql(0); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns correct counts with balanced tree', function() { |
| 26 | + [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15].forEach(v => this.tree.add(v)); |
| 27 | + expect(func(this.tree, 1)).to.equal(1); |
| 28 | + expect(func(this.tree, 5)).to.equal(2); |
| 29 | + expect(func(this.tree, 10)).to.equal(2); |
| 30 | + expect(func(this.tree, 11)).to.equal(2); |
| 31 | + }); |
| 32 | + |
| 33 | + it('returns correct counts with unbalanced tree', function() { |
| 34 | + [10, 8, 16, 4, 14, 22, 6, 12, 18, 5, 17, 19].forEach(v => this.tree.add(v)); |
| 35 | + expect(func(this.tree, 10)).to.equal(2); |
| 36 | + expect(func(this.tree, 18)).to.equal(3); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns correct counts with lots of paths that equal value', function() { |
| 40 | + [50, 10, 80, 40, 70, 150, 20, 30].forEach(v => this.tree.add(v)); |
| 41 | + expect(func(this.tree, 50)).to.equal(3); |
| 42 | + expect(func(this.tree, 150)).to.equal(3); |
| 43 | + }); |
| 44 | + |
| 45 | + }); |
| 46 | + |
| 47 | +} |
0 commit comments