75 lines
2.2 KiB
JavaScript
75 lines
2.2 KiB
JavaScript
import { describe, expect, test } from "vitest";
|
|
import { ConstArray, isHeadless, parse, serialize, sizeof, sizeofHead } from "../src";
|
|
import { expectDataViewEqual } from ".";
|
|
import { filledDataView, sizedDataView } from "../src";
|
|
|
|
describe(ConstArray.name, () => {
|
|
test('serialize, Number', () => {
|
|
const expected = filledDataView([
|
|
0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
])
|
|
let dv = sizedDataView(15)
|
|
expect(() => serialize(dv, [1, 2], ConstArray(2), Number)).toThrow()
|
|
|
|
dv = sizedDataView(sizeof(ConstArray(2), Number))
|
|
expect(16).toEqual(dv.byteLength)
|
|
|
|
expect(() => serialize(dv, [1, 2, 3], ConstArray(2), Number)).toThrow()
|
|
|
|
serialize(dv, [1, 2], ConstArray(2), Number)
|
|
expectDataViewEqual(dv, expected)
|
|
})
|
|
|
|
test('parse, Number', () => {
|
|
const expected = [1, 2]
|
|
const dv = filledDataView([
|
|
0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
])
|
|
const actual = parse(dv, ConstArray(2), Number)
|
|
expectDataViewEqual(actual, expected)
|
|
})
|
|
|
|
test('sizeof', () => {
|
|
expect(sizeofHead(ConstArray(2), Number)).toEqual(16)
|
|
})
|
|
|
|
const array_dv = filledDataView([
|
|
0x00, 0x00, 0x00, 0x08,
|
|
0x00, 0x00, 0x00, 0x11,
|
|
// hello
|
|
0x00, 0x00, 0x00, 0x05,
|
|
0x68, 0x65, 0x6C, 0x6C, 0x6F,
|
|
// sek1ro
|
|
0x00, 0x00, 0x00, 0x06,
|
|
0x73, 0x65, 0x6b, 0x31, 0x72, 0x6f,
|
|
])
|
|
const array = ['hello', 'sek1ro']
|
|
|
|
test('serialize, String', () => {
|
|
|
|
const actual = sizedDataView(sizeof(array, ConstArray(2), String))
|
|
|
|
expect(() => sizeof(ConstArray(2), String)).toThrow()
|
|
|
|
serialize(actual, array, ConstArray(2), String)
|
|
expectDataViewEqual(actual, array_dv)
|
|
})
|
|
|
|
test('parse, String', () => {
|
|
expect(parse(array_dv, ConstArray(2), String)).toEqual(array)
|
|
})
|
|
|
|
test('isHeadless', () => {
|
|
expect(isHeadless(ConstArray(2), Number)).toBeFalsy()
|
|
expect(isHeadless(ConstArray(2), String)).toBeTruthy()
|
|
})
|
|
|
|
test('sizeof', () => {
|
|
expect(sizeof(ConstArray(2), Number)).toBe(16)
|
|
expect(() => sizeof(ConstArray(2), String)).toThrow()
|
|
expect(sizeof(['hello', 'sek1ro'], ConstArray(2), String)).toBe(27)
|
|
})
|
|
})
|