Skip to content

Commit

Permalink
Re-use expectations inside bnToU8a
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Aug 22, 2023
1 parent 191029f commit 31c1a9a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 148 deletions.
161 changes: 51 additions & 110 deletions packages/util/src/bn/toU8a.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
});
2 changes: 1 addition & 1 deletion packages/util/src/u8a/toBigInt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

/// <reference types="@polkadot/dev-test/globals.d.ts" />

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 => {
Expand Down
37 changes: 1 addition & 36 deletions packages/util/src/u8a/toBn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,10 @@

/// <reference types="@polkadot/dev-test/globals.d.ts" />

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 => {
Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/u8a/toNumber.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

/// <reference types="@polkadot/dev-test/globals.d.ts" />

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 &&
Expand Down

0 comments on commit 31c1a9a

Please sign in to comment.