Skip to content

Commit 347b82d

Browse files
ihex: Add tests for findDataFieldLength() and other missing tests.
Mostly to be able to reach 100% coverage in this module.
1 parent 8af578e commit 347b82d

File tree

2 files changed

+113
-3
lines changed

2 files changed

+113
-3
lines changed

src/__tests__/ihex.spec.ts

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ describe('Test createRecord() for standard records', () => {
8585
ihex.createRecord(-1, ihex.RecordType.Data, new Uint8Array([]));
8686
}).toThrow('address out of range');
8787
});
88+
89+
it('Throws error when the record is invalid', () => {
90+
expect(() => {
91+
ihex.createRecord(0, 0xff, new Uint8Array([]));
92+
}).toThrow('is not valid');
93+
});
8894
});
8995

9096
describe('Test createRecord() for custom records', () => {
@@ -282,6 +288,20 @@ describe('Test parseRecord() for standard records', () => {
282288
}).toThrow('Could not parse');
283289
});
284290

291+
it('Throws error when record is invalid', () => {
292+
expect(() => {
293+
ihex.parseRecord(':000000');
294+
}).toThrow('Record length too small');
295+
});
296+
297+
it('Throws error when record is too large', () => {
298+
expect(() => {
299+
ihex.parseRecord(
300+
':2000600031F8000039F8000041F800008FFA00008FFA00008FFA00008FFA00008FFA0000FF40'
301+
);
302+
}).toThrow('Record length is too large');
303+
});
304+
285305
// TODO: Add tests for parsing EoF records
286306
// TODO: Add tests for parsing ExtendedSegmentAddress records
287307
// TODO: Add tests for parsing StartSegmentAddress records
@@ -322,7 +342,11 @@ describe('Test extLinAddressRecord()', () => {
322342
expect(ihex.extLinAddressRecord(0x72946)).toEqual(':020000040007F3');
323343
});
324344

325-
// TODO: Add tests for all thrown exceptions
345+
it('Throws error when address is out of range', () => {
346+
expect(() => {
347+
ihex.extLinAddressRecord(0x100000000);
348+
}).toThrow('Address record is out of range');
349+
});
326350
});
327351

