From 26e33f04188581c7da616290e57cacbee37d569a Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 22 Aug 2023 09:57:57 +0300 Subject: [PATCH] toNumber uses exposed toBn tests --- packages/util/src/u8a/toNumber.spec.ts | 173 +++---------------------- 1 file changed, 15 insertions(+), 158 deletions(-) diff --git a/packages/util/src/u8a/toNumber.spec.ts b/packages/util/src/u8a/toNumber.spec.ts index 3220494477..2c05a8f9fa 100644 --- a/packages/util/src/u8a/toNumber.spec.ts +++ b/packages/util/src/u8a/toNumber.spec.ts @@ -5,170 +5,27 @@ import { perf } from '../test/index.js'; import { u8aToNumber } from './index.js'; +import { TESTS } from './toBn.spec.js'; -describe('u8aToNumber', (): void => { - describe('unsigned', (): void => { - it('converts values (u8)', (): void => { - expect( - u8aToNumber( - new Uint8Array([0x12]) - ) - ).toBe(18); - }); - - it('converts values (u16)', (): void => { - expect( - u8aToNumber( - new Uint8Array([0x12, 0x34]) - ) - ).toBe(13330); - }); - - it('converts values (u24)', (): void => { - expect( - u8aToNumber( - new Uint8Array([0x12, 0x34, 0x56]) - ) - ).toBe(5649426); - }); - - it('converts values (u32)', (): void => { - expect( - u8aToNumber( - new Uint8Array([0x12, 0x34, 0x56, 0x78]) - ) - ).toBe(2018915346); - }); - - it('converts values (u40)', (): void => { - expect( - u8aToNumber( - new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x9a]) - ) - ).toBe(663443878930); - }); - - it('converts values (u48)', (): void => { - expect( - u8aToNumber( - new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc]) - ) - ).toBe(207371629900818); - }); - }); - - describe('signed', (): void => { - describe('positive values', (): void => { - it('converts positive numbers (i8)', (): void => { - expect( - u8aToNumber( - new Uint8Array([12]), - { isNegative: true } - ) - ).toBe(12); - }); - - it('converts positive numbers (i16)', (): void => { - expect( - u8aToNumber( - new Uint8Array([210, 4]), - { isNegative: true } - ) - ).toBe(1234); - }); +const TESTS_NUM = TESTS.filter(([isLe,, numarr]) => + isLe === true && + numarr.length <= 6 +); - it('converts positive numbers (i24)', (): void => { - expect( - u8aToNumber( - new Uint8Array([64, 226, 1]), - { isNegative: true } - ) - ).toBe(123456); - }); - - it('converts positive numbers (i32)', (): void => { - expect( - u8aToNumber( - new Uint8Array([21, 205, 91, 7]), - { isNegative: true } - ) - ).toBe(123456789); - }); - - it('converts positive numbers (i40)', (): void => { - expect( - u8aToNumber( - new Uint8Array([203, 36, 104, 12, 8]), - { isNegative: true } - ) - ).toBe(34567890123); - }); - - it('converts positive numbers (i48)', (): void => { - expect( - u8aToNumber( - new Uint8Array([255, 159, 114, 78, 24, 9]), - { isNegative: true } - ) - ).toBe(9999999999999); - }); - }); - - describe('negative values', (): void => { - it('converts values (i8)', (): void => { - expect( - u8aToNumber( - new Uint8Array([244]), - { isNegative: true } - ) - ).toBe(-12); - }); - - it('converts values (i16)', (): void => { - expect( - u8aToNumber( - new Uint8Array([46, 251]), - { isNegative: true } - ) - ).toBe(-1234); - }); - - it('converts values (i24)', (): void => { - expect( - u8aToNumber( - new Uint8Array([192, 29, 254]), - { isNegative: true } - ) - ).toBe(-123456); - }); - - it('converts values (i32)', (): void => { - expect( - u8aToNumber( - new Uint8Array([235, 50, 164, 248]), - { isNegative: true } - ) - ).toBe(-123456789); - }); - - it('converts values (i40)', (): void => { - expect( - u8aToNumber( - new Uint8Array([65, 86, 129, 173, 254]), - { isNegative: true } - ) - ).toBe(-5678999999); - }); +describe('u8aToNumber', (): void => { + describe('conversion tests', (): void => { + for (let i = 0, count = TESTS_NUM.length; i < count; i++) { + const [, isNegative, numarr, strval] = TESTS_NUM[i]; - it('converts values (i48)', (): void => { + it(`${i}: creates ${strval} (bitLength=${numarr.length * 8}, isNegative=${isNegative})`, (): void => { expect( u8aToNumber( - new Uint8Array([1, 96, 141, 177, 231, 246]), - { isNegative: true } - ) - ).toBe(-9999999999999); + new Uint8Array(numarr), + { isNegative } + ).toString() + ).toBe(strval); }); - }); + } }); describe('empty creation', (): void => {