summaryrefslogtreecommitdiff
path: root/tests/run/listcomp.pyx
blob: a143855bb3d52be684edb0c1a8c3f49616529308 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# mode: run
# tag: listcomp, comprehension

cimport cython


def smoketest():
    """
    >>> smoketest()
    [0, 4, 8]
    """
    x = 'abc'
    result = [x*2 for x in range(5) if x % 2 == 0]
    assert x != 'abc'
    return result


def list_genexp():
    """
    >>> list_genexp()
    [0, 4, 8]
    """
    x = 'abc'
    result = list(x*2 for x in range(5) if x % 2 == 0)
    assert x == 'abc'
    return result


def int_runvar():
    """
    >>> int_runvar()
    [0, 4, 8]
    """
    cdef int x
    print [x*2 for x in range(5) if x % 2 == 0]


cdef class A:
    def __repr__(self): return u"A"

def typed():
    """
    >>> typed()
    [A, A, A]
    """
    cdef A obj
    print [obj for obj in [A(), A(), A()]]


def inferred_type():
    """
    >>> inferred_type()
    ['A', 'A', 'A']
    """
    print [cython.typeof(obj) for obj in [A(), A(), A()]]


def not_inferred_type():
    """
    >>> not_inferred_type()
    ['Python object', 'Python object', 'Python object']
    """
    print [cython.typeof(obj) for obj in [1, A(), 'abc']]


def iterdict():
    """
    >>> iterdict()
    [1, 2, 3]
    """
    cdef dict d = dict(a=1,b=2,c=3)
    l = [d[key] for key in d]
    l.sort()
    print l


listcomp_result = [ i*i for i in range(5) ]
def global_listcomp():
    """
    >>> [ i*i for i in range(5) ]
    [0, 1, 4, 9, 16]
    >>> listcomp_result
    [0, 1, 4, 9, 16]
    """


class ListCompInClass(object):
    """
    >>> x = ListCompInClass()
    >>> x.listcomp
    [1, 2, 3]
    """
    listcomp = [i+1 for i in range(3)]


cdef class ListCompInCClass:
    """
    >>> x = ListCompInCClass()
    >>> x.listcomp
    [1, 2, 3]
    """
    listcomp = [i+1 for i in range(3)]


def nested_result():
    """
    >>> nested_result()
    [[], [-1], [-1, 0], [-1, 0, 1]]
    """
    result = [[a-1 for a in range(b)] for b in range(4)]
    return result


def listcomp_as_condition(sequence):
    """
    >>> listcomp_as_condition(['a', 'b', '+'])
    True
    >>> listcomp_as_condition('ab+')
    True
    >>> listcomp_as_condition('abc')
    False
    """
    if [1 for c in sequence if c in '+-*/<=>!%&|([^~,']:
        return True
    return False


@cython.test_fail_if_path_exists("//SimpleCallNode//ComprehensionNode")
@cython.test_assert_path_exists("//ComprehensionNode")
def sorted_listcomp(sequence):
    """
    >>> sorted_listcomp([])
    []
    >>> sorted_listcomp([1])
    [2]
    >>> sorted_listcomp([3,2,4])
    [3, 4, 5]
    """
    return sorted([ n+1 for n in sequence ])


@cython.test_fail_if_path_exists("//IfStatNode",
                                 "//ComprehensionAppendNode")
@cython.test_assert_path_exists("//ComprehensionNode")
def listcomp_const_condition_false():
    """
    >>> listcomp_const_condition_false()
    []
    """
    return [x*2 for x in range(3) if False]


@cython.test_fail_if_path_exists("//IfStatNode",
                                 "//ComprehensionAppendNode")
@cython.test_assert_path_exists("//ComprehensionNode")
def listcomp_const_condition_false_bool_test():
    """
    >>> listcomp_const_condition_false_bool_test()
    True
    """
    return not [l for l in [1] if False]


@cython.test_fail_if_path_exists("//IfStatNode",
                                 "//ComprehensionAppendNode")
@cython.test_assert_path_exists("//ComprehensionNode")
def listcomp_const_condition_false_assert():
    """
    >>> listcomp_const_condition_false_assert()
    """
    assert not [l for l in [1] if False]


@cython.test_fail_if_path_exists("//ComprehensionNode//IfStatNode",
                                 "//ComprehensionAppendNode")
@cython.test_assert_path_exists("//ComprehensionNode",
                                "//IfStatNode")
def listcomp_const_condition_false_if():
    """
    >>> listcomp_const_condition_false_if()
    True
    """
    if not [l for l in [1] if False]:
        return True
    return False


@cython.test_fail_if_path_exists("//ComprehensionNode//IfStatNode",
                                 "//ComprehensionAppendNode")
@cython.test_assert_path_exists("//ComprehensionNode",
                                "//IfStatNode")
def listcomp_const_condition_false_typed_error():
    """
    >>> listcomp_const_condition_false_typed_error()  # doctest: +ELLIPSIS
    Traceback (most recent call last):
    TypeError: ...
    """
    cdef str l
    if not [l for l in [1] if False]:
        return True
    return False


@cython.test_fail_if_path_exists("//IfStatNode")
@cython.test_assert_path_exists("//ComprehensionNode",
                                "//ComprehensionAppendNode")
def listcomp_const_condition_true():
    """
    >>> listcomp_const_condition_true()
    [0, 2, 4]
    """
    return [x*2 for x in range(3) if True]