Skip to content

Commit 129de26

Browse files
committed
Add recursive binary search spec
1 parent 8da32f3 commit 129de26

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
var binarySearch =
4+
require('../../src/searching/recursive-binarysearch').binarySearch;
5+
6+
describe('Binary search', function () {
7+
8+
it('should find the element at position 0 ', function () {
9+
expect(binarySearch([1, 2, 3, 4, 6, 8], 1)).toBe(0);
10+
});
11+
12+
it('should find the eleent in position arr.length', function () {
13+
expect(binarySearch([1, 2, 3, 4, 6, 8], 1)).toBe(0);
14+
});
15+
16+
it('should work with arrays with 2 elements', function () {
17+
expect(binarySearch([1, 8], 1)).toBe(0);
18+
expect(binarySearch([1, 8], 8)).toBe(1);
19+
});
20+
21+
it('should return a negative number for missing elements', function () {
22+
expect(binarySearch([1, 2, 3], 4)).toBeLessThan(0);
23+
});
24+
25+
it('should work with empty arrays', function () {
26+
expect(binarySearch([], 4)).toBe(-1);
27+
});
28+
29+
});

0 commit comments

Comments
 (0)