deno.land / std@0.224.0 / msgpack / encode_test.ts

encode_test.ts
View Documentation
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertThrows } from "../assert/mod.ts";import * as path from "../path/mod.ts";import { decode, encode } from "./mod.ts";
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));const testdataDir = path.resolve(moduleDir, "testdata");
Deno.test("encode() handles testdata", () => { const one = JSON.parse( Deno.readTextFileSync(path.join(testdataDir, "1.json")), ); assertEquals(decode(encode(one)), one);
const two = JSON.parse( Deno.readTextFileSync(path.join(testdataDir, "2.json")), ); assertEquals(decode(encode(two)), two);
const three = JSON.parse( Deno.readTextFileSync(path.join(testdataDir, "3.json")), ); assertEquals(decode(encode(three)), three);
const four = JSON.parse( Deno.readTextFileSync(path.join(testdataDir, "4.json")), ); assertEquals(decode(encode(four)), four);
const five = JSON.parse( Deno.readTextFileSync(path.join(testdataDir, "5.json")), ); assertEquals(decode(encode(five)), five);});
Deno.test("encode() handles positive numbers", () => { assertEquals(encode(1), Uint8Array.of(1)); assertEquals(decode(encode(1)), 1);
assertEquals(encode(255), Uint8Array.of(0xcc, 255)); assertEquals(decode(encode(255)), 255);
assertEquals(encode(2000), Uint8Array.of(0xcd, 7, 208)); assertEquals(decode(encode(2000)), 2000);
assertEquals(encode(70000), Uint8Array.of(0xce, 0, 1, 17, 112)); assertEquals(decode(encode(70000)), 70000);
assertEquals( encode(20000000000), Uint8Array.of(0xcb, 66, 18, 160, 95, 32, 0, 0, 0), ); assertEquals(decode(encode(20000000000)), 20000000000);});
Deno.test("encode() handles negative numbers", () => { assertEquals(encode(-1), Uint8Array.of(255)); assertEquals(decode(encode(-1)), -1);
assertEquals(encode(-127), Uint8Array.of(0xd0, 129)); assertEquals(decode(encode(-127)), -127);
assertEquals(encode(-1000), Uint8Array.of(0xd1, 252, 24)); assertEquals(decode(encode(-1000)), -1000);
assertEquals(encode(-60000), Uint8Array.of(0xd2, 255, 255, 21, 160)); assertEquals(decode(encode(-60000)), -60000);
assertEquals( encode(-600000000000), Uint8Array.of(0xcb, 194, 97, 118, 89, 46, 0, 0, 0), ); assertEquals(decode(encode(-600000000000)), -600000000000);});
Deno.test("encode() handles floats", () => { assertEquals( encode(0.3), Uint8Array.of(0xcb, 63, 211, 51, 51, 51, 51, 51, 51), ); assertEquals(decode(encode(0.3)), 0.3);});
Deno.test("encode() handles bigints", () => { assertEquals(encode(0n), Uint8Array.of(0xcf, 0, 0, 0, 0, 0, 0, 0, 0)); assertEquals(decode(encode(0n)), 0n); assertEquals( encode(-10n), Uint8Array.of(0xd3, 255, 255, 255, 255, 255, 255, 255, 246), ); assertEquals(decode(encode(-10n)), -10n); assertEquals(encode(10n), Uint8Array.of(0xcf, 0, 0, 0, 0, 0, 0, 0, 10)); assertEquals(decode(encode(10n)), 10n); assertEquals( encode(9999999999999999999n), Uint8Array.of(0xcf, 138, 199, 35, 4, 137, 231, 255, 255), ); assertEquals(decode(encode(9999999999999999999n)), 9999999999999999999n);
assertThrows(() => encode(99999999999999999999999n)); assertThrows(() => encode(-99999999999999999999999n));});
Deno.test("encode() handles strings", () => { assertEquals( encode("hello world"), Uint8Array.of(171, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100), ); assertEquals(decode(encode("hello world")), "hello world");
const mediumString = "a".repeat(255); assertEquals( encode(mediumString), Uint8Array.of(0xd9, 255, ...new Array(mediumString.length).fill(97)), ); assertEquals(decode(encode(mediumString)), mediumString);
const longString = "a".repeat(256); assertEquals( encode(longString), Uint8Array.of(0xda, 1, 0, ...new Array(longString.length).fill(97)), ); assertEquals(decode(encode(longString)), longString);
const reallyLongString = "a".repeat(65536); assertEquals( encode(reallyLongString), Uint8Array.of( 0xdb, 0, 1, 0, 0, ...new Array(reallyLongString.length).fill(97), ), ); assertEquals(decode(encode(reallyLongString)), reallyLongString);});
Deno.test("encode() handles arrays", () => { const arr0: never[] = []; assertEquals(decode(encode(arr0)), arr0);
const arr1 = [1, 2, 3, 4, 5, 6]; assertEquals(decode(encode(arr1)), arr1);
const arr2 = new Array(256).fill(0); assertEquals(decode(encode(arr2)), arr2);
const nestedArr = [[1, 2, 3], [1, 2], 5]; assertEquals(decode(encode(nestedArr)), nestedArr);});
Deno.test("encode() handles maps", () => { const map0 = {}; assertEquals(decode(encode(map0)), map0);
const map1 = { "a": 0, "b": 2, "c": "three", "d": null }; assertEquals(decode(encode(map1)), map1);
const nestedMap = { "a": -1, "b": 2, "c": "three", "d": null, "e": map1 }; assertEquals(decode(encode(nestedMap)), nestedMap);});
Deno.test("encode() handles huge array with 100k objects", () => { const bigArray = []; for (let i = 0; i < 100000; i++) { bigArray.push({ a: { i: `${i}` }, i: i }); } const bigObject = { a: bigArray };
assertEquals(decode(encode(bigObject)), bigObject);});
Deno.test("encode() handles huge object with 100k properties", () => { const bigObject = {}; for (let i = 0; i < 100000; i++) { const _ = Object.defineProperty(bigObject, `prop_${i}`, { value: i, enumerable: true, }); } assertEquals(decode(encode(bigObject)), bigObject);});
std

Version Info

Tagged at
6 months ago