test-buffer-compare-offset.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. var Buffer = require('../../').Buffer;
  3. var assert = require('assert');
  4. var a = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
  5. var b = Buffer.from([5, 6, 7, 8, 9, 0, 1, 2, 3, 4]);
  6. assert.equal(-1, a.compare(b));
  7. // Equivalent to a.compare(b).
  8. assert.equal(-1, a.compare(b, 0));
  9. assert.equal(-1, a.compare(b, '0'));
  10. // Equivalent to a.compare(b).
  11. assert.equal(-1, a.compare(b, 0, undefined, 0));
  12. // Zero-length targer, return 1
  13. assert.equal(1, a.compare(b, 0, 0, 0));
  14. assert.equal(1, a.compare(b, '0', '0', '0'));
  15. // Equivalent to Buffer.compare(a, b.slice(6, 10))
  16. assert.equal(1, a.compare(b, 6, 10));
  17. // Zero-length source, return -1
  18. assert.equal(-1, a.compare(b, 6, 10, 0, 0));
  19. // Equivalent to Buffer.compare(a.slice(4), b.slice(0, 5))
  20. assert.equal(1, a.compare(b, 0, 5, 4));
  21. // Equivalent to Buffer.compare(a.slice(1), b.slice(5))
  22. assert.equal(1, a.compare(b, 5, undefined, 1));
  23. // Equivalent to Buffer.compare(a.slice(2), b.slice(2, 4))
  24. assert.equal(-1, a.compare(b, 2, 4, 2));
  25. // Equivalent to Buffer.compare(a.slice(4), b.slice(0, 7))
  26. assert.equal(-1, a.compare(b, 0, 7, 4));
  27. // Equivalent to Buffer.compare(a.slice(4, 6), b.slice(0, 7));
  28. assert.equal(-1, a.compare(b, 0, 7, 4, 6));
  29. // zero length target
  30. assert.equal(1, a.compare(b, 0, null));
  31. // coerces to targetEnd == 5
  32. assert.equal(-1, a.compare(b, 0, {valueOf: () => 5}));
  33. // zero length target
  34. assert.equal(1, a.compare(b, Infinity, -Infinity));
  35. // zero length target because default for targetEnd <= targetSource
  36. assert.equal(1, a.compare(b, '0xff'));
  37. var oor = /out of range index/;
  38. assert.throws(() => a.compare(b, 0, 100, 0), oor);
  39. assert.throws(() => a.compare(b, 0, 1, 0, 100), oor);
  40. assert.throws(() => a.compare(b, -1), oor);
  41. assert.throws(() => a.compare(b, 0, '0xff'), oor);
  42. assert.throws(() => a.compare(b, 0, Infinity), oor);
  43. assert.throws(() => a.compare(b, -Infinity, Infinity), oor);
  44. assert.throws(() => a.compare(), /Argument must be a Buffer/);