8000 add test_optimize_unary_not · python/cpython@ef9221a · GitHub
[go: up one dir, main page]

Skip to content

Commit ef9221a

Browse files
committed
add test_optimize_unary_not
1 parent c1a1be5 commit ef9221a

File tree

1 file changed

+158
-15
lines changed

1 file changed

+158
-15
lines changed

Lib/test/test_peepholer.py

Lines changed: 158 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,31 +1637,57 @@ def test_optimize_literal_set_contains(self):
16371637
]
16381638
self.cfg_optimization_test(same, same, consts=[None], expected_consts=[None])
16391639

1640-
def test_optimize_unary_not_to_bool(self):
1640+
def test_optimize_unary_not(self):
1641+
# test folding
1642+
before = [
1643+
('LOAD_SMALL_INT', 1, 0),
1644+
('UNARY_NOT', None, 0),
1645+
('RETURN_VALUE', None, 0),
1646+
]
1647+
after = [
1648+
('LOAD_CONST', 0, 0),
1649+
('RETURN_VALUE', None, 0),
1650+
]
1651+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[False])
1652+
1653+
# test cancel out
1654+
before = [
1655+
('LOAD_SMALL_INT', 1, 0),
1656+
('UNARY_NOT', None, 0),
1657+
('UNARY_NOT', None, 0),
1658+
('UNARY_NOT', None, 0),
1659+
('UNARY_NOT', None, 0),
1660+
('RETURN_VALUE', None, 0),
1661+
]
1662+
after = [
1663+
('LOAD_SMALL_INT', 1, 0),
1664+
('RETURN_VALUE', None, 0),
1665+
]
1666+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1667+
1668+
# test eliminate to bool
16411669
before = [
16421670
('LOAD_NAME', 0, 0),
16431671
('UNARY_NOT', None, 0),
16441672
('TO_BOOL', None, 0),
16451673
('TO_BOOL', None, 0),
16461674
('TO_BOOL', None, 0),
1647-
('LOAD_CONST', 0, 0),
16481675
('RETURN_VALUE', None, 0),
16491676
]
16501677
after = [
16511678
('LOAD_NAME', 0, 0),
16521679
('UNARY_NOT', None, 0),
1653-
('LOAD_CONST', 0, 0),
16541680
('RETURN_VALUE', None, 0),
16551681
]
1656-
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None])
1682+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
16571683

1684+
# test folding & elimitnate to bool
16581685
before = [
16591686
('LOAD_SMALL_INT', 1, 0),
16601687
('UNARY_NOT', None, 0),
16611688
('TO_BOOL', None, 0),
16621689
('TO_BOOL', None, 0),
16631690
('TO_BOOL', None, 0),
1664-
('TO_BOOL', None, 0),
16651691
('RETURN_VALUE', None, 0),
16661692
]
16671693
after = [
@@ -1670,8 +1696,43 @@ def test_optimize_unary_not_to_bool(self):
16701696
]
16711697
self.cfg_optimization_test(before, after, consts=[], expected_consts=[False])
16721698

1673-
is_ = 0
1674-
isnot = 1
1699+
# test cancel out & eliminate to bool (to bool stays)
1700+
before = [
1701+
('LOAD_SMALL_INT', 1, 0),
1702+
('UNARY_NOT', None, 0),
1703+
('UNARY_NOT', None, 0),
1704+
('UNARY_NOT', None, 0),
1705+
('UNARY_NOT', None, 0),
1706+
('TO_BOOL', None, 0),
1707+
('RETURN_VALUE', None, 0),
1708+
]
1709+
after = [
1710+
('LOAD_SMALL_INT', 1, 0),
1711+
('TO_BOOL', None, 0),
1712+
('RETURN_VALUE', None, 0),
1713+
]
1714+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1715+
1716+
is_ = in_ = 0
1717+
isnot = notin = 1
1718+
1719+
# test is/isnot
1720+
before = [
1721+
('LOAD_NAME', 0, 0),
1722+
('LOAD_NAME', 1, 0),
1723+
('IS_OP', is_, 0),
1724+
('UNARY_NOT', None, 0),
1725+
('RETURN_VALUE', None, 0),
1726+
]
1727+
after = [
1728+
('LOAD_NAME', 0, 0),
1729+
('LOAD_NAME', 1, 0),
1730+
('IS_OP', isnot, 0),
1731+
('RETURN_VALUE', None, 0),
1732+
]
1733+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1734+
1735+
# test is/isnot eliminate to bool
16751736
before = [
16761737
('LOAD_NAME', 0, 0),
16771738
('LOAD_NAME', 1, 0),
@@ -1680,20 +1741,85 @@ def test_optimize_unary_not_to_bool(self):
16801741
('TO_BOOL', None, 0),
16811742
('TO_BOOL', None, 0),
16821743
('TO_BOOL', None, 0),
1683-
('LOAD_CONST', 0, 0),
16841744
('RETURN_VALUE', None, 0),
16851745
]
16861746
after = [
16871747
('LOAD_NAME', 0, 0),
16881748
('LOAD_NAME', 1, 0),
16891749
('IS_OP', isnot, 0),
1690-
('LOAD_CONST', 0, 0),
16911750
('RETURN_VALUE', None, 0),
16921751
]
1693-
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None])
1752+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
16941753

