-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbinarySearch.test.js
More file actions
39 lines (34 loc) · 1.02 KB
/
binarySearch.test.js
File metadata and controls
39 lines (34 loc) · 1.02 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
39
import binarySearch from './binarySearch';
var items = [
{ x: 10, y: 10 },
{ x: 15, y: 10 },
{ x: 16, y: 10 },
{ x: 20, y: 10 },
{ x: 50, y: 10 },
{ x: 100, y: 10 },
{ x: 140, y: 10 }
];
test('Search for item beyound left boundary of the collection should return leftmost item.', () => {
const result = binarySearch(items, function (item, offset) {
return 4 - item.x;
});
expect(result.item.x).toBe(10);
});
test('Search for item beyound right boundary of the collection should return the rightmost item.', () => {
const result = binarySearch(items, function (item, offset) {
return 200 - item.x;
});
expect(result.item.x).toBe(140);
});
test('Function should find item nearest to 60', () => {
const result = binarySearch(items, function (item, offset) {
return 60 - item.x;
});
expect(result.item.x).toBe(50);
});
test('Function should item nearest 90', () => {
const result = binarySearch(items, function (item, offset) {
return 90 - item.x;
});
expect(result.item.x).toBe(100);
});