This repository has been archived on 2024-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
keksAccountGUI/node_modulesOLD/to-arraybuffer/test.js

58 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2019-08-11 18:48:02 +00:00
var Buffer = require('buffer').Buffer
var test = require('tape')
var toArrayBuffer = require('.')
function elementsEqual (ab, buffer) {
var view = new Uint8Array(ab)
for (var i = 0; i < view.length; i++) {
if (view[i] !== buffer[i]) {
return false
}
}
return true
}
test('Basic behavior', function (t) {
var buf = new Buffer(10)
for (var i = 0; i < 10; i++) {
buf[i] = i
}
var ab = toArrayBuffer(buf)
t.equals(ab.byteLength, 10, 'correct length')
t.ok(elementsEqual(ab, buf), 'elements equal')
t.end()
})
test('Behavior when input is a subarray 1', function (t) {
var origBuf = new Buffer(10)
for (var i = 0; i < 10; i++) {
origBuf[i] = i
}
var buf = origBuf.slice(1)
var ab = toArrayBuffer(buf)
t.equals(ab.byteLength, 9, 'correct length')
t.ok(elementsEqual(ab, buf), 'elements equal')
t.notOk(ab === buf.buffer, 'the underlying ArrayBuffer is not returned when incorrect')
t.end()
})
test('Behavior when input is a subarray 2', function (t) {
var origBuf = new Buffer(10)
for (var i = 0; i < 10; i++) {
origBuf[i] = i
}
var buf = origBuf.slice(0, 9)
var ab = toArrayBuffer(buf)
t.equals(ab.byteLength, 9, 'correct length')
t.ok(elementsEqual(ab, buf), 'elements equal')
t.notOk(ab === buf.buffer, 'the underlying ArrayBuffer is not returned when incorrect')
t.end()
})