fix inop test
[cython.git] / tests / run / inop.pyx
1
2 cimport cython
3
4 def f(a,b):
5     """
6     >>> f(1,[1,2,3])
7     True
8     >>> f(5,[1,2,3])
9     False
10     >>> f(2,(1,2,3))
11     True
12     """
13     cdef object result = a in b
14     return result
15
16 def g(a,b):
17     """
18     >>> g(1,[1,2,3])
19     1
20     >>> g(5,[1,2,3])
21     0
22     >>> g(2,(1,2,3))
23     1
24     """
25     cdef int result = a in b
26     return result
27
28 def h(b):
29     """
30     >>> h([1,2,3,4])
31     True
32     >>> h([1,3,4])
33     False
34     """
35     cdef object result = 2 in b
36     return result
37
38 def j(b):
39     """
40     >>> j([1,2,3,4])
41     1
42     >>> j([1,3,4])
43     0
44     """
45     cdef int result = 2 in b
46     return result
47
48 @cython.test_fail_if_path_exists("//SwitchStatNode")
49 def k(a):
50     """
51     >>> k(1)
52     1
53     >>> k(5)
54     0
55     """
56     cdef int result = a in [1,2,3,4]
57     return result
58
59 @cython.test_assert_path_exists("//SwitchStatNode")
60 @cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
61 def m_list(int a):
62     """
63     >>> m_list(2)
64     1
65     >>> m_list(5)
66     0
67     """
68     cdef int result = a in [1,2,3,4]
69     return result
70
71 @cython.test_assert_path_exists("//SwitchStatNode")
72 @cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
73 def m_tuple(int a):
74     """
75     >>> m_tuple(2)
76     1
77     >>> m_tuple(5)
78     0
79     """
80     cdef int result = a in (1,2,3,4)
81     return result
82
83 @cython.test_assert_path_exists("//SwitchStatNode")
84 @cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
85 def m_set(int a):
86     """
87     >>> m_set(2)
88     1
89     >>> m_set(5)
90     0
91     """
92     cdef int result = a in {1,2,3,4}
93     return result
94
95 cdef bytes bytes_string = b'ab\0cde\0f\0g'
96 py_bytes_string = bytes_string
97
98 @cython.test_assert_path_exists("//PrimaryCmpNode")
99 @cython.test_fail_if_path_exists("//SwitchStatNode", "//BoolBinopNode")
100 def m_bytes(char a, bytes bytes_string):
101     """
102     >>> m_bytes(ord('f'), py_bytes_string)
103     1
104     >>> m_bytes(ord('X'), py_bytes_string)
105     0
106     >>> 'f'.encode('ASCII') in None    # doctest: +ELLIPSIS
107     Traceback (most recent call last):
108     TypeError: ...iterable...
109     >>> m_bytes(ord('f'), None)
110     Traceback (most recent call last):
111     TypeError: argument of type 'NoneType' is not iterable
112     """
113     cdef int result = a in bytes_string
114     return result
115
116 @cython.test_assert_path_exists("//PrimaryCmpNode")
117 @cython.test_fail_if_path_exists("//SwitchStatNode", "//BoolBinopNode")
118 def m_bytes_unsigned(unsigned char a, bytes bytes_string):
119     """
120     >>> m_bytes(ord('f'), py_bytes_string)
121     1
122     >>> m_bytes(ord('X'), py_bytes_string)
123     0
124     >>> 'f'.encode('ASCII') in None    # doctest: +ELLIPSIS
125     Traceback (most recent call last):
126     TypeError: ...iterable...
127     >>> m_bytes(ord('f'), None)
128     Traceback (most recent call last):
129     TypeError: argument of type 'NoneType' is not iterable
130     """
131     cdef int result = a in bytes_string
132     return result
133
134 @cython.test_assert_path_exists("//SwitchStatNode")
135 @cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
136 def m_bytes_literal(char a):
137     """
138     >>> m_bytes_literal(ord('f'))
139     1
140     >>> m_bytes_literal(ord('X'))
141     0
142     """
143     cdef int result = a in b'ab\0cde\0f\0g'
144     return result
145
146 @cython.test_assert_path_exists("//SwitchStatNode")
147 @cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
148 def m_bytes_literal_unsigned(unsigned char a):
149     """
150     >>> m_bytes_literal(ord('f'))
151     1
152     >>> m_bytes_literal(ord('X'))
153     0
154     """
155     cdef int result = a in b'ab\0cde\0f\0g'
156     return result
157
158 cdef unicode unicode_string = u'abc\0defg\u1234\uF8D2'
159 py_unicode_string = unicode_string
160
161 @cython.test_assert_path_exists("//PrimaryCmpNode")
162 @cython.test_fail_if_path_exists("//SwitchStatNode", "//BoolBinopNode")
163 def m_unicode(Py_UNICODE a, unicode unicode_string):
164     """
165     >>> m_unicode(ord('f'), py_unicode_string)
166     1
167     >>> m_unicode(ord('X'), py_unicode_string)
168     0
169     >>> m_unicode(ord(py_klingon_character), py_unicode_string)
170     1
171
172     >>> 'f' in None   # doctest: +ELLIPSIS
173     Traceback (most recent call last):
174     TypeError: ...iterable...
175     >>> m_unicode(ord('f'), None)
176     Traceback (most recent call last):
177     TypeError: argument of type 'NoneType' is not iterable
178     """
179     cdef int result = a in unicode_string
180     return result
181
182 cdef unicode klingon_character = u'\uF8D2'
183 py_klingon_character = klingon_character
184
185 @cython.test_assert_path_exists("//SwitchStatNode")
186 @cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
187 def m_unicode_literal(Py_UNICODE a):
188     """
189     >>> m_unicode_literal(ord('f'))
190     1
191     >>> m_unicode_literal(ord('X'))
192     0
193     >>> m_unicode_literal(ord(py_klingon_character))
194     1
195     """
196     cdef int result = a in u'abc\0defg\u1234\uF8D2'
197     return result
198
199 cdef unicode wide_unicode_character = u'\U0010FEDC'
200 py_wide_unicode_character = wide_unicode_character
201 wide_unicode_character_surrogate1 = 0xDBFF
202 wide_unicode_character_surrogate2 = 0xDEDC
203
204 @cython.test_fail_if_path_exists("//SwitchStatNode")
205 @cython.test_assert_path_exists("//PrimaryCmpNode")
206 def m_wide_unicode_literal(Py_UNICODE a):
207     """
208     >>> m_unicode_literal(ord('f'))
209     1
210     >>> m_unicode_literal(ord('X'))
211     0
212     >>> import sys
213     >>> if sys.maxunicode == 65535:
214     ...     m_wide_unicode_literal(wide_unicode_character_surrogate1)
215     ...     m_wide_unicode_literal(wide_unicode_character_surrogate2)
216     ... else:
217     ...     m_wide_unicode_literal(ord(py_wide_unicode_character))
218     ...     1
219     1
220     1
221     """
222     cdef int result = a in u'abc\0defg\u1234\uF8D2\U0010FEDC'
223     return result
224
225 @cython.test_assert_path_exists("//SwitchStatNode")
226 @cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
227 def conditional_int(int a):
228     """
229     >>> conditional_int(1)
230     1
231     >>> conditional_int(0)
232     2
233     >>> conditional_int(5)
234     2
235     """
236     return 1 if a in (1,2,3,4) else 2
237
238 @cython.test_assert_path_exists("//SwitchStatNode")
239 @cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
240 def conditional_object(int a):
241     """
242     >>> conditional_object(1)
243     1
244     >>> conditional_object(0)
245     '2'
246     >>> conditional_object(5)
247     '2'
248     """
249     return 1 if a in (1,2,3,4) else '2'
250
251 @cython.test_assert_path_exists("//SwitchStatNode")
252 @cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
253 def conditional_bytes(char a):
254     """
255     >>> conditional_bytes(ord('a'))
256     1
257     >>> conditional_bytes(ord('X'))
258     '2'
259     >>> conditional_bytes(0)
260     '2'
261     """
262     return 1 if a in b'abc' else '2'
263
264 @cython.test_assert_path_exists("//SwitchStatNode")
265 @cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
266 def conditional_unicode(Py_UNICODE a):
267     """
268     >>> conditional_unicode(ord('a'))
269     1
270     >>> conditional_unicode(ord('X'))
271     '2'
272     >>> conditional_unicode(0)
273     '2'
274     """
275     return 1 if a in u'abc' else '2'
276
277 @cython.test_assert_path_exists("//SwitchStatNode")
278 @cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
279 def conditional_none(int a):
280     """
281     >>> conditional_none(1)
282     >>> conditional_none(0)
283     1
284     >>> conditional_none(5)
285     1
286     """
287     return None if a in {1,2,3,4} else 1
288
289 def n(a):
290     """
291     >>> n('d *')
292     1
293     >>> n('xxx')
294     0
295     """
296     cdef int result = a.lower() in [u'a *',u'b *',u'c *',u'd *']
297     return result
298
299 def p(a):
300     """
301     >>> p(1)
302     0
303     >>> p('a')
304     1
305     """
306     cdef dict d = {u'a': 1, u'b': 2}
307     cdef int result = a in d
308     return result
309
310 def q(a):
311     """
312     >>> q(1)
313     Traceback (most recent call last):
314     TypeError: 'NoneType' object is not iterable
315         >>> l = [1,2,3,4]
316     >>> l2 = [l[1:],l[:-1],l]
317     >>> 2 in l in l2
318     True
319     """
320     cdef dict d = None
321     cdef int result = a in d # should fail with a TypeError
322     return result
323
324 def r(a):
325     """
326     >>> r(2)
327     1
328     """
329     cdef object l = [1,2,3,4]
330     cdef object l2 = [l[1:],l[:-1],l]
331     cdef int result = a in l in l2
332     return result
333
334 def s(a):
335     """
336     >>> s(2)
337     1
338     """
339     cdef int result = a in [1,2,3,4] in [[1,2,3],[2,3,4],[1,2,3,4]]
340     return result