예제 #1
0
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))
예제 #2
0
def test_split_pickle3(protocol):
    l = [T(1), T(2), T(3), T(4), T(5), T(3), T(7), T(8)]
    spl = split(l, equalsthreeT, keep=True)
    assert next(spl) == toT([1, 2])
    x = pickle.dumps(spl, protocol=protocol)
    assert list(
        pickle.loads(x)) == [toT(i) for i in [[3], [4, 5], [3], [7, 8]]]
예제 #3
0
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))
예제 #4
0
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)
예제 #5
0
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_roundrobin_failure_lengthhint3():
    # Check if by adding the different lengths it could lead to overflow.
    # We use two iterables both with sys.maxsize length.
    it1 = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize)
    it2 = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize)
    it = roundrobin(it1, it2)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
def test_deepflatten_normal3():
    # really deeply nested thingy
    assert list(
        deepflatten([[[[[[[[[[[T(5), T(4), T(3),
                               T(2), T(1), T(0)]]]]],
                          map(T, range(3))]]], (T(i) for i in range(5))]]
                     ])) == toT([5, 4, 3, 2, 1, 0, 0, 1, 2, 0, 1, 2, 3, 4])
예제 #8
0
def test_basic_examples():
    p = partial(capture, T(1), T(2), a=T(10), b=T(20))
    assert callable(p)
    assert p(T(3), T(4), b=T(30), c=T(40)) == ((T(1), T(2), T(3), T(4)),
                                               dict(a=T(10), b=T(30), c=T(40)))
    p = partial(map, lambda x: x * T(10))
    assert list(p([T(1), T(2), T(3), T(4)])) == toT([10, 20, 30, 40])
예제 #9
0
def test_intersperse_pickle4(protocol):
    its = intersperse([T(1), T(2), T(3)], T(0))
    assert next(its) == T(1)
    assert next(its) == T(0)
    assert next(its) == T(2)
    x = pickle.dumps(its, protocol=protocol)
    assert list(pickle.loads(x)) == toT([0, 3])
def test_deepflatten_normal4():
    # really deeply nested thingy with types
    assert list(
        deepflatten(
            [[[[[[[[[[[T(5), T(4), T(3), T(2),
                       T(1), T(0)]]]]], [T(0), T(1), T(2)]]]],
               [T(0), T(1), T(2), T(3), T(4)]]]],
            types=list)) == toT([5, 4, 3, 2, 1, 0, 0, 1, 2, 0, 1, 2, 3, 4])
def test_deepflatten_reduce1():
    # Earlier we were able to modify the iteratorlist (including deleting
    # parts of it). That could lead to segmentation faults.
    df = deepflatten(toT([1, 2, 3, 4, 5, 6]))
    next(df)
    # Clear the iteratorlist from all items.
    df.__reduce__()[2][0][:] = []
    next(df)
예제 #12
0
def test_accumulate_lengthhint_failure2():
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1)
    acc = accumulate(of_it)
    with pytest.raises(OverflowError):
        operator.length_hint(acc)

    with pytest.raises(OverflowError):
        list(acc)
def test_accumulate_lengthhint_failure1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    acc = accumulate(f_it)
    with pytest.raises(_hf.FailLengthHint.EXC_TYP, match=_hf.FailLengthHint.EXC_MSG):
        operator.length_hint(acc)

    with pytest.raises(_hf.FailLengthHint.EXC_TYP, match=_hf.FailLengthHint.EXC_MSG):
        list(acc)
예제 #14
0
def test_grouper_lengthhint_failure2():
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1)
    it = grouper(of_it, 2)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
예제 #15
0
def test_sideeffects_failure_lengthhint2():
    # This only checks for overflow if the length_hint is above PY_SSIZE_T_MAX
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1)
    it = sideeffects(of_it, return_None)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
예제 #16
0
def test_accumulate_attributes1():
    it = accumulate(toT([1, 2, 3]))
    assert it.func is None
    with pytest.raises(AttributeError):
        it.current

    for item in it:
        assert item == it.current
        assert it.func is None
예제 #17
0
def test_split_failure6():
    # more than one keep* parameter
    with pytest.raises(ValueError) as exc:
        split(toT([1, 2, 3, 4]),
              T(2),
              eq=True,
              keep_before=True,
              keep_after=True)
    assert '`keep`, `keep_before`, `keep_after`' in str(exc.value)
def test_roundrobin_failure_lengthhint2():
    # This only checks for overflow if the length_hint is above PY_SSIZE_T_MAX
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1)
    it = roundrobin(of_it)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
def test_unique_justseen_attributes1():
    it = unique_justseen(toT([1, 1, 2, 2, 3, 3]), None)
    assert it.key is None
    with pytest.raises(AttributeError):
        it.lastseen

    next(it)

    assert it.lastseen == T(1)
예제 #20
0
def test_sideeffects_failure_lengthhint1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    it = sideeffects(f_it, return_None)
    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        operator.length_hint(it)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)

    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        list(it)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)
def test_successive_failure_lengthhint1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    it = successive(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)
예제 #22
0
def test_intersperse_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 = intersperse(of_it, 2)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
예제 #23
0
def test_replicate_failure_lengthhint1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    it = replicate(f_it, 3)
    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        operator.length_hint(it)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)

    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        list(it)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)
def test_roundrobin_failure_lengthhint1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    it = roundrobin(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)
예제 #25
0
def test_accumulate_lengthhint_failure1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    acc = accumulate(f_it)
    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        operator.length_hint(acc)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)

    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        list(acc)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)
예제 #26
0
def test_sideeffects_failure_lengthhint1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    it = sideeffects(f_it, return_None)
    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)
예제 #27
0
def test_grouper_lengthhint_failure1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    it = grouper(f_it, 2)
    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        operator.length_hint(it)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)

    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        list(it)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)
예제 #28
0
def test_intersperse_lengthhint_failure1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    it = intersperse(f_it, 2)
    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)
예제 #29
0
def test_replicate_failure_lengthhint3():
    # It is also possible that the length_hint overflows when the length is
    # below maxsize but "times * length" is above maxsize.
    # In this case length = maxsize / 2 but times = 3
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize // 2)
    it = replicate(of_it, 3)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
예제 #30
0
def test_intersperse_lengthhint_failure3():
    # The length_hint method multiplies the length_hint of the iterable with
    # 2 (and adds/subtracts 1) so it's actually possible to have overflow even
    # if the length of the iterable doesn't trigger the overflow!
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize)
    it = intersperse(of_it, 2)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)