您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

constructor.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
  2. var B = require('../').Buffer
  3. var test = require('tape')
  4. test('new buffer from array', function (t) {
  5. t.equal(
  6. new B([1, 2, 3]).toString(),
  7. '\u0001\u0002\u0003'
  8. )
  9. t.end()
  10. })
  11. test('new buffer from array w/ negatives', function (t) {
  12. t.equal(
  13. new B([-1, -2, -3]).toString('hex'),
  14. 'fffefd'
  15. )
  16. t.end()
  17. })
  18. test('new buffer from array with mixed signed input', function (t) {
  19. t.equal(
  20. new B([-255, 255, -128, 128, 512, -512, 511, -511]).toString('hex'),
  21. '01ff80800000ff01'
  22. )
  23. t.end()
  24. })
  25. test('new buffer from string', function (t) {
  26. t.equal(
  27. new B('hey', 'utf8').toString(),
  28. 'hey'
  29. )
  30. t.end()
  31. })
  32. test('new buffer from buffer', function (t) {
  33. var b1 = new B('asdf')
  34. var b2 = new B(b1)
  35. t.equal(b1.toString('hex'), b2.toString('hex'))
  36. t.end()
  37. })
  38. test('new buffer from ArrayBuffer', function (t) {
  39. if (typeof ArrayBuffer !== 'undefined') {
  40. var arraybuffer = new Uint8Array([0, 1, 2, 3]).buffer
  41. var b = new B(arraybuffer)
  42. t.equal(b.length, 4)
  43. t.equal(b[0], 0)
  44. t.equal(b[1], 1)
  45. t.equal(b[2], 2)
  46. t.equal(b[3], 3)
  47. t.equal(b[4], undefined)
  48. }
  49. t.end()
  50. })
  51. test('new buffer from ArrayBuffer, shares memory', function (t) {
  52. if (Buffer.TYPED_ARRAY_SUPPORT) {
  53. var u = new Uint8Array([0, 1, 2, 3])
  54. var arraybuffer = u.buffer
  55. var b = new B(arraybuffer)
  56. t.equal(b.length, 4)
  57. t.equal(b[0], 0)
  58. t.equal(b[1], 1)
  59. t.equal(b[2], 2)
  60. t.equal(b[3], 3)
  61. t.equal(b[4], undefined)
  62. // changing the Uint8Array (and thus the ArrayBuffer), changes the Buffer
  63. u[0] = 10
  64. t.equal(b[0], 10)
  65. u[1] = 11
  66. t.equal(b[1], 11)
  67. u[2] = 12
  68. t.equal(b[2], 12)
  69. u[3] = 13
  70. t.equal(b[3], 13)
  71. }
  72. t.end()
  73. })
  74. test('new buffer from Uint8Array', function (t) {
  75. if (typeof Uint8Array !== 'undefined') {
  76. var b1 = new Uint8Array([0, 1, 2, 3])
  77. var b2 = new B(b1)
  78. t.equal(b1.length, b2.length)
  79. t.equal(b1[0], 0)
  80. t.equal(b1[1], 1)
  81. t.equal(b1[2], 2)
  82. t.equal(b1[3], 3)
  83. t.equal(b1[4], undefined)
  84. }
  85. t.end()
  86. })
  87. test('new buffer from Uint16Array', function (t) {
  88. if (typeof Uint16Array !== 'undefined') {
  89. var b1 = new Uint16Array([0, 1, 2, 3])
  90. var b2 = new B(b1)
  91. t.equal(b1.length, b2.length)
  92. t.equal(b1[0], 0)
  93. t.equal(b1[1], 1)
  94. t.equal(b1[2], 2)
  95. t.equal(b1[3], 3)
  96. t.equal(b1[4], undefined)
  97. }
  98. t.end()
  99. })
  100. test('new buffer from Uint32Array', function (t) {
  101. if (typeof Uint32Array !== 'undefined') {
  102. var b1 = new Uint32Array([0, 1, 2, 3])
  103. var b2 = new B(b1)
  104. t.equal(b1.length, b2.length)
  105. t.equal(b1[0], 0)
  106. t.equal(b1[1], 1)
  107. t.equal(b1[2], 2)
  108. t.equal(b1[3], 3)
  109. t.equal(b1[4], undefined)
  110. }
  111. t.end()
  112. })
  113. test('new buffer from Int16Array', function (t) {
  114. if (typeof Int16Array !== 'undefined') {
  115. var b1 = new Int16Array([0, 1, 2, 3])
  116. var b2 = new B(b1)
  117. t.equal(b1.length, b2.length)
  118. t.equal(b1[0], 0)
  119. t.equal(b1[1], 1)
  120. t.equal(b1[2], 2)
  121. t.equal(b1[3], 3)
  122. t.equal(b1[4], undefined)
  123. }
  124. t.end()
  125. })
  126. test('new buffer from Int32Array', function (t) {
  127. if (typeof Int32Array !== 'undefined') {
  128. var b1 = new Int32Array([0, 1, 2, 3])
  129. var b2 = new B(b1)
  130. t.equal(b1.length, b2.length)
  131. t.equal(b1[0], 0)
  132. t.equal(b1[1], 1)
  133. t.equal(b1[2], 2)
  134. t.equal(b1[3], 3)
  135. t.equal(b1[4], undefined)
  136. }
  137. t.end()
  138. })
  139. test('new buffer from Float32Array', function (t) {
  140. if (typeof Float32Array !== 'undefined') {
  141. var b1 = new Float32Array([0, 1, 2, 3])
  142. var b2 = new B(b1)
  143. t.equal(b1.length, b2.length)
  144. t.equal(b1[0], 0)
  145. t.equal(b1[1], 1)
  146. t.equal(b1[2], 2)
  147. t.equal(b1[3], 3)
  148. t.equal(b1[4], undefined)
  149. }
  150. t.end()
  151. })
  152. test('new buffer from Float64Array', function (t) {
  153. if (typeof Float64Array !== 'undefined') {
  154. var b1 = new Float64Array([0, 1, 2, 3])
  155. var b2 = new B(b1)
  156. t.equal(b1.length, b2.length)
  157. t.equal(b1[0], 0)
  158. t.equal(b1[1], 1)
  159. t.equal(b1[2], 2)
  160. t.equal(b1[3], 3)
  161. t.equal(b1[4], undefined)
  162. }
  163. t.end()
  164. })
  165. test('new buffer from buffer.toJSON() output', function (t) {
  166. if (typeof JSON === 'undefined') {
  167. // ie6, ie7 lack support
  168. t.end()
  169. return
  170. }
  171. var buf = new B('test')
  172. var json = JSON.stringify(buf)
  173. var obj = JSON.parse(json)
  174. var copy = new B(obj)
  175. t.ok(buf.equals(copy))
  176. t.end()
  177. })