選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

write.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
  2. var B = require('../').Buffer
  3. var test = require('tape')
  4. var isnan = require('is-nan')
  5. test('buffer.write string should get parsed as number', function (t) {
  6. var b = new B(64)
  7. b.writeUInt16LE('1003', 0)
  8. t.equal(b.readUInt16LE(0), 1003)
  9. t.end()
  10. })
  11. test('buffer.writeUInt8 a fractional number will get Math.floored', function (t) {
  12. // Some extra work is necessary to make this test pass with the Object implementation
  13. var b = new B(1)
  14. b.writeInt8(5.5, 0)
  15. t.equal(b[0], 5)
  16. t.end()
  17. })
  18. test('writeUint8 with a negative number throws', function (t) {
  19. var buf = new B(1)
  20. t.throws(function () {
  21. buf.writeUInt8(-3, 0)
  22. })
  23. t.end()
  24. })
  25. test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) {
  26. t.plan(2 * (2 * 2 * 2 + 2))
  27. var hex = [
  28. '03', '0300', '0003', '03000000', '00000003',
  29. 'fd', 'fdff', 'fffd', 'fdffffff', 'fffffffd'
  30. ]
  31. var reads = [ 3, 3, 3, 3, 3, -3, -3, -3, -3, -3 ]
  32. var xs = ['UInt', 'Int']
  33. var ys = [8, 16, 32]
  34. for (var i = 0; i < xs.length; i++) {
  35. var x = xs[i]
  36. for (var j = 0; j < ys.length; j++) {
  37. var y = ys[j]
  38. var endianesses = (y === 8) ? [''] : ['LE', 'BE']
  39. for (var k = 0; k < endianesses.length; k++) {
  40. var z = endianesses[k]
  41. var v1 = new B(y / 8)
  42. var writefn = 'write' + x + y + z
  43. var val = (x === 'Int') ? -3 : 3
  44. v1[writefn](val, 0)
  45. t.equal(
  46. v1.toString('hex'),
  47. hex.shift()
  48. )
  49. var readfn = 'read' + x + y + z
  50. t.equal(
  51. v1[readfn](0),
  52. reads.shift()
  53. )
  54. }
  55. }
  56. }
  57. t.end()
  58. })
  59. test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) {
  60. if (!B.TYPED_ARRAY_SUPPORT) {
  61. t.pass('object impl: skipping overflow test')
  62. t.end()
  63. return
  64. }
  65. t.plan(3 * (2 * 2 * 2 + 2))
  66. var hex = [
  67. '', '03', '00', '030000', '000000',
  68. '', 'fd', 'ff', 'fdffff', 'ffffff'
  69. ]
  70. var reads = [
  71. undefined, 3, 0, NaN, 0,
  72. undefined, 253, -256, 16777213, -256
  73. ]
  74. var xs = ['UInt', 'Int']
  75. var ys = [8, 16, 32]
  76. for (var i = 0; i < xs.length; i++) {
  77. var x = xs[i]
  78. for (var j = 0; j < ys.length; j++) {
  79. var y = ys[j]
  80. var endianesses = (y === 8) ? [''] : ['LE', 'BE']
  81. for (var k = 0; k < endianesses.length; k++) {
  82. var z = endianesses[k]
  83. var v1 = new B(y / 8 - 1)
  84. var next = new B(4)
  85. next.writeUInt32BE(0, 0)
  86. var writefn = 'write' + x + y + z
  87. var val = (x === 'Int') ? -3 : 3
  88. v1[writefn](val, 0, true)
  89. t.equal(
  90. v1.toString('hex'),
  91. hex.shift()
  92. )
  93. // check that nothing leaked to next buffer.
  94. t.equal(next.readUInt32BE(0), 0)
  95. // check that no bytes are read from next buffer.
  96. next.writeInt32BE(~0, 0)
  97. var readfn = 'read' + x + y + z
  98. var r = reads.shift()
  99. if (isnan(r)) t.pass('equal')
  100. else t.equal(v1[readfn](0, true), r)
  101. }
  102. }
  103. }
  104. t.end()
  105. })
  106. test('large values do not improperly roll over (ref #80)', function (t) {
  107. var nums = [-25589992, -633756690, -898146932]
  108. var out = new B(12)
  109. out.fill(0)
  110. out.writeInt32BE(nums[0], 0)
  111. var newNum = out.readInt32BE(0)
  112. t.equal(nums[0], newNum)
  113. out.writeInt32BE(nums[1], 4)
  114. newNum = out.readInt32BE(4)
  115. t.equal(nums[1], newNum)
  116. out.writeInt32BE(nums[2], 8)
  117. newNum = out.readInt32BE(8)
  118. t.equal(nums[2], newNum)
  119. t.end()
  120. })