2017-12-19 21:01:29 +02:00
|
|
|
require('app-module-path').addPath(__dirname);
|
|
|
|
|
|
|
|
const { time } = require('lib/time-utils.js');
|
|
|
|
const { fileContentEqual, setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync } = require('test-utils.js');
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove array elements', async (done) => {
|
|
|
|
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);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2018-01-29 22:51:14 +02:00
|
|
|
it('should find items using binary search', async (done) => {
|
|
|
|
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);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2018-05-10 13:02:39 +02:00
|
|
|
it('should compare arrays', async (done) => {
|
|
|
|
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);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2017-12-19 21:01:29 +02:00
|
|
|
});
|