Skip to content

Commit

Permalink
toNumber uses exposed toBn tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Aug 22, 2023
1 parent 54b65b0 commit 26e33f0
Showing 1 changed file with 15 additions and 158 deletions.
173 changes: 15 additions & 158 deletions packages/util/src/u8a/toNumber.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit 26e33f0

Please sign in to comment.