|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { Tree } from './helpers'; |
| 3 | +import * as funcs from './ch4-q06'; |
| 4 | + |
| 5 | +for (let key in funcs) { |
| 6 | + let func = funcs[key]; |
| 7 | + |
| 8 | + describe('ch4-q06: ' + key, function() { |
| 9 | + |
| 10 | + beforeEach(function() { |
| 11 | + this.tree = new Tree(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('throws an error if node is null', function() { |
| 15 | + expect(() => func(null)).to.throw('node cannot be null'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('returns undefined for root of single node tree', function() { |
| 19 | + this.tree.add(10); |
| 20 | + expect(func(this.tree.root)).to.be.undefined; |
| 21 | + }); |
| 22 | + |
| 23 | + it('returns right value for simple 3 node balanced tree', function() { |
| 24 | + [10, 9, 11].forEach(v => this.tree.add(v)); |
| 25 | + expect(func(this.tree.root.left)).to.equal(10); |
| 26 | + expect(func(this.tree.root)).to.equal(11); |
| 27 | + expect(func(this.tree.root.right)).to.be.undefined; |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns correct values for larger balanced tree', function() { |
| 31 | + [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15].forEach(v => this.tree.add(v)); |
| 32 | + |
| 33 | + expect(func(this.tree.root.left.left.left)).to.equal(2); |
| 34 | + expect(func(this.tree.root.left.left.right)).to.equal(4); |
| 35 | + expect(func(this.tree.root.left.right.left)).to.equal(6); |
| 36 | + expect(func(this.tree.root.left.right.right)).to.equal(8); |
| 37 | + |
| 38 | + expect(func(this.tree.root.right.left.left)).to.equal(10); |
| 39 | + expect(func(this.tree.root.right.left.right)).to.equal(12); |
| 40 | + expect(func(this.tree.root.right.right.left)).to.equal(14); |
| 41 | + expect(func(this.tree.root.right.right.right)).to.be.undefined; |
| 42 | + }); |
| 43 | + |
| 44 | + }); |
| 45 | + |
| 46 | +} |
0 commit comments