test(Struct): Token
This commit is contained in:
@ -1,5 +1,3 @@
|
||||
import { expect } from "vitest"
|
||||
|
||||
export function filledDataView(bytes) {
|
||||
const ab = new ArrayBuffer(bytes.length)
|
||||
const dv = new DataView(ab)
|
||||
@ -19,17 +17,19 @@ export function sizedDataView(length) {
|
||||
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)
|
||||
const arraysToString = () => {
|
||||
return `actual: [${Array.from(actualBytes).map(byte => '0x' + byte.toString(16)).join(', ')}]\n` +
|
||||
`expected: [${Array.from(expectedBytes).map(byte => '0x' + byte.toString(16)).join(', ')}]`
|
||||
}
|
||||
|
||||
if (actualBytes.length !== expectedBytes.length) {
|
||||
throw new Error(`DataView length mismatch: ${actualBytes.length} !== ${expectedBytes.length}`)
|
||||
throw new Error(`DataView length mismatch: actual: ${actualBytes.length}, expected: ${expectedBytes.length}\n` + arraysToString())
|
||||
}
|
||||
|
||||
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(', ')}]`
|
||||
`Byte mismatch at offset ${i}: 0x${actualBytes[i].toString(16)} !== 0x${expectedBytes[i].toString(16)}\n` + arraysToString()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user