test-buffer-iterator.js 925 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. var Buffer = require('../../').Buffer;
  3. var assert = require('assert');
  4. var buffer = Buffer.from([1, 2, 3, 4, 5]);
  5. var arr;
  6. var b;
  7. // buffers should be iterable
  8. arr = [];
  9. for (b of buffer)
  10. arr.push(b);
  11. assert.deepStrictEqual(arr, [1, 2, 3, 4, 5]);
  12. // buffer iterators should be iterable
  13. arr = [];
  14. for (b of buffer[Symbol.iterator]())
  15. arr.push(b);
  16. assert.deepStrictEqual(arr, [1, 2, 3, 4, 5]);
  17. // buffer#values() should return iterator for values
  18. arr = [];
  19. for (b of buffer.values())
  20. arr.push(b);
  21. assert.deepStrictEqual(arr, [1, 2, 3, 4, 5]);
  22. // buffer#keys() should return iterator for keys
  23. arr = [];
  24. for (b of buffer.keys())
  25. arr.push(b);
  26. assert.deepStrictEqual(arr, [0, 1, 2, 3, 4]);
  27. // buffer#entries() should return iterator for entries
  28. arr = [];
  29. for (b of buffer.entries())
  30. arr.push(b);
  31. assert.deepStrictEqual(arr, [
  32. [0, 1],
  33. [1, 2],
  34. [2, 3],
  35. [3, 4],
  36. [4, 5]
  37. ]);