From ca9ef4a5fd3de53ae76d0b6d6508bcb9b2fc33cd Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Thu, 19 Jan 2023 06:13:06 -0800 Subject: [PATCH] added float and symbol serialization tests --- src/tests/serialization.test.ts | 93 ++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/src/tests/serialization.test.ts b/src/tests/serialization.test.ts index a812062..631c2c3 100644 --- a/src/tests/serialization.test.ts +++ b/src/tests/serialization.test.ts @@ -498,6 +498,70 @@ describe('Floats and Ints Deserialization', () => { }); }); +describe('Float and Int Serialization', () => { + let textEncoder = new TextEncoder(); + let textDecoder = new TextDecoder(); + let transactionType: Map = ser.getTypesFromAbi(ser.createInitialTypes(), transactionAbi); + + /* reset buffer */ + beforeEach(() => { + buffer = new ser.SerialBuffer({ + textEncoder: textEncoder, + textDecoder: textDecoder, + }); + }); + it('Deserialize Uint64 Large Number', () => { + const expected = "FFFFFFFFFFFFFFFF"; + const testValue = BigInt(18446744073709551615); + const thisType = ser.getType(transactionType, "uint64"); + thisType.serialize(buffer, testValue); + const hex = ser.arrayToHex(buffer.asUint8Array()); + expect(hex).toBeTruthy(); + expect(hex).toEqual(expected); + }); + it('Deserialize Big Signed Number', () => { + const expected = "A38B82D9A906BDFE"; + const testValue = -90909090909090909; + const thisType = ser.getType(transactionType, "int64"); + thisType.serialize(buffer, testValue); + const hex = ser.arrayToHex(buffer.asUint8Array()); + expect(hex).toBeTruthy(); + expect(hex).toEqual(expected); + }); + it('check uint8 255', () => { + const expected = "FF"; + const testValue = 255; + const type = "uint8"; + const thisType = ser.getType(transactionType, type); + thisType.serialize(buffer, testValue); + const hex = ser.arrayToHex(buffer.asUint8Array()); + expect(hex).toBeTruthy(); + expect(hex).toEqual(expected); + }); + it('Deserialize Float32 Small Number', () => { + const expected = "0000003E"; + const testValue = 0.125; + const type = "float32"; + const thisType = ser.getType(transactionType, type); + thisType.serialize(buffer, testValue); + const hex = ser.arrayToHex(buffer.asUint8Array()); + expect(hex).toBeTruthy(); + expect(hex).toEqual(expected); + }); + it('Deserialize Float32 Neg Small Number', () => { + // -0.125 in float32 + // 000000BE + const expected = "000000BE"; + const testValue = -0.125; + const type = "float32"; + const thisType = ser.getType(transactionType, type); + thisType.serialize(buffer, testValue); + const hex = ser.arrayToHex(buffer.asUint8Array()); + expect(hex).toBeTruthy(); + expect(hex).toEqual(expected); + }); +}); + describe('Varint Deserialization', () => { let textEncoder = new TextEncoder(); let textDecoder = new TextDecoder(); @@ -2225,4 +2289,31 @@ describe('Symbol and Asset Deserialization', () => { const testValue = thisType.deserialize(buffer); expect(testValue).toEqual(expected); }); -}); \ No newline at end of file + it('check asset? "0.123456 SIX"', () => { + const hex = "0140E20100000000000653495800000000"; + const type = "asset?"; + const expected = "0.123456 SIX"; + buffer.pushArray(ser.hexToUint8Array(hex)); + const thisType = ser.getType(transactionType, type); + const testValue = thisType.deserialize(buffer); + expect(testValue).toEqual(expected); + }); + it('check extended_asset {"quantity":"0 FOO","contract":"bar"}', () => { + const hex = "000000000000000000464F4F00000000000000000000AE39"; + const type = "extended_asset"; + const expected = {"quantity":"0 FOO","contract":"bar"}; + buffer.pushArray(ser.hexToUint8Array(hex)); + const thisType = ser.getType(transactionType, type); + const testValue = thisType.deserialize(buffer); + expect(testValue).toEqual(expected); + }); + it('check symbol 0,A', () => { + const hex = "0041000000000000"; + const type = "symbol"; + const expected = "0,A"; + buffer.pushArray(ser.hexToUint8Array(hex)); + const thisType = ser.getType(transactionType, type); + const testValue = thisType.deserialize(buffer); + expect(testValue).toEqual(expected); + }); +});