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

Adds tests cases for issue #5702 #5703

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Changes from 1 commit
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
103 changes: 103 additions & 0 deletions packages/types-codec/src/base/Int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,109 @@
).toEqual(new Uint8Array([46, 251, 255, 255, 255, 255, 255, 255]));
});

it('decodes edge case to bytes correctly', (): void => {
// Zero
expect(
new Int(registry, 0, 8).toU8a()
).toEqual(new Uint8Array([0]));

expect(
new Int(registry, 0, 16).toU8a()
).toEqual(new Uint8Array([0, 0]));

expect(
new Int(registry, 0, 32).toU8a()
).toEqual(new Uint8Array([0, 0, 0, 0]));

expect(
new Int(registry, 0, 64).toU8a()
).toEqual(new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]));

expect(
new Int(registry, 0, 128).toU8a()
).toEqual(new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));

// One
expect(
new Int(registry, 0, 8).toU8a()
).toEqual(new Uint8Array([1]));

expect(
new Int(registry, 1, 16).toU8a()
).toEqual(new Uint8Array([1, 0]));

expect(
new Int(registry, 1, 32).toU8a()
).toEqual(new Uint8Array([1, 0, 0, 0]));

expect(
new Int(registry, 1, 64).toU8a()
).toEqual(new Uint8Array([1, 0, 0, 0, 0, 0, 0, 0]));

expect(
new Int(registry, 1, 128).toU8a()
).toEqual(new Uint8Array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));


Check failure on line 90 in packages/types-codec/src/base/Int.spec.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

More than 1 blank line not allowed
peetzweg marked this conversation as resolved.
Show resolved Hide resolved
// MIN
expect(
new Int(registry, -128, 8).toU8a()
).toEqual(new Uint8Array([128]));

expect(
new Int(registry, -32768, 16).toU8a()
).toEqual(new Uint8Array([0, 128]));

// MAX
expect(
new Int(registry, 127, 8).toU8a()
).toEqual(new Uint8Array([127]));

expect(
new Int(registry, 32767, 16).toU8a()
).toEqual(new Uint8Array([255, 127]));
});

it('decodes edge case to js number', (): void => {
// Zero
expect(
new Int(registry, new Uint8Array([0]), 8).toNumber()
).toEqual(0);

expect(
new Int(registry, new Uint8Array([0, 0]), 16).toNumber()
).toEqual(0);

jacogr marked this conversation as resolved.
Show resolved Hide resolved

Check failure on line 120 in packages/types-codec/src/base/Int.spec.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

More than 1 blank line not allowed
// One
expect(
new Int(registry, new Uint8Array([1]), 8).toNumber()
).toEqual(1);

expect(
new Int(registry, new Uint8Array([1, 0]), 16).toNumber()
).toEqual(1);

// MIN
expect(
new Int(registry, new Uint8Array([128]), 8).toNumber()
).toEqual(-128);

expect(
new Int(registry, new Uint8Array([0, 128]), 16).toNumber()
).toEqual(-128);

// MAX
expect(
new Int(registry, new Uint8Array([127]), 8).toNumber()
).toEqual(127);

expect(
new Int(registry, new Uint8Array([255, 127]), 16).toNumber()
).toEqual(32767);
});

jacogr marked this conversation as resolved.
Show resolved Hide resolved

Check failure on line 149 in packages/types-codec/src/base/Int.spec.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

More than 1 blank line not allowed
it('converts to Little Endian from the provided value (bitLength)', (): void => {
expect(
new Int(registry, -1234, 32).toU8a()
Expand Down
Loading