cython

Форк
0
/
constant_folding.py 
507 строк · 10.9 Кб
1
# coding=utf8
2
# mode: run
3
# tag: constant_folding
4

5
import cython
6

7

8
@cython.test_fail_if_path_exists(
9
    "//UnaryMinusNode",
10
    "//UnaryPlusNode",
11
)
12
def unop_floats():
13
    """
14
    >>> unop_floats()
15
    (False, 2.0, -2.0, False, 2.0, -2.0, -2.0)
16
    """
17
    not1   = not 2.0
18
    plus1  = + 2.0
19
    minus1 = - 2.0
20
    not3   = not not not 2.0
21
    plus3  = +++ 2.0
22
    minus3 = --- 2.0
23
    mix    = +-++-- 2.0
24
    return not1, plus1, minus1, not3, plus3, minus3, mix
25

26

27
@cython.test_fail_if_path_exists(
28
    "//UnaryMinusNode",
29
    "//UnaryPlusNode",
30
    "//CoerceToPyTypeNode",
31
)
32
def unop_py_floats_tuple():
33
    """
34
    >>> unop_floats()
35
    (False, 2.0, -2.0, False, 2.0, -2.0, -2.0)
36
    """
37
    return (
38
        not 2.0,
39
        + 2.0,
40
        - 2.0,
41
        not not not 2.0,
42
        +++ 2.0,
43
        --- 2.0,
44
        +-++-- 2.0)
45

46

47
@cython.test_fail_if_path_exists(
48
    "//UnaryMinusNode",
49
    "//UnaryPlusNode",
50
)
51
def unop_ints():
52
    """
53
    >>> unop_ints()
54
    (False, 2, -2, False, 2, -2, -2)
55
    """
56
    not1   = not 2
57
    plus1  = + 2
58
    minus1 = - 2
59
    not3   = not not not 2
60
    plus3  = +++ 2
61
    minus3 = --- 2
62
    mix    = +-++-- 2
63
    return not1, plus1, minus1, not3, plus3, minus3, mix
64

65

66
@cython.test_fail_if_path_exists(
67
    "//UnaryMinusNode",
68
    "//UnaryPlusNode",
69
    "//NotNode",
70
)
71
def unop_bool():
72
    """
73
    >>> unop_bool()
74
    (False, 1, -1, False, 1, -1, -1)
75
    """
76
    not1   = not True
77
    plus1  = + True
78
    minus1 = - True
79
    not3   = not not not True
80
    plus3  = +++ True
81
    minus3 = --- True
82
    mix    = +-++-- True
83
    return not1, plus1, minus1, not3, plus3, minus3, mix
84

85

86
@cython.test_fail_if_path_exists(
87
    "//AddNode",
88
    "//SubNode",
89
)
90
def binop_bool():
91
    """
92
    >>> binop_bool()
93
    (2, 1, 0, True, True, 1, False, 2, 2, -2, False, True, 1, False)
94
    """
95
    plus1  = True + True
96
    pmix1  = True + 0
97
    minus1 = True - True
98
    and1   = True & True
99
    or1    = True | True
100
    ormix1 = True | 0
101
    xor1   = True ^ True
102
    plus3  = False + True + False + True
103
    pmix3  = False + True + 0 + True
104
    minus3 = False - True - False - True
105
    and3   = False & True & False & True
106
    or3    = False | True | False | True
107
    ormix3 = False | 0 | False | True
108
    xor3   = False ^ True ^ False ^ True
109
    return plus1, pmix1, minus1, and1, or1, ormix1, xor1, plus3, pmix3, minus3, and3, or3, ormix3, xor3
110

111

112
@cython.test_fail_if_path_exists(
113
    "//MulNode",
114
    "//PowNode",
115
)
116
def binop_mul_pow():
117
    """
118
    >>> binop_mul_pow()
119
    (800, 12193263111263526900, 248832, 12467572902176589255564000298710470656)
120
    """
121
    mul_int = 20 * 40
122
    mul_large_int = 1234567890 * 9876543210
123
    pow_int = 12 ** 5
124
    pow_large_int = 1234 ** 12
125
    return (mul_int, mul_large_int, pow_int, pow_large_int)
126

127

