blob: 2af1ee35bff0abb10febe9c0fe68ecd09a1779d3 [file] [log] [blame]
Larry Hastings3a907972013-11-23 14:49:22 -08001import dis
Zachary Ware38c707e2015-04-13 15:00:43 -05002from test.support import import_module
Larry Hastings3a907972013-11-23 14:49:22 -08003import unittest
4
Larry Hastingsc8635b42013-11-23 16:11:17 -08005_opcode = import_module("_opcode")
6
Larry Hastings3a907972013-11-23 14:49:22 -08007class OpcodeTests(unittest.TestCase):
8
9 def test_stack_effect(self):
10 self.assertEqual(_opcode.stack_effect(dis.opmap['POP_TOP']), -1)
11 self.assertEqual(_opcode.stack_effect(dis.opmap['DUP_TOP_TWO']), 2)
12 self.assertEqual(_opcode.stack_effect(dis.opmap['BUILD_SLICE'], 0), -1)
13 self.assertEqual(_opcode.stack_effect(dis.opmap['BUILD_SLICE'], 1), -1)
14 self.assertEqual(_opcode.stack_effect(dis.opmap['BUILD_SLICE'], 3), -2)
15 self.assertRaises(ValueError, _opcode.stack_effect, 30000)
16 self.assertRaises(ValueError, _opcode.stack_effect, dis.opmap['BUILD_SLICE'])
17 self.assertRaises(ValueError, _opcode.stack_effect, dis.opmap['POP_TOP'], 0)
Serhiy Storchaka57faf342018-04-25 22:04:06 +030018 # All defined opcodes
19 for name, code in dis.opmap.items():
20 with self.subTest(opname=name):
21 if code < dis.HAVE_ARGUMENT:
22 _opcode.stack_effect(code)
23 self.assertRaises(ValueError, _opcode.stack_effect, code, 0)
24 else:
25 _opcode.stack_effect(code, 0)
26 self.assertRaises(ValueError, _opcode.stack_effect, code)
27 # All not defined opcodes
28 for code in set(range(256)) - set(dis.opmap.values()):
29 with self.subTest(opcode=code):
30 self.assertRaises(ValueError, _opcode.stack_effect, code)
31 self.assertRaises(ValueError, _opcode.stack_effect, code, 0)
32
Larry Hastings3a907972013-11-23 14:49:22 -080033
Larry Hastings3a907972013-11-23 14:49:22 -080034if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -050035 unittest.main()