})
test('convert to Uint8Array in modern browsers', function (t) {
- if (B.TYPED_ARRAY_SUPPORT) {
- var buf = new B([1, 2])
- var uint8array = new Uint8Array(buf.buffer)
- t.ok(uint8array instanceof Uint8Array)
- t.equal(uint8array[0], 1)
- t.equal(uint8array[1], 2)
- } else {
- t.pass('object impl: skipping test')
- }
+ var buf = new B([1, 2])
+ var uint8array = new Uint8Array(buf.buffer)
+ t.ok(uint8array instanceof Uint8Array)
+ t.equal(uint8array[0], 1)
+ t.equal(uint8array[1], 2)
t.end()
})
test('storing negative number should cast to unsigned', function (t) {
var buf = new B(1)
- if (B.TYPED_ARRAY_SUPPORT) {
- // This does not work with the object implementation -- nothing we can do!
- buf[0] = -3
- t.equal(buf[0], 253)
- }
+ buf[0] = -3
+ t.equal(buf[0], 253)
buf = new B(1)
buf.writeInt8(-3, 0)
})
test('test that memory is copied from array-like', function (t) {
- if (B.TYPED_ARRAY_SUPPORT) {
- var u = new Uint8Array(4)
- var b = new B(u)
- b[0] = 1
- b[1] = 2
- b[2] = 3
- b[3] = 4
+ var u = new Uint8Array(4)
+ var b = new B(u)
+ b[0] = 1
+ b[1] = 2
+ b[2] = 3
+ b[3] = 4
- t.equal(u[0], 0)
- t.equal(u[1], 0)
- t.equal(u[2], 0)
- t.equal(u[3], 0)
- } else {
- t.pass('object impl: skipping test')
- }
+ t.equal(u[0], 0)
+ t.equal(u[1], 0)
+ t.equal(u[2], 0)
+ t.equal(u[3], 0)
t.end()
})
})
test('new buffer from ArrayBuffer, shares memory', function (t) {
- if (Buffer.TYPED_ARRAY_SUPPORT) {
- var u = new Uint8Array([0, 1, 2, 3])
- var arraybuffer = u.buffer
- var b = new B(arraybuffer)
- t.equal(b.length, 4)
- t.equal(b[0], 0)
- t.equal(b[1], 1)
- t.equal(b[2], 2)
- t.equal(b[3], 3)
- t.equal(b[4], undefined)
-
- // changing the Uint8Array (and thus the ArrayBuffer), changes the Buffer
- u[0] = 10
- t.equal(b[0], 10)
- u[1] = 11
- t.equal(b[1], 11)
- u[2] = 12
- t.equal(b[2], 12)
- u[3] = 13
- t.equal(b[3], 13)
- }
+ var u = new Uint8Array([0, 1, 2, 3])
+ var arraybuffer = u.buffer
+ var b = new B(arraybuffer)
+ t.equal(b.length, 4)
+ t.equal(b[0], 0)
+ t.equal(b[1], 1)
+ t.equal(b[2], 2)
+ t.equal(b[3], 3)
+ t.equal(b[4], undefined)
+
+ // changing the Uint8Array (and thus the ArrayBuffer), changes the Buffer
+ u[0] = 10
+ t.equal(b[0], 10)
+ u[1] = 11
+ t.equal(b[1], 11)
+ u[2] = 12
+ t.equal(b[2], 12)
+ u[3] = 13
+ t.equal(b[3], 13)
t.end()
})
var test = require('tape')
test('modifying buffer created by .slice() modifies original memory', function (t) {
- if (!B.TYPED_ARRAY_SUPPORT) return t.end()
-
var buf1 = new B(26)
for (var i = 0; i < 26; i++) {
buf1[i] = i + 97 // 97 is ASCII a
})
test('modifying parent buffer modifies .slice() buffer\'s memory', function (t) {
- if (!B.TYPED_ARRAY_SUPPORT) return t.end()
-
var buf1 = new B(26)
for (var i = 0; i < 26; i++) {
buf1[i] = i + 97 // 97 is ASCII a
})
test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) {
- if (!B.TYPED_ARRAY_SUPPORT) {
- t.pass('object impl: skipping overflow test')
- t.end()
- return
- }
-
t.plan(3 * (2 * 2 * 2 + 2))
var hex = [
'', '03', '00', '030000', '000000',