Skip to content

Commit

Permalink
Add test for bfs traverse.js
Browse files Browse the repository at this point in the history
  • Loading branch information
brunops committed May 12, 2015
1 parent 8095e60 commit 9e2a6df
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions misc/javascript/traverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,39 @@ var bfs = function (root, callback) {
};

exports.bfs = bfs;

if (require.main === module) {
var Tree = require('../../data-structures/tree');

var assert = require('./assert');

// Create this super tree
// 1
// / \
// 2 3
// / | \ / | \ \
// 4 5 6 7 8 9 10

var tree = new Tree(1),
branch2 = tree.addChild(2),
branch3 = tree.addChild(3),
leaf4 = branch2.addChild(4),
leaf5 = branch2.addChild(5),
leaf6 = branch2.addChild(6),
leaf7 = branch3.addChild(7),
leaf8 = branch3.addChild(8),
leaf9 = branch3.addChild(9),
leaf10 = branch3.addChild(10);

// Generate `bfs` expected result
var expectedResult = (new Array(11)).join('0').split('').map(function (el, i) {
return i + 1;
}),
result = [];

bfs(tree, function (node) {
result.push(node.value);
});

assert(result, expectedResult, '#bfs returns [1..10]');
}

0 comments on commit 9e2a6df

Please sign in to comment.