cython

Форк
0
/
constants.pyx 
285 строк · 6.4 Кб
1
# mode: run
2

3
cimport cython
4

5
DEF INT_VAL = 1
6

7
def _func(a,b,c):
8
    return a+b+c
9

10
@cython.test_fail_if_path_exists("//AddNode")
11
def add():
12
    """
13
    >>> add() == 1+2+3+4
14
    True
15
    """
16
    return 1+2+3+4
17

18
#@cython.test_fail_if_path_exists("//AddNode")
19
def add_var(a):
20
    """
21
    >>> add_var(10) == 1+2+10+3+4
22
    True
23
    """
24
    return 1+2 +a+ 3+4
25

26
@cython.test_fail_if_path_exists("//AddNode", "//SubNode")
27
def neg():
28
    """
29
    >>> neg() == -1 -2 - (-3+4)
30
    True
31
    """
32
    return -1 -2 - (-3+4)
33

34
@cython.test_fail_if_path_exists("//AddNode", "//MulNode", "//DivNode")
35
def long_int_mix():
36
    """
37
    >>> long_int_mix() == 1 + (2 * 3) // 2
38
    True
39
    >>> type(long_int_mix()) is int  or type(long_int_mix())
40
    True
41
    """
42
    return 1L + (2 * 3L) // 2
43

44
@cython.test_fail_if_path_exists("//AddNode", "//MulNode", "//DivNode")
45
def char_int_mix():
46
    """
47
    >>> char_int_mix() == 1 + (ord(' ') * 3) // 2 + ord('A')
48
    True
49
    """
50
    return 1L + (c' ' * 3L) // 2 + c'A'
51

52
@cython.test_fail_if_path_exists("//AddNode", "//MulNode")
53
def int_cast():
54
    """
55
    >>> int_cast() == 1 + 2 * 6000
56
    True
57
    """
58
    return <int>(1 + 2 * 6000)
59

60
@cython.test_fail_if_path_exists("//MulNode")
61
def mul():
62
    """
63
    >>> mul() == 1*60*1000
64
    True
65
    """
66
    return 1*60*1000
67

68
@cython.test_fail_if_path_exists("//AddNode", "//MulNode")
69
def arithm():
70
    """
71
    >>> arithm() == 9*2+3*8//6-10
72
    True
73
    """
74
    return 9*2+3*8//6-10
75

76
@cython.test_fail_if_path_exists("//AddNode", "//MulNode")
77
def parameters():
78
    """
79
    >>> parameters() == _func(-1 -2, - (-3+4), 1*2*3)
80
    True
81
    """
82
    return _func(-1 -2, - (-3+4), 1*2*3)
83

84
#@cython.test_fail_if_path_exists("//AddNode")
85
def lists():
86
    """
87
    >>> lists() == [1,2,3] + [4,5,6]
88
    True
89
    """
90
    return [1,2,3] + [4,5,6]
91

92
@cython.test_fail_if_path_exists("//MulNode")
93
def multiplied_lists_right_len1():
94
    """
95
    >>> multiplied_lists_right_len1() == [1] * 5
96
    True
97
    """
98
    return [1] * 5
99

100
@cython.test_fail_if_path_exists("//MulNode")
101
def multiplied_lists_right():
102
    """
103
    >>> multiplied_lists_right() == [1,2,3] * 5
104
    True
105
    """
106
    return [1,2,3] * 5
107

108
@cython.test_fail_if_path_exists("//MulNode")
109
def multiplied_lists_left():
110
    """
111
    >>> multiplied_lists_left() == [1,2,3] * 5
112
    True
113
    """
114
    return 5 * [1,2,3]
115

116
@cython.test_fail_if_path_exists("//MulNode")
117
def multiplied_lists_neg():
118
    """
119
    >>> multiplied_lists_neg() == [1,2,3] * -5
120
    True
121
    """
122
    return [1,2,3] * -5
123

124
@cython.test_fail_if_path_exists("//MulNode")
125
def multiplied_lists_nonconst(x):
126
    """
127
    >>> multiplied_lists_nonconst(5) == [1,2,3] * 5
128
    True
129
    >>> multiplied_lists_nonconst(-5) == [1,2,3] * -5
130
    True
131
    >>> multiplied_lists_nonconst(0) == [1,2,3] * 0
132
    True
133

134
    >>> try: [1,2,3] * 'abc'
135
    ... except TypeError: pass
136
    >>> try: multiplied_nonconst_tuple_arg('abc')
137
    ... except TypeError: pass
138
    >>> try: [1,2,3] * 1.0
139
    ... except TypeError: pass
140
    >>> try: multiplied_nonconst_tuple_arg(1.0)
141
    ... except TypeError: pass
142
    """
143
    return [1,2,3] * x
144