128
def binop_pow_negative():
129
    """
130
    >>> binop_pow_negative()
131
    (4.018775720164609e-06, 8.020807320287816e-38, 0.1)
132
    """
133
    pow_int = 12 ** -5
134
    pow_large_int = 1234 ** -12
135
    pow_expression_int = 10 ** (1-2)
136
    return (pow_int, pow_large_int, pow_expression_int)
137

138

139
@cython.test_fail_if_path_exists(
140
    "//SliceIndexNode",
141
)
142
def slicing2():
143
    """
144
    >>> slicing2()
145
    ([1, 2, 3, 4], [3, 4], [1, 2, 3, 4], [3, 4], (1, 2, 3, 4), (3, 4), (1, 2, 3, 4), (3, 4))
146
    """
147
    lst0 = [1, 2, 3, 4][:]
148
    lst1 = [1, 2, 3, 4][2:]
149
    lst2 = [1, 2, 3, 4][:4]
150
    lst3 = [1, 2, 3, 4][2:4]
151

152
    tpl0 = (1, 2, 3, 4)[:]
153
    tpl1 = (1, 2, 3, 4)[2:]
154
    tpl2 = (1, 2, 3, 4)[:4]
155
    tpl3 = (1, 2, 3, 4)[2:4]
156

157
    return lst0, lst1, lst2, lst3, tpl0, tpl1, tpl2, tpl3
158

159

160
@cython.test_fail_if_path_exists(
161
    "//SliceIndexNode",
162
)
163
def str_slicing2():
164
    """
165
    >>> a,b,c,d = str_slicing2()
166
    >>> a == 'abc\\xE9def'[:]
167
    True
168
    >>> b == 'abc\\xE9def'[2:]
169
    True
170
    >>> c == 'abc\\xE9def'[:4]
171
    True
172
    >>> d == 'abc\\xE9def'[2:4]
173
    True
174
    """
175
    str0 = 'abc\xE9def'[:]
176
    str1 = 'abc\xE9def'[2:]
177
    str2 = 'abc\xE9def'[:4]
178
    str3 = 'abc\xE9def'[2:4]
179

180
    return str0, str1, str2, str3
181

182

183
@cython.test_fail_if_path_exists(
184
    "//IfStatNode",
185
)
186
def str_in_and_not_in():
187
    """
188
    >>> str_in_and_not_in()
189
    True
190
    """
191
    if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': return True
192
    else: return False
193

194

195
@cython.test_fail_if_path_exists(
196
    "//WhileStatNode",
197
)
198
def while_false():
199
    """
200
    >>> while_false()
201
    """
202
    while 1 == 0:
203
        return False
204

205

206
@cython.test_fail_if_path_exists(
207
    "//WhileStatNode",
208
    )
209
def while_false_else():
210
    """
211
    >>> while_false_else()
212
    True
213
    """
214
    while 1 == 0:
215
        return False
216
    else:
217
        return True
218

219

220
@cython.test_fail_if_path_exists(
221
    "//WhileStatNode//PrintStatNode",
222
    "//WhileStatNode//PrimaryCmpNode",
223
    "//WhileStatNode/BoolNode",
224
    "//WhileStatNode/IntNode",
225
)
226
@cython.test_assert_path_exists(
227
    "//WhileStatNode",
228
)
229
def while_true():
230
    """
231
    >>> while_true()
232
    True
233
    """
234
    while 1 == 1:
235
        return True
236
    else:
237
        print("FAIL")
238

239

240
@cython.test_fail_if_path_exists(
241
    "//ForInStatNode",
242
)
243
def for_in_empty():
244
    """
245
    >>> for_in_empty()
246
    """
247
    for i in []:
248
        print("LOOP")
249

250

251
@cython.test_fail_if_path_exists(
252
    "//ForInStatNode",
253
)
254
def for_in_empty_else():
255
    """
256
    >>> for_in_empty_else()
257
    True
258
    """
259
    for i in []:
260
        print("LOOP")
261
    else:
262
        return True
263

264

265
@cython.test_fail_if_path_exists(
266
    "//ComprehensionNode",
267
    "//ForInStatNode",
268
)
269
@cython.test_assert_path_exists(
270
    "//ListNode",
271
)
272
def for_in_empty_listcomp():
273
    """
274
    >>> for_in_empty_listcomp()
275
    []
276
    """
