test-buffer-iterator.js 925 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. ]);