Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group Int/Uint tests, size check compare for Int #5709

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 57 additions & 30 deletions packages/types-codec/src/base/Int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');

Expand All @@ -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');
});
});

Expand All @@ -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());
});
});
});
});
96 changes: 49 additions & 47 deletions packages/types-codec/src/base/UInt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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('utilities', (): void => {
it('provides a toBigInt interface', (): void => {
expect(
new UInt(registry, 9876543210123456789n).toBigInt()
).toEqual(9876543210123456789n);
});
});

describe('eq', (): void => {
const test = new UInt(registry, 12345);
it('provides a toBn interface', (): void => {
expect(
new UInt(registry, 987).toBn().toNumber()
).toEqual(987);
});

it('compares against other BN values', (): void => {
expect(test.eq(new BN(12345))).toBe(true);
it('provides a toNumber interface', (): void => {
expect(
new UInt(registry, 4567).toNumber()
).toEqual(4567);
});

it('compares against other number values', (): void => {
expect(test.eq(12345)).toBe(true);
it('has a working toBigInt', (): void => {
expect(
new UInt(registry, 4567).toBigInt() + BigInt(1)
).toEqual(BigInt(4568));
});

it('compares against hex values', (): void => {
expect(test.eq('0x3039')).toBe(true);
it('has a sane inspect', (): void => {
expect(
new UInt(registry, '0x12', 16).inspect()
).toEqual({
outer: [new Uint8Array([0x12, 0x00])]
});
});
});

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);
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);
});
});

it('is true when full', (): void => {
expect(new UInt(registry, '0xffffffff', 32).isMax()).toEqual(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);
});
});
});

Expand Down