diff --git a/packages/util/src/bn/toU8a.spec.ts b/packages/util/src/bn/toU8a.spec.ts index 3b45593027..7e9f4f6721 100644 --- a/packages/util/src/bn/toU8a.spec.ts +++ b/packages/util/src/bn/toU8a.spec.ts @@ -9,7 +9,58 @@ import { BN, bnToU8a } from './index.js'; const ptest = arrayRange(65536).map((v) => [v]); +// eslint-disable-next-line jest/no-export +export const TESTS: [isLe: boolean, isNegative: boolean, numarr: number[], strval: string][] = [ + // LE, positive numbers + [true, false, [0x12], '18'], + [true, false, [0x12, 0x34], '13330'], + [true, false, [0x12, 0x34, 0x56], '5649426'], + [true, false, [0x12, 0x34, 0x56, 0x78], '2018915346'], + [true, false, [0x12, 0x34, 0x56, 0x78, 0x9a], '663443878930'], + [true, false, [0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc], '207371629900818'], + [true, false, [0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78], '159954953172672629770948536149615195154'], + // LE, positivive numbers (w/ signed flag) + [true, true, [12], '12'], + [true, true, [210, 4], '1234'], + [true, true, [64, 226, 1], '123456'], + [true, true, [21, 205, 91, 7], '123456789'], + [true, true, [203, 36, 104, 12, 8], '34567890123'], + [true, true, [255, 159, 114, 78, 24, 9], '9999999999999'], + // LE, negative numbers + [true, true, [244], '-12'], + [true, true, [46, 251], '-1234'], + [true, true, [192, 29, 254], '-123456'], + [true, true, [235, 50, 164, 248], '-123456789'], + [true, true, [65, 86, 129, 173, 254], '-5678999999'], + [true, true, [1, 96, 141, 177, 231, 246], '-9999999999999'], + [true, true, [1, 0, 156, 88, 76, 73, 31, 242], '-999999999999999999'], + // BE + [false, false, [0x12], '18'], + [false, false, [0x12, 0x34], '4660'], + [false, false, [0x12, 0x34, 0x56], '1193046'], + [false, false, [0x12, 0x34, 0x56, 0x78], '305419896'], + [false, true, [0xf2, 0x34, 0x56, 0x78], '-231451016'], + [false, false, [0x12, 0x34, 0x56, 0x78, 0x9a], '78187493530'], + [false, false, [0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc], '20015998343868'], + [false, false, [0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78], '24197857161011715162171839636988778104'] +]; + describe('bnToU8a', (): void => { + describe('conversion tests', (): void => { + for (let i = 0, count = TESTS.length; i < count; i++) { + const [isLe, isNegative, numarr, strval] = TESTS[i]; + + it(`${i}: converts from ${strval} (bitLength=${numarr.length * 8}, isLe=${isLe}, isNegative=${isNegative})`, (): void => { + expect( + bnToU8a( + new BN(strval), + { isLe, isNegative } + ) + ).toEqual(new Uint8Array(numarr)); + }); + } + }); + it('converts null values to 0x00', (): void => { expect( bnToU8a(null) @@ -40,115 +91,5 @@ describe('bnToU8a', (): void => { ).toEqual(new Uint8Array([0x56, 0x34, 0x12, 0x00])); }); - describe('signed', (): void => { - it('converts positive numbers (BE, i8)', (): void => { - expect( - bnToU8a(new BN(12), { isLe: false, isNegative: true }) - ).toEqual(new Uint8Array([12])); - }); - - it('converts positive numbers (BE, i32)', (): void => { - expect( - bnToU8a(new BN(123456789), { isLe: false, isNegative: true }) - ).toEqual(new Uint8Array([7, 91, 205, 21])); - }); - - it('converts positive numbers (LE, i8)', (): void => { - expect( - bnToU8a(new BN(12), { isNegative: true }) - ).toEqual(new Uint8Array([12])); - }); - - it('converts positive numbers (LE, i16)', (): void => { - expect( - bnToU8a(new BN(1234), { isNegative: true }) - ).toEqual(new Uint8Array([210, 4])); - }); - - it('converts positive numbers (LE, i24)', (): void => { - expect( - bnToU8a(new BN(123456), { isNegative: true }) - ).toEqual(new Uint8Array([64, 226, 1])); - }); - - it('converts positive numbers (LE, i32)', (): void => { - expect( - bnToU8a(new BN(123456789), { isNegative: true }) - ).toEqual(new Uint8Array([21, 205, 91, 7])); - }); - - it('converts positive numbers (LE, i40)', (): void => { - expect( - bnToU8a(new BN(34567890123), { isNegative: true }) - ).toEqual(new Uint8Array([203, 36, 104, 12, 8])); - }); - - it('converts positive numbers (LE, i48)', (): void => { - expect( - bnToU8a(new BN(9999999999999), { isNegative: true }) - ).toEqual(new Uint8Array([255, 159, 114, 78, 24, 9])); - }); - - it('converts negative numbers (BE)', (): void => { - expect( - bnToU8a(new BN(-1234), { isLe: false, isNegative: true }) - ).toEqual(new Uint8Array([251, 46])); - }); - - it('converts negative numbers (LE, i8)', (): void => { - expect( - bnToU8a(new BN(-12), { isNegative: true }) - ).toEqual(new Uint8Array([244])); - }); - - it('converts negative numbers (LE, i16)', (): void => { - expect( - bnToU8a(new BN(-1234), { isNegative: true }) - ).toEqual(new Uint8Array([46, 251])); - }); - - it('converts negative numbers (LE, i24)', (): void => { - expect( - bnToU8a(new BN(-123456), { isNegative: true }) - ).toEqual(new Uint8Array([192, 29, 254])); - }); - - it('converts negative numbers (LE, i32)', (): void => { - expect( - bnToU8a(new BN(-123456789), { isNegative: true }) - ).toEqual(new Uint8Array([235, 50, 164, 248])); - }); - - it('converts negative numbers (LE, i40)', (): void => { - expect( - bnToU8a(new BN(-5678999999), { isNegative: true }) - ).toEqual(new Uint8Array([65, 86, 129, 173, 254])); - }); - - it('converts negative numbers (LE, i48)', (): void => { - expect( - bnToU8a(new BN(-9999999999999), { isNegative: true }) - ).toEqual(new Uint8Array([1, 96, 141, 177, 231, 246])); - }); - - it('converts negative numbers (LE, i64)', (): void => { - expect( - bnToU8a(new BN('-999999999999999999'), { isNegative: true }) - ).toEqual(new Uint8Array([1, 0, 156, 88, 76, 73, 31, 242])); - }); - - it('converts negative numbers (LE, bitLength)', (): void => { - expect( - bnToU8a(new BN(-1234), { bitLength: 32, isNegative: true }) - ).toEqual(new Uint8Array([46, 251, 255, 255])); - }); - - it('converts negative numbers (LE, bitLength, check)', (): void => { - expect( - bnToU8a(new BN(-123456), { bitLength: 32, isNegative: true }) - ).toEqual(new Uint8Array([192, 29, 254, 255])); - }); - }); - perf('bnToU8a', 250000, ptest, bnToU8a); }); diff --git a/packages/util/src/u8a/toBigInt.spec.ts b/packages/util/src/u8a/toBigInt.spec.ts index 3704661452..283eff6864 100644 --- a/packages/util/src/u8a/toBigInt.spec.ts +++ b/packages/util/src/u8a/toBigInt.spec.ts @@ -3,9 +3,9 @@ /// +import { TESTS } from '../bn/toU8a.spec.js'; import { perf } from '../test/index.js'; import { u8aToBigInt } from './index.js'; -import { TESTS } from './toBn.spec.js'; // test-cases are the same as in u8aToBn describe('u8aToBigInt', (): void => { diff --git a/packages/util/src/u8a/toBn.spec.ts b/packages/util/src/u8a/toBn.spec.ts index 1a450ebf8c..4dd824a032 100644 --- a/packages/util/src/u8a/toBn.spec.ts +++ b/packages/util/src/u8a/toBn.spec.ts @@ -3,45 +3,10 @@ /// +import { TESTS } from '../bn/toU8a.spec.js'; import { perf } from '../test/index.js'; import { u8aToBn } from './index.js'; -// eslint-disable-next-line jest/no-export -export const TESTS: [isLe: boolean, isNegative: boolean, numarr: number[], strval: string][] = [ - // LE, positive numbers - [true, false, [0x12], '18'], - [true, false, [0x12, 0x34], '13330'], - [true, false, [0x12, 0x34, 0x56], '5649426'], - [true, false, [0x12, 0x34, 0x56, 0x78], '2018915346'], - [true, false, [0x12, 0x34, 0x56, 0x78, 0x9a], '663443878930'], - [true, false, [0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc], '207371629900818'], - [true, false, [0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78], '159954953172672629770948536149615195154'], - // LE, positivive numbers (w/ signed flag) - [true, true, [12], '12'], - [true, true, [210, 4], '1234'], - [true, true, [64, 226, 1], '123456'], - [true, true, [21, 205, 91, 7], '123456789'], - [true, true, [203, 36, 104, 12, 8], '34567890123'], - [true, true, [255, 159, 114, 78, 24, 9], '9999999999999'], - // LE, negative numbers - [true, true, [244], '-12'], - [true, true, [46, 251], '-1234'], - [true, true, [192, 29, 254], '-123456'], - [true, true, [235, 50, 164, 248], '-123456789'], - [true, true, [65, 86, 129, 173, 254], '-5678999999'], - [true, true, [1, 96, 141, 177, 231, 246], '-9999999999999'], - [true, true, [1, 0, 156, 88, 76, 73, 31, 242], '-999999999999999999'], - // BE - [false, false, [0x12], '18'], - [false, false, [0x12, 0x34], '4660'], - [false, false, [0x12, 0x34, 0x56], '1193046'], - [false, false, [0x12, 0x34, 0x56, 0x78], '305419896'], - [false, true, [0xf2, 0x34, 0x56, 0x78], '-231451016'], - [false, false, [0x12, 0x34, 0x56, 0x78, 0x9a], '78187493530'], - [false, false, [0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc], '20015998343868'], - [false, false, [0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78], '24197857161011715162171839636988778104'] -]; - // test-cases are the same as in u8aToBigInt describe('u8aToBn', (): void => { it('converts little-endian by default', (): void => { diff --git a/packages/util/src/u8a/toNumber.spec.ts b/packages/util/src/u8a/toNumber.spec.ts index 2c05a8f9fa..3a14a43b7d 100644 --- a/packages/util/src/u8a/toNumber.spec.ts +++ b/packages/util/src/u8a/toNumber.spec.ts @@ -3,9 +3,9 @@ /// +import { TESTS } from '../bn/toU8a.spec.js'; import { perf } from '../test/index.js'; import { u8aToNumber } from './index.js'; -import { TESTS } from './toBn.spec.js'; const TESTS_NUM = TESTS.filter(([isLe,, numarr]) => isLe === true &&