test-buffer-bytelength.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use strict';
  2. var Buffer = require('../../').Buffer;
  3. var assert = require('assert');
  4. var Buffer = require('../../').Buffer;
  5. var SlowBuffer = require('../../').SlowBuffer;
  6. // coerce values to string
  7. assert.equal(Buffer.byteLength(32, 'latin1'), 2);
  8. assert.equal(Buffer.byteLength(NaN, 'utf8'), 3);
  9. assert.equal(Buffer.byteLength({}, 'latin1'), 15);
  10. assert.equal(Buffer.byteLength(), 9);
  11. var buff = new Buffer(10);
  12. assert(ArrayBuffer.isView(buff));
  13. var slowbuff = new SlowBuffer(10);
  14. assert(ArrayBuffer.isView(slowbuff));
  15. // buffer
  16. var incomplete = Buffer.from([0xe4, 0xb8, 0xad, 0xe6, 0x96]);
  17. assert.equal(Buffer.byteLength(incomplete), 5);
  18. var ascii = Buffer.from('abc');
  19. assert.equal(Buffer.byteLength(ascii), 3);
  20. // ArrayBuffer
  21. var buffer = new ArrayBuffer(8);
  22. assert.equal(Buffer.byteLength(buffer), 8);
  23. // TypedArray
  24. var int8 = new Int8Array(8);
  25. assert.equal(Buffer.byteLength(int8), 8);
  26. var uint8 = new Uint8Array(8);
  27. assert.equal(Buffer.byteLength(uint8), 8);
  28. var uintc8 = new Uint8ClampedArray(2);
  29. assert.equal(Buffer.byteLength(uintc8), 2);
  30. var int16 = new Int16Array(8);
  31. assert.equal(Buffer.byteLength(int16), 16);
  32. var uint16 = new Uint16Array(8);
  33. assert.equal(Buffer.byteLength(uint16), 16);
  34. var int32 = new Int32Array(8);
  35. assert.equal(Buffer.byteLength(int32), 32);
  36. var uint32 = new Uint32Array(8);
  37. assert.equal(Buffer.byteLength(uint32), 32);
  38. var float32 = new Float32Array(8);
  39. assert.equal(Buffer.byteLength(float32), 32);
  40. var float64 = new Float64Array(8);
  41. assert.equal(Buffer.byteLength(float64), 64);
  42. // DataView
  43. var dv = new DataView(new ArrayBuffer(2));
  44. assert.equal(Buffer.byteLength(dv), 2);
  45. // special case: zero length string
  46. assert.equal(Buffer.byteLength('', 'ascii'), 0);
  47. assert.equal(Buffer.byteLength('', 'HeX'), 0);
  48. // utf8
  49. assert.equal(Buffer.byteLength('∑éllö wørl∂!', 'utf-8'), 19);
  50. assert.equal(Buffer.byteLength('κλμνξο', 'utf8'), 12);
  51. assert.equal(Buffer.byteLength('挵挶挷挸挹', 'utf-8'), 15);
  52. assert.equal(Buffer.byteLength('𠝹𠱓𠱸', 'UTF8'), 12);
  53. // without an encoding, utf8 should be assumed
  54. assert.equal(Buffer.byteLength('hey there'), 9);
  55. assert.equal(Buffer.byteLength('𠱸挶νξ#xx :)'), 17);
  56. assert.equal(Buffer.byteLength('hello world', ''), 11);
  57. // it should also be assumed with unrecognized encoding
  58. assert.equal(Buffer.byteLength('hello world', 'abc'), 11);
  59. assert.equal(Buffer.byteLength('ßœ∑≈', 'unkn0wn enc0ding'), 10);
  60. // base64
  61. assert.equal(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'base64'), 11);
  62. assert.equal(Buffer.byteLength('bm9kZS5qcyByb2NrcyE=', 'base64'), 14);
  63. assert.equal(Buffer.byteLength('aGkk', 'base64'), 3);
  64. assert.equal(Buffer.byteLength('bHNrZGZsa3NqZmtsc2xrZmFqc2RsZmtqcw==',
  65. 'base64'), 25);
  66. // special padding
  67. assert.equal(Buffer.byteLength('aaa=', 'base64'), 2);
  68. assert.equal(Buffer.byteLength('aaaa==', 'base64'), 3);
  69. assert.equal(Buffer.byteLength('Il était tué'), 14);
  70. assert.equal(Buffer.byteLength('Il était tué', 'utf8'), 14);
  71. assert.equal(Buffer.byteLength('Il était tué', 'ascii'), 12);
  72. assert.equal(Buffer.byteLength('Il était tué', 'latin1'), 12);
  73. assert.equal(Buffer.byteLength('Il était tué', 'binary'), 12);
  74. ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
  75. assert.equal(24, Buffer.byteLength('Il était tué', encoding));
  76. });