def test_merge_stable1(): # Stability tests (no use of T on purpose!) it = merge([1], [1.]) item1 = next(it) assert isinstance(item1, int) item2 = next(it) assert isinstance(item2, float)
def test_traverse(): """To test the traverse implementation we call gc.collect() while instances of all the C objects are still valid.""" acc = iteration_utilities.accumulate([]) app = iteration_utilities.applyfunc(lambda x: x, 1) cha = iteration_utilities.chained(int, float) cla = iteration_utilities.clamp([], 0, 1) com = iteration_utilities.complement(int) con = iteration_utilities.constant(1) dee = iteration_utilities.deepflatten([]) dup = iteration_utilities.duplicates([]) fli = iteration_utilities.flip(int) gro = iteration_utilities.grouper([], 2) ine = iteration_utilities.intersperse([], 1) iik = iteration_utilities.ItemIdxKey(10, 2) ite = iteration_utilities.iter_except(int, TypeError) mer = iteration_utilities.merge([]) nth = iteration_utilities.nth(1) pac = iteration_utilities.packed(int) par = iteration_utilities.partial(int, 10) rep = iteration_utilities.replicate([], 3) rou = iteration_utilities.roundrobin([]) see = iteration_utilities.Seen() sid = iteration_utilities.sideeffects([], lambda x: x) spl = iteration_utilities.split([], lambda x: True) sta = iteration_utilities.starfilter(lambda x: True, []) suc = iteration_utilities.successive([]) tab = iteration_utilities.tabulate(int) une = iteration_utilities.unique_everseen([]) unj = iteration_utilities.unique_justseen([]) gc.collect()
def test_merge_failure_setstate7(): # __setstate__ with current containing itemidxkey without key even though # a key function is given from iteration_utilities import ItemIdxKey as IIK mg = merge(toT(range(5)), toT(range(3, 10, 2))) with pytest.raises(TypeError): mg.__setstate__((lambda x: x, 0, (IIK(-2, 0), IIK(-1, 1, 2)), 2))
def test_merge_failure_setstate8(): # __setstate__ with current containing itemidxkey with index that is out # of bounds from iteration_utilities import ItemIdxKey as IIK mg = merge(toT(range(5)), toT(range(3, 10, 2))) with pytest.raises(ValueError): mg.__setstate__((None, 0, (IIK(-2, 0), IIK(-1, 20)), 2))
def test_merge_key1(): # Key function tests seq = ([(T(1), T(0)), (T(2), T(0))], [(T(1), T(-1)), (T(2), T(-1))]) assert (list(merge(*seq, key=operator.itemgetter(0))) == [(T(1), T(0)), (T(1), T(-1)), (T(2), T(0)), (T(2), T(-1))])
def test_merge_keyreverse1(): # Key+reverse function tests seq = ([(T(2), T(0)), (T(1), T(0))], [(T(2), T(-1)), (T(1), T(-1))]) assert (list(merge(*seq, reverse=True, key=operator.itemgetter(0))) == [(T(2), T(0)), (T(2), T(-1)), (T(1), T(0)), (T(1), T(-1))])
def test_merge_reduce1(): # We shouldn't be able to alter the ItemIdxKey instances in the "current" # tuple that is returned from reduce. We could remove or add the key # attribute which would break the comparisons df = merge([T(1), T(2), T(3)], [T(1), T(2), T(3)]) next(df) # add a key even though we have no key function df.__reduce__()[2][2][0].key = 10 list(df)
def test_merge_lengthhint_failure2(): # This is the easy way to overflow the length_hint: If the iterable itself # has a length_hint > sys.maxsize of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1) it = merge(of_it) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_merge_lengthhint_failure1(): f_it = _hf.FailLengthHint(toT([1, 2, 3])) it = merge(f_it) with pytest.raises(_hf.FailLengthHint.EXC_TYP, match=_hf.FailLengthHint.EXC_MSG): operator.length_hint(it) with pytest.raises(_hf.FailLengthHint.EXC_TYP, match=_hf.FailLengthHint.EXC_MSG): list(it)
def test_merge_lengthhint_failure5(): # Like the test above but this time with a "started" merge of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize) it = merge(toT([1, 2, 3]), of_it) next(it) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_merge_lengthhint_failure4(): # Overflow could also happen when adding length_hints that individually are # below the sys.maxsize # In this case we have 3 + sys.maxsize of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize) it = merge(toT([1, 2, 3]), of_it) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_merge_lengthhint_failure3(): # Like the test case above but this time we take one item because # internally an unstarted "merge" and started "merge" are treated # differently of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1) it = merge(of_it) next(it) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_merge_setstate1(): # We shouldn't be able to alter the ItemIdxKey instances in the "current" # tuple that is used to setstate. We could remove or add the key # attribute which would break the comparisons df = merge([T(1), T(2), T(3)], [T(1), T(2), T(3)]) next(df) # we roundtrip the state but keep a reference so we can later add a key # even though we have no key function state = df.__reduce__()[2] df.__setstate__(state) state[2][0].key = 10 list(df)
def test_merge_lengthhint1(): it = merge([0], [1, 2, 3], [1]) _hf.check_lengthhint_iteration(it, 5)
def test_merge_empty4(): # generator, one ends immediately the other only after two items assert list(merge((i for i in []), (i for i in (T(1), T(2))), {})) == [T(1), T(2)]
def test_merge_normal4(): for seq in itertools.permutations([[T(1), T(3)], [T(0), T(2)], [T(4)]]): assert list(merge(*seq)) == toT([0, 1, 2, 3, 4])
def test_merge_normal6(): # key=None is identical to no key assert list(merge([T(1)], [T(2)], key=None)) == [T(1), T(2)]
def test_merge_pickle3(protocol): # reverse mge = merge([T(2), T(1)], [T(0)], [T(3)], reverse=True) assert next(mge) == T(3) x = pickle.dumps(mge, protocol=protocol) assert list(pickle.loads(x)) == toT([2, 1, 0])
def test_merge_pickle2(protocol): # with key mge = merge([T(1), T(2)], [T(0)], [T(-2)], key=abs) assert next(mge) == T(0) x = pickle.dumps(mge, protocol=protocol) assert list(pickle.loads(x)) == toT([1, 2, -2])
def test_merge_pickle1(protocol): # normal mge = merge([T(0)], [T(1), T(2)], [T(2)]) assert next(mge) == T(0) x = pickle.dumps(mge, protocol=protocol) assert list(pickle.loads(x)) == toT([1, 2, 2])
def test_merge_copy1(): _hf.iterator_copy(merge([T(0)], [T(1), T(2)], [T(2)]))
def test_merge_failure_setstate9(): _hf.iterator_setstate_list_fail(merge(toT(range(5)), toT(range(3, 10, 2))))
def test_merge_normal2(): for seq in itertools.permutations([[T(1)], [T(2)], [T(3)], []]): assert list(merge(*seq)) == toT([1, 2, 3])
def test_merge_pickle5(protocol): # pickle merge with no exhausted iterable mge = merge([T(0), T(1)], [T(1), T(2)]) assert next(mge) == T(0) x = pickle.dumps(mge, protocol=protocol) assert list(pickle.loads(x)) == toT([1, 1, 2])
def test_merge_normal5(): perms = itertools.permutations( [toT(range(5)), toT(range(3)), toT(range(4, 7))]) for seq in perms: assert list(merge(*seq)) == toT([0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 6])
def test_merge_failure_setstate10(): _hf.iterator_setstate_empty_fail(merge(toT(range(5)), toT(range(3, 10, 2))))
def test_merge_pickle4(protocol): # pickle unstarted merge instance mge = merge([T(0)], [T(1), T(2)], [T(2)]) x = pickle.dumps(mge, protocol=protocol) assert list(pickle.loads(x)) == toT([0, 1, 2, 2])
def test_merge_empty3(): assert list(merge([], (), {})) == []
def test_merge_reverse2(): for seq in itertools.permutations([[T(1)], [T(2)], [T(3)], []]): assert list(merge(*seq, reverse=True)) == toT([3, 2, 1])
def test_merge_empty5(): # generator assert list(merge((), {}, (i for i in []))) == []