2018-03-09 17:49:35 +00: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);
|
2017-12-19 19:01:29 +00:00
|
|
|
});
|
|
|
|
|
2018-03-09 17:49:35 +00:00
|
|
|
describe("ArrayUtils", function() {
|
|
|
|
beforeEach(async done => {
|
2017-12-19 19:01:29 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2018-03-09 17:49:35 +00:00
|
|
|
it("should remove array elements", async done => {
|
|
|
|
let a = ["un", "deux", "trois"];
|
|
|
|
a = ArrayUtils.removeElement(a, "deux");
|
2017-12-19 19:01:29 +00:00
|
|
|
|
2018-03-09 17:49:35 +00:00
|
|
|
expect(a[0]).toBe("un");
|
|
|
|
expect(a[1]).toBe("trois");
|
2017-12-19 19:01:29 +00:00
|
|
|
expect(a.length).toBe(2);
|
|
|
|
|
2018-03-09 17:49:35 +00:00
|
|
|
a = ["un", "deux", "trois"];
|
|
|
|
a = ArrayUtils.removeElement(a, "not in there");
|
2017-12-19 19:01:29 +00:00
|
|
|
expect(a.length).toBe(3);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2018-03-09 17:49:35 +00: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!
|
2018-01-29 20:51:14 +00:00
|
|
|
items.sort();
|
2018-03-09 17:49:35 +00:00
|
|
|
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);
|
2018-01-29 20:51:14 +00:00
|
|
|
|
|
|
|
items = [];
|
2018-03-09 17:49:35 +00:00
|
|
|
expect(ArrayUtils.binarySearch(items, "aaa")).toBe(-1);
|
2018-01-29 20:51:14 +00:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2018-03-09 17:49:35 +00:00
|
|
|
});
|