@reversense/dxc-struct 1.0.7

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.
@@ -0,0 +1,233 @@
1
+ import {StructEncoder} from '../src/StructEncoder.js';
2
+ import {expect} from 'chai';
3
+ import {Struct} from "../dist/index.js";
4
+ import {Endianness} from "../dist/src/common.js";
5
+
6
+
7
+ describe('StructEncoder', () => {
8
+
9
+ describe('.str', () => {
10
+ it('should copy characters from a string to an array with a given offset and length', () => {
11
+
12
+ const arr:number[] = [];
13
+
14
+ StructEncoder.str(arr, 0, 3, 'abcd');
15
+
16
+ expect(arr).to.deep.equal(['a'.charCodeAt(0), 'b'.charCodeAt(0), 'c'.charCodeAt(0)]);
17
+ });
18
+
19
+ it('should fill the rest of the array with zeros when the string is shorter than the length', () => {
20
+
21
+ const arr:number[] = [];
22
+
23
+ StructEncoder.str(arr, 0, 5, 'ab');
24
+
25
+ expect(arr).to.deep.equal(['a'.charCodeAt(0), 'b'.charCodeAt(0), 0, 0, 0]);
26
+ });
27
+
28
+ it('should not modify array values beyond the given length', () => {
29
+
30
+ const arr:number[] = [1, 2, 3, 4, 5];
31
+
32
+ StructEncoder.str(arr, 1, 2, 'xy');
33
+
34
+ expect(arr).to.deep.equal([1, 'x'.charCodeAt(0), 'y'.charCodeAt(0), 4, 5]);
35
+ });
36
+ });
37
+
38
+ describe('.int', () => {
39
+ describe("int8", () => {
40
+ it('uint_8 encoding', () => {
41
+ const arr: number[] = [];
42
+ const pEndianness = Endianness.BIG_ENDIAN;
43
+ const pOp = Struct.OP['B'];
44
+ const pOffset = 0;
45
+ const test_data: number[] = [0xf3, 0xdb, 0xcd, 0xff];
46
+ const buf = Buffer.from(test_data);
47
+ const v: number = buf.readUInt8(0);
48
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
49
+ expect(arr).to.deep.equal(test_data.slice(0, 1));
50
+ });
51
+
52
+ it('uint_8 encoding LE', () => {
53
+ const arr: number[] = [];
54
+ const pEndianness = Endianness.LITTLE_ENDIAN;
55
+ const pOp = Struct.OP['B'];
56
+ const pOffset = 0;
57
+ const test_data: number[] = [0xf3, 0xdb, 0xcd, 0xff];
58
+ const buf = Buffer.from(test_data);
59
+ const v: number = buf.readUInt8(0);
60
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
61
+ expect(arr).to.deep.equal(test_data.slice(0, 1));
62
+ });
63
+
64
+ it('uint_8 with offset encoding', () => {
65
+ const arr: number[] = [0x31, 0x63, 0xd1, 0xb2, 0x4a, 0x70, 0x08];
66
+ const pEndianness = Endianness.BIG_ENDIAN;
67
+ const pOp = Struct.OP['B'];
68
+ const pOffset: number = 2;
69
+ const test_data: number[] = [0xf3, 0xdb, 0xcd, 0xff];
70
+ const expected_arr: number[] = [0x31, 0x63, 0xf3, 0xb2, 0x4a, 0x70, 0x08];
71
+ const buf = Buffer.from(test_data);
72
+ const v: number = buf.readUInt8(0);
73
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
74
+ expect(arr).to.deep.equal(expected_arr);
75
+ });
76
+
77
+ it('int_8 encoding', () => {
78
+ const arr: number[] = [];
79
+ const pEndianness = Endianness.BIG_ENDIAN;
80
+ const pOp = Struct.OP['b'];
81
+ const pOffset = 0;
82
+ const test_data: number[] = [0xf0, 0xdb, 0xcd, 0xff];
83
+ const buf = Buffer.from(test_data);
84
+ const v: number = buf.readInt8(0);
85
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
86
+ expect(arr).to.deep.equal(test_data.slice(0, 1));
87
+ });
88
+
89
+ it('int_8 with offset encoding', () => {
90
+ const arr: number[] = [0x31, 0x63, 0xd1, 0xb2, 0x4a, 0x70, 0x08];
91
+ const pEndianness = Endianness.BIG_ENDIAN;
92
+ const pOp = Struct.OP['b'];
93
+ const pOffset: number = 2;
94
+ const test_data: number[] = [0xf3, 0xdb, 0xcd, 0xff];
95
+ const expected_arr: number[] = [0x31, 0x63, 0xf3, 0xb2, 0x4a, 0x70, 0x08];
96
+ const buf = Buffer.from(test_data);
97
+ const v: number = buf.readInt8(0);
98
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
99
+ expect(arr).to.deep.equal(expected_arr);
100
+ });
101
+ });
102
+ describe('.int16', () => {
103
+ it('uint_16 encoding', () => {
104
+ const arr:number[] = [];
105
+ const pEndianness = Endianness.BIG_ENDIAN;
106
+ const pOp = Struct.OP['H'];
107
+ const pOffset = 0;
108
+ const test_data : number[] = [0x12, 0xfb, 0xcd, 0xff];
109
+ const buf = Buffer.from(test_data);
110
+ const v : number = buf.readUInt16BE(0);
111
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
112
+ expect(arr).to.deep.equal(test_data.slice(0, 2));
113
+ });
114
+
115
+ it('uint_16 encoding LE', () => {
116
+ const arr: number[] = [];
117
+ const pEndianness = Endianness.LITTLE_ENDIAN;
118
+ const pOp = Struct.OP['H'];
119
+ const pOffset = 0;
120
+ const test_data: number[] = [0xf3, 0xdb, 0xcd, 0xff];
121
+ const buf = Buffer.from(test_data);
122
+ const v: number = buf.readUInt16LE(0);
123
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
124
+ expect(arr).to.deep.equal(test_data.slice(0, 2));
125
+ });
126
+
127
+ it('uint_16 with offset encoding BE', () => {
128
+ const arr: number[] = [0x31, 0x63, 0xd1, 0xb2, 0x4a, 0x70, 0x08];
129
+ const pEndianness = Endianness.BIG_ENDIAN;
130
+ const pOp = Struct.OP['H'];
131
+ const pOffset: number = 2;
132
+ const test_data: number[] = [0xf3, 0xdb, 0xcd, 0xff];
133
+ const expected_arr: number[] = [0x31, 0x63, 0xf3, 0xdb, 0x4a, 0x70, 0x08];
134
+ const buf = Buffer.from(test_data);
135
+ const v: number = buf.readUInt16BE(0);
136
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
137
+ expect(arr).to.deep.equal(expected_arr);
138
+ });
139
+
140
+ it('uint_16 with offset encoding LE', () => {
141
+ const arr: number[] = [0x31, 0x63, 0xd1, 0xb2, 0x4a, 0x70, 0x08];
142
+ const pEndianness = Endianness.LITTLE_ENDIAN;
143
+ const pOp = Struct.OP['H'];
144
+ const pOffset: number = 2;
145
+ const test_data: number[] = [0xf3, 0xdb, 0xcd, 0xff];
146
+ const expected_arr: number[] = [0x31, 0x63, 0xf3, 0xdb, 0x4a, 0x70, 0x08];
147
+ const buf = Buffer.from(test_data);
148
+ // Double bit inversion with pEndianness=LITTLE_ENDIAN and readUInt16LE
149
+ const v: number = buf.readUInt16LE(0);
150
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
151
+ expect(arr).to.deep.equal(expected_arr);
152
+ });
153
+
154
+ it('int_16 encoding', () => {
155
+ const arr:number[] = [];
156
+ const pEndianness = Endianness.BIG_ENDIAN;
157
+ const pOp = Struct.OP['h'];
158
+ const pOffset = 0;
159
+ const test_data : number[] = [0x12, 0xfb, 0xcd, 0xff];
160
+ const buf = Buffer.from(test_data);
161
+ const v : number = buf.readUInt16BE(0);
162
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
163
+ expect(arr).to.deep.equal(test_data.slice(0, 2));
164
+ });
165
+ });
166
+
167
+ describe('.int32', () => {
168
+ it('uint_32 encoding with number to encode > 2**31', () => {
169
+ const arr:number[] = [];
170
+ const pEndianness = Endianness.BIG_ENDIAN;
171
+ const pOp = Struct.OP['I'];
172
+ const pOffset = 0;
173
+ const test_data : number[] = [0xe2, 0xfb, 0xcd, 0xff, 0x17];
174
+ const buf = Buffer.from(test_data);
175
+ const v : number = buf.readUInt32BE(0);
176
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
177
+ expect(arr).to.deep.equal(test_data.slice(0, 4));
178
+ });
179
+
180
+ it('uint_32 encoding LE', () => {
181
+ const arr: number[] = [];
182
+ const pEndianness = Endianness.LITTLE_ENDIAN;
183
+ const pOp = Struct.OP['I'];
184
+ const pOffset = 0;
185
+ const test_data: number[] = [0xf3, 0xdb, 0xcd, 0xff];
186
+ const buf = Buffer.from(test_data);
187
+ // Double inversion with pEndianness=LITTLE_ENDIAN and readUInt32LE
188
+ const v: number = buf.readUInt32LE(0);
189
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
190
+ expect(arr).to.deep.equal(test_data.slice(0, 4));
191
+ });
192
+
193
+ it('uint_32 with offset encoding BE', () => {
194
+ const arr: number[] = [0x31, 0x63, 0xd1, 0xb2, 0x4a, 0x70, 0x08];
195
+ const pEndianness = Endianness.BIG_ENDIAN;
196
+ const pOp = Struct.OP['I'];
197
+ const pOffset: number = 2;
198
+ const test_data: number[] = [0xf3, 0xdb, 0xcd, 0xff];
199
+ const expected_arr: number[] = [0x31, 0x63, 0xf3, 0xdb, 0xcd, 0xff, 0x08];
200
+ const buf = Buffer.from(test_data);
201
+ const v: number = buf.readUInt32BE(0);
202
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
203
+ expect(arr).to.deep.equal(expected_arr);
204
+ });
205
+
206
+ it('uint_32 with offset encoding LE', () => {
207
+ const arr: number[] = [0x31, 0x63, 0xd1, 0xb2, 0x4a, 0x70, 0x08];
208
+ const pEndianness = Endianness.LITTLE_ENDIAN;
209
+ const pOp = Struct.OP['I'];
210
+ const pOffset: number = 2;
211
+ const test_data: number[] = [0xf3, 0xdb, 0xcd, 0xff];
212
+ const expected_arr: number[] = [0x31, 0x63, 0xf3, 0xdb, 0xcd, 0xff, 0x08];
213
+ const buf = Buffer.from(test_data);
214
+ // Double inversion with pEndianness=LITTLE_ENDIAN and readUInt16LE
215
+ const v: number = buf.readUInt32LE(0);
216
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
217
+ expect(arr).to.deep.equal(expected_arr);
218
+ });
219
+
220
+ it('int_32 encoding with number to encode > 2**31', () => {
221
+ const arr:number[] = [];
222
+ const pEndianness = Endianness.BIG_ENDIAN;
223
+ const pOp = Struct.OP['i'];
224
+ const pOffset = 0;
225
+ const test_data : number[] = [0x12, 0xfb, 0xcd, 0xff, 0x17];
226
+ const buf = Buffer.from(test_data);
227
+ const v : number = buf.readInt32BE(0);
228
+ StructEncoder.int(pEndianness, pOp, arr, pOffset, v);
229
+ expect(arr).to.deep.equal(test_data.slice(0, 4));
230
+ });
231
+ });
232
+ });
233
+ });
Binary file
Binary file
Binary file