-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.test.ts
261 lines (244 loc) · 4.5 KB
/
decode.test.ts
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import type { Equal, Expect } from "@type-challenges/utils";
import { assertEquals, assertIsError } from "assert/mod.ts";
import { decode } from "./decode.ts";
import { RelateRule, StringTransformer } from "./types.ts";
const textDecoder = new TextDecoder();
const textEncoder = new TextEncoder();
export const CustomStringTransformer: StringTransformer = {
encode: (value: string) => textEncoder.encode(value).map((v) => v + 1),
decode: (bytes: Uint8Array) => {
const found = bytes.findIndex((v) => v === 0);
return textDecoder.decode(bytes.map((v) => v - 1).slice(0, found));
},
};
Deno.test("decode, decode simple types", () => {
const rules = {
type: "object",
of: [
{ name: "i8", type: "i8" },
{ name: "u8", type: "u8" },
{ name: "i16", type: "i16" },
{ name: "u16", type: "u16" },
{ name: "i32", type: "i32" },
{ name: "u32", type: "u32" },
{ name: "i64", type: "i64" },
{ name: "u64", type: "u64" },
{ name: "f32", type: "f32" },
{ name: "f64", type: "f64" },
{ name: "string", type: "string", size: 16 },
],
} as const satisfies RelateRule;
const buffer = new Uint8Array([
255,
255,
255,
254,
255,
254,
255,
255,
255,
253,
255,
255,
255,
253,
255,
255,
255,
255,
255,
255,
255,
252,
255,
255,
255,
255,
255,
255,
255,
252,
64,
72,
245,
195,
64,
9,
33,
250,
252,
139,
0,
122,
104,
101,
108,
108,
111,
32,
119,
111,
114,
108,
100,
33,
0,
0,
0,
0,
0, // dangling
0,
0,
]);
const obj = decode(buffer.buffer, rules);
type TestCases = [
Expect<
Equal<
typeof obj,
{
i8: number;
u8: number;
i16: number;
u16: number;
i32: number;
u32: number;
i64: bigint;
u64: bigint;
f32: number;
f64: number;
string: string;
}
>
>,
];
assertEquals(obj, {
i8: -1,
u8: 255,
i16: -2,
u16: 65534,
i32: -3,
u32: 4294967293,
i64: -4n,
u64: 18446744073709551612n,
f32: 3.140000104904175,
f64: 3.141592,
string: "hello world!",
});
});
Deno.test("decode, decode array types", () => {
const rules = {
type: "array",
of: { type: "i32" },
size: 2,
} as const satisfies RelateRule;
const buffer = new Uint8Array([
0,
0,
0,
1,
0,
0,
0,
2,
0, // dangling
0,
0,
]);
const items = decode(buffer.buffer, rules);
assertEquals(items, [1, 2]);
});
Deno.test("decode, decode array types with error", () => {
const rules = {
type: "array",
of: { type: "i32" },
size: 3,
} as const satisfies RelateRule;
const buffer = new Uint8Array([
0,
]);
try {
decode(buffer.buffer, rules);
} catch (e) {
assertIsError(
e,
RangeError,
"Offset is outside the bounds of the DataView",
);
}
});
Deno.test("decode, transform string", () => {
const textDecoder = new TextDecoder();
const charOffset = "A".charCodeAt(0);
const rules = {
type: "object",
of: [
{
name: "string",
type: "string",
size: 8,
transformer: {
decode: (value) =>
textDecoder.decode(value.map((v) => v + charOffset)),
encode: () => {
throw new Error("never called");
},
},
},
],
} as const satisfies RelateRule;
const buffer = new Uint8Array([
0,
1,
2,
3,
4,
5,
6,
7,
]);
const item = decode(buffer.buffer, rules);
assertEquals(item, { string: "ABCDEFGH" });
});
Deno.test("decode, nest types", () => {
const point = {
type: "object",
of: [
{ name: "x", type: "u8" },
{ name: "y", type: "u8" },
],
} as const satisfies RelateRule;
const rules = {
type: "object",
of: [
{
name: "name",
type: "string",
size: 8,
transformer: CustomStringTransformer,
},
{ name: "start", ...point },
{ name: "end", ...point },
],
} as const satisfies RelateRule;
const buffer = new Uint8Array([
49,
50,
51,
52,
53,
54,
55,
0,
0,
1,
2,
3,
]);
const obj = decode(buffer.buffer, rules);
assertEquals(obj, {
name: "0123456",
start: { x: 0, y: 1 },
end: { x: 2, y: 3 },
});
});