def test_operators1(self): val1 = PyVMValue.pyint(10) val2 = PyVMValue.pyint(20) result = (val1 + val2) self.assertPyVMInt(result, 30) result = (val1 - val2) self.assertPyVMInt(result, -10) result = (val1 * val2) self.assertPyVMInt(result, 200) result = (val1 / val2) self.assertPyVMFloat(result, 0.5) result = (val1 // val2) self.assertPyVMInt(result, 0) result = (val2 % val1) self.assertPyVMInt(result, 0) result = (val1 % val2) self.assertPyVMInt(result, 10) result = val1 >> 1 self.assertPyVMInt(result, 5) result = val1 << 1 self.assertPyVMInt(result, 20)
def test_inc2(self): method = self.pyklass.get_method("inc2:(D)D") self.assertEqual(method.signature, "(D)D") self.assertEqual(method.num_params, 1) result = self.intrptr.execute( method, IntrptVars([ PyVMValue.pyint(0), PyVMValue.pyint(5), ])) self.assertPyVMDouble(result, 6)
def test_imul(self): stack = IntrptEvalStack() stack.push(PyVMValue.pyint(10)) stack.push(PyVMValue.pyint(20)) self.assertEqual(stack.size(), 2) stack.imul() self.assertEqual(stack.size(), 1) val = stack.pop() self.assertPyVMInt(val, 200) self.assertEqual(stack.size(), 0)
def test_dneg(self): stack = IntrptEvalStack() stack.push(PyVMValue.pydouble(10)) stack.push(PyVMValue.pydouble(20)) self.assertEqual(stack.size(), 2) stack.dneg() self.assertEqual(stack.size(), 2) val = stack.pop() self.assertPyVMDouble(val, -20) self.assertEqual(stack.size(), 1)
def test_ior(self): stack = IntrptEvalStack() stack.push(PyVMValue.pyint(6)) stack.push(PyVMValue.pyint(8)) self.assertEqual(stack.size(), 2) stack.ior() self.assertEqual(stack.size(), 1) val = stack.pop() self.assertPyVMInt(val, 14) self.assertEqual(stack.size(), 0)
def test_div(self): method = self.pyklass.get_method("div:(II)I") self.assertEqual(method.signature, '(II)I') self.assertEqual(method.num_params, 2) # pass function body vars. result = self.intrptr.execute( method, IntrptVars([ PyVMValue.pyint(20), PyVMValue.pyint(5), ])) self.assertPyVMInt(result, 4)
def test_div(self): method = self.pyklass.get_method("div:(DD)D") self.assertEqual(method.signature, "(DD)D") self.assertEqual(method.num_params, 2) result = self.intrptr.execute( method, IntrptVars([ PyVMValue.pyint(0), PyVMValue.pyint(3), PyVMValue.pyint(0), PyVMValue.pyint(2), ])) self.assertPyVMDouble(result, 1)
def test_push_pop(self): stack = IntrptEvalStack() self.assertEqual(stack.size(), 0) val = PyVMValue.pyint(10) stack.push(val) self.assertEqual(stack.size(), 1) val1 = stack.pop() self.assertEqual(val, val1) self.assertEqual(stack.size(), 0)
def test_two(self): method = self.pyklass.get_method("two:(II)I") self.assertEqual(method.signature, '(II)I') self.assertEqual(method.num_params, 2) # pass function body vars. result = self.intrptr.execute( method, IntrptVars(args=[ PyVMValue.pyint(1), PyVMValue.pyint(3), ])) self.assertPyVMInt(result, 4) # pass other vars. result = self.intrptr.execute( method, IntrptVars(args=[ PyVMValue.pyint(100), PyVMValue.pyint(300), ])) self.assertPyVMInt(result, 400)
def test_drem(self): stack = IntrptEvalStack() stack.push(PyVMValue.pydouble(5)) stack.push(PyVMValue.pydouble(3)) self.assertEqual(stack.size(), 2) stack.drem() self.assertEqual(stack.size(), 1) val = stack.pop() self.assertPyVMDouble(val, 2) self.assertEqual(stack.size(), 0) stack.push(PyVMValue.pydouble(3)) stack.push(PyVMValue.pydouble(5)) self.assertEqual(stack.size(), 2) stack.drem() self.assertEqual(stack.size(), 1) val = stack.pop() self.assertPyVMDouble(val, 3) self.assertEqual(stack.size(), 0)
def test_irem(self): stack = IntrptEvalStack() stack.push(PyVMValue.pyint(5)) stack.push(PyVMValue.pyint(3)) self.assertEqual(stack.size(), 2) stack.irem() self.assertEqual(stack.size(), 1) val = stack.pop() self.assertPyVMInt(val, 2) self.assertEqual(stack.size(), 0) stack.push(PyVMValue.pyint(3)) stack.push(PyVMValue.pyint(5)) self.assertEqual(stack.size(), 2) stack.irem() self.assertEqual(stack.size(), 1) val = stack.pop() self.assertPyVMInt(val, 3) self.assertEqual(stack.size(), 0)
def assertPyVMDouble(self, actual: PyVMValue, expected: float) -> None: self.assertValue(actual, PyVMValue.pydouble(expected))
def iinc(self, offset, amount): var = self.args[offset & 0xff] self.args[offset & 0xff] = PyVMValue.pyint(var.value + amount)
def dload(self, index: int): p1: PyVMValue = self.args[index].clone() p2: PyVMValue = self.args[index + 1].clone() return PyVMValue.pydouble((p1.value << 8) + p2.value)
def assertPyVMInt(self, actual: PyVMValue, expected: int) -> None: self.assertValue(actual, PyVMValue.pyint(expected))
def assertPyVMFloat(self, actual: PyVMValue, expected: float) -> None: self.assertValue(actual, PyVMValue.pyfloat(expected))
def assertPyVMLong(self, actual: PyVMValue, expected: int) -> None: self.assertValue(actual, PyVMValue.pylong(expected))