34 lines
1014 B
JavaScript
34 lines
1014 B
JavaScript
import {expect, test} from 'vitest';
|
||
|
||
import Serializer from './serializer.js';
|
||
|
||
test('can encode and decode', () => {
|
||
const entries = [
|
||
['uint8', 255],
|
||
['int8', -128],
|
||
['int8', 127],
|
||
['uint16', 65535],
|
||
['int16', -32768],
|
||
['int16', 32767],
|
||
['uint32', 4294967295],
|
||
['int32', -2147483648],
|
||
['int32', 2147483647],
|
||
['uint64', 18446744073709551615n],
|
||
['int64', -9223372036854775808n],
|
||
['int64', 9223372036854775807n],
|
||
['float32', 0.5],
|
||
['float64', 1.234],
|
||
['string', 'hello world'],
|
||
['string', 'α'],
|
||
];
|
||
const schema = entries.reduce((r, [type]) => ({...r, [Object.keys(r).length]: type}), {});
|
||
const data = entries.reduce((r, [, value]) => ({...r, [Object.keys(r).length]: value}), {});
|
||
const serializer = new Serializer(schema);
|
||
const view = new DataView(new ArrayBuffer(serializer.schema.sizeOf(data)));
|
||
serializer.encode(data, view);
|
||
const result = {};
|
||
serializer.decode(view, result);
|
||
expect(data)
|
||
.to.deep.equal(result);
|
||
});
|