def test___mul__(): x = Iter('ABCD') y = Iter('ABCD') z = (x * y).collect(list) assert len(z) == 16 assert z == [('A', 'A'), ('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('B', 'D'), ('C', 'A'), ('C', 'B'), ('C', 'C'), ('C', 'D'), ('D', 'A'), ('D', 'B'), ('D', 'C'), ('D', 'D')]
def test_permutations(): x = Iter('ABCD').permutations(r=2).collect(list) assert len(x) == 12 assert x == [('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'A'), ('B', 'C'), ('B', 'D'), ('C', 'A'), ('C', 'B'), ('C', 'D'), ('D', 'A'), ('D', 'B'), ('D', 'C')]
def test_count(): count = Iter.count(start=5, step=2) assert next(count) == 5 assert next(count) == 7 assert next(count) == 9 assert next(count) == 11
def test_star_map(): def pow(x, y): return x**y x = Iter(range(4)).zip(range(4)).star_map(pow) assert list(x) == [0**0, 1**1, 2**2, 3**3]
def test_product(): x = Iter('ABCD').product(repeat=2).collect(list) assert len(x) == 16 assert x == [('A', 'A'), ('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('B', 'D'), ('C', 'A'), ('C', 'B'), ('C', 'C'), ('C', 'D'), ('D', 'A'), ('D', 'B'), ('D', 'C'), ('D', 'D')]
def test_zip_longest_other_way(): x = Iter(range(2)).zip_longest(range(3)) assert list(x) == [(0, 0), (1, 1), (None, 2)]
def char_iter(): return Iter(c for c in HELLO_WORLD)
def test_enumerate(): d = Iter(('a', 'b', 'c')) assert tuple(d.enumerate()) == ((0, 'a'), (1, 'b'), (2, 'c'))
def test_on_list(): d = Iter([0, 1, 2, 3, 4]) assert tuple(d.map(lambda x: 2 * x)) == (0, 2, 4, 6, 8)
def test_combinations_with_replacement(): x = Iter('ABCD').combinations_with_replacement(2).collect(list) assert len(x) == 10 assert x == [('A', 'A'), ('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'B'), ('B', 'C'), ('B', 'D'), ('C', 'C'), ('C', 'D'), ('D', 'D')]
def test_cycle(): x = Iter('ABCD').cycle() cycle = itertools.cycle('ABCD') for _ in range(1000): assert next(x) == next(cycle)
def int_iter(): return Iter(range(5))
def test_combinations(): x = Iter('ABCD').combinations(2).collect(list) assert len(x) == 6 assert x == [('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]
def test_repeat(): repeat = Iter.repeat(True) for _ in range(100): assert next(repeat)
def test_zip_longest(): x = Iter(range(3)).zip_longest(range(2)) assert list(x) == [(0, 0), (1, 1), (2, None)]
def test_unzip(): foo = Iter(range(3)).zip(range(3), range(3)) a, b, c = foo.unzip() assert a == b == c == [0, 1, 2]
def test_matmul(): a = Iter(range(3)) b = Iter(range(4)) return a @ b == 1**2 + 2**2 + 3**2
def test_dot(): a = Iter(range(3)) b = Iter(range(4)) return a.dot(b) == 1**2 + 2**2 + 3**2
def test_compress(): x = Iter('hello!') selectors = [0, 1, 0, 1, 1, 0] assert ''.join(x.compress(selectors)) == 'elo'
def test_or(): x = Iter(range(3)) | range(2) assert list(x) == [(0, 0), (1, 1), (2, None)]
from hypoxia import Iter x = (c for c in 'hello') print(x) h = Iter(x) print(h) # y = h.map(lambda c: c.upper()) y = h.filter(lambda c: c != 'l') print(y) print(''.join(y)) # x = (c for c in 'hello')
def test_repeat_limit(): repeat = Iter.repeat(True, 10) assert list(repeat) == [True for _ in range(10)]