Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

methods.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
  2. var B = require('../').Buffer
  3. var test = require('tape')
  4. test('buffer.toJSON', function (t) {
  5. var data = [1, 2, 3, 4]
  6. t.deepEqual(
  7. new B(data).toJSON(),
  8. { type: 'Buffer', data: [ 1, 2, 3, 4 ] }
  9. )
  10. t.end()
  11. })
  12. test('buffer.copy', function (t) {
  13. // copied from nodejs.org example
  14. var buf1 = new B(26)
  15. var buf2 = new B(26)
  16. for (var i = 0; i < 26; i++) {
  17. buf1[i] = i + 97 // 97 is ASCII a
  18. buf2[i] = 33 // ASCII !
  19. }
  20. buf1.copy(buf2, 8, 16, 20)
  21. t.equal(
  22. buf2.toString('ascii', 0, 25),
  23. '!!!!!!!!qrst!!!!!!!!!!!!!'
  24. )
  25. t.end()
  26. })
  27. test('test offset returns are correct', function (t) {
  28. var b = new B(16)
  29. t.equal(4, b.writeUInt32LE(0, 0))
  30. t.equal(6, b.writeUInt16LE(0, 4))
  31. t.equal(7, b.writeUInt8(0, 6))
  32. t.equal(8, b.writeInt8(0, 7))
  33. t.equal(16, b.writeDoubleLE(0, 8))
  34. t.end()
  35. })
  36. test('concat() a varying number of buffers', function (t) {
  37. var zero = []
  38. var one = [ new B('asdf') ]
  39. var long = []
  40. for (var i = 0; i < 10; i++) {
  41. long.push(new B('asdf'))
  42. }
  43. var flatZero = B.concat(zero)
  44. var flatOne = B.concat(one)
  45. var flatLong = B.concat(long)
  46. var flatLongLen = B.concat(long, 40)
  47. t.equal(flatZero.length, 0)
  48. t.equal(flatOne.toString(), 'asdf')
  49. t.deepEqual(flatOne, one[0])
  50. t.equal(flatLong.toString(), (new Array(10 + 1).join('asdf')))
  51. t.equal(flatLongLen.toString(), (new Array(10 + 1).join('asdf')))
  52. t.end()
  53. })
  54. test('fill', function (t) {
  55. var b = new B(10)
  56. b.fill(2)
  57. t.equal(b.toString('hex'), '02020202020202020202')
  58. t.end()
  59. })
  60. test('fill (string)', function (t) {
  61. var b = new B(10)
  62. b.fill('abc')
  63. t.equal(b.toString(), 'abcabcabca')
  64. b.fill('է')
  65. t.equal(b.toString(), 'էէէէէ')
  66. t.end()
  67. })
  68. test('copy() empty buffer with sourceEnd=0', function (t) {
  69. var source = new B([42])
  70. var destination = new B([43])
  71. source.copy(destination, 0, 0, 0)
  72. t.equal(destination.readUInt8(0), 43)
  73. t.end()
  74. })
  75. test('copy() after slice()', function (t) {
  76. var source = new B(200)
  77. var dest = new B(200)
  78. var expected = new B(200)
  79. for (var i = 0; i < 200; i++) {
  80. source[i] = i
  81. dest[i] = 0
  82. }
  83. source.slice(2).copy(dest)
  84. source.copy(expected, 0, 2)
  85. t.deepEqual(dest, expected)
  86. t.end()
  87. })
  88. test('copy() ascending', function (t) {
  89. var b = new B('abcdefghij')
  90. b.copy(b, 0, 3, 10)
  91. t.equal(b.toString(), 'defghijhij')
  92. t.end()
  93. })
  94. test('copy() descending', function (t) {
  95. var b = new B('abcdefghij')
  96. b.copy(b, 3, 0, 7)
  97. t.equal(b.toString(), 'abcabcdefg')
  98. t.end()
  99. })
  100. test('buffer.slice sets indexes', function (t) {
  101. t.equal((new B('hallo')).slice(0, 5).toString(), 'hallo')
  102. t.end()
  103. })
  104. test('buffer.slice out of range', function (t) {
  105. t.plan(2)
  106. t.equal((new B('hallo')).slice(0, 10).toString(), 'hallo')
  107. t.equal((new B('hallo')).slice(10, 2).toString(), '')
  108. t.end()
  109. })