as-scale-codec
Version:
AssemblyScript implementation of the SCALE codec used in the Parity Substrate framework
77 lines (64 loc) • 3.65 kB
text/typescript
import { IntArray } from "../../Arrays/IntArray";
describe("IntArray", () => {
it("should encode int array", () => {
const dataInput: Array<Array<i64>> = [
[], // Expected output: [0x04, 0x04]
[], // Expected output: [0x10, 0x04, 0x08, 0x0c, 0x10]
[], // Expected output: [0x10, 0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10]
[]
];
const expectedOutput: Array<Array<u8>> = [
[],
[],
[],
[]
];
for (let i = 0; i < dataInput.length; i++) {
const intArray = new IntArray(dataInput[i]);
expect<Array<u8>>(intArray.toU8a()).toStrictEqual(expectedOutput[i]);
}
});
it("should decode int array", () => {
const dataInput: Array<Array<u8>> = [
[], // Expected output: [1]
[], // Expected output: [1, 2, 3, 4]
[], // Expected output: [16384, 2, 3, 4]
[]
];
const expectedOutput: Array<Array<i64>> = [
[],
[],
[],
[]
];
for (let i = 0; i < dataInput.length; i++) {
const result = IntArray.fromU8a(dataInput[i]);
expect<IntArray>(result).toStrictEqual(new IntArray(expectedOutput[i]));
}
});
it("should decode int array with populate method", () => {
const dataInput: Array<Array<u8>> = [
[], // Expected output: [1]
[], // Expected output: [1, 2, 3, 4]
[], // Expected output: [16384, 2, 3, 4]
[]
];
const expectedOutput: Array<Array<i64>> = [
[],
[],
[],
[]
];
for (let i = 0; i < dataInput.length; i++) {
const result = new IntArray();
result.populateFromBytes(dataInput[i]);
expect<IntArray>(result).toStrictEqual(new IntArray(expectedOutput[i]));
}
})
itThrows("should throw on incorrect encoding", () => {
const invalidEncodedArray1: u8[] = [0x10, 0x04];
IntArray.fromU8a(invalidEncodedArray1);
const invalidEncodedArray2: u8[] = [0x10];
IntArray.fromU8a(invalidEncodedArray2);
});
});