2021-05-21 15:17:21 +02:00
|
|
|
const ArrayUtils = require('./ArrayUtils');
|
2017-12-19 21:01:29 +02:00
|
|
|
|
2018-01-30 23:10:54 +02:00
|
|
|
describe('ArrayUtils', function() {
|
2017-12-19 21:01:29 +02:00
|
|
|
|
2022-11-15 12:23:50 +02:00
|
|
|
|
2017-12-19 21:01:29 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should remove array elements', (async () => {
|
2017-12-19 21:01:29 +02:00
|
|
|
let a = ['un', 'deux', 'trois'];
|
|
|
|
a = ArrayUtils.removeElement(a, 'deux');
|
|
|
|
|
|
|
|
expect(a[0]).toBe('un');
|
|
|
|
expect(a[1]).toBe('trois');
|
|
|
|
expect(a.length).toBe(2);
|
|
|
|
|
|
|
|
a = ['un', 'deux', 'trois'];
|
|
|
|
a = ArrayUtils.removeElement(a, 'not in there');
|
|
|
|
expect(a.length).toBe(3);
|
2019-09-24 00:23:10 +02:00
|
|
|
}));
|
2017-12-19 21:01:29 +02:00
|
|
|
|
2022-05-26 16:57:44 +02:00
|
|
|
it('should pull array elements', (async () => {
|
|
|
|
expect(ArrayUtils.pull(['a', 'b', 'c', 'a', 'b', 'c'], 'a')).toEqual(['b', 'c', 'b', 'c']);
|
|
|
|
expect(ArrayUtils.pull(['b', 'c', 'b', 'c'], 'a')).toEqual(['b', 'c', 'b', 'c']);
|
|
|
|
expect(ArrayUtils.pull(['a', 'b', 'c', 'a', 'b', 'c'], 'a', 'c')).toEqual(['b', 'b']);
|
|
|
|
expect(ArrayUtils.pull([], 'a')).toEqual([]);
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should find items using binary search', (async () => {
|
2018-01-29 22:51:14 +02:00
|
|
|
let items = ['aaa', 'ccc', 'bbb'];
|
|
|
|
expect(ArrayUtils.binarySearch(items, 'bbb')).toBe(-1); // Array not sorted!
|
|
|
|
items.sort();
|
|
|
|
expect(ArrayUtils.binarySearch(items, 'bbb')).toBe(1);
|
|
|
|
expect(ArrayUtils.binarySearch(items, 'ccc')).toBe(2);
|
|
|
|
expect(ArrayUtils.binarySearch(items, 'oops')).toBe(-1);
|
|
|
|
expect(ArrayUtils.binarySearch(items, 'aaa')).toBe(0);
|
|
|
|
|
|
|
|
items = [];
|
|
|
|
expect(ArrayUtils.binarySearch(items, 'aaa')).toBe(-1);
|
2019-09-24 00:23:10 +02:00
|
|
|
}));
|
2018-01-29 22:51:14 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should compare arrays', (async () => {
|
2018-05-10 13:02:39 +02:00
|
|
|
expect(ArrayUtils.contentEquals([], [])).toBe(true);
|
|
|
|
expect(ArrayUtils.contentEquals(['a'], ['a'])).toBe(true);
|
|
|
|
expect(ArrayUtils.contentEquals(['b', 'a'], ['a', 'b'])).toBe(true);
|
|
|
|
expect(ArrayUtils.contentEquals(['b'], ['a', 'b'])).toBe(false);
|
2019-09-24 00:23:10 +02:00
|
|
|
}));
|
2018-05-10 13:02:39 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should merge overlapping intervals', (async () => {
|
2020-03-28 15:05:00 +02:00
|
|
|
const testCases = [
|
|
|
|
[
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[[0, 50]],
|
|
|
|
[[0, 50]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[[0, 20], [20, 30]],
|
|
|
|
[[0, 30]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[[0, 10], [10, 50], [15, 30], [20, 80], [80, 95]],
|
|
|
|
[[0, 95]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[[0, 5], [0, 10], [25, 35], [30, 60], [50, 60], [85, 100]],
|
|
|
|
[[0, 10], [25, 60], [85, 100]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[[0, 5], [10, 40], [35, 50], [35, 75], [50, 60], [80, 85], [80, 90]],
|
|
|
|
[[0, 5], [10, 75], [80, 90]],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
testCases.forEach((t, i) => {
|
|
|
|
const intervals = t[0];
|
|
|
|
const expected = t[1];
|
|
|
|
|
|
|
|
const actual = ArrayUtils.mergeOverlappingIntervals(intervals, intervals.length);
|
|
|
|
expect(actual).toEqual(expected, `Test case ${i}`);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|