as-scale-codec
Version:
AssemblyScript implementation of the SCALE codec used in the Parity Substrate framework
120 lines (107 loc) • 6.37 kB
text/typescript
import { u128 } from "as-bignum";
import { UInt128Array } from "../../Arrays/UInt128Array";
describe("UInt128Array", () => {
it("should encode uint128 array", () => {
const dataInput: Array<Array<u128>> = [
[],
[],
[],
[
u128.fromU32(1), u128.fromU64(123456789), u128.fromString('123456789012345'),
u128.fromString('12345678901234567890'), u128.fromString('1234567890123456789012345')
],
[],
[]
];
const expectedOutput: Array<Array<u8>> = [
[], // Expected output: [1]
[], // Expected output: 54321
[], // Expected output: [20001, 123456]
[
20, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 205, 91, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121, 223, 13, 134, 72, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 210, 10, 31, 235, 140, 169, 84, 171, 0, 0, 0, 0, 0, 0, 0, 0,
121, 223, 226, 61, 68, 166, 54, 15, 110, 5, 1, 0, 0, 0, 0, 0
],
[
12, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
],
[]
];
for (let i = 0; i < dataInput.length; i++) {
const intArray = new UInt128Array(dataInput[i]);
expect<Array<u8>>(intArray.toU8a()).toStrictEqual(expectedOutput[i]);
}
});
it("should decode uint128 array", () => {
const dataInput: Array<Array<u8>> = [
[], // Expected output: [1]
[], // Expected output: 54321
[], // Expected output: [20001, 123456]
[
20, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 205, 91, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121, 223, 13, 134, 72, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 210, 10, 31, 235, 140, 169, 84, 171, 0, 0, 0, 0, 0, 0, 0, 0,
121, 223, 226, 61, 68, 166, 54, 15, 110, 5, 1, 0, 0, 0, 0, 0
],
[
12, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
],
[]
];
const expectedOutput: Array<Array<u128>> = [
[],
[],
[],
[
u128.fromU32(1), u128.fromU64(123456789), u128.fromString('123456789012345'),
u128.fromString('12345678901234567890'), u128.fromString('1234567890123456789012345')
],
[],
[]
];
for (let i = 0; i < dataInput.length; i++) {
const result = UInt128Array.fromU8a(dataInput[i]);
expect<UInt128Array>(result).toStrictEqual(new UInt128Array(expectedOutput[i]));
}
});
it("should decode uint128 array with populate method", () => {
const dataInput: Array<Array<u8>> = [
[], // Expected output: [1]
[], // Expected output: 54321
[], // Expected output: [20001, 123456]
[
20, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 205, 91, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121, 223, 13, 134, 72, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 210, 10, 31, 235, 140, 169, 84, 171, 0, 0, 0, 0, 0, 0, 0, 0,
121, 223, 226, 61, 68, 166, 54, 15, 110, 5, 1, 0, 0, 0, 0, 0
],
[
12, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
],
[]
];
const expectedOutput: Array<Array<u128>> = [
[],
[],
[],
[
u128.fromU32(1), u128.fromU64(123456789), u128.fromString('123456789012345'),
u128.fromString('12345678901234567890'), u128.fromString('1234567890123456789012345')
],
[],
[]
];
for(let i = 0; i < dataInput.length; i++){
const instance = new UInt128Array();
instance.populateFromBytes(dataInput[i]);
expect<UInt128Array>(instance).toStrictEqual(new UInt128Array(expectedOutput[i]));
}
})
itThrows("should throw on incorrect encoding", () => {
const invalidEncodedArray1: u8[] = [0x4, 0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10];
UInt128Array.fromU8a(invalidEncodedArray1);
const invalidEncodedArray2: u8[] = [0x13, 0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10];
UInt128Array.fromU8a(invalidEncodedArray2);
});
});