test-buffer-inheritance.js 824B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var Buffer = require('../../').Buffer;
  3. var assert = require('assert');
  4. function T(n) {
  5. var ui8 = new Uint8Array(n);
  6. Object.setPrototypeOf(ui8, T.prototype);
  7. return ui8;
  8. }
  9. Object.setPrototypeOf(T.prototype, Buffer.prototype);
  10. Object.setPrototypeOf(T, Buffer);
  11. T.prototype.sum = function sum() {
  12. var cntr = 0;
  13. for (var i = 0; i < this.length; i++)
  14. cntr += this[i];
  15. return cntr;
  16. };
  17. var vals = [new T(4), T(4)];
  18. vals.forEach(function(t) {
  19. assert.equal(t.constructor, T);
  20. assert.equal(Object.getPrototypeOf(t), T.prototype);
  21. assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(t)),
  22. Buffer.prototype);
  23. t.fill(5);
  24. var cntr = 0;
  25. for (var i = 0; i < t.length; i++)
  26. cntr += t[i];
  27. assert.equal(t.length * 5, cntr);
  28. // Check this does not throw
  29. t.toString();
  30. });