1# mode: run
2# tag: all, builtins, werror
3
4cdef class VerboseGetItem(object):
5    cdef object sequence
6    def __init__(self, seq):
7        self.sequence = seq
8    def __getitem__(self, i):
9        print i
10        return self.sequence[i] # may raise IndexError
11
12
13cimport cython
14
15@cython.test_assert_path_exists("//SimpleCallNode")
16@cython.test_fail_if_path_exists("//ForInStatNode")
17def all_item(x):
18    """
19    >>> all_item([1,1,1,1,1])
20    True
21    >>> all_item([1,1,1,1,0])
22    False
23    >>> all_item([0,1,1,1,0])
24    False
25
26    >>> all(VerboseGetItem([1,1,1,0,0]))
27    0
28    1
29    2
30    3
31    False
32    >>> all_item(VerboseGetItem([1,1,1,0,0]))
33    0
34    1
35    2
36    3
37    False
38
39    >>> all(VerboseGetItem([1,1,1,1,1]))
40    0
41    1
42    2
43    3
44    4
45    5
46    True
47    >>> all_item(VerboseGetItem([1,1,1,1,1]))
48    0
49    1
50    2
51    3
52    4
53    5
54    True
55    """
56    return all(x)
57
58
59@cython.test_assert_path_exists(
60    "//ForInStatNode",
61    "//InlinedGeneratorExpressionNode"
62)
63@cython.test_fail_if_path_exists(
64    "//SimpleCallNode",
65    "//YieldExprNode"
66)
67def all_in_simple_gen(seq):
68    """
69    >>> all_in_simple_gen([1,1,1])
70    True
71    >>> all_in_simple_gen([1,1,0])
72    False
73    >>> all_in_simple_gen([1,0,1])
74    False
75
76    >>> all_in_simple_gen(VerboseGetItem([1,1,1,1,1]))
77    0
78    1
79    2
80    3
81    4
82    5
83    True
84    >>> all_in_simple_gen(VerboseGetItem([1,1,0,1,1]))
85    0
86    1
87    2
88    False
89    """
90    return all(x for x in seq)
91
92
93@cython.test_assert_path_exists(
94    "//ForInStatNode",
95    "//InlinedGeneratorExpressionNode"
96)
97@cython.test_fail_if_path_exists(
98    "//SimpleCallNode",
99    "//YieldExprNode"
100)
101def all_in_simple_gen_scope(seq):
102    """
103    >>> all_in_simple_gen_scope([1,1,1])
104    True
105    >>> all_in_simple_gen_scope([1,1,0])
106    False
107    >>> all_in_simple_gen_scope([1,0,1])
108    False
109
110    >>> all_in_simple_gen_scope(VerboseGetItem([1,1,1,1,1]))
111    0
112    1
113    2
114    3
115    4
116    5
117    True
118    >>> all_in_simple_gen_scope(VerboseGetItem([1,1,0,1,1]))
119    0
120    1
121    2
122    False
123    """
124    x = 'abc'
125    result = all(x for x in seq)
126    assert x == 'abc'
127    return result
128
129
130@cython.test_assert_path_exists(
131    "//ForInStatNode",
132    "//InlinedGeneratorExpressionNode"
133)
134@cython.test_fail_if_path_exists(
135    "//SimpleCallNode",
136    "//YieldExprNode"
137)
138def all_in_conditional_gen(seq):
139    """
140    >>> all_in_conditional_gen([3,6,9])
141    False
142    >>> all_in_conditional_gen([0,3,7])
143    False
144    >>> all_in_conditional_gen([1,0,1])
145    True
146
147    >>> all_in_conditional_gen(VerboseGetItem([1,1,1,1,1]))
148    0
149    1
150    2
151    3
152    4
153    5
154    True
155    >>> all_in_conditional_gen(VerboseGetItem([1,1,0,1,1]))
156    0
157    1
158    2
159    3
160    4
161    5
162    True
163    """
164    return all(x%3 for x in seq if x%2 == 1)
165
166
167mixed_ustring = u'AbcDefGhIjKlmnoP'
168lower_ustring = mixed_ustring.lower()
169upper_ustring = mixed_ustring.upper()
170
171
172@cython.test_assert_path_exists(
173    '//PythonCapiCallNode',
174    '//ForFromStatNode'
175)
176@cython.test_fail_if_path_exists(
177    '//SimpleCallNode',
178    '//ForInStatNode'
179)
180def all_lower_case_characters(unicode ustring):
181    """
182    >>> all_lower_case_characters(mixed_ustring)
183    False
184    >>> all_lower_case_characters(upper_ustring)
185    False
186    >>> all_lower_case_characters(lower_ustring)
187    True
188    """
189    return all(uchar.islower() for uchar in ustring)
190
191
192@cython.test_assert_path_exists(
193    "//ForInStatNode",
194    "//InlinedGeneratorExpressionNode",
195    "//InlinedGeneratorExpressionNode//IfStatNode"
196)
197@cython.test_fail_if_path_exists(
198    "//SimpleCallNode",
199    "//YieldExprNode",
200#    "//IfStatNode//CoerceToBooleanNode"
201)
202def all_in_typed_gen(seq):
203    """
204    >>> all_in_typed_gen([1,1,1])
205    True
206    >>> all_in_typed_gen([1,0,0])
207    False
208
209    >>> all_in_typed_gen(VerboseGetItem([1,1,1,1,1]))
210    0
211    1
212    2
213    3
214    4
215    5
216    True
217    >>> all_in_typed_gen(VerboseGetItem([1,1,1,1,0]))
218    0
219    1
220    2
221    3
222    4
223    False
224    """
225    cdef int x
226    return all(x for x in seq)
227
228
229@cython.test_assert_path_exists(
230    "//ForInStatNode",
231    "//InlinedGeneratorExpressionNode",
232    "//InlinedGeneratorExpressionNode//IfStatNode"
233)
234@cython.test_fail_if_path_exists(
235    "//SimpleCallNode",
236    "//YieldExprNode",
237#    "//IfStatNode//CoerceToBooleanNode"
238)
239def all_in_double_gen(seq):
240    """
241    >>> all(x for L in [[1,1,1],[1,1,1],[1,1,1]] for x in L)
242    True
243    >>> all_in_double_gen([[1,1,1],[1,1,1],[1,1,1]])
244    True
245
246    >>> all(x for L in [[1,1,1],[1,1,1],[1,1,0]] for x in L)
247    False
248    >>> all_in_double_gen([[1,1,1],[1,1,1],[1,1,0]])
249    False
250
251    >>> all(x for L in [[1,1,1],[0,1,1],[1,1,1]] for x in L)
252    False
253    >>> all_in_double_gen([[1,1,1],[0,1,1],[1,1,1]])
254    False
255
256    >>> all_in_double_gen([VerboseGetItem([1,1,1]), VerboseGetItem([1,1,1,1,1])])
257    0
258    1
259    2
260    3
261    0
262    1
263    2
264    3
265    4
266    5
267    True
268    >>> all_in_double_gen([VerboseGetItem([1,1,1]),VerboseGetItem([1,1]),VerboseGetItem([1,1,0])])
269    0
270    1
271    2
272    3
273    0
274    1
275    2
276    0
277    1
278    2
279    False
280    >>> all_in_double_gen([VerboseGetItem([1,1,1]),VerboseGetItem([1,0,1]),VerboseGetItem([1,1])])
281    0
282    1
283    2
284    3
285    0
286    1
287    False
288    """
289    cdef int x
290    return all(x for L in seq for x in L)
291