2024-01-29 08:55:56 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2024-05-21 12:54:22 +00:00
|
|
|
import { assertFp, backTrace, createCommon, createFp, diff } from "./diff.ts";
|
|
|
|
import { assertEquals, assertThrows } from "@std/assert";
|
2024-01-29 08:55:56 +00:00
|
|
|
|
|
|
|
Deno.test({
|
2024-02-08 22:00:55 +00:00
|
|
|
name: "diff() with empty values",
|
2024-01-29 08:55:56 +00:00
|
|
|
fn() {
|
|
|
|
assertEquals(diff([], []), []);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2024-02-08 22:00:55 +00:00
|
|
|
name: 'diff() "a" vs "b"',
|
2024-01-29 08:55:56 +00:00
|
|
|
fn() {
|
|
|
|
assertEquals(diff(["a"], ["b"]), [
|
2024-05-09 06:14:09 +00:00
|
|
|
{ type: "removed", value: "a" },
|
|
|
|
{ type: "added", value: "b" },
|
2024-01-29 08:55:56 +00:00
|
|
|
]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2024-02-08 22:00:55 +00:00
|
|
|
name: 'diff() "a" vs "a"',
|
2024-01-29 08:55:56 +00:00
|
|
|
fn() {
|
2024-05-09 06:14:09 +00:00
|
|
|
assertEquals(diff(["a"], ["a"]), [{ type: "common", value: "a" }]);
|
2024-01-29 08:55:56 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2024-02-08 22:00:55 +00:00
|
|
|
name: 'diff() "a" vs ""',
|
2024-01-29 08:55:56 +00:00
|
|
|
fn() {
|
2024-05-09 06:14:09 +00:00
|
|
|
assertEquals(diff(["a"], []), [{ type: "removed", value: "a" }]);
|
2024-01-29 08:55:56 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2024-02-08 22:00:55 +00:00
|
|
|
name: 'diff() "" vs "a"',
|
2024-01-29 08:55:56 +00:00
|
|
|
fn() {
|
2024-05-09 06:14:09 +00:00
|
|
|
assertEquals(diff([], ["a"]), [{ type: "added", value: "a" }]);
|
2024-01-29 08:55:56 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2024-02-08 22:00:55 +00:00
|
|
|
name: 'diff() "a" vs "a, b"',
|
2024-01-29 08:55:56 +00:00
|
|
|
fn() {
|
|
|
|
assertEquals(diff(["a"], ["a", "b"]), [
|
2024-05-09 06:14:09 +00:00
|
|
|
{ type: "common", value: "a" },
|
|
|
|
{ type: "added", value: "b" },
|
2024-01-29 08:55:56 +00:00
|
|
|
]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-05-21 12:54:22 +00:00
|
|
|
Deno.test({
|
|
|
|
name: 'diff() "a, x, c" vs "a, b, c"',
|
|
|
|
fn() {
|
|
|
|
assertEquals(diff(["a", "x", "c"], ["a", "b", "c"]), [
|
|
|
|
{ type: "common", value: "a" },
|
|
|
|
{ type: "removed", value: "x" },
|
|
|
|
{ type: "added", value: "b" },
|
|
|
|
{ type: "common", value: "c" },
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-01-29 08:55:56 +00:00
|
|
|
Deno.test({
|
2024-02-08 22:00:55 +00:00
|
|
|
name: 'diff() "strength" vs "string"',
|
2024-01-29 08:55:56 +00:00
|
|
|
fn() {
|
|
|
|
assertEquals(diff(Array.from("strength"), Array.from("string")), [
|
2024-05-09 06:14:09 +00:00
|
|
|
{ type: "common", value: "s" },
|
|
|
|
{ type: "common", value: "t" },
|
|
|
|
{ type: "common", value: "r" },
|
|
|
|
{ type: "removed", value: "e" },
|
|
|
|
{ type: "added", value: "i" },
|
|
|
|
{ type: "common", value: "n" },
|
|
|
|
{ type: "common", value: "g" },
|
|
|
|
{ type: "removed", value: "t" },
|
|
|
|
{ type: "removed", value: "h" },
|
2024-01-29 08:55:56 +00:00
|
|
|
]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2024-02-08 22:00:55 +00:00
|
|
|
name: 'diff() "strength" vs ""',
|
2024-01-29 08:55:56 +00:00
|
|
|
fn() {
|
|
|
|
assertEquals(diff(Array.from("strength"), Array.from("")), [
|
2024-05-09 06:14:09 +00:00
|
|
|
{ type: "removed", value: "s" },
|
|
|
|
{ type: "removed", value: "t" },
|
|
|
|
{ type: "removed", value: "r" },
|
|
|
|
{ type: "removed", value: "e" },
|
|
|
|
{ type: "removed", value: "n" },
|
|
|
|
{ type: "removed", value: "g" },
|
|
|
|
{ type: "removed", value: "t" },
|
|
|
|
{ type: "removed", value: "h" },
|
2024-01-29 08:55:56 +00:00
|
|
|
]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2024-02-08 22:00:55 +00:00
|
|
|
name: 'diff() "" vs "strength"',
|
2024-01-29 08:55:56 +00:00
|
|
|
fn() {
|
|
|
|
assertEquals(diff(Array.from(""), Array.from("strength")), [
|
2024-05-09 06:14:09 +00:00
|
|
|
{ type: "added", value: "s" },
|
|
|
|
{ type: "added", value: "t" },
|
|
|
|
{ type: "added", value: "r" },
|
|
|
|
{ type: "added", value: "e" },
|
|
|
|
{ type: "added", value: "n" },
|
|
|
|
{ type: "added", value: "g" },
|
|
|
|
{ type: "added", value: "t" },
|
|
|
|
{ type: "added", value: "h" },
|
2024-01-29 08:55:56 +00:00
|
|
|
]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2024-02-08 22:00:55 +00:00
|
|
|
name: 'diff() "abc", "c" vs "abc", "bcd", "c"',
|
2024-01-29 08:55:56 +00:00
|
|
|
fn() {
|
|
|
|
assertEquals(diff(["abc", "c"], ["abc", "bcd", "c"]), [
|
2024-05-09 06:14:09 +00:00
|
|
|
{ type: "common", value: "abc" },
|
|
|
|
{ type: "added", value: "bcd" },
|
|
|
|
{ type: "common", value: "c" },
|
2024-01-29 08:55:56 +00:00
|
|
|
]);
|
|
|
|
},
|
|
|
|
});
|
2024-05-21 12:54:22 +00:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "assertFp()",
|
|
|
|
fn() {
|
|
|
|
const fp = { y: 0, id: 0 };
|
|
|
|
assertEquals(assertFp(fp), undefined);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "assertFp() throws",
|
|
|
|
fn() {
|
2024-08-22 05:35:34 +00:00
|
|
|
assertThrows(
|
|
|
|
() => assertFp({ id: 0 }),
|
|
|
|
Error,
|
|
|
|
"Unexpected value, expected 'FarthestPoint': received object",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => assertFp({ y: 0 }),
|
|
|
|
Error,
|
|
|
|
"Unexpected value, expected 'FarthestPoint': received object",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => assertFp(undefined),
|
|
|
|
Error,
|
|
|
|
"Unexpected value, expected 'FarthestPoint': received undefined",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => assertFp(null),
|
|
|
|
Error,
|
|
|
|
"Unexpected value, expected 'FarthestPoint': received object",
|
|
|
|
);
|
2024-05-21 12:54:22 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "backTrace()",
|
|
|
|
fn() {
|
|
|
|
assertEquals(
|
|
|
|
backTrace([], [], { y: 0, id: 0 }, false, new Uint32Array(0), 0),
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
backTrace(["a"], ["b"], { y: 1, id: 3 }, false, new Uint32Array(10), 5),
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "createCommon()",
|
|
|
|
fn() {
|
|
|
|
assertEquals(createCommon([], []), []);
|
|
|
|
assertEquals(createCommon([1], []), []);
|
|
|
|
assertEquals(createCommon([], [1]), []);
|
|
|
|
assertEquals(createCommon([1], [1]), [1]);
|
|
|
|
assertEquals(createCommon([1, 2], [1]), [1]);
|
|
|
|
assertEquals(createCommon([1], [1, 2]), [1]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "createFp()",
|
|
|
|
fn() {
|
|
|
|
assertEquals(
|
|
|
|
createFp(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
new Uint32Array(0),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
{ y: -1, id: 0 },
|
|
|
|
{ y: -1, id: 0 },
|
|
|
|
),
|
|
|
|
{ y: 0, id: 0 },
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: 'createFp() "isAdding"',
|
|
|
|
fn() {
|
|
|
|
assertEquals(
|
|
|
|
createFp(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
new Uint32Array(0),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
{ y: 0, id: 0 },
|
|
|
|
{ y: -1, id: 0 },
|
|
|
|
),
|
|
|
|
{ y: 0, id: 1 },
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: 'createFp() "!isAdding"',
|
|
|
|
fn() {
|
|
|
|
assertEquals(
|
|
|
|
createFp(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
new Uint32Array(0),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
{ y: -1, id: 0 },
|
|
|
|
{ y: 0, id: 0 },
|
|
|
|
),
|
|
|
|
{ y: -1, id: 1 },
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "createFp() throws",
|
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => createFp(0, 0, new Uint32Array(0), 0, 0),
|
|
|
|
Error,
|
|
|
|
"Unexpected missing FarthestPoint",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|