Newer
Older
alert / js / node_modules / to-arraybuffer / test.js
@Réz István Réz István on 18 Nov 2021 1 KB first commit
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()
})