8000 add tests · python/cpython@74d2275 · GitHub
[go: up one dir, main page]

Skip to content

Commit 74d2275

Browse files
committed
add tests
1 parent 3240d8e commit 74d2275

File tree

1 file changed

+123
-4
lines changed

1 file changed

+123
-4
lines changed

Lib/test/test_peepholer.py

Lines changed: 123 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,6 @@ def test_constant_folding(self):
483483

484484
def test_constant_folding_small_int(self):
485485
tests = [
486-
# subscript
487486
('(0, )[0]', 0),
488487
('(1 + 2, )[0]', 3),
489488
('(2 + 2 * 2, )[0]', 6),
@@ -494,7 +493,18 @@ def test_constant_folding_small_int(self):
494493
('(256 10000 , )[0]', None),
495494
('(1000, )[0]', None),
496495
('(1 - 2, )[0]', None),
496+
('255 + 0', 255),
497497
('255 + 1', None),
498+
('-1', None),
499+
('--1', 1),
500+
('--255', 255),
501+
('--256', None),
502+
('~1', None),
503+
('~~1', 1),
504+
('~~255', 255),
505+
('~~256', None),
506+
('++255', 255),
507+
('++256', None),
498508
]
499509
for expr, oparg in tests:
500510
with self.subTest(expr=expr, oparg=oparg):
@@ -505,6 +515,9 @@ def test_constant_folding_small_int(self):
505515
self.assertNotInBytecode(code, 'LOAD_SMALL_INT')
506516
self.check_lnotab(code)
507517

518+
def test_folding_unaryop(self):
519+
pass
520+
508521
def test_folding_binop(self):
509522
add = get_binop_argval('NB_ADD')
510523
sub = get_binop_argval('NB_SUBTRACT')
@@ -1225,14 +1238,21 @@ def f():
12251238
self.assertEqual(f(), frozenset(range(40)))
12261239

12271240
def test_multiple_foldings(self):
1228-
# (1, (2 + 2 * 2 // 2 - 2, )[0], ) ==> (1, 2)
1241+
# (1, (--2 + ++2 * 2 // 2 - 2, )[0], ~~3, not not True) ==> (1, 2, 3, True)
1242+
intrinsic_positive = 5
12291243
before = [
12301244
('LOAD_SMALL_INT', 1, 0),
12311245
('NOP', None, 0),
12321246
('LOAD_SMALL_INT', 2, 0),
1247+
('UNARY_NEGATIVE', None, 0),
1248+
('NOP', None, 0),
1249+
('UNARY_NEGATIVE', None, 0),
12331250
('NOP', None, 0),
12341251
('NOP', None, 0),
12351252
('LOAD_SMALL_INT', 2, 0),
1253+
('CALL_INTRINSIC_1', intrinsic_positive, 0),
1254+
('NOP', None, 0),
1255+
('CALL_INTRINSIC_1', intrinsic_positive, 0),
12361256
('BINARY_OP', get_binop_argval('NB_MULTIPLY')),
12371257
('LOAD_SMALL_INT', 2, 0),
12381258
('NOP', None, 0),
@@ -1242,18 +1262,34 @@ def test_multiple_foldings(self):
12421262
('BINARY_OP', get_binop_argval('NB_ADD')),
12431263
('NOP', None, 0),
12441264
('LOAD_SMALL_INT', 2, 0),
1265+
('NOP', None, 0),
12451266
('BINARY_OP', get_binop_argval('NB_SUBTRACT')),
1267+
('NOP', None, 0),
12461268
('BUILD_TUPLE', 1, 0),
12471269
('LOAD_SMALL_INT', 0, 0),
12481270
('BINARY_OP', get_binop_argval('NB_SUBSCR'), 0),
1249-
('BUILD_TUPLE', 2, 0),
1271+
('NOP', None, 0),
1272+
('LOAD_SMALL_INT', 3, 0),
1273+
('NOP', None, 0),
1274+
('UNARY_INVERT', None, 0),
1275+
('NOP', None, 0),
1276+
('UNARY_INVERT', None, 0),
1277+
('NOP', None, 0),
1278+
('LOAD_SMALL_INT', 3, 0),
1279+
('NOP', None, 0),
1280+
('UNARY_NOT', None, 0),
1281+
('NOP', None, 0),
1282+
('UNARY_NOT', None, 0),
1283+
('NOP', None, 0),
1284+
('BUILD_TUPLE', 4, 0),
1285+
('NOP', None, 0),
12501286
('RETURN_VALUE', None, 0)
12511287
]
12521288
after = [
12531289
('LOAD_CONST', 1, 0),
12541290
('RETURN_VALUE', None, 0)
12551291
]
1256-
self.cfg_optimization_test(before, after, consts=[], expected_consts=[(2,), (1, 2)])
1292+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[-2, (1, 2, 3, True)])
12571293

