| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"use strict"; |
| 5 |
|
| 6 |
const assert = require("../../assert.js"); |
| 7 |
|
| 8 |
function assertValueRead(actual, expected) { |
| 9 |
assert.sameValue(actual, expected, "value read does not match; ") |
| 10 |
} |
| 11 |
|
| 12 |
function assertBytesWritten(actual, expected) { |
| 13 |
assert.sameValue(actual, expected, "bytesWritten does not match; ") |
| 14 |
} |
| 15 |
|
| 16 |
function assertBufferWriteRead(buffer, writeMethod, readMethod, value, offset = 0) { |
| 17 |
const bytesWritten = buffer[writeMethod](value, offset); |
| 18 |
const bytesPerElement = getBufferElementSize(writeMethod); |
| 19 |
assertBytesWritten(bytesWritten, offset + bytesPerElement); |
| 20 |
|
| 21 |
const readValue = buffer[readMethod](offset); |
| 22 |
assertValueRead(readValue, value); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
function getBufferElementSize(methodName) { |
| 27 |
if (methodName.includes('64')) return 8; |
| 28 |
if (methodName.includes('Double')) return 8; |
| 29 |
if (methodName.includes('32')) return 4; |
| 30 |
if (methodName.includes('Float')) return 4; |
| 31 |
if (methodName.includes('16')) return 2; |
| 32 |
if (methodName.includes('8')) return 1; |
| 33 |
return 1; |
| 34 |
} |