37 lines
1.2 KiB
JavaScript
37 lines
1.2 KiB
JavaScript
import { expect } from "vitest"
|
|
|
|
export function filledDataView(bytes) {
|
|
const ab = new ArrayBuffer(bytes.length)
|
|
const dv = new DataView(ab)
|
|
|
|
for (let i = 0; i < bytes.length; i++) {
|
|
dv.setInt8(i, bytes[i])
|
|
}
|
|
|
|
return dv
|
|
}
|
|
|
|
export function sizedDataView(length) {
|
|
const ab = new ArrayBuffer(length)
|
|
return new DataView(ab)
|
|
}
|
|
|
|
export function expectDataViewEqual(actual, expected) {
|
|
const actualBytes = new Uint8Array(actual.buffer, actual.byteOffset, actual.byteLength)
|
|
const expectedBytes = new Uint8Array(expected.buffer, expected.byteOffset, expected.byteLength)
|
|
|
|
if (actualBytes.length !== expectedBytes.length) {
|
|
throw new Error(`DataView length mismatch: ${actualBytes.length} !== ${expectedBytes.length}`)
|
|
}
|
|
|
|
for (let i = 0; i < actualBytes.length; i++) {
|
|
if (actualBytes[i] !== expectedBytes[i]) {
|
|
throw new Error(
|
|
`Byte mismatch at offset ${i}: 0x${actualBytes[i].toString(16)} !== 0x${expectedBytes[i].toString(16)}\n` +
|
|
`actual: [${Array.from(actualBytes).map(byte => '0x' + byte.toString(16)).join(', ')}]\n` +
|
|
`expected: [${Array.from(expectedBytes).map(byte => '0x' + byte.toString(16)).join(', ')}]`
|
|
)
|
|
}
|
|
}
|
|
}
|