Skip to content

Commit 8af578e

Browse files
ihex: Update tests to cover more 32 byte records.
1 parent 6d591be commit 8af578e

File tree

2 files changed

+75
-61
lines changed

2 files changed

+75
-61
lines changed

src/__tests__/ihex.spec.ts

Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,30 @@ import * as ihex from '../ihex';
88

99
describe('Test createRecord() for standard records', () => {
1010
it('Creates standard data records', () => {
11+
let a = [0x64, 0x27, 0x00, 0x20, 0x03, 0x4b, 0x19, 0x60];
12+
let b = [0x43, 0x68, 0x03, 0x49, 0x9b, 0x00, 0x5a, 0x50];
1113
// Examples taken from a random micro:bit hex file
1214
expect(
1315
ihex.createRecord(
1416
0x4290,
1517
ihex.RecordType.Data,
16-
new Uint8Array([
17-
0x64,
18-
0x27,
19-
0x00,
20-
0x20,
21-
0x03,
22-
0x4b,
23-
0x19,
24-
0x60,
25-
0x43,
26-
0x68,
27-
0x03,
28-
0x49,
29-
0x9b,
30-
0x00,
31-
0x5a,
32-
0x50,
33-
])
18+
new Uint8Array(a.concat(b))
3419
)
3520
).toEqual(':1042900064270020034B1960436803499B005A5070');
3621

22+
a = [0x12, 0xf0, 0xd0, 0xfb, 0x07, 0xee, 0x90, 0x0a, 0xf5, 0xee, 0xc0];
23+
b = [0x7a, 0xf1, 0xee, 0x10, 0xfa, 0x44, 0xbf, 0x9f, 0xed, 0x08, 0x7a];
24+
const c = [0x77, 0xee, 0x87, 0x7a, 0xfd, 0xee, 0xe7, 0x7a, 0x17, 0xee];
25+
expect(
26+
ihex.createRecord(
27+
0x07e0,
28+
ihex.RecordType.Data,
29+
new Uint8Array(a.concat(b).concat(c))
30+
)
31+
).toEqual(
32+
':2007E00012F0D0FB07EE900AF5EEC07AF1EE10FA44BF9FED087A77EE877AFDEEE77A17EECF'
33+
);
34+
3735
expect(
3836
ihex.createRecord(
3937
0xf870,
@@ -51,7 +49,7 @@ describe('Test createRecord() for standard records', () => {
5149
).toEqual(':08E7D4000C1AFF7F0100000098');
5250
});
5351

54-
it('Creates a custom End Of File record', () => {
52+
it('Creates an End Of File record', () => {
5553
expect(
5654
ihex.createRecord(0, ihex.RecordType.EndOfFile, new Uint8Array([]))
5755
).toEqual(':00000001FF');
@@ -73,7 +71,7 @@ describe('Test createRecord() for standard records', () => {
7371

7472
expect(() => {
7573
ihex.createRecord(0, ihex.RecordType.Data, new Uint8Array(data));
76-
}).toThrow();
74+
}).toThrow('data has too many bytes');
7775
});
7876

7977
it('Throws error when the address is too large', () => {
@@ -180,12 +178,20 @@ describe('Test getRecordData()', () => {
180178
);
181179
});
182180

183-
it('Get the data from a full Padding record', () => {
181+
it('Get the data from a half Padding record', () => {
184182
expect(
185183
ihex.getRecordData(':1080B00DFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC3')
186184
).toEqual(new Uint8Array(new Array(16).fill(0xff)));
187185
});
188186

187+
it('Get the data from a full Padding record', () => {
188+
expect(
189+
ihex.getRecordData(
190+
':1080B00DFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC3'
191+
)
192+
).toEqual(new Uint8Array(new Array(32).fill(0xff)));
193+
});
194+
189195
it('Empty array when record is too short', () => {
190196
expect(ihex.getRecordData(':00000001')).toEqual(new Uint8Array());
191197
});
@@ -198,54 +204,53 @@ describe('Test getRecordData()', () => {
198204
});
199205

200206
describe('Test parseRecord() for standard records', () => {
201-
it('Parses data records of different lengths', () => {
202-
let result = ihex.parseRecord(
207+
it('Parses data records of different lengths (32)', () => {
208+
const result = ihex.parseRecord(
209+
':201C400010F0D2FF3246CDE900013B463046394610F0EEFC02460B46DDE9000110F09AFE2C'
210+
);
211+
212+
expect(result.byteCount).toEqual(0x20);
213+
expect(result.address).toEqual(0x1c40);
214+
expect(result.recordType).toEqual(0x00);
215+
const a = [0x10, 0xf0, 0xd2, 0xff, 0x32, 0x46, 0xcd, 0xe9, 0x00, 0x01];
216+
const b = [0x3b, 0x46, 0x30, 0x46, 0x39, 0x46, 0x10, 0xf0, 0xee, 0xfc];
217+
const c = [0x02, 0x46, 0x0b, 0x46, 0xdd, 0xe9, 0x00, 0x01, 0x10, 0xf0];
218+
const d = [0x9a, 0xfe];
219+
expect(result.data).toEqual(
220+
new Uint8Array(a.concat(b).concat(c).concat(d))
221+
);
222+
expect(result.checksum).toEqual(0x2c);
223+
});
224+
225+
it('Parses data records of different lengths (16)', () => {
226+
const result = ihex.parseRecord(
203227
':10FFF0009B6D9847A06810F039FF0621A06810F0AB'
204228
);
229+
205230
expect(result.byteCount).toEqual(0x10);
206231
expect(result.address).toEqual(0xfff0);
207232
expect(result.recordType).toEqual(0x00);
208-
expect(result.data).toEqual(
209-
new Uint8Array([
210-
0x9b,
211-
0x6d,
212-
0x98,
213-
0x47,
214-
0xa0,
215-
0x68,
216-
0x10,
217-
0xf0,
218-
0x39,
219-
0xff,
220-
0x06,
221-
0x21,
222-
0xa0,
223-
0x68,
224-
0x10,
225-
0xf0,
226-
])
227-
);
233+
const a = [0x9b, 0x6d, 0x98, 0x47, 0xa0, 0x68, 0x10, 0xf0];
234+
const b = [0x39, 0xff, 0x06, 0x21, 0xa0, 0x68, 0x10, 0xf0];
235+
expect(result.data).toEqual(new Uint8Array(a.concat(b)));
228236
expect(result.checksum).toEqual(0xab);
237+
});
238+
239+
it('Parses data records of different lengths (8)', () => {
240+
const result = ihex.parseRecord(':08AEE0007C53FF7F010000001C');
229241

230-
result = ihex.parseRecord(':08AEE0007C53FF7F010000001C');
231242
expect(result.byteCount).toEqual(0x08);
232243
expect(result.address).toEqual(0xaee0);
233244
expect(result.recordType).toEqual(0x00);
234245
expect(result.data).toEqual(
235246
new Uint8Array([0x7c, 0x53, 0xff, 0x7f, 0x01, 0x00, 0x00, 0x00])
236247
);
237248
expect(result.checksum).toEqual(0x1c);
238-
239-
result = ihex.parseRecord(':04F870000000000094');
240-
expect(result.byteCount).toEqual(0x04);
241-
expect(result.address).toEqual(0xf870);
242-
expect(result.recordType).toEqual(0x00);
243-
expect(result.data).toEqual(new Uint8Array([0x00, 0x00, 0x00, 0x00]));
244-
expect(result.checksum).toEqual(0x94);
245249
});
246250

247-
it('Parses data records of different lengths', () => {
248-
const result = ihex.parseRecord(':04F870000000000094\r\n');
251+
it('Parses data records of different lengths (4)', () => {
252+
const result = ihex.parseRecord(':04F870000000000094');
253+
249254
expect(result.byteCount).toEqual(0x04);
250255
expect(result.address).toEqual(0xf870);
251256
expect(result.recordType).toEqual(0x00);
@@ -307,6 +312,14 @@ describe('Test extLinAddressRecord()', () => {
307312
expect(ihex.extLinAddressRecord(0x20000)).toEqual(':020000040002F8');
308313
expect(ihex.extLinAddressRecord(0x30000)).toEqual(':020000040003F7');
309314
expect(ihex.extLinAddressRecord(0x31234)).toEqual(':020000040003F7');
315+
expect(ihex.extLinAddressRecord(0x40000)).toEqual(':020000040004F6');
316+
expect(ihex.extLinAddressRecord(0x48264)).toEqual(':020000040004F6');
317+
expect(ihex.extLinAddressRecord(0x50000)).toEqual(':020000040005F5');
318+
expect(ihex.extLinAddressRecord(0x55555)).toEqual(':020000040005F5');
319+
expect(ihex.extLinAddressRecord(0x60000)).toEqual(':020000040006F4');
320+
expect(ihex.extLinAddressRecord(0x61230)).toEqual(':020000040006F4');
321+
expect(ihex.extLinAddressRecord(0x70000)).toEqual(':020000040007F3');
322+
expect(ihex.extLinAddressRecord(0x72946)).toEqual(':020000040007F3');
310323
});
311324

312325
// TODO: Add tests for all thrown exceptions
@@ -339,6 +352,9 @@ describe('Test blockEndRecord()', () => {
339352
expect(ihex.blockEndRecord(0x10)).toEqual(
340353
':1000000BFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5'
341354
);
355+
expect(ihex.blockEndRecord(0x20)).toEqual(
356+
':2000000BFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5'
357+
);
342358
});
343359

344360
it('Throws error when the number of bytes to pad is a negative value', () => {
@@ -366,6 +382,9 @@ describe('Test paddedDataRecord()', () => {
366382
expect(ihex.paddedDataRecord(0x10)).toEqual(
367383
':1000000CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF4'
368384
);
385+
expect(ihex.paddedDataRecord(0x20)).toEqual(
386+
':2000000CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF4'
387+
);
369388
});
370389

371390
it('Throws error when the number of bytes to pad is a negative value', () => {

src/ihex.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,7 @@ const MIN_RECORD_STR_LEN =
6363
DATA_STR_LEN_MIN +
6464
CHECKSUM_STR_LEN;
6565
const MAX_RECORD_STR_LEN =
66-
START_CODE_STR_LEN +
67-
BYTE_COUNT_STR_LEN +
68-
ADDRESS_STR_LEN +
69-
RECORD_TYPE_STR_LEN +
70-
RECORD_DATA_MAX_BYTES * 2 +
71-
CHECKSUM_STR_LEN;
66+
MIN_RECORD_STR_LEN - DATA_STR_LEN_MIN + RECORD_DATA_MAX_BYTES * 2;
7267

7368
/**
7469
* Checks if a given number is a valid Record type.
@@ -142,7 +137,7 @@ function createRecord(
142137
}
143138

144139
/**
145-
* Check if the an Intel Hex record conforms to the following rules:
140+
* Check if an Intel Hex record conforms to the following rules:
146141
* - Correct length of characters
147142
* - Starts with a colon
148143
*
@@ -271,7 +266,7 @@ function endOfFileRecord(): string {
271266
function extLinAddressRecord(address: number): string {
272267
if (address < 0 || address > 0xffffffff) {
273268
throw new Error(
274-
`Address '${address}' for Extended Linear Address record is in range.`
269+
`Address '${address}' for Extended Linear Address record is out of range.`
275270
);
276271
}
277272
return createRecord(

0 commit comments

Comments
 (0)