@chr33s/pdf-restructure 5.0.0 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -1
- package/test/array.test.ts +0 -95
- package/test/bitfield.test.ts +0 -52
- package/test/boolean.test.ts +0 -35
- package/test/buffer.test.ts +0 -49
- package/test/decode-stream.test.ts +0 -104
- package/test/encode-stream.test.ts +0 -111
- package/test/enum.test.ts +0 -30
- package/test/lazy-array.test.ts +0 -70
- package/test/number.test.ts +0 -222
- package/test/optional.test.ts +0 -105
- package/test/pointer.test.ts +0 -248
- package/test/reserved.test.ts +0 -28
- package/test/string.test.ts +0 -114
- package/test/struct.test.ts +0 -164
- package/test/versioned-struct.test.ts +0 -462
- package/tsconfig.json +0 -9
- package/tsconfig.typecheck.json +0 -14
- package/vitest.config.ts +0 -12
package/test/number.test.ts
DELETED
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
DecodeStream,
|
|
4
|
-
double,
|
|
5
|
-
doublebe,
|
|
6
|
-
doublele,
|
|
7
|
-
EncodeStream,
|
|
8
|
-
fixed16,
|
|
9
|
-
fixed16be,
|
|
10
|
-
fixed16le,
|
|
11
|
-
fixed32,
|
|
12
|
-
fixed32be,
|
|
13
|
-
fixed32le,
|
|
14
|
-
float,
|
|
15
|
-
floatbe,
|
|
16
|
-
floatle,
|
|
17
|
-
int16,
|
|
18
|
-
int16be,
|
|
19
|
-
int16le,
|
|
20
|
-
int24,
|
|
21
|
-
int24be,
|
|
22
|
-
int24le,
|
|
23
|
-
int32,
|
|
24
|
-
int32be,
|
|
25
|
-
int32le,
|
|
26
|
-
int8,
|
|
27
|
-
uint16,
|
|
28
|
-
uint16be,
|
|
29
|
-
uint16le,
|
|
30
|
-
uint24,
|
|
31
|
-
uint24be,
|
|
32
|
-
uint24le,
|
|
33
|
-
uint32,
|
|
34
|
-
uint32be,
|
|
35
|
-
uint32le,
|
|
36
|
-
uint8,
|
|
37
|
-
} from "../src/index.js";
|
|
38
|
-
|
|
39
|
-
type IntegerCase = {
|
|
40
|
-
name: string;
|
|
41
|
-
type: any;
|
|
42
|
-
buffer: number[];
|
|
43
|
-
expected: number[];
|
|
44
|
-
size: number;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const integerCases: IntegerCase[] = [
|
|
48
|
-
{ name: "uint8", type: uint8, buffer: [0xab, 0xff], expected: [0xab, 0xff], size: 1 },
|
|
49
|
-
{ name: "uint16be", type: uint16be, buffer: [0xab, 0xff], expected: [0xabff], size: 2 },
|
|
50
|
-
{ name: "uint16le", type: uint16le, buffer: [0xff, 0xab], expected: [0xabff], size: 2 },
|
|
51
|
-
{ name: "uint24be", type: uint24be, buffer: [0xff, 0xab, 0x24], expected: [0xffab24], size: 3 },
|
|
52
|
-
{ name: "uint24le", type: uint24le, buffer: [0x24, 0xab, 0xff], expected: [0xffab24], size: 3 },
|
|
53
|
-
{
|
|
54
|
-
name: "uint32be",
|
|
55
|
-
type: uint32be,
|
|
56
|
-
buffer: [0xff, 0xab, 0x24, 0xbf],
|
|
57
|
-
expected: [0xffab24bf],
|
|
58
|
-
size: 4,
|
|
59
|
-
},
|
|
60
|
-
{
|
|
61
|
-
name: "uint32le",
|
|
62
|
-
type: uint32le,
|
|
63
|
-
buffer: [0xbf, 0x24, 0xab, 0xff],
|
|
64
|
-
expected: [0xffab24bf],
|
|
65
|
-
size: 4,
|
|
66
|
-
},
|
|
67
|
-
{ name: "int8", type: int8, buffer: [0x7f, 0xff], expected: [127, -1], size: 1 },
|
|
68
|
-
{ name: "int16be", type: int16be, buffer: [0xff, 0xab], expected: [-85], size: 2 },
|
|
69
|
-
{ name: "int16le", type: int16le, buffer: [0xab, 0xff], expected: [-85], size: 2 },
|
|
70
|
-
{ name: "int24be", type: int24be, buffer: [0xff, 0xab, 0x24], expected: [-21724], size: 3 },
|
|
71
|
-
{ name: "int24le", type: int24le, buffer: [0x24, 0xab, 0xff], expected: [-21724], size: 3 },
|
|
72
|
-
{
|
|
73
|
-
name: "int32be",
|
|
74
|
-
type: int32be,
|
|
75
|
-
buffer: [0xff, 0xab, 0x24, 0xbf],
|
|
76
|
-
expected: [-5561153],
|
|
77
|
-
size: 4,
|
|
78
|
-
},
|
|
79
|
-
{
|
|
80
|
-
name: "int32le",
|
|
81
|
-
type: int32le,
|
|
82
|
-
buffer: [0xbf, 0x24, 0xab, 0xff],
|
|
83
|
-
expected: [-5561153],
|
|
84
|
-
size: 4,
|
|
85
|
-
},
|
|
86
|
-
];
|
|
87
|
-
|
|
88
|
-
type FloatCase = {
|
|
89
|
-
name: string;
|
|
90
|
-
type: any;
|
|
91
|
-
buffer: number[];
|
|
92
|
-
expected: number;
|
|
93
|
-
size: number;
|
|
94
|
-
precision?: number;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const floatCases: FloatCase[] = [
|
|
98
|
-
{
|
|
99
|
-
name: "floatbe",
|
|
100
|
-
type: floatbe,
|
|
101
|
-
buffer: [0x43, 0x7a, 0x8c, 0xcd],
|
|
102
|
-
expected: 250.55,
|
|
103
|
-
size: 4,
|
|
104
|
-
precision: 0.005,
|
|
105
|
-
},
|
|
106
|
-
{
|
|
107
|
-
name: "floatle",
|
|
108
|
-
type: floatle,
|
|
109
|
-
buffer: [0xcd, 0x8c, 0x7a, 0x43],
|
|
110
|
-
expected: 250.55,
|
|
111
|
-
size: 4,
|
|
112
|
-
precision: 0.005,
|
|
113
|
-
},
|
|
114
|
-
{
|
|
115
|
-
name: "doublebe",
|
|
116
|
-
type: doublebe,
|
|
117
|
-
buffer: [0x40, 0x93, 0x4a, 0x3d, 0x70, 0xa3, 0xd7, 0x0a],
|
|
118
|
-
expected: 1234.56,
|
|
119
|
-
size: 8,
|
|
120
|
-
},
|
|
121
|
-
{
|
|
122
|
-
name: "doublele",
|
|
123
|
-
type: doublele,
|
|
124
|
-
buffer: [0x0a, 0xd7, 0xa3, 0x70, 0x3d, 0x4a, 0x93, 0x40],
|
|
125
|
-
expected: 1234.56,
|
|
126
|
-
size: 8,
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
name: "fixed16be",
|
|
130
|
-
type: fixed16be,
|
|
131
|
-
buffer: [0x19, 0x57],
|
|
132
|
-
expected: 25.34,
|
|
133
|
-
size: 2,
|
|
134
|
-
precision: 0.005,
|
|
135
|
-
},
|
|
136
|
-
{
|
|
137
|
-
name: "fixed16le",
|
|
138
|
-
type: fixed16le,
|
|
139
|
-
buffer: [0x57, 0x19],
|
|
140
|
-
expected: 25.34,
|
|
141
|
-
size: 2,
|
|
142
|
-
precision: 0.005,
|
|
143
|
-
},
|
|
144
|
-
{
|
|
145
|
-
name: "fixed32be",
|
|
146
|
-
type: fixed32be,
|
|
147
|
-
buffer: [0x00, 0xfa, 0x8c, 0xcc],
|
|
148
|
-
expected: 250.55,
|
|
149
|
-
size: 4,
|
|
150
|
-
precision: 0.005,
|
|
151
|
-
},
|
|
152
|
-
{
|
|
153
|
-
name: "fixed32le",
|
|
154
|
-
type: fixed32le,
|
|
155
|
-
buffer: [0xcc, 0x8c, 0xfa, 0x00],
|
|
156
|
-
expected: 250.55,
|
|
157
|
-
size: 4,
|
|
158
|
-
precision: 0.005,
|
|
159
|
-
},
|
|
160
|
-
];
|
|
161
|
-
|
|
162
|
-
const aliasCases: Array<[string, any, any]> = [
|
|
163
|
-
["uint16", uint16, uint16be],
|
|
164
|
-
["uint24", uint24, uint24be],
|
|
165
|
-
["uint32", uint32, uint32be],
|
|
166
|
-
["int16", int16, int16be],
|
|
167
|
-
["int24", int24, int24be],
|
|
168
|
-
["int32", int32, int32be],
|
|
169
|
-
["float", float, floatbe],
|
|
170
|
-
["double", double, doublebe],
|
|
171
|
-
["fixed16", fixed16, fixed16be],
|
|
172
|
-
["fixed32", fixed32, fixed32be],
|
|
173
|
-
];
|
|
174
|
-
|
|
175
|
-
describe("Number", () => {
|
|
176
|
-
test.each(aliasCases)("%s should alias the big-endian variant", (name, alias, target) => {
|
|
177
|
-
expect(alias).to.equal(target);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
describe.each(integerCases)("$name", ({ type, buffer, expected, size }) => {
|
|
181
|
-
test("should decode", () => {
|
|
182
|
-
const stream = new DecodeStream(new Uint8Array(buffer));
|
|
183
|
-
expected.forEach((value) => {
|
|
184
|
-
expect(type.decode(stream)).to.equal(value);
|
|
185
|
-
});
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
test("should report size", () => {
|
|
189
|
-
expect(type.size()).to.equal(size);
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
test("should encode", () => {
|
|
193
|
-
const stream = new EncodeStream(new Uint8Array(buffer.length));
|
|
194
|
-
expected.forEach((value) => {
|
|
195
|
-
type.encode(stream, value);
|
|
196
|
-
});
|
|
197
|
-
expect(stream.buffer).to.deep.equal(new Uint8Array(buffer));
|
|
198
|
-
});
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
describe.each(floatCases)("$name", ({ type, buffer, expected, size, precision }) => {
|
|
202
|
-
test("should decode", () => {
|
|
203
|
-
const stream = new DecodeStream(new Uint8Array(buffer));
|
|
204
|
-
const value = type.decode(stream);
|
|
205
|
-
if (precision) {
|
|
206
|
-
expect(value).to.be.closeTo(expected, precision);
|
|
207
|
-
} else {
|
|
208
|
-
expect(value).to.equal(expected);
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
test("should report size", () => {
|
|
213
|
-
expect(type.size()).to.equal(size);
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
test("should encode", () => {
|
|
217
|
-
const stream = new EncodeStream(new Uint8Array(buffer.length));
|
|
218
|
-
type.encode(stream, expected);
|
|
219
|
-
expect(stream.buffer).to.deep.equal(new Uint8Array(buffer));
|
|
220
|
-
});
|
|
221
|
-
});
|
|
222
|
-
});
|
package/test/optional.test.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
2
|
-
import { DecodeStream, EncodeStream, Optional, uint8 } from "../src/index.js";
|
|
3
|
-
|
|
4
|
-
describe("Optional", () => {
|
|
5
|
-
describe("decode", () => {
|
|
6
|
-
test("should not decode when condition is falsy", () => {
|
|
7
|
-
const stream = new DecodeStream(new Uint8Array([0]));
|
|
8
|
-
const optional = new Optional(uint8, false);
|
|
9
|
-
expect(optional.decode(stream)).to.equal(undefined);
|
|
10
|
-
expect(stream.pos).to.equal(0);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test("should not decode when condition is a function and falsy", () => {
|
|
14
|
-
const stream = new DecodeStream(new Uint8Array([0]));
|
|
15
|
-
const optional = new Optional(uint8, () => false);
|
|
16
|
-
expect(optional.decode(stream)).to.equal(undefined);
|
|
17
|
-
expect(stream.pos).to.equal(0);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test("should decode when condition is omitted", () => {
|
|
21
|
-
const stream = new DecodeStream(new Uint8Array([0]));
|
|
22
|
-
const optional = new Optional(uint8);
|
|
23
|
-
expect(optional.decode(stream)).to.equal(0);
|
|
24
|
-
expect(stream.pos).to.equal(1);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test("should decode when condition is truthy", () => {
|
|
28
|
-
const stream = new DecodeStream(new Uint8Array([0]));
|
|
29
|
-
const optional = new Optional(uint8, true);
|
|
30
|
-
expect(optional.decode(stream)).to.equal(0);
|
|
31
|
-
expect(stream.pos).to.equal(1);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test("should decode when condition is a function and truthy", () => {
|
|
35
|
-
const stream = new DecodeStream(new Uint8Array([0]));
|
|
36
|
-
const optional = new Optional(uint8, () => true);
|
|
37
|
-
expect(optional.decode(stream)).to.equal(0);
|
|
38
|
-
expect(stream.pos).to.equal(1);
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
describe("size", () => {
|
|
43
|
-
test("should return 0 when condition is falsy", () => {
|
|
44
|
-
const optional = new Optional(uint8, false);
|
|
45
|
-
expect(optional.size()).to.equal(0);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
test("should return 0 when condition is a function and falsy", () => {
|
|
49
|
-
const optional = new Optional(uint8, () => false);
|
|
50
|
-
expect(optional.size()).to.equal(0);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test("should return given type size when condition is omitted", () => {
|
|
54
|
-
const optional = new Optional(uint8);
|
|
55
|
-
expect(optional.size()).to.equal(1);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test("should return given type size when condition is truthy", () => {
|
|
59
|
-
const optional = new Optional(uint8, true);
|
|
60
|
-
expect(optional.size()).to.equal(1);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test("should return given type size when condition is a function and truthy", () => {
|
|
64
|
-
const optional = new Optional(uint8, () => true);
|
|
65
|
-
expect(optional.size()).to.equal(1);
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
describe("encode", () => {
|
|
70
|
-
test("should not encode when condition is falsy", () => {
|
|
71
|
-
const optional = new Optional(uint8, false);
|
|
72
|
-
const stream = new EncodeStream(new Uint8Array(0));
|
|
73
|
-
optional.encode(stream, 128);
|
|
74
|
-
expect(stream.buffer).to.deep.equal(new Uint8Array(0));
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
test("should not encode when condition is a function and falsy", () => {
|
|
78
|
-
const optional = new Optional(uint8, () => false);
|
|
79
|
-
const stream = new EncodeStream(new Uint8Array(0));
|
|
80
|
-
optional.encode(stream, 128);
|
|
81
|
-
expect(stream.buffer).to.deep.equal(new Uint8Array(0));
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test("should encode when condition is omitted", () => {
|
|
85
|
-
const optional = new Optional(uint8);
|
|
86
|
-
const stream = new EncodeStream(new Uint8Array(1));
|
|
87
|
-
optional.encode(stream, 128);
|
|
88
|
-
expect(stream.buffer).to.deep.equal(new Uint8Array([128]));
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test("should encode when condition is truthy", () => {
|
|
92
|
-
const optional = new Optional(uint8, true);
|
|
93
|
-
const stream = new EncodeStream(new Uint8Array(1));
|
|
94
|
-
optional.encode(stream, 128);
|
|
95
|
-
expect(stream.buffer).to.deep.equal(new Uint8Array([128]));
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
test("should encode when condition is a function and truthy", () => {
|
|
99
|
-
const optional = new Optional(uint8, () => true);
|
|
100
|
-
const stream = new EncodeStream(new Uint8Array(1));
|
|
101
|
-
optional.encode(stream, 128);
|
|
102
|
-
expect(stream.buffer).to.deep.equal(new Uint8Array([128]));
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
});
|
package/test/pointer.test.ts
DELETED
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
2
|
-
import { DecodeStream, EncodeStream, Pointer, Struct, VoidPointer, uint8 } from "../src/index.js";
|
|
3
|
-
|
|
4
|
-
describe("Pointer", () => {
|
|
5
|
-
describe("decode", () => {
|
|
6
|
-
test("should handle null pointers", () => {
|
|
7
|
-
const stream = new DecodeStream(new Uint8Array([0]));
|
|
8
|
-
const pointer = new Pointer(uint8, uint8);
|
|
9
|
-
expect(pointer.decode(stream, { _startOffset: 50 })).to.equal(null);
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
test("should use local offsets from start of parent by default", () => {
|
|
13
|
-
const stream = new DecodeStream(new Uint8Array([1, 53]));
|
|
14
|
-
const pointer = new Pointer(uint8, uint8);
|
|
15
|
-
expect(pointer.decode(stream, { _startOffset: 0 })).to.equal(53);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test("should support immediate offsets", () => {
|
|
19
|
-
const stream = new DecodeStream(new Uint8Array([1, 53]));
|
|
20
|
-
const pointer = new Pointer(uint8, uint8, { type: "immediate" });
|
|
21
|
-
expect(pointer.decode(stream, {})).to.equal(53);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test("should support offsets relative to the parent", () => {
|
|
25
|
-
const stream = new DecodeStream(new Uint8Array([0, 0, 1, 53]));
|
|
26
|
-
stream.pos = 2;
|
|
27
|
-
const pointer = new Pointer(uint8, uint8, { type: "parent" });
|
|
28
|
-
expect(pointer.decode(stream, { parent: { _startOffset: 2 } })).to.equal(53);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test("should support global offsets", () => {
|
|
32
|
-
const stream = new DecodeStream(new Uint8Array([1, 2, 4, 0, 0, 0, 53]));
|
|
33
|
-
const pointer = new Pointer(uint8, uint8, { type: "global" });
|
|
34
|
-
stream.pos = 2;
|
|
35
|
-
expect(pointer.decode(stream, { parent: { parent: { _startOffset: 2 } } })).to.equal(53);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test("should support offsets relative to a property on the parent", () => {
|
|
39
|
-
const stream = new DecodeStream(new Uint8Array([1, 0, 0, 0, 0, 53]));
|
|
40
|
-
const pointer = new Pointer(uint8, uint8, { relativeTo: "parent.ptr" });
|
|
41
|
-
expect(pointer.decode(stream, { _startOffset: 0, parent: { ptr: 4 } })).to.equal(53);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test("should support returning pointer if there is no decode type", () => {
|
|
45
|
-
const stream = new DecodeStream(new Uint8Array([4]));
|
|
46
|
-
const pointer = new Pointer(uint8, "void");
|
|
47
|
-
expect(pointer.decode(stream, { _startOffset: 0 })).to.equal(4);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test("should support decoding pointers lazily", () => {
|
|
51
|
-
const stream = new DecodeStream(new Uint8Array([1, 53]));
|
|
52
|
-
const struct = new Struct({
|
|
53
|
-
ptr: new Pointer(uint8, uint8, { lazy: true }),
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
const res = struct.decode(stream);
|
|
57
|
-
const descriptor = Object.getOwnPropertyDescriptor(res, "ptr");
|
|
58
|
-
expect(typeof descriptor?.get).to.equal("function");
|
|
59
|
-
expect(descriptor?.enumerable).to.equal(true);
|
|
60
|
-
expect(res.ptr).to.equal(53);
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
describe("size", () => {
|
|
65
|
-
test("should add to local pointerSize", () => {
|
|
66
|
-
const pointer = new Pointer(uint8, uint8);
|
|
67
|
-
const ctx: any = { pointerSize: 0 };
|
|
68
|
-
expect(pointer.size(10, ctx)).to.equal(1);
|
|
69
|
-
expect(ctx.pointerSize).to.equal(1);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
test("should add to immediate pointerSize", () => {
|
|
73
|
-
const pointer = new Pointer(uint8, uint8, { type: "immediate" });
|
|
74
|
-
const ctx: any = { pointerSize: 0 };
|
|
75
|
-
expect(pointer.size(10, ctx)).to.equal(1);
|
|
76
|
-
expect(ctx.pointerSize).to.equal(1);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test("should add to parent pointerSize", () => {
|
|
80
|
-
const pointer = new Pointer(uint8, uint8, { type: "parent" });
|
|
81
|
-
const ctx: any = { parent: { pointerSize: 0 } };
|
|
82
|
-
expect(pointer.size(10, ctx)).to.equal(1);
|
|
83
|
-
expect(ctx.parent.pointerSize).to.equal(1);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
test("should add to global pointerSize", () => {
|
|
87
|
-
const pointer = new Pointer(uint8, uint8, { type: "global" });
|
|
88
|
-
const ctx: any = { parent: { parent: { parent: { pointerSize: 0 } } } };
|
|
89
|
-
expect(pointer.size(10, ctx)).to.equal(1);
|
|
90
|
-
expect(ctx.parent.parent.parent.pointerSize).to.equal(1);
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
test("should handle void pointers", () => {
|
|
94
|
-
const pointer = new Pointer(uint8, "void");
|
|
95
|
-
const ctx: any = { pointerSize: 0 };
|
|
96
|
-
expect(pointer.size(new VoidPointer(uint8, 50), ctx)).to.equal(1);
|
|
97
|
-
expect(ctx.pointerSize).to.equal(1);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
test("should throw if no type and not a void pointer", () => {
|
|
101
|
-
const pointer = new Pointer(uint8, "void");
|
|
102
|
-
const ctx: any = { pointerSize: 0 };
|
|
103
|
-
expect(() => pointer.size(30, ctx)).to.throw();
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
test("should return a fixed size without a value", () => {
|
|
107
|
-
const pointer = new Pointer(uint8, uint8);
|
|
108
|
-
expect(pointer.size()).to.equal(1);
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
describe("encode", () => {
|
|
113
|
-
test("should handle null pointers", () => {
|
|
114
|
-
const stream = new EncodeStream(new Uint8Array(1));
|
|
115
|
-
const ptr = new Pointer(uint8, uint8);
|
|
116
|
-
const ctx: any = {
|
|
117
|
-
pointerSize: 0,
|
|
118
|
-
startOffset: 0,
|
|
119
|
-
pointerOffset: 0,
|
|
120
|
-
pointers: [],
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
ptr.encode(stream, null, ctx);
|
|
124
|
-
expect(ctx.pointerSize).to.equal(0);
|
|
125
|
-
expect(stream.buffer.subarray(0, stream.pos)).to.deep.equal(new Uint8Array([0]));
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
test("should handle local offsets", () => {
|
|
129
|
-
const stream = new EncodeStream(new Uint8Array(1));
|
|
130
|
-
const ptr = new Pointer(uint8, uint8);
|
|
131
|
-
const ctx: any = {
|
|
132
|
-
pointerSize: 0,
|
|
133
|
-
startOffset: 0,
|
|
134
|
-
pointerOffset: 1,
|
|
135
|
-
pointers: [],
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
ptr.encode(stream, 10, ctx);
|
|
139
|
-
expect(ctx.pointerOffset).to.equal(2);
|
|
140
|
-
expect(ctx.pointers).to.deep.equal([{ type: uint8, val: 10, parent: ctx }]);
|
|
141
|
-
expect(stream.buffer.subarray(0, stream.pos)).to.deep.equal(new Uint8Array([1]));
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
test("should handle immediate offsets", () => {
|
|
145
|
-
const stream = new EncodeStream(new Uint8Array(1));
|
|
146
|
-
const ptr = new Pointer(uint8, uint8, { type: "immediate" });
|
|
147
|
-
const ctx: any = {
|
|
148
|
-
pointerSize: 0,
|
|
149
|
-
startOffset: 0,
|
|
150
|
-
pointerOffset: 1,
|
|
151
|
-
pointers: [],
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
ptr.encode(stream, 10, ctx);
|
|
155
|
-
expect(ctx.pointerOffset).to.equal(2);
|
|
156
|
-
expect(ctx.pointers).to.deep.equal([{ type: uint8, val: 10, parent: ctx }]);
|
|
157
|
-
expect(stream.buffer.subarray(0, stream.pos)).to.deep.equal(new Uint8Array([0]));
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
test("should handle offsets relative to parent", () => {
|
|
161
|
-
const stream = new EncodeStream(new Uint8Array(1));
|
|
162
|
-
const ptr = new Pointer(uint8, uint8, { type: "parent" });
|
|
163
|
-
const ctx: any = {
|
|
164
|
-
parent: {
|
|
165
|
-
pointerSize: 0,
|
|
166
|
-
startOffset: 3,
|
|
167
|
-
pointerOffset: 5,
|
|
168
|
-
pointers: [],
|
|
169
|
-
},
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
ptr.encode(stream, 10, ctx);
|
|
173
|
-
expect(ctx.parent.pointerOffset).to.equal(6);
|
|
174
|
-
expect(ctx.parent.pointers).to.deep.equal([{ type: uint8, val: 10, parent: ctx }]);
|
|
175
|
-
expect(stream.buffer.subarray(0, stream.pos)).to.deep.equal(new Uint8Array([2]));
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
test("should handle global offsets", () => {
|
|
179
|
-
const stream = new EncodeStream(new Uint8Array(1));
|
|
180
|
-
const ptr = new Pointer(uint8, uint8, { type: "global" });
|
|
181
|
-
const ctx: any = {
|
|
182
|
-
parent: {
|
|
183
|
-
parent: {
|
|
184
|
-
parent: {
|
|
185
|
-
pointerSize: 0,
|
|
186
|
-
startOffset: 3,
|
|
187
|
-
pointerOffset: 5,
|
|
188
|
-
pointers: [],
|
|
189
|
-
},
|
|
190
|
-
},
|
|
191
|
-
},
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
ptr.encode(stream, 10, ctx);
|
|
195
|
-
expect(ctx.parent.parent.parent.pointerOffset).to.equal(6);
|
|
196
|
-
expect(ctx.parent.parent.parent.pointers).to.deep.equal([
|
|
197
|
-
{ type: uint8, val: 10, parent: ctx },
|
|
198
|
-
]);
|
|
199
|
-
expect(stream.buffer.subarray(0, stream.pos)).to.deep.equal(new Uint8Array([5]));
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
test("should support offsets relative to a property on the parent", () => {
|
|
203
|
-
const stream = new EncodeStream(new Uint8Array(1));
|
|
204
|
-
const ptr = new Pointer(uint8, uint8, { relativeTo: "ptr" });
|
|
205
|
-
const ctx: any = {
|
|
206
|
-
pointerSize: 0,
|
|
207
|
-
startOffset: 0,
|
|
208
|
-
pointerOffset: 10,
|
|
209
|
-
pointers: [],
|
|
210
|
-
val: { ptr: 4 },
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
ptr.encode(stream, 10, ctx);
|
|
214
|
-
expect(ctx.pointerOffset).to.equal(11);
|
|
215
|
-
expect(ctx.pointers).to.deep.equal([{ type: uint8, val: 10, parent: ctx }]);
|
|
216
|
-
expect(stream.buffer.subarray(0, stream.pos)).to.deep.equal(new Uint8Array([6]));
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
test("should support void pointers", () => {
|
|
220
|
-
const stream = new EncodeStream(new Uint8Array(1));
|
|
221
|
-
const ptr = new Pointer(uint8, "void");
|
|
222
|
-
const ctx: any = {
|
|
223
|
-
pointerSize: 0,
|
|
224
|
-
startOffset: 0,
|
|
225
|
-
pointerOffset: 1,
|
|
226
|
-
pointers: [],
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
ptr.encode(stream, new VoidPointer(uint8, 55), ctx);
|
|
230
|
-
expect(ctx.pointerOffset).to.equal(2);
|
|
231
|
-
expect(ctx.pointers).to.deep.equal([{ type: uint8, val: 55, parent: ctx }]);
|
|
232
|
-
expect(stream.buffer.subarray(0, stream.pos)).to.deep.equal(new Uint8Array([1]));
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
test("should throw if not a void pointer instance", () => {
|
|
236
|
-
const stream = new EncodeStream(new Uint8Array(10));
|
|
237
|
-
const ptr = new Pointer(uint8, "void");
|
|
238
|
-
const ctx: any = {
|
|
239
|
-
pointerSize: 0,
|
|
240
|
-
startOffset: 0,
|
|
241
|
-
pointerOffset: 1,
|
|
242
|
-
pointers: [],
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
expect(() => ptr.encode(stream, 44, ctx)).to.throw();
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
|
-
});
|
package/test/reserved.test.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
2
|
-
import { DecodeStream, EncodeStream, Reserved, uint16, uint8 } from "../src/index.js";
|
|
3
|
-
|
|
4
|
-
describe("Reserved", () => {
|
|
5
|
-
test("should have a default count of 1", () => {
|
|
6
|
-
const reserved = new Reserved(uint8);
|
|
7
|
-
expect(reserved.size()).to.equal(1);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
test("should allow custom counts and types", () => {
|
|
11
|
-
const reserved = new Reserved(uint16, 10);
|
|
12
|
-
expect(reserved.size()).to.equal(20);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test("should decode", () => {
|
|
16
|
-
const stream = new DecodeStream(new Uint8Array([0, 0]));
|
|
17
|
-
const reserved = new Reserved(uint16);
|
|
18
|
-
expect(reserved.decode(stream)).to.equal(undefined);
|
|
19
|
-
expect(stream.pos).to.equal(2);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("should encode", () => {
|
|
23
|
-
const reserved = new Reserved(uint16);
|
|
24
|
-
const stream = new EncodeStream(new Uint8Array(2));
|
|
25
|
-
reserved.encode(stream);
|
|
26
|
-
expect(stream.buffer).to.deep.equal(new Uint8Array([0, 0]));
|
|
27
|
-
});
|
|
28
|
-
});
|