forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbisect.test.js
83 lines (66 loc) · 3.36 KB
/
bisect.test.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import { bisectionLeft, bisectionRight } from '../../utils/bisect';
describe('bisectionRight', function () {
const array = [0, 1, 2, 3, 3, 3, 3, 3, 5, 6, 7, 8, 9];
// The new element to be inserted into this array is referred to as x
it('returns index of the first number greater than x', function () {
expect(bisectionRight(array, 4)).toBe(8);
expect(bisectionRight(array, 3)).toBe(8);
});
it('returns index of the first number greater than x, occuring after low', function () {
expect(bisectionRight(array, 4, 8)).toBe(8);
expect(bisectionRight(array, 3, 9)).toBe(9);
});
it('returns index of the first number greater than x, between low and high', function () {
expect(bisectionRight(array, 4, 1, 7)).toBe(7);
expect(bisectionRight(array, 3, 4, 6)).toBe(6);
});
it('returns 0 if all elements are greater than x', function () {
expect(bisectionRight(array, -5)).toBe(0);
});
it('returns array length if x is greater than all elements', function () {
expect(bisectionRight(array, 15)).toBe(13);
});
it('throws TypeError if either low or high are outside the range of the array', function () {
expect(() => bisectionRight(array, 15, -2)).toThrow(TypeError);
expect(() => bisectionRight(array, 15, 100)).toThrow(TypeError);
expect(() => bisectionRight(array, 15, 2, -10)).toThrow(TypeError);
expect(() => bisectionRight(array, 15, 2, 100)).toThrow(TypeError);
expect(() => bisectionRight(array, 15, -20, -10)).toThrow(TypeError);
expect(() => bisectionRight(array, 15, 100, 200)).toThrow(TypeError);
});
});
describe('bisectionLeft', function () {
const array = [0, 1, 2, 3, 3, 3, 3, 3, 5, 6, 7, 8, 9];
it('returns index of the first number greater than x, if x does not exist in the array', function () {
expect(bisectionLeft(array, 4)).toBe(8);
});
it('returns index of first occurence of x, if x exists in the array', function () {
expect(bisectionLeft(array, 3)).toBe(3);
});
it('returns index of the first number greater than x or the first occurence of x, occuring after low', function () {
expect(bisectionLeft(array, 2, 1)).toBe(2);
expect(bisectionLeft(array, 3, 6)).toBe(6);
});
it('returns index of the first number greater than x or the first occurence of x, between low and high', function () {
expect(bisectionLeft(array, 2, 5, 8)).toBe(5);
expect(bisectionLeft(array, 3, 4, 6)).toBe(4);
});
it('returns 0 if all elements are greater than x', function () {
expect(bisectionLeft(array, -2)).toBe(0);
});
it('returns array length if x is greater than all elements', function () {
expect(bisectionLeft(array, 15)).toBe(13);
});
it('throws TypeError if either low or high are outside the range of the array', function () {
expect(() => bisectionLeft(array, 15, -2)).toThrow(TypeError);
expect(() => bisectionLeft(array, 15, 100)).toThrow(TypeError);
expect(() => bisectionLeft(array, 15, 2, -10)).toThrow(TypeError);
expect(() => bisectionLeft(array, 15, 2, 100)).toThrow(TypeError);
expect(() => bisectionLeft(array, 15, -20, -10)).toThrow(TypeError);
expect(() => bisectionLeft(array, 15, 100, 200)).toThrow(TypeError);
});
});