From 30d5f5c118380c9161792d81b8d05e2d254337eb Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 23 Mar 2022 13:18:14 +0100 Subject: [PATCH] Additional BN & BigInt negative tests (#1443) --- packages/util/src/bn/toU8a.spec.ts | 8 +++++++- packages/util/src/u8a/toBigInt.spec.ts | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/util/src/bn/toU8a.spec.ts b/packages/util/src/bn/toU8a.spec.ts index 822b84a7b..cc5ae61a7 100644 --- a/packages/util/src/bn/toU8a.spec.ts +++ b/packages/util/src/bn/toU8a.spec.ts @@ -50,12 +50,18 @@ describe('bnToU8a', (): void => { ).toEqual(new Uint8Array([251, 46])); }); - it('converts negative numbers (bitLength)', (): void => { + it('converts negative numbers (LE)', (): void => { expect( bnToU8a(new BN(-1234), { bitLength: 32, isNegative: true }) ).toEqual(new Uint8Array([46, 251, 255, 255])); }); + it('converts negative numbers (LE, bitLength)', (): void => { + expect( + bnToU8a(new BN(-123456), { bitLength: 32, isNegative: true }) + ).toEqual(new Uint8Array([192, 29, 254, 255])); + }); + it('handles backwards compatibility', (): void => { expect( bnToU8a(new BN(1234), 32, false) diff --git a/packages/util/src/u8a/toBigInt.spec.ts b/packages/util/src/u8a/toBigInt.spec.ts index ad5cd5913..d15497121 100644 --- a/packages/util/src/u8a/toBigInt.spec.ts +++ b/packages/util/src/u8a/toBigInt.spec.ts @@ -42,10 +42,10 @@ describe('u8aToBigInt', (): void => { it('handles negative numbers (little-endian)', (): void => { expect( u8aToBigInt( - new Uint8Array([46, 251]), + new Uint8Array([192, 29, 254]), { isLe: true, isNegative: true } ) - ).toEqual(-1234n); + ).toEqual(-123456n); }); it('handles negative numbers (big-endian)', (): void => {