Group Int/Uint tests, size check compare for Int (#5709)
* Group In/Uint tests, size check compare for Int * Adjust
This commit is contained in:
@@ -3,30 +3,27 @@
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import type { UIntBitLength } from '../types/index.js';
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types';
|
||||
import { Int } from '@polkadot/types-codec';
|
||||
import { BN } from '@polkadot/util';
|
||||
|
||||
const TESTS: [bitLength: UIntBitLength, value: string | number | Uint8Array, expected?: string][] = [
|
||||
[8, 0],
|
||||
[8, 127],
|
||||
[8, -128],
|
||||
[8, new Uint8Array([0])],
|
||||
[8, new Uint8Array([127])],
|
||||
[8, new Uint8Array([128]), '-128'],
|
||||
[32, 0],
|
||||
[32, 2147483647],
|
||||
[32, -2147483648]
|
||||
];
|
||||
|
||||
describe('Int', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
it('provides a toBigInt interface', (): void => {
|
||||
expect(
|
||||
new Int(registry, -1234).toBigInt()
|
||||
).toEqual(-1234n);
|
||||
});
|
||||
|
||||
it('provides a toBn interface', (): void => {
|
||||
expect(
|
||||
new Int(registry, -1234).toBn().toNumber()
|
||||
).toEqual(-1234);
|
||||
});
|
||||
|
||||
it('provides a toNumber interface', (): void => {
|
||||
expect(
|
||||
new Int(registry, -1234).toNumber()
|
||||
).toEqual(-1234);
|
||||
});
|
||||
|
||||
it('can construct via a single-entry struct', (): void => {
|
||||
expect(
|
||||
// @ts-expect-error We could receive these via JSON
|
||||
@@ -50,13 +47,6 @@ describe('Int', (): void => {
|
||||
).toEqual(new Uint8Array([46, 251, 255, 255]));
|
||||
});
|
||||
|
||||
it('converts to hex/string', (): void => {
|
||||
const i = new Int(registry, '0x12', 16);
|
||||
|
||||
expect(i.toHex()).toEqual('0x0012');
|
||||
expect(i.toString()).toEqual('18');
|
||||
});
|
||||
|
||||
it('converts to equivalents', (): void => {
|
||||
const a = new Int(registry, '-123');
|
||||
|
||||
@@ -74,11 +64,38 @@ describe('Int', (): void => {
|
||||
).toEqual(0);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new Int(registry, '0x12', 16).inspect()
|
||||
).toEqual({
|
||||
outer: [new Uint8Array([0x12, 0x00])]
|
||||
describe('utilities', (): void => {
|
||||
it('provides a toBigInt interface', (): void => {
|
||||
expect(
|
||||
new Int(registry, -1234).toBigInt()
|
||||
).toEqual(-1234n);
|
||||
});
|
||||
|
||||
it('provides a toBn interface', (): void => {
|
||||
expect(
|
||||
new Int(registry, -1234).toBn().toNumber()
|
||||
).toEqual(-1234);
|
||||
});
|
||||
|
||||
it('provides a toNumber interface', (): void => {
|
||||
expect(
|
||||
new Int(registry, -1234).toNumber()
|
||||
).toEqual(-1234);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new Int(registry, '0x12', 16).inspect()
|
||||
).toEqual({
|
||||
outer: [new Uint8Array([0x12, 0x00])]
|
||||
});
|
||||
});
|
||||
|
||||
it('converts to hex/string', (): void => {
|
||||
const i = new Int(registry, '0x12', 16);
|
||||
|
||||
expect(i.toHex()).toEqual('0x0012');
|
||||
expect(i.toString()).toEqual('18');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -95,4 +112,14 @@ describe('Int', (): void => {
|
||||
).toEqual('SomethingElse');
|
||||
});
|
||||
});
|
||||
|
||||
describe('conversion tests', (): void => {
|
||||
TESTS.forEach(([bitLength, input, expected], i): void => {
|
||||
it(`#${i}: converts ${input as string}`, (): void => {
|
||||
expect(
|
||||
new Int(registry, Array.isArray(input) ? new Uint8Array(input) : input, bitLength).toString()
|
||||
).toEqual(expected || new BN(input).toString());
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -70,30 +70,6 @@ describe('UInt', (): void => {
|
||||
).toEqual('123,456,789,123,456,789,123,456,789');
|
||||
});
|
||||
|
||||
it('provides a toBigInt interface', (): void => {
|
||||
expect(
|
||||
new UInt(registry, 9876543210123456789n).toBigInt()
|
||||
).toEqual(9876543210123456789n);
|
||||
});
|
||||
|
||||
it('provides a toBn interface', (): void => {
|
||||
expect(
|
||||
new UInt(registry, 987).toBn().toNumber()
|
||||
).toEqual(987);
|
||||
});
|
||||
|
||||
it('provides a toNumber interface', (): void => {
|
||||
expect(
|
||||
new UInt(registry, 4567).toNumber()
|
||||
).toEqual(4567);
|
||||
});
|
||||
|
||||
it('has a working toBigInt', (): void => {
|
||||
expect(
|
||||
new UInt(registry, 4567).toBigInt() + BigInt(1)
|
||||
).toEqual(BigInt(4568));
|
||||
});
|
||||
|
||||
it('converts to Little Endian from the provided value', (): void => {
|
||||
expect(
|
||||
new UInt(registry, 1234567).toU8a()
|
||||
@@ -128,40 +104,66 @@ describe('UInt', (): void => {
|
||||
expect(new UInt(registry, 1, 256).toJSON()).toEqual('0x0000000000000000000000000000000000000000000000000000000000000001');
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new UInt(registry, '0x12', 16).inspect()
|
||||
).toEqual({
|
||||
outer: [new Uint8Array([0x12, 0x00])]
|
||||
});
|
||||
});
|
||||
|
||||
describe('eq', (): void => {
|
||||
const test = new UInt(registry, 12345);
|
||||
|
||||
it('compares against other BN values', (): void => {
|
||||
expect(test.eq(new BN(12345))).toBe(true);
|
||||
describe('utilities', (): void => {
|
||||
it('provides a toBigInt interface', (): void => {
|
||||
expect(
|
||||
new UInt(registry, 9876543210123456789n).toBigInt()
|
||||
).toEqual(9876543210123456789n);
|
||||
});
|
||||
|
||||
it('compares against other number values', (): void => {
|
||||
expect(test.eq(12345)).toBe(true);
|
||||
it('provides a toBn interface', (): void => {
|
||||
expect(
|
||||
new UInt(registry, 987).toBn().toNumber()
|
||||
).toEqual(987);
|
||||
});
|
||||
|
||||
it('compares against hex values', (): void => {
|
||||
expect(test.eq('0x3039')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isMax()', (): void => {
|
||||
it('is false where not full', (): void => {
|
||||
expect(new UInt(registry, '0x1234', 32).isMax()).toEqual(false);
|
||||
expect(new UInt(registry, '0xffffff', 32).isMax()).toEqual(false);
|
||||
expect(new UInt(registry, '0x12345678', 32).isMax()).toEqual(false);
|
||||
expect(new UInt(registry, '0xfffffff0', 32).isMax()).toEqual(false);
|
||||
it('provides a toNumber interface', (): void => {
|
||||
expect(
|
||||
new UInt(registry, 4567).toNumber()
|
||||
).toEqual(4567);
|
||||
});
|
||||
|
||||
it('is true when full', (): void => {
|
||||
expect(new UInt(registry, '0xffffffff', 32).isMax()).toEqual(true);
|
||||
it('has a working toBigInt', (): void => {
|
||||
expect(
|
||||
new UInt(registry, 4567).toBigInt() + BigInt(1)
|
||||
).toEqual(BigInt(4568));
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new UInt(registry, '0x12', 16).inspect()
|
||||
).toEqual({
|
||||
outer: [new Uint8Array([0x12, 0x00])]
|
||||
});
|
||||
});
|
||||
|
||||
describe('eq', (): void => {
|
||||
const test = new UInt(registry, 12345);
|
||||
|
||||
it('compares against other BN values', (): void => {
|
||||
expect(test.eq(new BN(12345))).toBe(true);
|
||||
});
|
||||
|
||||
it('compares against other number values', (): void => {
|
||||
expect(test.eq(12345)).toBe(true);
|
||||
});
|
||||
|
||||
it('compares against hex values', (): void => {
|
||||
expect(test.eq('0x3039')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isMax()', (): void => {
|
||||
it('is false where not full', (): void => {
|
||||
expect(new UInt(registry, '0x1234', 32).isMax()).toEqual(false);
|
||||
expect(new UInt(registry, '0xffffff', 32).isMax()).toEqual(false);
|
||||
expect(new UInt(registry, '0x12345678', 32).isMax()).toEqual(false);
|
||||
expect(new UInt(registry, '0xfffffff0', 32).isMax()).toEqual(false);
|
||||
});
|
||||
|
||||
it('is true when full', (): void => {
|
||||
expect(new UInt(registry, '0xffffffff', 32).isMax()).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user