Some small test loop cleanups (#1869)
This commit is contained in:
@@ -7,7 +7,7 @@ import { BN } from '../bn/index.js';
|
||||
import { _sqrt2pow53n, nSqrt } from './index.js';
|
||||
|
||||
// eslint-disable-next-line jest/no-export
|
||||
export const SQRT_TESTS: [string | number | BN | bigint, string | number][] = [
|
||||
export const TESTS: [value: string | number | BN | bigint, expected: string | number][] = [
|
||||
[0, 0],
|
||||
[1, 1],
|
||||
[4, 2],
|
||||
@@ -47,11 +47,13 @@ describe('nSqrt', (): void => {
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
SQRT_TESTS.forEach(([value, expected], index): void => {
|
||||
it(`calcs for test #${index}`, (): void => {
|
||||
expect(
|
||||
nSqrt(value) === BigInt(expected)
|
||||
).toEqual(true);
|
||||
describe('conversion tests', (): void => {
|
||||
TESTS.forEach(([value, expected], i): void => {
|
||||
it(`#${i}: calcs ${expected}`, (): void => {
|
||||
expect(
|
||||
nSqrt(value) === BigInt(expected)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,19 +4,57 @@
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { arrayRange } from '../array/index.js';
|
||||
import { TESTS } from '../bn/toU8a.spec.js';
|
||||
import { perf } from '../test/index.js';
|
||||
import { nToU8a } 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, positive 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, [255, 255, 255, 255], '-1'],
|
||||
[true, true, [254, 255, 255, 255], '-2'],
|
||||
[true, true, [235, 50, 164, 248], '-123456789'],
|
||||
[true, true, [0, 0, 0, 128], '-2147483648'],
|
||||
[true, true, [0, 0, 0, 240], '-268435456'],
|
||||
[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']
|
||||
];
|
||||
|
||||
const ptest = arrayRange(65536).map((v) => [v]);
|
||||
|
||||
describe('nToU8a', (): void => {
|
||||
describe('conversion tests', (): void => {
|
||||
for (let i = 0, count = TESTS.length; i < count; i++) {
|
||||
const [isLe, isNegative, numarr, strval] = TESTS[i];
|
||||
TESTS.forEach(([isLe, isNegative, numarr, strval], i): void => {
|
||||
const bitLength = numarr.length * 8;
|
||||
|
||||
it(`${i}: converts from ${strval} (bitLength=${bitLength}, isLe=${isLe}, isNegative=${isNegative})`, (): void => {
|
||||
it(`#${i}: converts from ${strval} (bitLength=${bitLength}, isLe=${isLe}, isNegative=${isNegative})`, (): void => {
|
||||
expect(
|
||||
nToU8a(
|
||||
BigInt(strval),
|
||||
@@ -24,7 +62,7 @@ describe('nToU8a', (): void => {
|
||||
)
|
||||
).toEqual(new Uint8Array(numarr));
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('converts null values to 0x00', (): void => {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { SQRT_TESTS } from '../bi/sqrt.spec.js';
|
||||
import { TESTS } from '../bi/sqrt.spec.js';
|
||||
import { BN, BN_SQRT_MAX_INTEGER, bnSqrt } from './index.js';
|
||||
|
||||
describe('bnSqrt', (): void => {
|
||||
@@ -25,11 +25,13 @@ describe('bnSqrt', (): void => {
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
SQRT_TESTS.forEach(([value, expected], index): void => {
|
||||
it(`calcs for test #${index}`, (): void => {
|
||||
expect(
|
||||
bnSqrt(value).eq(new BN(expected))
|
||||
).toEqual(true);
|
||||
describe('conversion tests', (): void => {
|
||||
TESTS.forEach(([value, expected], i): void => {
|
||||
it(`#${i}: calcs ${expected}`, (): void => {
|
||||
expect(
|
||||
bnSqrt(value).eq(new BN(expected))
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,68 +4,13 @@
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { arrayRange } from '../array/index.js';
|
||||
import { TESTS } from '../bi/toU8a.spec.js';
|
||||
import { perf } from '../test/index.js';
|
||||
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, positive 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, [255, 255, 255, 255], '-1'],
|
||||
[true, true, [254, 255, 255, 255], '-2'],
|
||||
[true, true, [235, 50, 164, 248], '-123456789'],
|
||||
[true, true, [0, 0, 0, 128], '-2147483648'],
|
||||
[true, true, [0, 0, 0, 240], '-268435456'],
|
||||
[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];
|
||||
const bitLength = numarr.length * 8;
|
||||
|
||||
it(`${i}: converts from ${strval} (bitLength=${bitLength}, isLe=${isLe}, isNegative=${isNegative})`, (): void => {
|
||||
expect(
|
||||
bnToU8a(
|
||||
new BN(strval),
|
||||
{ bitLength, isLe, isNegative }
|
||||
)
|
||||
).toEqual(new Uint8Array(numarr));
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it('converts null values to 0x00', (): void => {
|
||||
expect(
|
||||
bnToU8a(null)
|
||||
@@ -96,5 +41,20 @@ describe('bnToU8a', (): void => {
|
||||
).toEqual(new Uint8Array([0x56, 0x34, 0x12, 0x00]));
|
||||
});
|
||||
|
||||
describe('conversion tests', (): void => {
|
||||
TESTS.forEach(([isLe, isNegative, numarr, strval], i): void => {
|
||||
const bitLength = numarr.length * 8;
|
||||
|
||||
it(`#${i}: converts from ${strval} (bitLength=${bitLength}, isLe=${isLe}, isNegative=${isNegative})`, (): void => {
|
||||
expect(
|
||||
bnToU8a(
|
||||
new BN(strval),
|
||||
{ bitLength, isLe, isNegative }
|
||||
)
|
||||
).toEqual(new Uint8Array(numarr));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
perf('bnToU8a', 250000, ptest, bnToU8a);
|
||||
});
|
||||
|
||||
@@ -6,6 +6,24 @@
|
||||
import { BN } from '../bn/index.js';
|
||||
import { compactToU8a } from './index.js';
|
||||
|
||||
// Copied from https://github.com/paritytech/parity-codec/blob/master/src/codec.rs
|
||||
const TESTS: { expected: string, value: BN }[] = [
|
||||
{ expected: '00', value: new BN('0') },
|
||||
{ expected: 'fc', value: new BN('63') },
|
||||
{ expected: '01 01', value: new BN('64') },
|
||||
{ expected: 'fd ff', value: new BN('16383') },
|
||||
{ expected: '02 00 01 00', value: new BN('16384') },
|
||||
{ expected: 'fe ff ff ff', value: new BN('1073741823') },
|
||||
{ expected: '03 00 00 00 40', value: new BN('1073741824') },
|
||||
{ expected: '03 ff ff ff ff', value: new BN(`${1}${'0'.repeat(32)}`, 2).subn(1) },
|
||||
{ expected: '07 00 00 00 00 01', value: new BN(`${1}${'0'.repeat(32)}`, 2) },
|
||||
{ expected: '0b 00 00 00 00 00 01', value: new BN(`${1}${'0'.repeat(40)}`, 2) },
|
||||
{ expected: '0f 00 00 00 00 00 00 01', value: new BN(`${1}${'0'.repeat(48)}`, 2) },
|
||||
{ expected: '0f ff ff ff ff ff ff ff', value: new BN(`${1}${'0'.repeat(56)}`, 2).subn(1) },
|
||||
{ expected: '13 00 00 00 00 00 00 00 01', value: new BN(`${1}${'0'.repeat(56)}`, 2) },
|
||||
{ expected: '13 ff ff ff ff ff ff ff ff', value: new BN(`${1}${'0'.repeat(64)}`, 2).subn(1) }
|
||||
];
|
||||
|
||||
describe('encode', (): void => {
|
||||
it('encodes short u8', (): void => {
|
||||
expect(
|
||||
@@ -90,37 +108,15 @@ describe('encode', (): void => {
|
||||
expect(original.toString()).toEqual('123456');
|
||||
});
|
||||
|
||||
describe('from Rust', (): void => {
|
||||
// Copied from https://github.com/paritytech/parity-codec/blob/master/src/codec.rs
|
||||
const testCases = [
|
||||
{ expected: '00', value: new BN('0') },
|
||||
{ expected: 'fc', value: new BN('63') },
|
||||
{ expected: '01 01', value: new BN('64') },
|
||||
{ expected: 'fd ff', value: new BN('16383') },
|
||||
{ expected: '02 00 01 00', value: new BN('16384') },
|
||||
{ expected: 'fe ff ff ff', value: new BN('1073741823') },
|
||||
{ expected: '03 00 00 00 40', value: new BN('1073741824') },
|
||||
{ expected: '03 ff ff ff ff', value: new BN(`${1}${'0'.repeat(32)}`, 2).subn(1) },
|
||||
{ expected: '07 00 00 00 00 01', value: new BN(`${1}${'0'.repeat(32)}`, 2) },
|
||||
{ expected: '0b 00 00 00 00 00 01', value: new BN(`${1}${'0'.repeat(40)}`, 2) },
|
||||
{ expected: '0f 00 00 00 00 00 00 01', value: new BN(`${1}${'0'.repeat(48)}`, 2) },
|
||||
{ expected: '0f ff ff ff ff ff ff ff', value: new BN(`${1}${'0'.repeat(56)}`, 2).subn(1) },
|
||||
{ expected: '13 00 00 00 00 00 00 00 01', value: new BN(`${1}${'0'.repeat(56)}`, 2) },
|
||||
{ expected: '13 ff ff ff ff ff ff ff ff', value: new BN(`${1}${'0'.repeat(64)}`, 2).subn(1) }
|
||||
];
|
||||
|
||||
function testEncode (value: BN, expected: string): void {
|
||||
it(`encodes ${value.toString()}`, (): void => {
|
||||
describe('conversion tests', (): void => {
|
||||
TESTS.forEach(({ expected, value }, i): void => {
|
||||
it(`#${i}: encodes ${value.toString()}`, (): void => {
|
||||
expect(
|
||||
compactToU8a(value)
|
||||
).toEqual(
|
||||
Uint8Array.from(
|
||||
expected.split(' ').map((s): number => parseInt(s, 16))
|
||||
)
|
||||
Uint8Array.from(expected.split(' ').map((s) => parseInt(s, 16)))
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
testCases.forEach(({ expected, value }): void => testEncode(value, expected));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { TESTS } from '../bn/toU8a.spec.js';
|
||||
import { TESTS } from '../bi/toU8a.spec.js';
|
||||
import { perf } from '../test/index.js';
|
||||
import { u8aToBigInt } from './index.js';
|
||||
|
||||
@@ -17,43 +17,6 @@ describe('u8aToBigInt', (): void => {
|
||||
).toBe('3412');
|
||||
});
|
||||
|
||||
describe('conversion tests', (): void => {
|
||||
for (let i = 0, count = TESTS.length; i < count; i++) {
|
||||
const [isLe, isNegative, numarr, strval] = TESTS[i];
|
||||
|
||||
it(`${i}: creates ${strval} (bitLength=${numarr.length * 8}, isLe=${isLe}, isNegative=${isNegative})`, (): void => {
|
||||
expect(
|
||||
u8aToBigInt(
|
||||
new Uint8Array(numarr),
|
||||
{ isLe, isNegative }
|
||||
).toString()
|
||||
).toBe(strval);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('length tests', (): void => {
|
||||
[true, false].forEach((isLe) => {
|
||||
for (let i = 1; i < 32; i++) {
|
||||
const tu8a = [0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78];
|
||||
const tstr = tu8a.map((n) => n.toString(16));
|
||||
|
||||
it(`converts values with bitLength=${i * 8}, isLe=${isLe}`, (): void => {
|
||||
expect(
|
||||
u8aToBigInt(
|
||||
new Uint8Array(tu8a.slice(0, i)),
|
||||
{ isLe }
|
||||
).toString(16)
|
||||
).toBe(
|
||||
isLe
|
||||
? tstr.slice(0, i).reverse().join('')
|
||||
: tstr.slice(0, i).join('')
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('empty creation', (): void => {
|
||||
it('handles unsigned (le)', (): void => {
|
||||
expect(
|
||||
@@ -101,6 +64,41 @@ describe('u8aToBigInt', (): void => {
|
||||
).toBe(256n);
|
||||
});
|
||||
|
||||
describe('length tests', (): void => {
|
||||
[true, false].forEach((isLe) => {
|
||||
for (let i = 1; i < 32; i++) {
|
||||
const tu8a = [0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78];
|
||||
const tstr = tu8a.map((n) => n.toString(16));
|
||||
|
||||
it(`converts values with bitLength=${i * 8}, isLe=${isLe}`, (): void => {
|
||||
expect(
|
||||
u8aToBigInt(
|
||||
new Uint8Array(tu8a.slice(0, i)),
|
||||
{ isLe }
|
||||
).toString(16)
|
||||
).toBe(
|
||||
isLe
|
||||
? tstr.slice(0, i).reverse().join('')
|
||||
: tstr.slice(0, i).join('')
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('conversion tests', (): void => {
|
||||
TESTS.forEach(([isLe, isNegative, numarr, strval], i): void => {
|
||||
it(`#${i}: creates ${strval} (bitLength=${numarr.length * 8}, isLe=${isLe}, isNegative=${isNegative})`, (): void => {
|
||||
expect(
|
||||
u8aToBigInt(
|
||||
new Uint8Array(numarr),
|
||||
{ isLe, isNegative }
|
||||
).toString()
|
||||
).toBe(strval);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
perf('u8aToBigInt (i32)', 750_000, [[new Uint8Array([0x9c, 0x9c, 0x9c, 0x9c])]], (v: Uint8Array) => u8aToBigInt(v, { isNegative: true }));
|
||||
perf('u8aToBigInt (u32)', 750_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], u8aToBigInt);
|
||||
perf('u8aToBigInt (u64)', 750_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x68, 0x65, 0x6c, 0x6c])]], u8aToBigInt);
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { TESTS } from '../bn/toU8a.spec.js';
|
||||
import { TESTS } from '../bi/toU8a.spec.js';
|
||||
import { perf } from '../test/index.js';
|
||||
import { u8aToBn } from './index.js';
|
||||
|
||||
@@ -17,43 +17,6 @@ describe('u8aToBn', (): void => {
|
||||
).toBe('3412');
|
||||
});
|
||||
|
||||
describe('conversion tests', (): void => {
|
||||
for (let i = 0, count = TESTS.length; i < count; i++) {
|
||||
const [isLe, isNegative, numarr, strval] = TESTS[i];
|
||||
|
||||
it(`${i}: creates ${strval} (bitLength=${numarr.length * 8}, isLe=${isLe}, isNegative=${isNegative})`, (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array(numarr),
|
||||
{ isLe, isNegative }
|
||||
).toString()
|
||||
).toBe(strval);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('length tests', (): void => {
|
||||
[true, false].forEach((isLe) => {
|
||||
for (let i = 1; i < 32; i++) {
|
||||
const tu8a = [0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78];
|
||||
const tstr = tu8a.map((n) => n.toString(16));
|
||||
|
||||
it(`converts values with bitLength=${i * 8}, isLe=${isLe}`, (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array(tu8a.slice(0, i)),
|
||||
{ isLe }
|
||||
).toString(16)
|
||||
).toBe(
|
||||
isLe
|
||||
? tstr.slice(0, i).reverse().join('')
|
||||
: tstr.slice(0, i).join('')
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('empty creation', (): void => {
|
||||
it('handles unsigned (le)', (): void => {
|
||||
expect(
|
||||
@@ -101,6 +64,41 @@ describe('u8aToBn', (): void => {
|
||||
).toBe(256);
|
||||
});
|
||||
|
||||
describe('length tests', (): void => {
|
||||
[true, false].forEach((isLe) => {
|
||||
for (let i = 1; i < 32; i++) {
|
||||
const tu8a = [0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78];
|
||||
const tstr = tu8a.map((n) => n.toString(16));
|
||||
|
||||
it(`converts values with bitLength=${i * 8}, isLe=${isLe}`, (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array(tu8a.slice(0, i)),
|
||||
{ isLe }
|
||||
).toString(16)
|
||||
).toBe(
|
||||
isLe
|
||||
? tstr.slice(0, i).reverse().join('')
|
||||
: tstr.slice(0, i).join('')
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('conversion tests', (): void => {
|
||||
TESTS.forEach(([isLe, isNegative, numarr, strval], i): void => {
|
||||
it(`#${i}: creates ${strval} (bitLength=${numarr.length * 8}, isLe=${isLe}, isNegative=${isNegative})`, (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array(numarr),
|
||||
{ isLe, isNegative }
|
||||
).toString()
|
||||
).toBe(strval);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
perf('u8aToBn (i32)', 750_000, [[new Uint8Array([0x9c, 0x9c, 0x9c, 0x9c])]], (v: Uint8Array) => u8aToBn(v, { isNegative: true }));
|
||||
perf('u8aToBn (u32)', 750_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], u8aToBn);
|
||||
perf('u8aToBn (u64)', 750_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x68, 0x65, 0x6c, 0x6c])]], u8aToBn);
|
||||
|
||||
@@ -3,28 +3,13 @@
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { TESTS } from '../bn/toU8a.spec.js';
|
||||
import { TESTS } from '../bi/toU8a.spec.js';
|
||||
import { perf } from '../test/index.js';
|
||||
import { u8aToNumber } from './index.js';
|
||||
|
||||
const TESTS_NUM = TESTS.filter(([,, numarr]) => numarr.length <= 6);
|
||||
|
||||
describe('u8aToNumber', (): void => {
|
||||
describe('conversion tests', (): void => {
|
||||
for (let i = 0, count = TESTS_NUM.length; i < count; i++) {
|
||||
const [isLe, isNegative, numarr, strval] = TESTS_NUM[i];
|
||||
|
||||
it(`${i}: creates ${strval} (bitLength=${numarr.length * 8}, isLe=${isLe}, isNegative=${isNegative})`, (): void => {
|
||||
expect(
|
||||
u8aToNumber(
|
||||
new Uint8Array(numarr),
|
||||
{ isLe, isNegative }
|
||||
).toString()
|
||||
).toBe(strval);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('empty creation', (): void => {
|
||||
it('handles unsigned (le)', (): void => {
|
||||
expect(
|
||||
@@ -44,6 +29,19 @@ describe('u8aToNumber', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('conversion tests', (): void => {
|
||||
TESTS_NUM.forEach(([isLe, isNegative, numarr, strval], i): void => {
|
||||
it(`#${i}: creates ${strval} (bitLength=${numarr.length * 8}, isLe=${isLe}, isNegative=${isNegative})`, (): void => {
|
||||
expect(
|
||||
u8aToNumber(
|
||||
new Uint8Array(numarr),
|
||||
{ isLe, isNegative }
|
||||
).toString()
|
||||
).toBe(strval);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
perf('u8aToNumber (i32)', 750_000, [[new Uint8Array([0x9c, 0x9c, 0x9c, 0x9c])]], (v: Uint8Array) => u8aToNumber(v, { isNegative: true }));
|
||||
perf('u8aToNumber (u32)', 750_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], u8aToNumber);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user