2019-07-30 09:35:42 +02:00
|
|
|
/* eslint-disable no-unused-vars */
|
|
|
|
|
2017-12-19 21:01:29 +02:00
|
|
|
require('app-module-path').addPath(__dirname);
|
|
|
|
|
|
|
|
const { time } = require('lib/time-utils.js');
|
2019-09-24 00:23:10 +02:00
|
|
|
const { asyncTest, fileContentEqual, setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync } = require('test-utils.js');
|
2017-12-19 21:01:29 +02:00
|
|
|
const ArrayUtils = require('lib/ArrayUtils.js');
|
|
|
|
|
|
|
|
process.on('unhandledRejection', (reason, p) => {
|
|
|
|
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
|
|
|
|
});
|
|
|
|
|
2018-01-30 23:10:54 +02:00
|
|
|
describe('ArrayUtils', function() {
|
2017-12-19 21:01:29 +02:00
|
|
|
|
|
|
|
beforeEach(async (done) => {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2019-09-24 00:23:10 +02:00
|
|
|
it('should remove array elements', asyncTest(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
|
|
|
|
2019-09-24 00:23:10 +02:00
|
|
|
it('should find items using binary search', asyncTest(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
|
|
|
|
2019-09-24 00:23:10 +02:00
|
|
|
it('should compare arrays', asyncTest(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-03-28 15:05:00 +02:00
|
|
|
it('should merge overlapping intervals', asyncTest(async () => {
|
|
|
|
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
|
|
|
});
|