Sha256: a8d4c402aa9f5e5d8d98e8e1ef8488a278297e22574c2a1d847f9cf282b0eacd

Contents?: true

Size: 1.29 KB

Versions: 43

Compression:

Stored size: 1.29 KB

Contents

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()
})

Version data entries

43 entries across 42 versions & 15 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/to-arraybuffer/test.js
disco_app-0.18.0 test/dummy/node_modules/to-arraybuffer/test.js
disco_app-0.18.2 test/dummy/node_modules/to-arraybuffer/test.js
disco_app-0.16.1 test/dummy/node_modules/to-arraybuffer/test.js
disco_app-0.15.2 test/dummy/node_modules/to-arraybuffer/test.js
disco_app-0.18.4 test/dummy/node_modules/to-arraybuffer/test.js
disco_app-0.18.1 test/dummy/node_modules/to-arraybuffer/test.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/to-arraybuffer/test.js
disco_app-0.14.0 test/dummy/node_modules/to-arraybuffer/test.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/to-arraybuffer/test.js
tang-0.2.1 spec/tang_app/node_modules/to-arraybuffer/test.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/to-arraybuffer/test.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/to-arraybuffer/test.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/to-arraybuffer/test.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/to-arraybuffer/test.js
tang-0.2.0 spec/tang_app/node_modules/to-arraybuffer/test.js
tang-0.1.0 spec/tang_app/node_modules/to-arraybuffer/test.js
tang-0.0.9 spec/tang_app/node_modules/to-arraybuffer/test.js
enju_library-0.3.8 spec/dummy/node_modules/to-arraybuffer/test.js
ilog-0.4.1 node_modules/to-arraybuffer/test.js