1695-
in_ = 0
1696-
notin = 1
1754+
# test in/notin
1755+
before = [
1756+
('LOAD_NAME', 0, 0),
1757+
('LOAD_NAME', 1, 0),
1758+
('CONTAINS_OP', in_, 0),
1759+
('UNARY_NOT', None, 0),
1760+
('RETURN_VALUE', None, 0),
1761+
]
1762+
after = [
1763+
('LOAD_NAME', 0, 0),
1764+
('LOAD_NAME', 1, 0),
1765+
('CONTAINS_OP', notin, 0),
1766+
('RETURN_VALUE', None, 0),
1767+
]
1768+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1769+
1770+
# test in/notin cancel out
1771+
before = [
1772+
('LOAD_NAME', 0, 0),
1773+
('LOAD_NAME', 1, 0),
1774+
('CONTAINS_OP', in_, 0),
1775+
('UNARY_NOT', None, 0),
1776+
('UNARY_NOT', None, 0),
1777+
('RETURN_VALUE', None, 0),
1778+
]
1779+
after = [
1780+
('LOAD_NAME', 0, 0),
1781+
('LOAD_NAME', 1, 0),
1782+
('CONTAINS_OP', in_, 0),
1783+
('RETURN_VALUE', None, 0),
1784+
]
1785+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1786+
1787+
# test in/notin
1788+
before = [
1789+
('LOAD_NAME', 0, 0),
1790+
('LOAD_NAME', 1, 0),
1791+
('CONTAINS_OP', in_, 0),
1792+
('UNARY_NOT', None, 0),
1793+
('RETURN_VALUE', None, 0),
1794+
]
1795+
after = [
1796+
('LOAD_NAME', 0, 0),
1797+
('LOAD_NAME', 1, 0),
1798+
('CONTAINS_OP', notin, 0),
1799+
('RETURN_VALUE', None, 0),
1800+
]
1801+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1802+
1803+
# test in/notin cancel out eliminate to bool (to bool stays) (maybe optimize later ???)
1804+
before = [
1805+
('LOAD_NAME', 0, 0),
1806+
('LOAD_NAME', 1, 0),
1807+
('CONTAINS_OP', in_, 0),
1808+
('UNARY_NOT', None, 0),
1809+
('UNARY_NOT', None, 0),
1810+
('TO_BOOL', None, 0),
1811+
('RETURN_VALUE', None, 0),
1812+
]
1813+
after = [
1814+
('LOAD_NAME', 0, 0),
1815+
('LOAD_NAME', 1, 0),
1816+
('CONTAINS_OP', in_, 0),
1817+
('TO_BOOL', None, 0),
1818+
('RETURN_VALUE', None, 0),
1819+
]
1820+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1821+
1822+
# test in/notin eliminate to bool
16971823
before = [
16981824
('LOAD_NAME', 0, 0),
16991825
('LOAD_NAME', 1, 0),
@@ -1702,17 +1828,34 @@ def test_optimize_unary_not_to_bool(self):
17021828
('TO_BOOL', None, 0),
17031829
('TO_BOOL', None, 0),
17041830
('TO_BOOL', None, 0),
1705-
('LOAD_CONST', 0, 0),
17061831
('RETURN_VALUE', None, 0),
17071832
]
17081833
after = [
17091834
('LOAD_NAME', 0, 0),
17101835
('LOAD_NAME', 1, 0),
17111836
('CONTAINS_OP', notin, 0),
1712-
('LOAD_CONST', 0, 0),
17131837
('RETURN_VALUE', None, 0),
17141838
]
1715-
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None])
1839+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1840+
1841+
# test in/notin cancel out eliminate to bool (to bool stays) (maybe optimize later ???)
1842+
before = [
1843+
('LOAD_NAME', 0, 0),
1844+
('LOAD_NAME', 1, 0),
1845+
('CONTAINS_OP', in_, 0),
1846+
('UNARY_NOT', None, 0),
1847+
('UNARY_NOT', None, 0),
1848+
('TO_BOOL', None, 0),
1849+
('RETURN_VALUE', None, 0),
1850+
]
1851+
after = [
1852+
('LOAD_NAME', 0, 0),
1853+
('LOAD_NAME', 1, 0),
1854+
('CONTAINS_OP', in_, 0),
1855+
('TO_BOOL', None, 0),
1856+
('RETURN_VALUE', None, 0),
1857+
]
1858+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
17161859

17171860
def test_optimize_if_const_unaryop(self):
17181861
pass

0 commit comments

Comments
 (0)
0