From b8b372808de9b4f81f5382ccd7f3efabd37bbefc Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Tue, 25 Apr 2023 16:29:33 +0100 Subject: [PATCH 1/4] test: refactor and increase verbosity of u64 coder tests --- packages/abi-coder/src/coders/u64.test.ts | 118 ++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/packages/abi-coder/src/coders/u64.test.ts b/packages/abi-coder/src/coders/u64.test.ts index e69de29bb2d..093c70bf11b 100644 --- a/packages/abi-coder/src/coders/u64.test.ts +++ b/packages/abi-coder/src/coders/u64.test.ts @@ -0,0 +1,118 @@ +import { hexlify } from '@ethersproject/bytes'; +import { BN, bn } from '@fuel-ts/math'; + +import { U8_MAX, U16_MAX, U32_MAX, U64_MAX } from '../../test/utils/constants'; + +import U64Coder from './u64'; + +// [new U64Coder(), 0, bn(0)], +// [new U64Coder(), toHex(100), bn(100)], +// [new U64Coder(), bn(U8_MAX), bn(U8_MAX)], +// [new U64Coder(), U16_MAX, bn(U16_MAX)], +// [new U64Coder(), U32_MAX, bn(U32_MAX)], +// [new U64Coder(), U64_MAX, U64_MAX], + +describe('U64Coder', () => { + const coder = new U64Coder(); + + it('should encode a u64 number', () => { + const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]); + const actual = coder.encode(0); + + expect(actual).toStrictEqual(expected); + }); + + it('should decode a u64 number', () => { + const expectedValue = 0; + const expectedLength = 8; + const [actualValue, actualLength] = coder.decode(new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]), 0); + + expect(actualValue).toBeInstanceOf(BN); + expect(actualValue.toNumber()).toBe(expectedValue); + expect(actualLength).toBe(expectedLength); + }); + + it('should encode u8 max number', () => { + const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 255]); + const actual = coder.encode(U8_MAX); + + expect(actual).toStrictEqual(expected); + }); + + it('should decode u8 max number', () => { + const expectedValue = U8_MAX; + const expectedLength = 8; + const [actualValue, actualLength] = coder.decode(new Uint8Array([0, 0, 0, 0, 0, 0, 0, 255]), 0); + + expect(actualValue).toBeInstanceOf(BN); + expect(actualValue.toNumber()).toBe(expectedValue); + expect(actualLength).toBe(expectedLength); + }); + + it('should encode u16 max number', () => { + const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 255, 255]); + const actual = coder.encode(U16_MAX); + + expect(actual).toStrictEqual(expected); + }); + + it('should decode u16 max number', () => { + const expectedValue = U16_MAX; + const expectedLength = 8; + const [actualValue, actualLength] = coder.decode( + new Uint8Array([0, 0, 0, 0, 0, 0, 255, 255]), + 0 + ); + + expect(actualValue).toBeInstanceOf(BN); + expect(actualValue.toNumber()).toBe(expectedValue); + expect(actualLength).toBe(expectedLength); + }); + + it('should encode u32 max number', () => { + const expected = new Uint8Array([0, 0, 0, 0, 255, 255, 255, 255]); + const actual = coder.encode(U32_MAX); + + expect(actual).toStrictEqual(expected); + }); + + it('should decode u32 max number', () => { + const expectedValue = U32_MAX; + const expectedLength = 8; + const [actualValue, actualLength] = coder.decode( + new Uint8Array([0, 0, 0, 0, 255, 255, 255, 255]), + 0 + ); + + expect(actualValue).toBeInstanceOf(BN); + expect(actualValue.toNumber()).toBe(expectedValue); + expect(actualLength).toBe(expectedLength); + }); + + it('should encode u64 max number', () => { + const expected = new Uint8Array([255, 255, 255, 255, 255, 255, 255, 255]); + const actual = coder.encode(U64_MAX); + + expect(actual).toStrictEqual(expected); + }); + + it('should decode u64 max number', () => { + const expectedValue = U64_MAX; + const expectedLength = 8; + + const [actualValue, actualLength] = coder.decode( + new Uint8Array([255, 255, 255, 255, 255, 255, 255, 255]), + 0 + ); + + expect(actualValue).toBeInstanceOf(BN); + expect(JSON.stringify(actualValue)).toEqual(JSON.stringify(expectedValue)); + expect(actualLength).toBe(expectedLength); + }); + + it('should throw an error when encoding an invalid u64', () => { + expect(() => { + coder.encode(bn(U64_MAX).add(1)); + }).toThrow('Invalid u64'); + }); +}); From b98334fbfbb48b8445ac88bb114964804150b68d Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Wed, 26 Apr 2023 10:13:39 +0100 Subject: [PATCH 2/4] test: remove redundant imports and comments --- packages/abi-coder/src/coders/u64.test.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/abi-coder/src/coders/u64.test.ts b/packages/abi-coder/src/coders/u64.test.ts index 093c70bf11b..08599ae8c0a 100644 --- a/packages/abi-coder/src/coders/u64.test.ts +++ b/packages/abi-coder/src/coders/u64.test.ts @@ -1,17 +1,9 @@ -import { hexlify } from '@ethersproject/bytes'; import { BN, bn } from '@fuel-ts/math'; import { U8_MAX, U16_MAX, U32_MAX, U64_MAX } from '../../test/utils/constants'; import U64Coder from './u64'; -// [new U64Coder(), 0, bn(0)], -// [new U64Coder(), toHex(100), bn(100)], -// [new U64Coder(), bn(U8_MAX), bn(U8_MAX)], -// [new U64Coder(), U16_MAX, bn(U16_MAX)], -// [new U64Coder(), U32_MAX, bn(U32_MAX)], -// [new U64Coder(), U64_MAX, U64_MAX], - describe('U64Coder', () => { const coder = new U64Coder(); From 27365e81ff631ddc94ffad4c6e2dfcc608e9fe84 Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Fri, 28 Apr 2023 12:54:02 +0100 Subject: [PATCH 3/4] chore: changeset --- .changeset/good-otters-hug.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changeset/good-otters-hug.md diff --git a/.changeset/good-otters-hug.md b/.changeset/good-otters-hug.md new file mode 100644 index 00000000000..a49ba48448f --- /dev/null +++ b/.changeset/good-otters-hug.md @@ -0,0 +1,2 @@ +--- +--- \ No newline at end of file From ce12a1ea40b31aa2fb1d5ac137fbb7dbf7915035 Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Tue, 2 May 2023 15:56:20 +0100 Subject: [PATCH 4/4] chore: force rebuild