forked from SiZapPaaiGwat/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarysearch.spec.js
More file actions
38 lines (29 loc) · 1.12 KB
/
binarysearch.spec.js
File metadata and controls
38 lines (29 loc) · 1.12 KB
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
38
'use strict';
var binarySearch =
require('../../src/searching/binarysearch').binarySearch;
describe('Binary search', function () {
it('should find the element at position 0 ', function () {
expect(binarySearch([1, 2, 3, 4, 6, 8], 1)).toBe(0);
});
it('should find the element in position arr.length - 1', function () {
var arr = [1, 2, 3, 4, 6, 8];
expect(binarySearch(arr, 8)).toBe(arr.length - 1);
});
it('should work with arrays with 2 elements', function () {
expect(binarySearch([1, 8], 1)).toBe(0);
expect(binarySearch([1, 8], 8)).toBe(1);
});
it('should return a negative number for missing elements', function () {
expect(binarySearch([1, 2, 3], 4)).toBeLessThan(0);
});
it('should work with empty arrays', function () {
expect(binarySearch([], 4)).toBe(-1);
});
it('should work with a key string', function () {
expect(binarySearch([{ x: 1 }, { x: 2 }, { x: 3 }], { x: 2 }, 'x')).toBe(1);
});
it('should work with a key function', function () {
expect(binarySearch([{ x: 1 }, { x: 2 }, { x: 3 }],
{ x: 2 }, function (o) { return o.x; })).toBe(1);
});
});