328352
describe('Test blockStartRecord()', () => {
@@ -546,3 +570,89 @@ describe('Test iHexToRecordStrs()', () => {
546570
expect(ihex.iHexToRecordStrs('')).toEqual([]);
547571
});
548572
});
573+
574+
describe('Test findDataFieldLength()', () => {
575+
it('Standard 16-byte record hex', () => {
576+
const records = [
577+
':020000040000FA',
578+
':10000000C0070000D1060000D1000000B1060000CA',
579+
':1000100000000000000000000000000000000000E0',
580+
':100020000000000000000000000000005107000078',
581+
':100030000000000000000000DB000000E500000000',
582+
':10004000EF000000F9000000030100000D010000B6',
583+
':1000500017010000210100002B0100003501000004',
584+
':100160000968095808477020344909680958084740',
585+
':100170007420324909680958084778202F490968CE',
586+
':10018000095808477C202D490968095808478020EC',
587+
':100190002A490968095808478420284909680958E4',
588+
':020000040001F9',
589+
':10000000058209E003984179027909021143490404',
590+
':10001000490C0171090A417103AA04A90898FFF764',
591+
':1000200068FF0028EED0822C02D020460BB0F0BD35',
592+
':100030000020FBE730B50446406B002597B0002850',
593+
':00000001FF',
594+
];
595+
596+
const recordLength = ihex.findDataFieldLength(records);
597+
598+
expect(recordLength).toEqual(16);
599+
});
600+
601+
it('Finds a mixed of records with enough 32 byte long records', () => {
602+
const records = [
603+
':020000040000FA',
604+
':10000000C0070000D1060000D1000000B1060000CA',
605+
':1000100000000000000000000000000000000000E0',
606+
':100020000000000000000000000000005107000078',
607+
':100030000000000000000000DB000000E500000000',
608+
':10004000EF000000F9000000030100000D010000B6',
609+
':1000500017010000210100002B0100003501000004',
610+
':2000600031F8000039F8000041F800008FFA00008FFA00008FFA00008FFA00008FFA000040',
611+
':200080008FFA00008FFA00008FFA0000410101008FFA00008FFA00008FFA00008FFA00005E',
612+
':2000A0008FFA00008FFA000049F8000051F800008FFA00008FFA0000000000000000000092',
613+
':2000C0008FFA00008FFA00008FFA0000350101008FFA00008FFA00008FFA000000000000B3',
614+
':2000E000000000000000000000000000000000000000000000000000000000000000000000',
615+
':200100000000000000000000000000000000000000000000000000000000000000000000DF',
616+
':200120000000000000000000000000000000000000000000000000000000000000000000BF',
617+
':2001400000000000000000000000000000000000000000000000000000000000000000009F',
618+
':100160000968095808477020344909680958084740',
619+
':100170007420324909680958084778202F490968CE',
620+
':10018000095808477C202D490968095808478020EC',
621+
':100190002A490968095808478420284909680958E4',
622+
':1001A0000847882025490968095808478C202349B1',
623+
':1001B00009680958084790202049096809580847E4',
624+
':1001C00094201E4909680958084798201B49096866',
625+
':1001D000095808479C201949096809580847A02070',
626+
':020000040001F9',
627+
':10000000058209E003984179027909021143490404',
628+
':10001000490C0171090A417103AA04A90898FFF764',
629+
':1000200068FF0028EED0822C02D020460BB0F0BD35',
630+
':100030000020FBE730B50446406B002597B0002850',
631+
':200040000098C3F83415C3F83825D3F80012E26141F02001C3F800127A1906EB82025268EB',
632+
':2000600052B14FF404722948C3F8042303B0F0BD00293AD1002C3ED1D3F81021D3F8441186',
633+
':20008000D3F82441002AF3D03D4421481F4F214B002206EB8506944208BF3846B2619142E0',
634+
':2000A00018BF1846E2E70368A261C3F810E1D3F8100101900198C3F844E1D3F844010090A2',
635+
':2000C0000120E160C4F81CE0009CC3F83415C3F838251860C0E733B103680F484FF40472D0',
636+
':00000001FF',
637+
];
638+
639+
const recordLength = ihex.findDataFieldLength(records);
640+
641+
expect(recordLength).toEqual(32);
642+
});
643+
644+
it('Longer than 32 byte records throws an error', () => {
645+
const records = [
646+
':020000040000FA',
647+
':10000000C0070000D1060000D1000000B1060000CA',
648+
':2000600031F8000039F8000041F800008FFA00008FFA00008FFA00008FFA00008FFA000040',
649+
':300080008FFA00008FFA00008FFA0000410101008FFA00008FFA00008FFA00008FFA0000C0070000D1060000D1000000B106000028',
650+
':00000001FF',
651+
];
652+
const throwsError = () => {
653+
const recordLength = ihex.findDataFieldLength(records);
654+
};
655+
656+
expect(throwsError).toThrow('data size is too large');
657+
});
658+
});

src/ihex.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ function iHexToRecordStrs(iHexStr: string): string[] {
397397
* Iterates through the beginning of an array of Intel Hex records to find the
398398
* longest record data field length.
399399
*
400-
* Once it finds 10 records at the maximum size found so far (starts at 16
400+
* Once it finds 12 records at the maximum size found so far (starts at 16
401401
* bytes) it will stop iterating.
402402
*
403403
* This is useful to identify the expected max size of the data records for an
@@ -417,7 +417,7 @@ function findDataFieldLength(iHexRecords: string[]): number {
417417
} else if (dataBytesLength === maxDataBytes) {
418418
maxDataBytesCount++;
419419
}
420-
if (maxDataBytesCount > 10) {
420+
if (maxDataBytesCount > 12) {
421421
break;
422422
}
423423
}

0 commit comments

Comments
 (0)