277
    return [i for i in []]
278

279

280
@cython.test_fail_if_path_exists(
281
    "//ComprehensionNode",
282
    "//ForInStatNode",
283
)
284
@cython.test_assert_path_exists(
285
    "//ListNode",
286
)
287
def for_in_empty_nested_listcomp():
288
    """
289
    >>> for_in_empty_nested_listcomp()
290
    []
291
    """
292
    return [x for _ in [] for x in [1, 2, 3]]
293

294

295
@cython.test_fail_if_path_exists(
296
    "//ForInStatNode//ForInStatNode",
297
)
298
@cython.test_assert_path_exists(
299
    "//ForInStatNode",
300
    "//ComprehensionNode",
301
)
302
def for_in_nested_listcomp():
303
    """
304
    >>> for_in_nested_listcomp()
305
    []
306
    """
307
    return [x for x in [1, 2, 3] for _ in []]
308

309

310
@cython.test_fail_if_path_exists(
311
    "//MulNode",
312
)
313
def mult_empty_list():
314
    """
315
    >>> mult_empty_list()
316
    []
317
    """
318
    return 5 * [] * 100
319

320

321
@cython.test_fail_if_path_exists(
322
    "//MulNode",
323
)
324
def mult_list_int_int():
325
    """
326
    >>> mult_list_int_int()
327
    [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
328
    """
329
    return [1, 2] * 2 * 3
330

331

332
@cython.test_fail_if_path_exists(
333
    "//MulNode",
334
)
335
def mult_int_list_int():
336
    """
337
    >>> mult_int_list_int()
338
    [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
339
    """
340
    return 3 * [1, 2] * 2
341

342

343
@cython.test_fail_if_path_exists(
344
    "//MulNode",
345
    "//ListNode//IntNode",
346
)
347
def neg_mult_list():
348
    """
349
    >>> neg_mult_list()
350
    []
351
    """
352
    return -5 * [1, 2] * -100
353

354

355
@cython.test_fail_if_path_exists(
356
    "//MulNode",
357
    "//ListNode//IntNode",
358
)
359
def zero_mult_list():
360
    """
361
    >>> zero_mult_list()
362
    []
363
    """
364
    return 0 * [1, 2] * 0
365

366

367
@cython.test_assert_path_exists(
368
    "//BoolNode",
369
)
370
@cython.test_fail_if_path_exists(
371
    "//PrimaryCmpNode",
372
    "//MulNode",
373
    "//ListNode//IntNode",
374
)
375
def in_mult_list():
376
    """
377
    >>> in_mult_list()
378
    False
379
    """
380
    return 5 in 100 * [1, 2] * 0
381

382

383
@cython.test_assert_path_exists(
384
    "//BoolNode",
385
)
386
@cython.test_fail_if_path_exists(
387
    "//PrimaryCmpNode",
388
    "//MulNode",
389
    "//ListNode//IntNode",
390
)
391
def not_in_mult_list():
392
    """
393
    >>> not_in_mult_list()
394
    True
395
    """
396
    return 5 not in 100 * [1, 2] * 0
397

398

399
@cython.test_assert_path_exists(
400
    "//BoolNode",
401
)
402
@cython.test_fail_if_path_exists(
403
    "//PrimaryCmpNode",
404
    "//MulNode",
405
    "//ListNode//IntNode",
406
)
407
def combined():
408
    """
409
    >>> combined()
410
    True
411
    """
412
    return 5 in 100 * [1, 2] * 0  or  5 not in 100 * [] * 10
413

414

