summaryrefslogtreecommitdiff
path: root/tests/run/bytearray_iter.py
blob: 60df9fcc14f5e4e78bdb0cfcd0be4f48d58a8852 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# mode: run
# tag: pure3, pure2

import cython

@cython.test_assert_path_exists("//ForFromStatNode")
@cython.test_fail_if_path_exists("//ForInStatNode")
@cython.locals(x=bytearray)
def basic_bytearray_iter(x):
    """
    >>> basic_bytearray_iter(bytearray(b"hello"))
    h
    e
    l
    l
    o
    """
    for a in x:
        print(chr(a))

@cython.test_assert_path_exists("//ForFromStatNode")
@cython.test_fail_if_path_exists("//ForInStatNode")
@cython.locals(x=bytearray)
def reversed_bytearray_iter(x):
    """
    >>> reversed_bytearray_iter(bytearray(b"hello"))
    o
    l
    l
    e
    h
    """
    for a in reversed(x):
        print(chr(a))

@cython.test_assert_path_exists("//ForFromStatNode")
@cython.test_fail_if_path_exists("//ForInStatNode")
@cython.locals(x=bytearray)
def modifying_bytearray_iter1(x):
    """
    >>> modifying_bytearray_iter1(bytearray(b"abcdef"))
    a
    b
    c
    3
    """
    count = 0
    for a in x:
        print(chr(a))
        del x[-1]
        count += 1
    print(count)

@cython.test_assert_path_exists("//ForFromStatNode")
@cython.test_fail_if_path_exists("//ForInStatNode")
@cython.locals(x=bytearray)
def modifying_bytearray_iter2(x):
    """
    >>> modifying_bytearray_iter2(bytearray(b"abcdef"))
    a
    c
    e
    3
    """
    count = 0
    for a in x:
        print(chr(a))
        del x[0]
        count += 1
    print(count)

@cython.test_assert_path_exists("//ForFromStatNode")
@cython.test_fail_if_path_exists("//ForInStatNode")
@cython.locals(x=bytearray)
def modifying_reversed_bytearray_iter(x):
    """
    NOTE - I'm not 100% sure how well-defined this behaviour is in Python.
    However, for the moment Python and Cython seem to do the same thing.
    Testing that it doesn't crash is probably more important than the exact output!
    >>> modifying_reversed_bytearray_iter(bytearray(b"abcdef"))
    f
    f
    f
    f
    f
    f
    """
    for a in reversed(x):
        print(chr(a))
        del x[0]

# ticket: 3473

def test_bytearray_iteration(src):
    """
    >>> src = b'123'
    >>> test_bytearray_iteration(src)
    49
    50
    51
    """

    data = bytearray(src)
    for elem in data:
        print(elem)