basic.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. var B = require('../').Buffer
  2. var test = require('tape')
  3. test('new buffer from array', function (t) {
  4. t.equal(
  5. new B([1, 2, 3]).toString(),
  6. '\u0001\u0002\u0003'
  7. )
  8. t.end()
  9. })
  10. test('new buffer from string', function (t) {
  11. t.equal(
  12. new B('hey', 'utf8').toString(),
  13. 'hey'
  14. )
  15. t.end()
  16. })
  17. test('new buffer from buffer', function (t) {
  18. var b1 = new B('asdf')
  19. var b2 = new B(b1)
  20. t.equal(b1.toString('hex'), b2.toString('hex'))
  21. t.end()
  22. })
  23. test('new buffer from uint8array', function (t) {
  24. if (typeof Uint8Array !== 'undefined') {
  25. var b1 = new Uint8Array([0, 1, 2, 3])
  26. var b2 = new B(b1)
  27. t.equal(b1.length, b2.length)
  28. t.equal(b1[0], 0)
  29. t.equal(b1[1], 1)
  30. t.equal(b1[2], 2)
  31. t.equal(b1[3], 3)
  32. t.equal(b1[4], undefined)
  33. }
  34. t.end()
  35. })
  36. test('new buffer from uint16array', function (t) {
  37. if (typeof Uint16Array !== 'undefined') {
  38. var b1 = new Uint16Array([0, 1, 2, 3])
  39. var b2 = new B(b1)
  40. t.equal(b1.length, b2.length)
  41. t.equal(b1[0], 0)
  42. t.equal(b1[1], 1)
  43. t.equal(b1[2], 2)
  44. t.equal(b1[3], 3)
  45. t.equal(b1[4], undefined)
  46. }
  47. t.end()
  48. })
  49. test('new buffer from uint32array', function (t) {
  50. if (typeof Uint32Array !== 'undefined') {
  51. var b1 = new Uint32Array([0, 1, 2, 3])
  52. var b2 = new B(b1)
  53. t.equal(b1.length, b2.length)
  54. t.equal(b1[0], 0)
  55. t.equal(b1[1], 1)
  56. t.equal(b1[2], 2)
  57. t.equal(b1[3], 3)
  58. t.equal(b1[4], undefined)
  59. }
  60. t.end()
  61. })
  62. test('new buffer from int16array', function (t) {
  63. if (typeof Int16Array !== 'undefined') {
  64. var b1 = new Int16Array([0, 1, 2, 3])
  65. var b2 = new B(b1)
  66. t.equal(b1.length, b2.length)
  67. t.equal(b1[0], 0)
  68. t.equal(b1[1], 1)
  69. t.equal(b1[2], 2)
  70. t.equal(b1[3], 3)
  71. t.equal(b1[4], undefined)
  72. }
  73. t.end()
  74. })
  75. test('new buffer from int32array', function (t) {
  76. if (typeof Int32Array !== 'undefined') {
  77. var b1 = new Int32Array([0, 1, 2, 3])
  78. var b2 = new B(b1)
  79. t.equal(b1.length, b2.length)
  80. t.equal(b1[0], 0)
  81. t.equal(b1[1], 1)
  82. t.equal(b1[2], 2)
  83. t.equal(b1[3], 3)
  84. t.equal(b1[4], undefined)
  85. }
  86. t.end()
  87. })
  88. test('new buffer from float32array', function (t) {
  89. if (typeof Float32Array !== 'undefined') {
  90. var b1 = new Float32Array([0, 1, 2, 3])
  91. var b2 = new B(b1)
  92. t.equal(b1.length, b2.length)
  93. t.equal(b1[0], 0)
  94. t.equal(b1[1], 1)
  95. t.equal(b1[2], 2)
  96. t.equal(b1[3], 3)
  97. t.equal(b1[4], undefined)
  98. }
  99. t.end()
  100. })
  101. test('new buffer from float64array', function (t) {
  102. if (typeof Float64Array !== 'undefined') {
  103. var b1 = new Float64Array([0, 1, 2, 3])
  104. var b2 = new B(b1)
  105. t.equal(b1.length, b2.length)
  106. t.equal(b1[0], 0)
  107. t.equal(b1[1], 1)
  108. t.equal(b1[2], 2)
  109. t.equal(b1[3], 3)
  110. t.equal(b1[4], undefined)
  111. }
  112. t.end()
  113. })
  114. test('buffer toArrayBuffer()', function (t) {
  115. var data = [1, 2, 3, 4, 5, 6, 7, 8]
  116. if (typeof Uint8Array !== 'undefined') {
  117. var result = new B(data).toArrayBuffer()
  118. var expected = new Uint8Array(data).buffer
  119. for (var i = 0; i < expected.byteLength; i++) {
  120. t.equal(result[i], expected[i])
  121. }
  122. } else {
  123. t.pass('No toArrayBuffer() method provided in old browsers')
  124. }
  125. t.end()
  126. })
  127. test('buffer toJSON()', function (t) {
  128. var data = [1, 2, 3, 4]
  129. t.deepEqual(
  130. new B(data).toJSON(),
  131. { type: 'Buffer', data: [1,2,3,4] }
  132. )
  133. t.end()
  134. })
  135. test('buffer copy example', function (t) {
  136. var buf1 = new B(26)
  137. var buf2 = new B(26)
  138. for (var i = 0 ; i < 26 ; i++) {
  139. buf1[i] = i + 97; // 97 is ASCII a
  140. buf2[i] = 33; // ASCII !
  141. }
  142. buf1.copy(buf2, 8, 16, 20)
  143. t.equal(
  144. buf2.toString('ascii', 0, 25),
  145. '!!!!!!!!qrst!!!!!!!!!!!!!'
  146. )
  147. t.end()
  148. })