From f33bbc8340d66f46b77da041bfc1467b3d33c450 Mon Sep 17 00:00:00 2001 From: peetzweg/ Date: Thu, 17 Aug 2023 13:25:22 +0200 Subject: [PATCH 1/4] adds tests cases describe in #5702 --- packages/types-codec/src/base/Int.spec.ts | 103 ++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/packages/types-codec/src/base/Int.spec.ts b/packages/types-codec/src/base/Int.spec.ts index 852b004db206..fcc72b4c6bd4 100644 --- a/packages/types-codec/src/base/Int.spec.ts +++ b/packages/types-codec/src/base/Int.spec.ts @@ -44,6 +44,109 @@ describe('Int', (): void => { ).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])); + + + // 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); + + + // 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); + }); + + it('converts to Little Endian from the provided value (bitLength)', (): void => { expect( new Int(registry, -1234, 32).toU8a() From 6fa42f5a62790b556ff5db723ea1a72ef7c65136 Mon Sep 17 00:00:00 2001 From: peetzweg/ Date: Fri, 25 Aug 2023 12:51:36 +0200 Subject: [PATCH 2/4] fix wrong test outputs --- packages/types-codec/src/base/Int.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/types-codec/src/base/Int.spec.ts b/packages/types-codec/src/base/Int.spec.ts index 8e92baae2a52..1aaab8adf2de 100644 --- a/packages/types-codec/src/base/Int.spec.ts +++ b/packages/types-codec/src/base/Int.spec.ts @@ -66,7 +66,7 @@ describe('Int', (): void => { // One expect( new Int(registry, 0, 8).toU8a() - ).toEqual(new Uint8Array([1])); + ).toEqual(new Uint8Array([0])); expect( new Int(registry, 1, 16).toU8a() @@ -130,7 +130,7 @@ describe('Int', (): void => { ).toEqual(-128); expect( - new Int(registry, new Uint8Array([0, 128]), 16).toNumber() + new Int(registry, new Uint8Array([128, 255]), 16).toNumber() ).toEqual(-128); // MAX From 52259859ccb2e2fd28f72d3e38b2039452070978 Mon Sep 17 00:00:00 2001 From: peetzweg/ <839848+peetzweg@users.noreply.github.com> Date: Tue, 29 Aug 2023 11:43:50 +0200 Subject: [PATCH 3/4] Update packages/types-codec/src/base/Int.spec.ts Co-authored-by: Jaco --- packages/types-codec/src/base/Int.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/types-codec/src/base/Int.spec.ts b/packages/types-codec/src/base/Int.spec.ts index 1aaab8adf2de..b296f7950cb1 100644 --- a/packages/types-codec/src/base/Int.spec.ts +++ b/packages/types-codec/src/base/Int.spec.ts @@ -84,7 +84,6 @@ describe('Int', (): void => { 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])); - // MIN expect( new Int(registry, -128, 8).toU8a() From 2897dfc08a71e3563ec15c969c364cb1ffc0d540 Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 29 Aug 2023 13:02:29 +0300 Subject: [PATCH 4/4] Apply suggestions from code review --- packages/types-codec/src/base/Int.spec.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/types-codec/src/base/Int.spec.ts b/packages/types-codec/src/base/Int.spec.ts index b296f7950cb1..9e85ed07cca9 100644 --- a/packages/types-codec/src/base/Int.spec.ts +++ b/packages/types-codec/src/base/Int.spec.ts @@ -113,7 +113,6 @@ describe('Int', (): void => { new Int(registry, new Uint8Array([0, 0]), 16).toNumber() ).toEqual(0); - // One expect( new Int(registry, new Uint8Array([1]), 8).toNumber() @@ -142,7 +141,6 @@ describe('Int', (): void => { ).toEqual(32767); }); - it('converts to Little Endian from the provided value (bitLength)', (): void => { expect( new Int(registry, -1234, 32).toU8a()