|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var mod = require('../../src/data-structures/size-balanced-tree.js'); |
| 4 | +var Node = mod.Node; |
| 5 | +var Nil = mod.Nil; |
| 6 | +var SBTree = mod.SBTree; |
| 7 | +var updateChild = mod.updateChild; |
| 8 | + |
| 9 | +describe('Node', function () { |
| 10 | + it('should be a constructor function', function () { |
| 11 | + expect(typeof Node).toBe('function'); |
| 12 | + }); |
| 13 | + it('should be a construct properly', function () { |
| 14 | + var node = new Node(10, Nil, Nil, Nil, 1); |
| 15 | + expect(node.value).toBe(10); |
| 16 | + expect(node.left).toBe(Nil); |
| 17 | + expect(node.right).toBe(Nil); |
| 18 | + expect(node.parent).toBe(Nil); |
| 19 | + expect(node.size).toBe(1); |
| 20 | + }); |
| 21 | + it('should reference children/parent properly', function () { |
| 22 | + var root = new Node(10, Nil, Nil, Nil, 1); |
| 23 | + var left = new Node(5, root, Nil, Nil, 1); |
| 24 | + var right = new Node(15, root, Nil, Nil, 1); |
| 25 | + root.left = left; |
| 26 | + root.right = right; |
| 27 | + expect(root.value).toBe(10); |
| 28 | + expect(root.left).toBe(left); |
| 29 | + expect(root.right).toBe(right); |
| 30 | + expect(root.parent).toBe(Nil); |
| 31 | + expect(right.parent).toBe(root); |
| 32 | + expect(left.parent).toBe(root); |
| 33 | + expect(right.size).toBe(1); |
| 34 | + expect(left.size).toBe(1); |
| 35 | + expect(root.size).toBe(1); |
| 36 | + root.updateSize(); |
| 37 | + expect(root.size).toBe(3); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe('SBTree', function () { |
| 42 | + it('should be a constructor function', function () { |
| 43 | + expect(typeof SBTree).toBe('function'); |
| 44 | + }); |
| 45 | + it('should start with null root', function () { |
| 46 | + expect(new SBTree()._root).toBe(Nil); |
| 47 | + }); |
| 48 | + it('should insert and remove correctly', function () { |
| 49 | + var sTree = new SBTree(); |
| 50 | + expect(sTree.size).toBe(0); |
| 51 | + sTree.insert(0, 10); |
| 52 | + expect(sTree.size).toBe(1); |
| 53 | + sTree.remove(0); |
| 54 | + expect(sTree.size).toBe(0); |
| 55 | + expect(sTree._root).toBe(Nil); |
| 56 | + }); |
| 57 | + |
| 58 | + function checkNil() { |
| 59 | + expect(Nil.size).toBe(0); |
| 60 | + expect(Nil.left).toBe(Nil); |
| 61 | + expect(Nil.right).toBe(Nil); |
| 62 | + expect(Nil.parent).toBe(Nil); |
| 63 | + expect(Nil.value).toBe(null); |
| 64 | + } |
| 65 | + it('test updateChild', function() { |
| 66 | + var e = updateChild(Nil, Nil); |
| 67 | + checkNil(); |
| 68 | + expect(e).toBe(Nil); |
| 69 | + var root = new Node(10, Nil, Nil, Nil, 1); |
| 70 | + var left = new Node(5, root, Nil, Nil, 1); |
| 71 | + var right = new Node(15, root, Nil, Nil, 1); |
| 72 | + var leftLeft = new Node(10, left, Nil, Nil, 1); |
| 73 | + left.left = leftLeft; |
| 74 | + root.left = left; |
| 75 | + root.right = right; |
| 76 | + updateChild(left, leftLeft); |
| 77 | + expect(leftLeft.parent).toBe(root); |
| 78 | + expect(root.left).toBe(leftLeft); |
| 79 | + updateChild(leftLeft, Nil); |
| 80 | + checkNil(); |
| 81 | + expect(root.left).toBe(Nil); |
| 82 | + updateChild(Nil, right); |
| 83 | + expect(right.parent).toBe(Nil); |
| 84 | + }); |
| 85 | +}); |
0 commit comments