iterators.spec.ts 945 B

12345678910111213141516171819202122232425262728293031
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import Iterator from '../iterator'
  7. function iteratorToArray<T>(it: Iterator<T>): T[] {
  8. const ret = [];
  9. while (it.hasNext) {
  10. const v = it.move();
  11. ret[ret.length] = v;
  12. }
  13. return ret;
  14. }
  15. describe('basic iterators', () => {
  16. function check<T>(name: string, iter: Iterator<T>, expected: T[]) {
  17. it(name, () => {
  18. expect(iteratorToArray(iter)).toEqual(expected);
  19. });
  20. }
  21. check('empty', Iterator.Empty, []);
  22. check('singleton', Iterator.Value(10), [10]);
  23. check('array', Iterator.Array([1, 2, 3]), [1, 2, 3]);
  24. check('range', Iterator.Range(0, 3), [0, 1, 2, 3]);
  25. check('map', Iterator.map(Iterator.Range(0, 1), x => x + 1), [1, 2]);
  26. check('filter', Iterator.filter(Iterator.Range(0, 3), x => x >= 2), [2, 3]);
  27. });