2022-01-07 22:09:52 -05:00
|
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
|
"use strict";
|
2020-11-03 10:19:29 -05:00
|
|
|
|
function assert(cond) {
|
|
|
|
|
if (!cond) {
|
|
|
|
|
throw Error("assert");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-15 10:31:55 -04:00
|
|
|
|
|
|
|
|
|
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() {
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2020-03-15 10:31:55 -04:00
|
|
|
|
const fixture1 = [
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xae,
|
|
|
|
|
0xf0, 0x9d, 0x94, 0x81,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd
|
|
|
|
|
];
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
|
|
|
|
const fixture2 = [
|
|
|
|
|
72, 101, 108, 108,
|
|
|
|
|
111, 32, 239, 191,
|
|
|
|
|
189, 239, 191, 189,
|
|
|
|
|
32, 87, 111, 114,
|
|
|
|
|
108, 100
|
2020-03-15 10:31:55 -04:00
|
|
|
|
];
|
|
|
|
|
|
2022-08-11 09:56:56 -04:00
|
|
|
|
const empty = Deno.core.ops.op_encode("");
|
2020-03-19 16:31:56 -04:00
|
|
|
|
if (empty.length !== 0) throw new Error("assert");
|
|
|
|
|
|
2020-03-15 10:31:55 -04:00
|
|
|
|
assertArrayEquals(
|
2022-08-11 09:56:56 -04:00
|
|
|
|
Array.from(Deno.core.ops.op_encode("𝓽𝓮𝔁𝓽")),
|
2022-06-07 05:25:10 -04:00
|
|
|
|
fixture1,
|
|
|
|
|
);
|
|
|
|
|
assertArrayEquals(
|
2022-08-11 09:56:56 -04:00
|
|
|
|
Array.from(Deno.core.ops.op_encode("Hello \udc12\ud834 World")),
|
2020-07-14 15:24:17 -04:00
|
|
|
|
fixture2,
|
2020-03-15 10:31:55 -04:00
|
|
|
|
);
|
|
|
|
|
|
2022-08-11 09:56:56 -04:00
|
|
|
|
const emptyBuf = Deno.core.ops.op_decode(new Uint8Array(0));
|
2020-03-19 16:31:56 -04:00
|
|
|
|
if (emptyBuf !== "") throw new Error("assert");
|
|
|
|
|
|
2022-08-11 09:56:56 -04:00
|
|
|
|
assert(Deno.core.ops.op_decode(new Uint8Array(fixture1)) === "𝓽𝓮𝔁𝓽");
|
2022-06-07 05:25:10 -04:00
|
|
|
|
assert(
|
2022-08-11 09:56:56 -04:00
|
|
|
|
Deno.core.ops.op_decode(new Uint8Array(fixture2)) ===
|
2022-06-07 05:25:10 -04:00
|
|
|
|
"Hello <20><> World",
|
|
|
|
|
);
|
2020-09-09 14:52:11 -04:00
|
|
|
|
|
|
|
|
|
// See https://github.com/denoland/deno/issues/6649
|
|
|
|
|
let thrown = false;
|
|
|
|
|
try {
|
2022-08-11 09:56:56 -04:00
|
|
|
|
Deno.core.ops.op_decode(new Uint8Array(2 ** 29));
|
2020-09-09 14:52:11 -04:00
|
|
|
|
} catch (e) {
|
|
|
|
|
thrown = true;
|
|
|
|
|
assert(e instanceof RangeError);
|
|
|
|
|
assert(e.message === "string too long");
|
|
|
|
|
}
|
|
|
|
|
assert(thrown);
|
2020-03-15 10:31:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main();
|