summaryrefslogtreecommitdiff
path: root/core/encode_decode_test.js
blob: 5165b196c9f04f8d3e91deaf50645ef22f6cfd98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
"use strict";
function assert(cond) {
  if (!cond) {
    throw Error("assert");
  }
}

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() {
  // deno-fmt-ignore
  const fixture1 = [
    0xf0, 0x9d, 0x93, 0xbd,
    0xf0, 0x9d, 0x93, 0xae,
    0xf0, 0x9d, 0x94, 0x81,
    0xf0, 0x9d, 0x93, 0xbd
  ];
  // deno-fmt-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.opSync("op_encode", "");
  if (empty.length !== 0) throw new Error("assert");

  assertArrayEquals(
    Array.from(Deno.core.opSync("op_encode", "𝓽𝓮𝔁𝓽")),
    fixture1,
  );
  assertArrayEquals(
    Array.from(Deno.core.opSync("op_encode", "Hello \udc12\ud834 World")),
    fixture2,
  );

  const emptyBuf = Deno.core.opSync("op_decode", new Uint8Array(0));
  if (emptyBuf !== "") throw new Error("assert");

  assert(Deno.core.opSync("op_decode", new Uint8Array(fixture1)) === "𝓽𝓮𝔁𝓽");
  assert(
    Deno.core.opSync("op_decode", new Uint8Array(fixture2)) ===
      "Hello �� World",
  );

  // See https://github.com/denoland/deno/issues/6649
  let thrown = false;
  try {
    Deno.core.opSync("op_decode", new Uint8Array(2 ** 29));
  } catch (e) {
    thrown = true;
    assert(e instanceof RangeError);
    assert(e.message === "string too long");
  }
  assert(thrown);
}

main();