198 lines
7.2 KiB
JavaScript

import { BASIC_TYPES, FixedArrayHandler, DynamicArrayHandler, CompoundTypeHandler, serializeToBinary, deserializeFromBinary } from '../index.mjs';
import assert from 'node:assert/strict';
import { areArrayBuffersEqual } from '@og/utility';
import { UUID } from '@og/uuid';
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
foo() {
return this.x + this.y;
}
}
Point.typedef = [
{ field: 'x', type: BASIC_TYPES.u8 },
{ field: 'y', type: BASIC_TYPES.u8 },
];
describe('binary-struct', () => {
describe('serializeToBinary', () => {
it('type i8', () => {
let res = serializeToBinary(-123, BASIC_TYPES.i8); // Example value for i8
let ans = new Uint8Array([0x85]); // Byte representation of -123 as i8
assert.ok(areArrayBuffersEqual(res, ans));
});
it('type i16', () => {
let res = serializeToBinary(-12345, BASIC_TYPES.i16); // Example value for i16
let ans = new Uint8Array([0xc7, 0xcf]); // Byte representation of -12345 as i16
assert.ok(areArrayBuffersEqual(res, ans));
});
it('type i32', () => {
let res = serializeToBinary(-1234567890, BASIC_TYPES.i32); // Example value for i32
let ans = new Uint8Array([0x2e, 0xfd, 0x69, 0xb6]); // Byte representation of -1234567890 as i32
assert.ok(areArrayBuffersEqual(res, ans));
});
it('type i64', () => {
let res = serializeToBinary(-1234567890123456789n, BASIC_TYPES.i64); // Example value for i64
let ans = new Uint8Array([0xeb, 0x7e, 0x16, 0x82, 0x0b, 0xef, 0xdd, 0xee]); // Byte representation of -1234567890123456789 as i64
assert.ok(areArrayBuffersEqual(res, ans));
});
it('type u8', () => {
let res = serializeToBinary(60, BASIC_TYPES.u8); // Example value for u8
let ans = new Uint8Array([0x3c]); // Byte representation of 60 as u8
assert.ok(areArrayBuffersEqual(res, ans));
});
it('type u16', () => {
let res = serializeToBinary(56789, BASIC_TYPES.u16); // Example value for u16
let ans = new Uint8Array([0xd5, 0xdd]); // Byte representation of 56789 as u16
assert.ok(areArrayBuffersEqual(res, ans));
});
it('type u32', () => {
let res = serializeToBinary(1234567890, BASIC_TYPES.u32); // Example value for u32
let ans = new Uint8Array([0xd2, 0x02, 0x96, 0x49]); // Byte representation of 1234567890 as u32
assert.ok(areArrayBuffersEqual(res, ans));
});
it('type u64', () => {
let res = serializeToBinary(123456789009876543n, BASIC_TYPES.u64); // Example value for u64
let ans = new Uint8Array([0x3f, 0x46, 0x0b, 0xa6, 0x4b, 0x9b, 0xb6, 0x01]); // Byte representation of 123456789009876543 as u64
assert.ok(areArrayBuffersEqual(res, ans));
});
it('type uuid', () => {
let res = serializeToBinary(new UUID('f5b3ad32-2a77-4f2e-a3b8-3eb8a8546f2b'), BASIC_TYPES.uuid);
let ans = new Uint8Array([0xf5, 0xb3, 0xad, 0x32, 0x2a, 0x77, 0x4f, 0x2e, 0xa3, 0xb8, 0x3e, 0xb8, 0xa8, 0x54, 0x6f, 0x2b]);
assert.ok(areArrayBuffersEqual(res, ans));
});
it('type string', () => {
let res = serializeToBinary('hello world', BASIC_TYPES.str);
let ans = new Uint8Array([0x0b, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]);
assert.ok(areArrayBuffersEqual(res, ans));
});
it('type u16[]', () => {
let res = serializeToBinary([0x1770, 0x3c3a, 0x9012], new DynamicArrayHandler(BASIC_TYPES.u16));
let ans = new Uint8Array([0x03, 0x00, 0x00, 0x00, 0x70, 0x17, 0x3a, 0x3c, 0x12, 0x90]);
assert.ok(areArrayBuffersEqual(res, ans));
});
it('type u16[3]', () => {
let res = serializeToBinary([0x1770, 0x3c3a, 0x9012], new FixedArrayHandler(3, BASIC_TYPES.u16));
let ans = new Uint8Array([0x70, 0x17, 0x3a, 0x3c, 0x12, 0x90]);
assert.ok(areArrayBuffersEqual(res, ans));
});
it('type Point', () => {
let res = serializeToBinary(new Point(2, 3), Point);
let ans = new Uint8Array([0x02, 0x03]);
assert.ok(areArrayBuffersEqual(res, ans));
});
});
describe('deserializeFromBinary', () => {
it('type i8', () => {
let binaryData = new Uint8Array([0x85]).buffer;
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.i8);
let ans = -123;
assert.equal(res, ans);
});
it('type i16', () => {
let binaryData = new Uint8Array([0xc7, 0xcf]).buffer;
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.i16);
let ans = -12345;
assert.equal(res, ans);
});
it('type i32', () => {
let binaryData = new Uint8Array([0x2e, 0xfd, 0x69, 0xb6]).buffer;
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.i32);
let ans = -1234567890;
assert.equal(res, ans);
});
it('type i64', () => {
let binaryData = new Uint8Array([0xeb, 0x7e, 0x16, 0x82, 0x0b, 0xef, 0xdd, 0xee]).buffer;
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.i64);
let ans = BigInt('-1234567890123456789');
assert.equal(res, ans);
});
it('type u8', () => {
let binaryData = new Uint8Array([0x3c]).buffer;
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.u8);
let ans = 60;
assert.equal(res, ans);
});
it('type u16', () => {
let binaryData = new Uint8Array([0xd5, 0xdd]).buffer;
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.u16);
let ans = 56789;
assert.equal(res, ans);
});
it('type u32', () => {
let binaryData = new Uint8Array([0xd2, 0x02, 0x96, 0x49]).buffer;
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.u32);
let ans = 1234567890;
assert.equal(res, ans);
});
it('type u64', () => {
let binaryData = new Uint8Array([0x3f, 0x46, 0x0b, 0xa6, 0x4b, 0x9b, 0xb6, 0x01]).buffer;
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.u64);
let ans = BigInt('123456789009876543');
assert.equal(res, ans);
});
it('type uuid', () => {
let binaryData = new Uint8Array([0xf5, 0xb3, 0xad, 0x32, 0x2a, 0x77, 0x4f, 0x2e, 0xa3, 0xb8, 0x3e, 0xb8, 0xa8, 0x54, 0x6f, 0x2b]).buffer;
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.uuid);
let ans = 'f5b3ad32-2a77-4f2e-a3b8-3eb8a8546f2b';
assert.equal(res.toString(), ans);
});
it('type string', () => {
let binaryData = new Uint8Array([0x0b, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]).buffer;
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.str);
let ans = 'hello world';
assert.equal(res, ans);
});
it('type u16[]', () => {
let binaryData = new Uint8Array([0x03, 0x00, 0x00, 0x00, 0x70, 0x17, 0x3a, 0x3c, 0x12, 0x90]).buffer;
let res = deserializeFromBinary(new DataView(binaryData), new DynamicArrayHandler(BASIC_TYPES.u16));
let ans = [0x1770, 0x3c3a, 0x9012];
assert.deepEqual(res, ans);
});
it('type u16[3]', () => {
let binaryData = new Uint8Array([0x70, 0x17, 0x3a, 0x3c, 0x12, 0x90]).buffer;
let res = deserializeFromBinary(new DataView(binaryData), new FixedArrayHandler(3, BASIC_TYPES.u16));
let ans = [0x1770, 0x3c3a, 0x9012];
assert.deepEqual(res, ans);
});
it('type Point', () => {
let binaryData = new Uint8Array([0x02, 0x03]).buffer;
let res = deserializeFromBinary(new DataView(binaryData), Point);
let ans = new Point(2, 3);
assert.equal(res.x, ans.x);
assert.equal(res.y, ans.y);
assert.equal(res.foo(), ans.foo())
});
});
});