test-buffer-slow.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. var Buffer = require('../../').Buffer;
  3. var assert = require('assert');
  4. var buffer = require('../../');
  5. var Buffer = buffer.Buffer;
  6. var SlowBuffer = buffer.SlowBuffer;
  7. var ones = [1, 1, 1, 1];
  8. // should create a Buffer
  9. var sb = SlowBuffer(4);
  10. assert(sb instanceof Buffer);
  11. assert.strictEqual(sb.length, 4);
  12. sb.fill(1);
  13. for (var [key, value] of sb.entries()) {
  14. assert.deepStrictEqual(value, ones[key]);
  15. }
  16. // underlying ArrayBuffer should have the same length
  17. assert.strictEqual(sb.buffer.byteLength, 4);
  18. // should work without new
  19. sb = SlowBuffer(4);
  20. assert(sb instanceof Buffer);
  21. assert.strictEqual(sb.length, 4);
  22. sb.fill(1);
  23. for (var [key, value] of sb.entries()) {
  24. assert.deepStrictEqual(value, ones[key]);
  25. }
  26. // should work with edge cases
  27. assert.strictEqual(SlowBuffer(0).length, 0);
  28. try {
  29. assert.strictEqual(
  30. SlowBuffer(buffer.kMaxLength).length, buffer.kMaxLength);
  31. } catch (e) {
  32. assert.equal(e.message, 'Array buffer allocation failed');
  33. }
  34. // should work with number-coercible values
  35. assert.strictEqual(SlowBuffer('6').length, 6);
  36. assert.strictEqual(SlowBuffer(true).length, 1);
  37. // should create zero-length buffer if parameter is not a number
  38. assert.strictEqual(SlowBuffer().length, 0);
  39. assert.strictEqual(SlowBuffer(NaN).length, 0);
  40. assert.strictEqual(SlowBuffer({}).length, 0);
  41. assert.strictEqual(SlowBuffer('string').length, 0);
  42. // should throw with invalid length
  43. assert.throws(function() {
  44. SlowBuffer(Infinity);
  45. }, 'invalid Buffer length');
  46. assert.throws(function() {
  47. SlowBuffer(-1);
  48. }, 'invalid Buffer length');
  49. assert.throws(function() {
  50. SlowBuffer(buffer.kMaxLength + 1);
  51. }, 'invalid Buffer length');