-
Notifications
You must be signed in to change notification settings - Fork 497
/
Copy pathArrayUtilities.test.ts
499 lines (443 loc) · 16.6 KB
/
ArrayUtilities.test.ts
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// Copyright (c) 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import * as Platform from './platform.js';
function comparator(a: number, b: number): number {
return a < b ? -1 : (a > b ? 1 : 0);
}
describe('ArrayUtilities', () => {
describe('removeElement', () => {
it('removes elements', () => {
const testCases = [
{input: [], expectedFirstOnlyTrue: [], expectedFirstOnlyFalse: []},
{input: [1], expectedFirstOnlyTrue: [1], expectedFirstOnlyFalse: [1]},
{
input: [1, 2, 3, 4, 5, 4, 3, 2, 1],
expectedFirstOnlyTrue: [1, 3, 4, 5, 4, 3, 2, 1],
expectedFirstOnlyFalse: [1, 3, 4, 5, 4, 3, 1],
},
{input: [2, 2, 2, 2, 2], expectedFirstOnlyTrue: [2, 2, 2, 2], expectedFirstOnlyFalse: []},
{input: [2, 2, 2, 1, 2, 2, 3, 2], expectedFirstOnlyTrue: [2, 2, 1, 2, 2, 3, 2], expectedFirstOnlyFalse: [1, 3]},
];
for (const testCase of testCases) {
const actualFirstOnlyTrue = [...testCase.input];
Platform.ArrayUtilities.removeElement(actualFirstOnlyTrue, 2, true);
assert.deepEqual(actualFirstOnlyTrue, testCase.expectedFirstOnlyTrue, 'Removing firstOnly (true) failed');
const actualFirstOnlyFalse = [...testCase.input];
Platform.ArrayUtilities.removeElement(actualFirstOnlyFalse, 2, false);
assert.deepEqual(actualFirstOnlyFalse, testCase.expectedFirstOnlyFalse, 'Removing firstOnly (false) failed');
}
});
});
const fixtures = [
[],
[1],
[2, 1],
[6, 4, 2, 7, 10, 15, 1],
[10, 44, 3, 6, 56, 66, 10, 55, 32, 56, 2, 5],
];
for (let i = 0; i < fixtures.length; i++) {
const fixture = fixtures[i];
it(`sorts ranges, fixture ${i}`, () => {
for (let left = 0, l = fixture.length - 1; left < l; ++left) {
for (let right = left, r = fixture.length; right < r; ++right) {
for (let first = left; first <= right; ++first) {
for (let count = 1, k = right - first + 1; count <= k; ++count) {
const actual = fixture.slice(0);
Platform.ArrayUtilities.sortRange(actual, comparator, left, right, first, first + count - 1);
assert.deepEqual(
fixture.slice(0, left), actual.slice(0, left), 'left ' + left + ' ' + right + ' ' + count);
assert.deepEqual(
fixture.slice(right + 1), actual.slice(right + 1), 'right ' + left + ' ' + right + ' ' + count);
const middle = fixture.slice(left, right + 1);
middle.sort(comparator);
assert.deepEqual(
middle.slice(first - left, first - left + count), actual.slice(first, first + count),
'sorted ' + left + ' ' + right + ' ' + first + ' ' + count);
const actualRest = actual.slice(first + count, right + 1);
actualRest.sort(comparator);
assert.deepEqual(
middle.slice(first - left + count), actualRest,
'unsorted ' + left + ' ' + right + ' ' + first + ' ' + count);
}
}
}
}
});
}
describe('binaryIndexOf', () => {
it('calculates the correct binary index', () => {
const fixtures = [
[],
[1],
[1, 10],
[1, 10, 11, 12, 13, 14, 100],
[-100, -50, 0, 50, 100],
[-100, -14, -13, -12, -11, -10, -1],
];
function testArray(array: number[]) {
function comparator(a: number, b: number) {
return a < b ? -1 : (a > b ? 1 : 0);
}
for (let i = -100; i <= 100; ++i) {
const reference = array.indexOf(i);
const actual = Platform.ArrayUtilities.binaryIndexOf(array, i, comparator);
assert.strictEqual(reference, actual);
}
}
for (const fixture of fixtures) {
testArray(fixture);
}
});
});
describe('merge and intersect', () => {
it('orders merge intersect', () => {
function comparator(a: number, b: number) {
return a - b;
}
function count(a: number[], x: number) {
return Platform.ArrayUtilities.upperBound(a, x, Platform.ArrayUtilities.DEFAULT_COMPARATOR) -
Platform.ArrayUtilities.lowerBound(a, x, Platform.ArrayUtilities.DEFAULT_COMPARATOR);
}
function testAll(a: number[], b: number[]) {
testOperation(a, b, Platform.ArrayUtilities.mergeOrdered(a, b, comparator), Math.max, 'U');
testOperation(a, b, Platform.ArrayUtilities.intersectOrdered(a, b, comparator), Math.min, 'x');
}
function testOperation(
a: number[], b: number[], actual: number[], checkOperation: (...values: number[]) => number, opName: string) {
const allValues = a.concat(b).concat(actual);
let expectedCount: number;
let actualCount: number;
for (let i = 0; i < allValues.length; ++i) {
const value = allValues[i];
expectedCount = checkOperation(count(a, value), count(b, value));
actualCount = count(actual, value);
assert.strictEqual(
expectedCount, actualCount,
'Incorrect result for value: ' + value + ' at [' + a + '] ' + opName + ' [' + b + '] -> [' + actual +
']');
}
const shallowCopy = [...actual];
assert.deepEqual(actual.sort(), shallowCopy, 'Result array is ordered');
}
const fixtures = new Map([
[[], []],
[[1], []],
[[1, 2, 2, 2, 3], []],
[[4, 5, 5, 8, 8], [1, 1, 1, 2, 6]],
[[1, 2, 2, 2, 2, 3, 3, 4], [2, 2, 2, 3, 3, 3, 3]],
[[1, 2, 3, 4, 5], [1, 2, 3]],
]);
for (const [a, b] of fixtures) {
testAll(a, b);
testAll(b, a);
}
});
});
describe('upperBound', () => {
it('finds the first object after the needle whose value is greater than the needle', async () => {
const input = [0, 1, 2, 3, 4, 5];
const index = Platform.ArrayUtilities.upperBound(input, 2, Platform.ArrayUtilities.DEFAULT_COMPARATOR);
assert.strictEqual(index, 3);
});
it('can take left and right params to alter the range', async () => {
const input = [0, 1, 2, 3, 4, 5];
const index = Platform.ArrayUtilities.upperBound(input, 2, Platform.ArrayUtilities.DEFAULT_COMPARATOR, 4, 6);
assert.strictEqual(index, 4);
});
it('can take a custom comparator to determine how to compare elements', async () => {
const input = [{time: 0, name: 'test1'}, {time: 6, name: 'test2'}];
const index = Platform.ArrayUtilities.upperBound(input, 2, (needle, element) => {
if (needle > element.time) {
return 1;
}
if (element.time > needle) {
return -1;
}
return 0;
});
assert.strictEqual(index, 1);
});
});
describe('lowerBound', () => {
it('finds the first object after the needle whose value is equal to or greater than the needle', async () => {
const input = [0, 1, 2, 3, 4, 5];
const index = Platform.ArrayUtilities.lowerBound(input, 2, Platform.ArrayUtilities.DEFAULT_COMPARATOR);
assert.strictEqual(index, 2);
});
it('can take left and right params to alter the range', async () => {
const input = [0, 1, 2, 3, 4, 5];
const index = Platform.ArrayUtilities.lowerBound(input, 2, Platform.ArrayUtilities.DEFAULT_COMPARATOR, 5, 6);
assert.strictEqual(index, 5);
});
it('can take a custom comparator to determine how to compare elements', async () => {
const input = [{time: 0, name: 'test1'}, {time: 2, name: 'test2'}, {time: 3, name: 'test3'}];
const index = Platform.ArrayUtilities.lowerBound(input, 2, (needle, element) => {
if (needle > element.time) {
return 1;
}
if (element.time > needle) {
return -1;
}
return 0;
});
assert.strictEqual(index, 1);
});
});
describe('Nearest', () => {
describe('Finding the last item where predicate is true', () => {
it('works with an even number of entries', () => {
const ascEntries = [{a: 1}, {a: 3}, {a: 3}, {a: 12}, {a: 13}, {a: 18}, {a: 23}, {a: 24}];
let nearest = Platform.ArrayUtilities.nearestIndexFromEnd(ascEntries, value => value.a < 7);
assert.strictEqual(nearest, 2);
const descEntries = [{a: 23}, {a: 18}, {a: 13}, {a: 12}, {a: 12}, {a: 3}, {a: 1}, {a: 0}];
nearest = Platform.ArrayUtilities.nearestIndexFromEnd(descEntries, value => value.a > 7);
assert.strictEqual(nearest, 4);
});
it('works with an odd number of entries', () => {
const ascEntries = [
{a: 1},
{a: 3},
{a: 12},
{a: 13},
{a: 18},
{a: 23},
{a: 23},
{a: 32},
{a: 33},
];
let nearest = Platform.ArrayUtilities.nearestIndexFromEnd(ascEntries, value => value.a < 31);
assert.strictEqual(nearest, 6);
const descEntries = [
{a: 32},
{a: 23},
{a: 18},
{a: 13},
{a: 12},
{a: 3},
{a: 3},
{a: 1},
];
nearest = Platform.ArrayUtilities.nearestIndexFromEnd(descEntries, value => value.a > 2);
assert.strictEqual(nearest, 6);
});
it('returns null if there are no matches at all', () => {
const ascEntries = [
{a: 1},
{a: 3},
{a: 12},
{a: 13},
{a: 18},
{a: 23},
{a: 32},
];
let zeroth = Platform.ArrayUtilities.nearestIndexFromEnd(ascEntries, value => value.a < 0);
assert.isNull(zeroth);
const descEntries = [
{a: 32},
{a: 23},
{a: 18},
{a: 13},
{a: 12},
{a: 3},
{a: 1},
];
zeroth = Platform.ArrayUtilities.nearestIndexFromEnd(descEntries, value => value.a > 40);
assert.isNull(zeroth);
});
it('works when the result is the last item', () => {
const ascEntries = [
{a: 1},
{a: 3},
{a: 12},
{a: 13},
{a: 18},
{a: 23},
{a: 32},
{a: 32},
];
let last = Platform.ArrayUtilities.nearestIndexFromEnd(ascEntries, value => value.a < 40);
assert.strictEqual(last, ascEntries.length - 1);
const descEntries = [
{a: 32},
{a: 23},
{a: 18},
{a: 13},
{a: 12},
{a: 3},
{a: 1},
{a: 1},
];
last = Platform.ArrayUtilities.nearestIndexFromEnd(descEntries, value => value.a > 0);
assert.strictEqual(last, descEntries.length - 1);
});
it('works on exact values', () => {
const ascEntries = [
{a: 1},
{a: 2},
{a: 3},
{a: 3},
{a: 4},
{a: 5},
{a: 6},
];
const predicateFunc = (value: {a: number}) => value.a <= 3;
// Odd number of entries.
// Note that the predicate is allowing an the exact match.
let nearest = Platform.ArrayUtilities.nearestIndexFromEnd(ascEntries, predicateFunc);
assert.strictEqual(nearest, 3);
// Even number of entries.
ascEntries.push({a: 7});
nearest = Platform.ArrayUtilities.nearestIndexFromEnd(ascEntries, predicateFunc);
assert.strictEqual(nearest, 3);
const descEntries = [
{a: 6},
{a: 5},
{a: 4},
{a: 3},
{a: 3},
{a: 2},
{a: 1},
];
// Note that the predicate is allowing an the exact match.
const gePredicate = (value: {a: number}) => value.a >= 3;
// Odd number of entries.
nearest = Platform.ArrayUtilities.nearestIndexFromEnd(descEntries, gePredicate);
assert.strictEqual(nearest, 4);
// Even number of entries.
descEntries.push({a: 7});
nearest = Platform.ArrayUtilities.nearestIndexFromEnd(descEntries, gePredicate);
assert.strictEqual(nearest, 4);
});
});
describe('Finding the first item in the array where predicate is true', () => {
it('works with an even number of entries', () => {
const ascEntries = [{a: 1}, {a: 3}, {a: 12}, {a: 12}, {a: 13}, {a: 18}, {a: 23}, {a: 24}];
let nearest = Platform.ArrayUtilities.nearestIndexFromBeginning(ascEntries, value => value.a > 7);
assert.strictEqual(nearest, 2);
const descEntries = [{a: 23}, {a: 18}, {a: 13}, {a: 12}, {a: 12}, {a: 3}, {a: 1}, {a: 0}];
nearest = Platform.ArrayUtilities.nearestIndexFromBeginning(descEntries, value => value.a < 13);
assert.strictEqual(nearest, 3);
});
it('works with an odd number of entries', () => {
const ascEntries = [
{a: 1},
{a: 3},
{a: 12},
{a: 13},
{a: 18},
{a: 23},
{a: 32},
{a: 32},
{a: 33},
];
let nearest = Platform.ArrayUtilities.nearestIndexFromBeginning(ascEntries, value => value.a > 31);
assert.strictEqual(nearest, 6);
const descEntries = [
{a: 33},
{a: 32},
{a: 23},
{a: 23},
{a: 18},
{a: 23},
{a: 32},
{a: 3},
{a: 1},
];
nearest = Platform.ArrayUtilities.nearestIndexFromBeginning(descEntries, value => value.a < 32);
assert.strictEqual(nearest, 2);
});
it('returns null if there are no matches at all', () => {
const entries = [
{a: 1},
{a: 3},
{a: 12},
{a: 13},
{a: 18},
{a: 23},
{a: 32},
];
const predicate = (value: {a: number}) => value.a > 33;
const nearest = Platform.ArrayUtilities.nearestIndexFromBeginning(entries, predicate);
assert.isNull(nearest);
});
it('works when the result is the first item', () => {
const ascEntries = [
{a: 1},
{a: 1},
{a: 3},
{a: 12},
{a: 13},
{a: 18},
{a: 23},
{a: 32},
];
const greaterThanPredicate = (value: {a: number}) => value.a > 0;
let first = Platform.ArrayUtilities.nearestIndexFromBeginning(ascEntries, greaterThanPredicate);
assert.strictEqual(first, 0);
const descEntries = [
{a: 32},
{a: 32},
{a: 23},
{a: 18},
{a: 13},
{a: 12},
{a: 5},
{a: 5},
];
const predicate = (value: {a: number}) => value.a < 64;
first = Platform.ArrayUtilities.nearestIndexFromBeginning(descEntries, predicate);
assert.strictEqual(first, 0);
});
it('works on exact values', () => {
const ascEntries = [
{a: 1},
{a: 2},
{a: 3},
{a: 3},
{a: 4},
{a: 5},
{a: 6},
];
// Note that the predicate is allowing an the exact match.
const gePredicate = (value: {a: number}) => value.a >= 3;
// Even number of entries.
let nearest = Platform.ArrayUtilities.nearestIndexFromBeginning(ascEntries, gePredicate);
assert.strictEqual(nearest, 2);
// Odd number of entries.
ascEntries.push({a: 7});
nearest = Platform.ArrayUtilities.nearestIndexFromBeginning(ascEntries, gePredicate);
assert.strictEqual(nearest, 2);
const descEntries = [
{a: 6},
{a: 5},
{a: 4},
{a: 3},
{a: 3},
{a: 2},
{a: 1},
];
// Note that the predicate is allowing an the exact match.
const predicateFunc = (value: {a: number}) => value.a <= 3;
// Even number of entries.
nearest = Platform.ArrayUtilities.nearestIndexFromBeginning(descEntries, predicateFunc);
assert.strictEqual(nearest, 3);
// Odd number of entries.
descEntries.push({a: 7});
nearest = Platform.ArrayUtilities.nearestIndexFromBeginning(descEntries, predicateFunc);
assert.strictEqual(nearest, 3);
});
});
});
describe('arrayDoesNotContainNullOrUndefined', () => {
it('should return false when array contains null', () => {
assert.isFalse(Platform.ArrayUtilities.arrayDoesNotContainNullOrUndefined([1, null, 2]));
});
it('should return false when array contains undefined', () => {
assert.isFalse(Platform.ArrayUtilities.arrayDoesNotContainNullOrUndefined([1, undefined, 2]));
});
it('should return true when array does not contain undefined and null', () => {
assert.isTrue(Platform.ArrayUtilities.arrayDoesNotContainNullOrUndefined([1, 2]));
});
});
});