iterators.spec.ts 776 B

1234567891011121314151617181920212223242526
  1. /**
  2. * Copyright (c) 2017 molio 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. for (let v = it.move(); !it.done; v = it.move()) ret[ret.length] = v;
  10. return ret;
  11. }
  12. describe('basic iterators', () => {
  13. function check<T>(name: string, iter: Iterator<T>, expected: T[]) {
  14. it(name, () => {
  15. expect(iteratorToArray(iter)).toEqual(expected);
  16. });
  17. }
  18. check('empty', Iterator.Empty, []);
  19. check('singleton', Iterator.Value(10), [10]);
  20. check('array', Iterator.Array([1, 2, 3]), [1, 2, 3]);
  21. check('range', Iterator.Range(0, 3), [0, 1, 2, 3]);
  22. });