2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-11-24 07:36:10 +00:00
|
|
|
|
2024-01-16 18:41:34 +00:00
|
|
|
import { runLengthDecode, runLengthEncode } from "./_run_length.ts";
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals, assertThrows } from "@std/assert";
|
2023-11-24 07:36:10 +00:00
|
|
|
|
|
|
|
const runLengthTestCases: {
|
|
|
|
list: number[];
|
|
|
|
compressed: { d: string; r: string };
|
|
|
|
testName: string;
|
|
|
|
}[] = [
|
|
|
|
{
|
|
|
|
list: [1, 2, 3, 4, 5],
|
|
|
|
compressed: { d: "AQIDBAU=", r: "AQEBAQE=" },
|
|
|
|
testName: "return expected value if input is normal value",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
list: [1, 1, 1, 1],
|
|
|
|
compressed: { d: "AQ==", r: "BA==" },
|
|
|
|
testName: "return expected value if input includes an continuous value",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
list: [],
|
|
|
|
compressed: { d: "", r: "" },
|
|
|
|
testName: "return expected value if input is empty",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
Deno.test("runLengthEncode()", async (t) => {
|
|
|
|
for (const { list, compressed, testName } of runLengthTestCases) {
|
|
|
|
await t.step(`runLengthEncode() ${testName}`, () => {
|
|
|
|
const encoded = runLengthEncode(list);
|
|
|
|
assertEquals(encoded, compressed);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await t.step(
|
|
|
|
`runLengthEncode() throw an error if input is an array containing more than 256 numbers`,
|
|
|
|
() => {
|
|
|
|
assertThrows(() => runLengthEncode([1, 2, 3, 256]));
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
await t.step(
|
|
|
|
`runLengthEncode() throw an error if the input is an array longer than 256`,
|
|
|
|
() => {
|
|
|
|
assertThrows(() =>
|
|
|
|
runLengthEncode([...Array.from({ length: 256 }, () => 0)])
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("runLengthDecode()", async (t) => {
|
|
|
|
for (const { list, compressed, testName } of runLengthTestCases) {
|
|
|
|
await t.step(`runLengthDecode() ${testName}`, () => {
|
|
|
|
const decoded = runLengthDecode(compressed);
|
|
|
|
assertEquals(decoded, new Uint8Array(list));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await t.step(`runLengthDecode() throw an error if input is wrong`, () => {
|
|
|
|
assertThrows(() => runLengthDecode({ d: "wrong", r: "wrong" }));
|
|
|
|
});
|
|
|
|
});
|