deno/core/encode_decode_test.js
Bartek Iwańczuk 87d2ba42bf
perf: Optimize TextEncoder and TextDecoder (#4430)
* add tests for "Deno.core.encode" and "Deno.core.decode" for empty inputs
* use "Deno.core.encode" in "TextEncoder"
* use "Deno.core.decode" in "TextDecoder"
* remove "core_decode" and "core_encode" benchmarks
2020-03-19 21:31:56 +01:00

46 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
function assertArrayEquals(a1, a2) {
if (a1.length !== a2.length) throw Error("assert");
for (const index in a1) {
if (a1[index] !== a2[index]) {
throw Error("assert");
}
}
}
function main() {
// prettier-ignore
const fixture1 = [
0xf0, 0x9d, 0x93, 0xbd,
0xf0, 0x9d, 0x93, 0xae,
0xf0, 0x9d, 0x94, 0x81,
0xf0, 0x9d, 0x93, 0xbd
];
// prettier-ignore
const fixture2 = [
72, 101, 108, 108,
111, 32, 239, 191,
189, 239, 191, 189,
32, 87, 111, 114,
108, 100
];
const empty = Deno.core.encode("");
if (empty.length !== 0) throw new Error("assert");
assertArrayEquals(Array.from(Deno.core.encode("𝓽𝓮𝔁𝓽")), fixture1);
assertArrayEquals(
Array.from(Deno.core.encode("Hello \udc12\ud834 World")),
fixture2
);
const emptyBuf = Deno.core.decode(new Uint8Array(0));
if (emptyBuf !== "") throw new Error("assert");
assert(Deno.core.decode(new Uint8Array(fixture1)) === "𝓽𝓮𝔁𝓽");
assert(Deno.core.decode(new Uint8Array(fixture2)) === "Hello <20><> World");
}
main();