12581294
def test_build_empty_tuple(self):
12591295
before = [
@@ -1601,6 +1637,89 @@ def test_optimize_literal_set_contains(self):
16011637
]
16021638
self.cfg_optimization_test(same, same, consts=[None], expected_consts=[None])
16031639

1640+
def test_optimize_unary_not_to_bool(self):
1641+
before = [
1642+
('LOAD_NAME', 0, 0),
1643+
('UNARY_NOT', None, 0),
1644+
('TO_BOOL', None, 0),
1645+
('TO_BOOL', None, 0),
1646+
('TO_BOOL', None, 0),
1647+
('LOAD_CONST', 0, 0),
1648+
('RETURN_VALUE', None, 0),
1649+
]
1650+
after = [
1651+
('LOAD_NAME', 0, 0),
1652+
('UNARY_NOT', None, 0),
1653+
('LOAD_CONST', 0, 0),
1654+
('RETURN_VALUE', None, 0),
1655+
]
1656+
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None])
1657+
1658+
before = [
1659+
('LOAD_SMALL_INT', 1, 0),
1660+
('UNARY_NOT', None, 0),
1661+
('TO_BOOL', None, 0),
1662+
('TO_BOOL', None, 0),
1663+
('TO_BOOL', None, 0),
1664+
('TO_BOOL', None, 0),
1665+
('RETURN_VALUE', None, 0),
1666+
]
1667+
after = [
1668+
('LOAD_CONST', 0, 0),
1669+
('RETURN_VALUE', None, 0),
1670+
]
1671+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[False])
1672+
1673+
is_ = 0
1674+
isnot = 1
1675+
before = [
1676+
('LOAD_NAME', 0, 0),
1677+
('LOAD_NAME', 1, 0),
1678+
('IS_OP', is_, 0),
1679+
('UNARY_NOT', None, 0),
1680+
('TO_BOOL', None, 0),
1681+
('TO_BOOL', None, 0),
1682+
('TO_BOOL', None, 0),
1683+
('LOAD_CONST', 0, 0),
1684+
('RETURN_VALUE', None, 0),
1685+
]
1686+
after = [
1687+
('LOAD_NAME', 0, 0),
1688+
('LOAD_NAME', 1, 0),
1689+
('IS_OP', isnot, 0),
1690+
('LOAD_CONST', 0, 0),
1691+
('RETURN_VALUE', None, 0),
1692+
]
1693+
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None])
1694+
1695+
in_ = 0
1696+
notin = 1
1697+
before = [
1698+
('LOAD_NAME', 0, 0),
1699+
('LOAD_NAME', 1, 0),
1700+
('CONTAINS_OP', in_, 0),
1701+
('UNARY_NOT', None, 0),
1702+
('TO_BOOL', None, 0),
1703+
('TO_BOOL', None, 0),
1704+
('TO_BOOL', None, 0),
1705+
('LOAD_CONST', 0, 0),
1706+
('RETURN_VALUE', None, 0),
1707+
]
1708+
after = [
1709+
('LOAD_NAME', 0, 0),
1710+
('LOAD_NAME', 1, 0),
1711+
('CONTAINS_OP', notin, 0),
1712+
('LOAD_CONST', 0, 0),
1713+
('RETURN_VALUE', None, 0),
1714+
]
1715+
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None])
1716+
1717+
def test_optimize_if_const_unaryop(self):
1718+
pass
1719+
1720+
def test_optimize_if_const_binop(self):
1721+
pass
1722+
16041723
def test_conditional_jump_forward_const_condition(self):
16051724
# The unreachable branch of the jump is removed, the jump
16061725
# becomes redundant and is replaced by a NOP (for the lineno)

0 commit comments

Comments
 (0)
0