forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialize_deserialize_test.js
77 lines (67 loc) · 1.85 KB
/
serialize_deserialize_test.js
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
70
71
72
73
74
75
76
77
// 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: (index ${index}) ${a1[index]} !== ${a2[index]}`);
}
}
}
function main() {
const emptyString = "";
const emptyStringSerialized = [255, 15, 34, 0];
assertArrayEquals(
Deno.core.opSync("op_serialize", emptyString),
emptyStringSerialized,
);
assert(
Deno.core.opSync(
"op_deserialize",
new Uint8Array(emptyStringSerialized),
) ===
emptyString,
);
const primitiveValueArray = ["test", "a", null, undefined];
// deno-fmt-ignore
const primitiveValueArraySerialized = [
255, 15, 65, 4, 34, 4, 116, 101, 115, 116,
34, 1, 97, 48, 95, 36, 0, 4,
];
assertArrayEquals(
Deno.core.opSync("op_serialize", primitiveValueArray),
primitiveValueArraySerialized,
);
assertArrayEquals(
Deno.core.opSync(
"op_deserialize",
new Uint8Array(primitiveValueArraySerialized),
),
primitiveValueArray,
);
const circularObject = { test: null, test2: "dd", test3: "aa" };
circularObject.test = circularObject;
// deno-fmt-ignore
const circularObjectSerialized = [
255, 15, 111, 34, 4, 116, 101, 115,
116, 94, 0, 34, 5, 116, 101, 115,
116, 50, 34, 2, 100, 100, 34, 5,
116, 101, 115, 116, 51, 34, 2, 97,
97, 123, 3,
];
assertArrayEquals(
Deno.core.opSync("op_serialize", circularObject),
circularObjectSerialized,
);
const deserializedCircularObject = Deno.core.opSync(
"op_deserialize",
new Uint8Array(circularObjectSerialized),
);
assert(deserializedCircularObject.test == deserializedCircularObject);
}
main();