31 lines
871 B
JavaScript
31 lines
871 B
JavaScript
import { describe, expect, test } from "vitest";
|
|
import { expectDataViewEqual } from '.';
|
|
import { filledDataView, sizedDataView } from '../src'
|
|
import { Int, parse, serialize, sizeof } from "../src";
|
|
|
|
describe(Int.name, () => {
|
|
|
|
const Type = Int(32, 'unsigned')
|
|
const type = 512
|
|
const type_dv = filledDataView([0x00, 0x00, 0x02, 0x00])
|
|
|
|
test('serialize', () => {
|
|
let actual = sizedDataView(3);
|
|
expect(() => serialize(actual, type, Type)).toThrow()
|
|
|
|
actual = sizedDataView(sizeof(Type))
|
|
serialize(actual, type, Type)
|
|
|
|
expectDataViewEqual(actual, type_dv)
|
|
})
|
|
|
|
test('parse', () => {
|
|
expect(parse(type_dv, Type)).toEqual(type)
|
|
})
|
|
|
|
test('serialize, overflow', () => {
|
|
expect(() => serialize(sizedDataView(1), 128, Int(8, 'signed'))).toThrow()
|
|
expect(() => serialize(sizedDataView(1), -129, Int(8, 'signed'))).toThrow()
|
|
})
|
|
})
|