forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbisect.test.js
223 lines (190 loc) · 7.69 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* 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,
bisectionRightByKey,
bisectionRightByStrKey,
} 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('bisectionRightByKey', function () {
// T is string
// We're going to use a key function which maps a string to its length.
// This array is sorted by string length.
const array = [
'',
'a',
'in',
'cat',
'dog',
'moo',
'bus',
'fob',
'camel',
'stairs',
'kitchen',
'profiler',
'profiling',
];
function strLen(s: string): number {
return s.length;
}
// The new element to be inserted into this array is referred to as x
it('returns index of the first element whose key is greater than x', function () {
expect(bisectionRightByKey(array, 4, strLen)).toBe(8);
expect(bisectionRightByKey(array, 3, strLen)).toBe(8);
});
it('returns index of the first element whose key is greater than x, occuring after low', function () {
expect(bisectionRightByKey(array, 4, strLen, 8)).toBe(8);
expect(bisectionRightByKey(array, 3, strLen, 9)).toBe(9);
});
it('returns index of the first element whose key is greater than x, between low and high', function () {
expect(bisectionRightByKey(array, 4, strLen, 1, 7)).toBe(7);
expect(bisectionRightByKey(array, 3, strLen, 4, 6)).toBe(6);
});
it('returns 0 if the keys of all elements are greater than x', function () {
expect(bisectionRightByKey(array, -5, strLen)).toBe(0);
});
it('returns array length if x is greater than the key of all elements', function () {
expect(bisectionRightByKey(array, 15, strLen)).toBe(13);
});
it('throws TypeError if either low or high are outside the range of the array', function () {
expect(() => bisectionRightByKey(array, 15, strLen, -2)).toThrow(TypeError);
expect(() => bisectionRightByKey(array, 15, strLen, 100)).toThrow(
TypeError
);
expect(() => bisectionRightByKey(array, 15, strLen, 2, -10)).toThrow(
TypeError
);
expect(() => bisectionRightByKey(array, 15, strLen, 2, 100)).toThrow(
TypeError
);
expect(() => bisectionRightByKey(array, 15, strLen, -20, -10)).toThrow(
TypeError
);
expect(() => bisectionRightByKey(array, 15, strLen, 100, 200)).toThrow(
TypeError
);
});
});
describe('bisectionRightByStrKey', function () {
const array = [
{ name: 'a0' },
{ name: 'a1' },
{ name: 'a2' },
{ name: 'a3' },
{ name: 'a3' },
{ name: 'a3' },
{ name: 'a3' },
{ name: 'a3' },
{ name: 'a5' },
{ name: 'a6' },
{ name: 'a7' },
{ name: 'a8' },
{ name: 'a9' },
];
function getName(x): string {
return x.name;
}
// The new element to be inserted into this array is referred to as x
it('returns index of the first element whose key is greater than x', function () {
expect(bisectionRightByStrKey(array, 'a4', getName)).toBe(8);
expect(bisectionRightByStrKey(array, 'a3', getName)).toBe(8);
});
it('returns index of the first element whose key is greater than x, occuring after low', function () {
expect(bisectionRightByStrKey(array, 'a4', getName, 8)).toBe(8);
expect(bisectionRightByStrKey(array, 'a3', getName, 9)).toBe(9);
});
it('returns index of the first element whose key is greater than x, between low and high', function () {
expect(bisectionRightByStrKey(array, 'a4', getName, 1, 7)).toBe(7);
expect(bisectionRightByStrKey(array, 'a3', getName, 4, 6)).toBe(6);
});
it('returns 0 if all the keys of elements are greater than x', function () {
expect(bisectionRightByStrKey(array, '_', getName)).toBe(0);
});
it('returns array length if x is greater than keys of all elements', function () {
expect(bisectionRightByStrKey(array, 'b', getName)).toBe(13);
});
it('throws TypeError if either low or high are outside the range of the array', function () {
expect(() => bisectionRightByStrKey(array, 'b', getName, -2)).toThrow(
TypeError
);
expect(() => bisectionRightByStrKey(array, 'b', getName, 100)).toThrow(
TypeError
);
expect(() => bisectionRightByStrKey(array, 'b', getName, 2, -10)).toThrow(
TypeError
);
expect(() => bisectionRightByStrKey(array, 'b', getName, 2, 100)).toThrow(
TypeError
);
expect(() => bisectionRightByStrKey(array, 'b', getName, -20, -10)).toThrow(
TypeError
);
expect(() => bisectionRightByStrKey(array, 'b', getName, 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);
});
});