From 72f427a2509e63038ad27c4fa04241cfe7ce058f Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 22 Feb 2022 09:51:40 +0100 Subject: [PATCH] Map initialU8aLength test (#4590) --- .../types-codec/src/extended/BTreeSet.spec.ts | 123 ++++++++++-------- packages/types-codec/src/extended/Map.spec.ts | 27 ++++ 2 files changed, 93 insertions(+), 57 deletions(-) diff --git a/packages/types-codec/src/extended/BTreeSet.spec.ts b/packages/types-codec/src/extended/BTreeSet.spec.ts index 39437e6fbf..077572a520 100644 --- a/packages/types-codec/src/extended/BTreeSet.spec.ts +++ b/packages/types-codec/src/extended/BTreeSet.spec.ts @@ -126,69 +126,78 @@ describe('BTreeSet', (): void => { ).toThrowError(/BTreeSet: cannot decode type/); }); - it('correctly encodes length', (): void => { - expect( - new ( - BTreeSet.with(U32))(registry, mockU32Set).encodedLength - ).toEqual(17); + describe('enocodedLength & initialU8aLength', (): void => { + it('correctly encodes length', (): void => { + expect( + new ( + BTreeSet.with(U32))(registry, mockU32Set).encodedLength + ).toEqual(17); + }); + + it('correctly encodes/decodes empty', (): void => { + const none = new (BTreeSet.with(U32))(registry, []); + + // only the length byte + expect(none.toHex()).toEqual('0x00'); + expect(none.encodedLength).toEqual(1); + expect( + (new (BTreeSet.with(U32))(registry, none.toHex())).initialU8aLength + ).toEqual(none.encodedLength); + }); + + it('correctly encodes/decodes filled', (): void => { + const some = new (BTreeSet.with(U32))(registry, [1, 2]); + + // length byte + 2 values, 2 << 2 with u32 values + expect(some.toHex()).toEqual('0x080100000002000000'); + expect(some.encodedLength).toEqual(1 + (4 * 2)); + expect( + (new (BTreeSet.with(U32))(registry, some.toHex())).initialU8aLength + ).toEqual(some.encodedLength); + }); }); - it('correctly encodes/decodes the length', (): void => { - const none = new (BTreeSet.with(U32))(registry, []).toHex(); - const some = new (BTreeSet.with(U32))(registry, [1, 2]).toHex(); + describe('sorting', (): void => { + it('correctly sorts numeric values', (): void => { + expect( + Array.from(new (BTreeSet.with(I32))(registry, mockI32SetObj)).map((k) => k.toNumber()) + ).toEqual([-1000, -255, 0, 255, 1000]); + }); - // only the length byte - expect(none).toEqual('0x00'); - expect( - (new (BTreeSet.with(U32))(registry, none)).initialU8aLength - ).toEqual(1); + it('correctly sorts text values', (): void => { + expect( + Array.from(new (BTreeSet.with(Text))(registry, mockTextSetObj)).map((k) => k.toString()) + ).toEqual(['b', 'ba', 'baz', 'bb', 'c']); + }); - // length byte + 2 values, 2 << 2 with u32 values - expect(some).toEqual('0x080100000002000000'); - expect( - (new (BTreeSet.with(U32))(registry, some)).initialU8aLength - ).toEqual(1 + (2 * 4)); - }); + it('correctly sorts complex tuple values', (): void => { + expect( + Array.from(new (BTreeSet.with(U32TextTuple))(registry, mockTupleSetObj)).map((k) => k.toJSON()) + ).toEqual([[1, 'baz'], [2, 'b'], [2, 'ba'], [2, 'bb']]); + }); - it('correctly sorts numeric values', (): void => { - expect( - Array.from(new (BTreeSet.with(I32))(registry, mockI32SetObj)).map((k) => k.toNumber()) - ).toEqual([-1000, -255, 0, 255, 1000]); - }); + it('correctly sorts complex struct values', (): void => { + expect( + Array.from(new (BTreeSet.with(MockStruct))(registry, mockStructSetObj)).map((k) => k.toJSON()) + ).toEqual([ + { int: -1, text: 'b' }, + { int: 1, text: 'b' }, + { int: -1, text: 'ba' }, + { int: -2, text: 'baz' } + ]); + }); - it('correctly sorts text values', (): void => { - expect( - Array.from(new (BTreeSet.with(Text))(registry, mockTextSetObj)).map((k) => k.toString()) - ).toEqual(['b', 'ba', 'baz', 'bb', 'c']); - }); - - it('correctly sorts complex tuple values', (): void => { - expect( - Array.from(new (BTreeSet.with(U32TextTuple))(registry, mockTupleSetObj)).map((k) => k.toJSON()) - ).toEqual([[1, 'baz'], [2, 'b'], [2, 'ba'], [2, 'bb']]); - }); - - it('correctly sorts complex struct values', (): void => { - expect( - Array.from(new (BTreeSet.with(MockStruct))(registry, mockStructSetObj)).map((k) => k.toJSON()) - ).toEqual([ - { int: -1, text: 'b' }, - { int: 1, text: 'b' }, - { int: -1, text: 'ba' }, - { int: -2, text: 'baz' } - ]); - }); - - it('correctly sorts complex enum values', (): void => { - expect( - Array.from(new (BTreeSet.with(MockEnum))(registry, mockEnumSetObj)).map((k) => k.toJSON()) - ).toEqual([ - { key1: { int: -1, text: 'b' } }, - { key1: { int: 1, text: 'b' } }, - { key2: { int: -1, text: 'b' } }, - { key3: [2, 'b'] }, - { key3: [2, 'ba'] } - ]); + it('correctly sorts complex enum values', (): void => { + expect( + Array.from(new (BTreeSet.with(MockEnum))(registry, mockEnumSetObj)).map((k) => k.toJSON()) + ).toEqual([ + { key1: { int: -1, text: 'b' } }, + { key1: { int: 1, text: 'b' } }, + { key2: { int: -1, text: 'b' } }, + { key3: [2, 'b'] }, + { key3: [2, 'ba'] } + ]); + }); }); it('generates sane toRawTypes', (): void => { diff --git a/packages/types-codec/src/extended/Map.spec.ts b/packages/types-codec/src/extended/Map.spec.ts index 89b0d5350f..c8d288f74b 100644 --- a/packages/types-codec/src/extended/Map.spec.ts +++ b/packages/types-codec/src/extended/Map.spec.ts @@ -75,6 +75,33 @@ describe('CodecMap', (): void => { testEncode('toString', mockU32U32MapString); }); + describe('enocodedLength & initialU8aLength', (): void => { + it('correctly encodes/decodes empty', (): void => { + const none = new CodecMap(registry, Text, Text, new Map([])); + + // only the length byte + expect(none.toHex()).toEqual('0x00'); + expect(none.encodedLength).toEqual(1); + expect( + new CodecMap(registry, Text, Text, none.toHex()).initialU8aLength + ).toEqual(none.encodedLength); + }); + + it('correctly encodes/decodes filled', (): void => { + const some = new CodecMap(registry, Text, Text, new Map([ + [new Text(registry, '1'), new Text(registry, 'foo')], + [new Text(registry, '2'), new Text(registry, 'bar')] + ])); + + // length byte + 2 values, 2 << 2 with Text values + expect(some.toHex()).toEqual('0x0804310c666f6f04320c626172'); + expect(some.encodedLength).toEqual(1 + ((1 + 1) * 2) + ((1 + 3) * 2)); + expect( + new CodecMap(registry, Text, Text, some.toHex()).initialU8aLength + ).toEqual(some.encodedLength); + }); + }); + it('has a sane inspect', (): void => { expect( new CodecMap(registry, Text, Text, new Map([