basic.js 1.6 KB

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