Websocket
[VSoRC/.git] / node_modules / websocket / node_modules / typedarray-to-buffer / test / basic.js
1 var test = require('tape')
2 var toBuffer = require('../')
3
4 test('convert to buffer from Uint8Array', function (t) {
5   if (typeof Uint8Array !== 'undefined') {
6     var arr = new Uint8Array([1, 2, 3])
7     arr = toBuffer(arr)
8
9     t.deepEqual(arr, Buffer.from([1, 2, 3]), 'contents equal')
10     t.ok(Buffer.isBuffer(arr), 'is buffer')
11     t.equal(arr.readUInt8(0), 1)
12     t.equal(arr.readUInt8(1), 2)
13     t.equal(arr.readUInt8(2), 3)
14   } else {
15     t.pass('browser lacks Uint8Array support, skip test')
16   }
17   t.end()
18 })
19
20 test('convert to buffer from another arrayview type (Uint32Array)', function (t) {
21   if (typeof Uint32Array !== 'undefined' && Buffer.TYPED_ARRAY_SUPPORT !== false) {
22     var arr = new Uint32Array([1, 2, 3])
23     arr = toBuffer(arr)
24
25     t.deepEqual(arr, Buffer.from([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]), 'contents equal')
26     t.ok(Buffer.isBuffer(arr), 'is buffer')
27     t.equal(arr.readUInt32LE(0), 1)
28     t.equal(arr.readUInt32LE(4), 2)
29     t.equal(arr.readUInt32LE(8), 3)
30     t.equal(arr instanceof Uint8Array, true)
31   } else {
32     t.pass('browser lacks Uint32Array support, skip test')
33   }
34   t.end()
35 })
36
37 test('convert to buffer from ArrayBuffer', function (t) {
38   if (typeof Uint32Array !== 'undefined' && Buffer.TYPED_ARRAY_SUPPORT !== false) {
39     var arr = new Uint32Array([1, 2, 3]).subarray(1, 2)
40     arr = toBuffer(arr)
41
42     t.deepEqual(arr, Buffer.from([2, 0, 0, 0]), 'contents equal')
43     t.ok(Buffer.isBuffer(arr), 'is buffer')
44     t.equal(arr.readUInt32LE(0), 2)
45     t.equal(arr instanceof Uint8Array, true)
46   } else {
47     t.pass('browser lacks ArrayBuffer support, skip test')
48   }
49   t.end()
50 })