Skip to content

Commit a02ac66

Browse files
committed
add unaryop folding tests
1 parent f0f044a commit a02ac66

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

Lib/test/test_peepholer.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,26 @@ def test_constant_folding_small_int(self):
516516
self.check_lnotab(code)
517517

518518
def test_folding_unaryop(self):
519-
pass
519+
intrinsic_positive = 5
520+
tests = [
521+
('---1', 'UNARY_NEGATIVE', None, True),
522+
('---""', 'UNARY_NEGATIVE', None, False),
523+
('~~~1', 'UNARY_INVERT', None, True),
524+
('~~~""', 'UNARY_INVERT', None, False),
525+
('not not True', 'UNARY_NOT', None, True),
526+
('not not x', 'UNARY_NOT', None, False),
527+
('+++1', 'CALL_INTRINSIC_1', intrinsic_positive, True),
528+
('+++x', 'CALL_INTRINSIC_1', intrinsic_positive, False),
529+
]
530+
531+
for expr, opcode, oparg, optimized in tests:
532+
with self.subTest(expr=expr, optimized=optimized):
533+
code = compile(expr, '', 'single')
534+
if optimized:
535+
self.assertNotInBytecode(code, opcode, argval=oparg)
536+
else:
537+
self.assertInBytecode(code, opcode, argval=oparg)
538+
self.check_lnotab(code)
520539

521540
def test_folding_binop(self):
522541
add = get_binop_argval('NB_ADD')

0 commit comments

Comments
 (0)