summaryrefslogtreecommitdiff
path: root/tests/run/constants.pyx
blob: 37772eaef9d9be0c5c609db65481296108a36a5f (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import sys
IS_PY3 = sys.version_info[0] >= 3

cimport cython

DEF INT_VAL = 1

def _func(a,b,c):
    return a+b+c

@cython.test_fail_if_path_exists("//AddNode")
def add():
    """
    >>> add() == 1+2+3+4
    True
    """
    return 1+2+3+4

#@cython.test_fail_if_path_exists("//AddNode")
def add_var(a):
    """
    >>> add_var(10) == 1+2+10+3+4
    True
    """
    return 1+2 +a+ 3+4

@cython.test_fail_if_path_exists("//AddNode", "//SubNode")
def neg():
    """
    >>> neg() == -1 -2 - (-3+4)
    True
    """
    return -1 -2 - (-3+4)

@cython.test_fail_if_path_exists("//AddNode", "//MulNode", "//DivNode")
def long_int_mix():
    """
    >>> long_int_mix() == 1 + (2 * 3) // 2
    True
    >>> if IS_PY3: type(long_int_mix()) is int  or type(long_int_mix())
    ... else:      type(long_int_mix()) is long or type(long_int_mix())
    True
    """
    return 1L + (2 * 3L) // 2

@cython.test_fail_if_path_exists("//AddNode", "//MulNode", "//DivNode")
def char_int_mix():
    """
    >>> char_int_mix() == 1 + (ord(' ') * 3) // 2 + ord('A')
    True
    """
    return 1L + (c' ' * 3L) // 2 + c'A'

@cython.test_fail_if_path_exists("//AddNode", "//MulNode")
def int_cast():
    """
    >>> int_cast() == 1 + 2 * 6000
    True
    """
    return <int>(1 + 2 * 6000)

@cython.test_fail_if_path_exists("//MulNode")
def mul():
    """
    >>> mul() == 1*60*1000
    True
    """
    return 1*60*1000

@cython.test_fail_if_path_exists("//AddNode", "//MulNode")
def arithm():
    """
    >>> arithm() == 9*2+3*8//6-10
    True
    """
    return 9*2+3*8//6-10

@cython.test_fail_if_path_exists("//AddNode", "//MulNode")
def parameters():
    """
    >>> parameters() == _func(-1 -2, - (-3+4), 1*2*3)
    True
    """
    return _func(-1 -2, - (-3+4), 1*2*3)

#@cython.test_fail_if_path_exists("//AddNode")
def lists():
    """
    >>> lists() == [1,2,3] + [4,5,6]
    True
    """
    return [1,2,3] + [4,5,6]

@cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists_right_len1():
    """
    >>> multiplied_lists_right_len1() == [1] * 5
    True
    """
    return [1] * 5

@cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists_right():
    """
    >>> multiplied_lists_right() == [1,2,3] * 5
    True
    """
    return [1,2,3] * 5

@cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists_left():
    """
    >>> multiplied_lists_left() == [1,2,3] * 5
    True
    """
    return 5 * [1,2,3]

@cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists_neg():
    """
    >>> multiplied_lists_neg() == [1,2,3] * -5
    True
    """
    return [1,2,3] * -5

@cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists_nonconst(x):
    """
    >>> multiplied_lists_nonconst(5) == [1,2,3] * 5
    True
    >>> multiplied_lists_nonconst(-5) == [1,2,3] * -5
    True
    >>> multiplied_lists_nonconst(0) == [1,2,3] * 0
    True

    >>> try: [1,2,3] * 'abc'
    ... except TypeError: pass
    >>> try: multiplied_nonconst_tuple_arg('abc')
    ... except TypeError: pass
    >>> try: [1,2,3] * 1.0
    ... except TypeError: pass
    >>> try: multiplied_nonconst_tuple_arg(1.0)
    ... except TypeError: pass
    """
    return [1,2,3] * x

@cython.test_assert_path_exists("//MulNode")
def multiplied_lists_nonconst_left(x):
    """
    >>> multiplied_lists_nonconst_left(5) == 5 * [1,2,3]
    True
    >>> multiplied_lists_nonconst_left(-5) == -5 * [1,2,3]
    True
    >>> multiplied_lists_nonconst_left(0) == 0 * [1,2,3]
    True
    """
    return x * [1,2,3]

@cython.test_fail_if_path_exists("//MulNode//ListNode")
@cython.test_assert_path_exists("//MulNode")
def multiplied_lists_nonconst_expression(x):
    """
    >>> multiplied_lists_nonconst_expression(5) == [1,2,3] * (5 * 2)
    True
    >>> multiplied_lists_nonconst_expression(-5) == [1,2,3] * (-5 * 2)
    True
    >>> multiplied_lists_nonconst_expression(0) == [1,2,3] * (0 * 2)
    True
    """
    return [1,2,3] * (x*2)

cdef side_effect(int x):
    print x
    return x

@cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists_with_side_effects():
    """
    >>> multiplied_lists_with_side_effects() == [1,2,3] * 5
    1
    2
    3
    True
    """
    return [side_effect(1), side_effect(2), side_effect(3)] * 5

@cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists_nonconst_with_side_effects(x):
    """
    >>> multiplied_lists_nonconst_with_side_effects(5) == [1,2,3] * 5
    1
    2
    3
    True
    """
    return [side_effect(1), side_effect(2), side_effect(3)] * x

@cython.test_fail_if_path_exists("//MulNode")
def multiplied_nonconst_tuple_arg(x):
    """
    >>> multiplied_nonconst_tuple_arg(5) == (1,2) * 5
    True
    >>> multiplied_nonconst_tuple_arg(-5) == (1,2) * -5
    True
    >>> multiplied_nonconst_tuple_arg(0) == (1,2) * 0
    True

    >>> try: (1,2) * 'abc'
    ... except TypeError: pass
    >>> try: multiplied_nonconst_tuple_arg('abc')
    ... except TypeError: pass
    >>> try: (1,2) * 1.0
    ... except TypeError: pass
    >>> try: multiplied_nonconst_tuple_arg(1.0)
    ... except TypeError: pass
    """
    return (1,2) * x

@cython.test_fail_if_path_exists("//MulNode")
def multiplied_nonconst_tuple_int_arg(int x):
    """
    >>> multiplied_nonconst_tuple_int_arg(5) == (1,2) * 5
    True
    """
    return (1,2) * x

@cython.test_fail_if_path_exists("//MulNode")
def multiplied_nonconst_tuple(x):
    """
    >>> multiplied_nonconst_tuple(5) == (1,2) * (5+1)
    True
    """
    return (1,2) * (x + 1)

MULT = 5

@cython.test_fail_if_path_exists("//MulNode")
def multiplied_global_nonconst_tuple():
    """
    >>> multiplied_global_nonconst_tuple() == (1,2,3) * 5
    1
    2
    3
    True
    """
    return (side_effect(1), side_effect(2), side_effect(3)) * MULT

@cython.test_fail_if_path_exists("//MulNode")
def multiplied_const_tuple():
    """
    >>> multiplied_const_tuple() == (1,2) * 5
    True
    """
    return (1,2) * 5

@cython.test_fail_if_path_exists("//MulNode")
def multiplied_const_tuple_len1():
    """
    >>> multiplied_const_tuple_len1() == (1,) * 5
    True
    """
    return (1,) * 5

@cython.test_fail_if_path_exists("//PrimaryCmpNode")
def compile_time_DEF():
    """
    >>> compile_time_DEF()
    (1, False, True, True, False)
    """
    return INT_VAL, INT_VAL == 0, INT_VAL != 0, INT_VAL == 1, INT_VAL != 1

@cython.test_fail_if_path_exists("//PrimaryCmpNode")
def cascaded_compare():
    """
    >>> cascaded_compare()
    True
    """
    return 1 < 2 < 3 < 4