415
@cython.test_assert_path_exists(
416
    '//IntNode[@base_10_value = "2"]',
417
    '//IntNode[@base_10_value = "4"]',
418
    '//IntNode[@base_10_value = "5"]',
419
    '//IntNode[@base_10_value = "7"]',
420
    '//BoolBinopNode//PrimaryCmpNode',
421
    '//BoolBinopNode[.//PrimaryCmpNode//IntNode[@base_10_value = "4"] and .//PrimaryCmpNode//IntNode[@base_10_value = "5"]]',
422
    '//PrimaryCmpNode[.//IntNode[@base_10_value = "2"] and .//IntNode[@base_10_value = "4"]]',
423
    '//PrimaryCmpNode[.//IntNode[@base_10_value = "5"] and .//IntNode[@base_10_value = "7"]]',
424
)
425
@cython.test_fail_if_path_exists(
426
    '//IntNode[@base_10_value = "1"]',
427
    '//IntNode[@base_10_value = "8"]',
428
    '//PrimaryCmpNode[.//IntNode[@base_10_value = "4"] and .//IntNode[@base_10_value = "5"]]',
429
    '//PrimaryCmpNode[.//IntNode[@base_10_value = "2"] and .//IntNode[@base_10_value = "7"]]',
430
    '//BoolNode',
431
)
432
def cascaded_cmp_with_partial_constants(a, b):
433
    """
434
    >>> cascaded_cmp_with_partial_constants(3, 6)
435
    True
436
    >>> cascaded_cmp_with_partial_constants(1, 6)
437
    False
438
    >>> cascaded_cmp_with_partial_constants(4, 6)
439
    False
440
    >>> cascaded_cmp_with_partial_constants(3, 7)
441
    False
442
    >>> cascaded_cmp_with_partial_constants(3, 6)
443
    True
444
    """
445
    return 1 < 2 < a < 4 < 5 < b < 7 < 8
446

447

448
@cython.test_assert_path_exists(
449
    '//IntNode[@base_10_value = "2"]',
450
    '//IntNode[@base_10_value = "4"]',
451
    '//IntNode[@base_10_value = "5"]',
452
    '//IntNode[@base_10_value = "7"]',
453
    '//BoolBinopNode',
454
    '//SingleAssignmentNode//BoolBinopNode',
455
    '//SingleAssignmentNode//BoolBinopNode//NameNode[@name = "a"]',
456
    '//SingleAssignmentNode//BoolBinopNode//NameNode[@name = "b"]',
457
    '//BoolBinopNode[.//PrimaryCmpNode//IntNode[@base_10_value = "4"] and .//PrimaryCmpNode//IntNode[@base_10_value = "5"]]',
458
    '//BoolNode[@value = False]',
459
)
460
@cython.test_fail_if_path_exists(
461
    '//SingleAssignmentNode//NameNode[@name = "c"]',
462
    '//IntNode[@base_10_value = "1"]',
463
    '//PrimaryCmpNode[.//IntNode[@base_10_value = "4"] and .//IntNode[@base_10_value = "5"]]',
464
    '//PrimaryCmpNode[.//IntNode[@base_10_value = "2"] and .//IntNode[@base_10_value = "7"]]',
465
    '//BoolNode[@value = True]',
466
)
467
def cascaded_cmp_with_partial_constants_and_false_end(a, b, c):
468
    """
469
    >>> cascaded_cmp_with_partial_constants_and_false_end(3, 6, 8)
470
    False
471
    >>> cascaded_cmp_with_partial_constants_and_false_end(1, 6, 8)
472
    False
473
    >>> cascaded_cmp_with_partial_constants_and_false_end(4, 6, 8)
474
    False
475
    >>> cascaded_cmp_with_partial_constants_and_false_end(3, 7, 8)
476
    False
477
    """
478
    x = 1 < 2 < a < 4 < 5 < b < 7 < 7 < c
479
    return x
480

481

482
@cython.test_assert_path_exists(
483
    '//PrimaryCmpNode',
484
    '//PrimaryCmpNode//IntNode',
485
    '//PrimaryCmpNode//IntNode[@base_10_value = "0"]',
486
    '//PrimaryCmpNode//IntNode[@base_10_value = "4294967296"]',
487
)
488
@cython.test_fail_if_path_exists(
489
    '//PrimaryCmpNode//IntBinopNode',
490
    '//PrimaryCmpNode//IntNode[@base_10_value = "1"]',
491
    '//PrimaryCmpNode//IntNode[@base_10_value = "32"]',
492
)
493
def const_in_binop(v):
494
    """
495
    >>> const_in_binop(-1)
496
    1
497
    >>> const_in_binop(0)
498
    0
499
    >>> const_in_binop(1 << 32)
500
    1
501
    >>> const_in_binop(1 << 32 - 1)
502
    0
503
    """
504
    if v < 0 or v >= (1 << 32):
505
        return 1
506
    else:
507
        return 0
508

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

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

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

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