145
@cython.test_assert_path_exists("//MulNode")
146
def multiplied_lists_nonconst_left(x):
147
    """
148
    >>> multiplied_lists_nonconst_left(5) == 5 * [1,2,3]
149
    True
150
    >>> multiplied_lists_nonconst_left(-5) == -5 * [1,2,3]
151
    True
152
    >>> multiplied_lists_nonconst_left(0) == 0 * [1,2,3]
153
    True
154
    """
155
    return x * [1,2,3]
156

157

158
@cython.test_fail_if_path_exists("//MulNode")
159
def multiplied_nonconst_list_const_int(x):
160
    """
161
    >>> multiplied_nonconst_list_const_int(2)
162
    [1, 2, 3, 1, 2, 3]
163
    """
164
    return [1,x,3] * 2
165

166

167
@cython.test_fail_if_path_exists("//MulNode//ListNode")
168
def multiplied_lists_nonconst_expression(x):
169
    """
170
    >>> multiplied_lists_nonconst_expression(5) == [1,2,3] * (5 * 2)
171
    True
172
    >>> multiplied_lists_nonconst_expression(-5) == [1,2,3] * (-5 * 2)
173
    True
174
    >>> multiplied_lists_nonconst_expression(0) == [1,2,3] * (0 * 2)
175
    True
176
    """
177
    return [1,2,3] * (x*2)
178

179
cdef side_effect(int x):
180
    print x
181
    return x
182

183
@cython.test_fail_if_path_exists("//MulNode")
184
def multiplied_lists_with_side_effects():
185
    """
186
    >>> multiplied_lists_with_side_effects() == [1,2,3] * 5
187
    1
188
    2
189
    3
190
    True
191
    """
192
    return [side_effect(1), side_effect(2), side_effect(3)] * 5
193

194
@cython.test_fail_if_path_exists("//MulNode")
195
def multiplied_lists_nonconst_with_side_effects(x):
196
    """
197
    >>> multiplied_lists_nonconst_with_side_effects(5) == [1,2,3] * 5
198
    1
199
    2
200
    3
201
    True
202
    """
203
    return [side_effect(1), side_effect(2), side_effect(3)] * x
204

205
@cython.test_fail_if_path_exists("//MulNode")
206
def multiplied_nonconst_tuple_arg(x):
207
    """
208
    >>> multiplied_nonconst_tuple_arg(5) == (1,2) * 5
209
    True
210
    >>> multiplied_nonconst_tuple_arg(-5) == (1,2) * -5
211
    True
212
    >>> multiplied_nonconst_tuple_arg(0) == (1,2) * 0
213
    True
214

215
    >>> try: (1,2) * 'abc'
216
    ... except TypeError: pass
217
    >>> try: multiplied_nonconst_tuple_arg('abc')
218
    ... except TypeError: pass
219
    >>> try: (1,2) * 1.0
220
    ... except TypeError: pass
221
    >>> try: multiplied_nonconst_tuple_arg(1.0)
222
    ... except TypeError: pass
223
    """
224
    return (1,2) * x
225

226
@cython.test_fail_if_path_exists("//MulNode")
227
def multiplied_nonconst_tuple_int_arg(int x):
228
    """
229
    >>> multiplied_nonconst_tuple_int_arg(5) == (1,2) * 5
230
    True
231
    """
232
    return (1,2) * x
233

234
@cython.test_fail_if_path_exists("//MulNode")
235
def multiplied_nonconst_tuple(x):
236
    """
237
    >>> multiplied_nonconst_tuple(5) == (1,2) * (5+1)
238
    True
239
    """
240
    return (1,2) * (x + 1)
241

242
MULT = 5
243

244
@cython.test_fail_if_path_exists("//MulNode")
245
def multiplied_global_nonconst_tuple():
246
    """
247
    >>> multiplied_global_nonconst_tuple() == (1,2,3) * 5
248
    1
249
    2
250
    3
251
    True
252
    """
253
    return (side_effect(1), side_effect(2), side_effect(3)) * MULT
254

255
@cython.test_fail_if_path_exists("//MulNode")
256
def multiplied_const_tuple():
257
    """
258
    >>> multiplied_const_tuple() == (1,2) * 5
259
    True
260
    """
261
    return (1,2) * 5
262

263
@cython.test_fail_if_path_exists("//MulNode")
264
def multiplied_const_tuple_len1():
265
    """
266
    >>> multiplied_const_tuple_len1() == (1,) * 5
267
    True
268
    """
269
    return (1,) * 5
270

271
@cython.test_fail_if_path_exists("//PrimaryCmpNode")
272
def compile_time_DEF():
273
    """
274
    >>> compile_time_DEF()
275
    (1, False, True, True, False)
276
    """
277
    return INT_VAL, INT_VAL == 0, INT_VAL != 0, INT_VAL == 1, INT_VAL != 1
278

279
@cython.test_fail_if_path_exists("//PrimaryCmpNode")
280
def cascaded_compare():
281
    """
282
    >>> cascaded_compare()
283
    True
284
    """
285
    return 1 < 2 < 3 < 4
286

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.