Code coverage report for spec/algo/4-searching/depthFirstSearch.spec.js

Statements: 100% (31 / 31)      Branches: 100% (0 / 0)      Functions: 100% (8 / 8)      Lines: 100% (28 / 28)      Ignored: none     

All files » spec/algo/4-searching/ » depthFirstSearch.spec.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 381 1   1 1 1 3 3 3 3 3 3 3 3     1 7     1 7     1 7 1   1 1 1 1 1 1 1 1    
var depthFirstSearch = require("../../../lib/algorithms/4-searching/depthFirstSearch.js");
var bst = require("../../../lib/dataStructures/binarySearchTree.js");
 
describe('Given a binary tree containing the value 16, determine if the depth first search', function () {
    var balanced;
    beforeEach(function() {
      balanced = new bst();
      balanced.add(10);
      balanced.add(5);
      balanced.add(15);
      balanced.add(8);
      balanced.add(2);
      balanced.add(12);
      balanced.add(16);
    });
 
  it('can find the value 16 in the tree', function () {
    expect(depthFirstSearch(balanced.head, function(value) { return value === 16 })).toBe(true);
  });
 
  it('cannot find the value 11', function () {
    expect(depthFirstSearch(balanced.head, function(value) { return value === 11 })).toBe(false);
  });
 
  it('the order of search is correct', function () {
    var matcher = { matcher: function(value) { return value === 16 }};
    spyOn(matcher, "matcher").andCallThrough();
 
    expect(depthFirstSearch(balanced.head, matcher.matcher)).toBe(true);
    expect(matcher.matcher).toHaveBeenCalledWith(10);
    expect(matcher.matcher).toHaveBeenCalledWith(5);
    expect(matcher.matcher).toHaveBeenCalledWith(15);
    expect(matcher.matcher).toHaveBeenCalledWith(2);
    expect(matcher.matcher).toHaveBeenCalledWith(8);
    expect(matcher.matcher).toHaveBeenCalledWith(12);
    expect(matcher.matcher).toHaveBeenCalledWith(16);
  });
});