import unittest import inspect from gte.turmit import Turmit from gte.rpnlib import _op_list class TurmitTestCase(unittest.TestCase): def test_init(self): ''' Test Turmit class __init__ ''' t = Turmit(42) self.assertEqual(len(t._stack), 42) self.assertEqual(t.shead, 0) self.assertEqual(len(t._stack) - 1 , t._cur) def test_init_args(self): ''' Test Turmit __init__ arguments ''' t = Turmit() t = Turmit(prog_size = 42) t = Turmit(prog = 'G R B 44 MOD ADD 43 MOD ADD 42 MOD') t = Turmit(prog_size = 10) self.assertEqual(len(t._prog), 10) t = Turmit(max_int = 42) self.assertEqual(t._max_int, 42) t = Turmit(signed_int = False) self.assertFalse(t._signed_int) t = Turmit(prog = 'G R B ADD ADD 42 MOD', max_int = 0xFFFF, signed_int = False, stack_size=42) def test_call(self): ''' Test Turmit.__call__ ''' t = Turmit(prog = 'G R B ADD ADD 42 MOD', max_int = 0xFFFF, signed_int = False, stack_size=42) r = t(x=1, y=2, r=255, g=255, b=255) self.assertEqual(r, 9) r = t(x=2, y=2, r=255, g=0, b=0) self.assertEqual(r, 3) def test_init_badargs(self): ''' Test Turmit __init__ bad arguments ''' with self.assertRaises(RuntimeError): t = Turmit(foobar=42) with self.assertRaises(ValueError): t = Turmit(-1) with self.assertRaises(ValueError): t = Turmit(max_int = -1) with self.assertRaises(ValueError): t = Turmit(prog='foobar') class TurmitOperationTestCase(unittest.TestCase): def test_push(self): ''' Test Turmit _push() method ''' t = Turmit() t._push(1) self.assertEqual(t.shead, 1) t._push(2) self.assertEqual(t.shead, 2) def _rpn(self, rpn, narg = 2): if rpn not in _op_list: raise unittest.SkipTest('"%s" not implemented' % rpn) method, wrapped = _op_list[rpn] if narg != len(inspect.signature(method).parameters) - 1: self.fail('Expected %s to take %d arguments, but Turmit.%s() takes \ %d arguments' % (rpn, narg, rpn, len(inspect.signature(method).parameters) - 1)) def test_add(self): ''' Test turmit add method ''' self._rpn('add') t = Turmit() t._push(42) t._push(8) t.add() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 50) t._push(42) t._push(8) t.add() self.assertEqual(t._cur, 1) self.assertEqual(t.shead, 50) t.add() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 100) def test_mem_sz(self): ''' Test turmit mem_sz() operation ''' self._rpn('mem_sz', 1) t = Turmit(8) self.assertEqual(len(t._stack), 8) t._push(42) t.mem_sz() self.assertEqual(len(t._stack), 42) self.assertEqual(t._cur, 7) t._push(42) t._push(8) t.mem_sz() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 42) def test_sub(self): ''' Test turmit sub() method ''' self._rpn('sub') t = Turmit() t._push(50) t._push(8) t.sub() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 42) t._push(50) t._push(8) t.sub() self.assertEqual(t._cur, 1) self.assertEqual(t.shead, 42) t.sub() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 0) def test_mul(self): ''' Test turmit mul() method ''' self._rpn('mul') t = Turmit() t._push(7) t._push(6) t.mul() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 42) t._push(7) t._push(6) t.mul() self.assertEqual(t._cur, 1) self.assertEqual(t.shead, 42) t.mul() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 42 *42) def test_div(self): ''' Test turmit division capabilities ''' self._rpn('div') t = Turmit() t._push(43) t._push(7) t.div() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 6) def test_mod(self): ''' Test turmit mod() method ''' self._rpn('mod') t = Turmit() t._push(42) t._push(4) t.mod() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 2) def test_pow(self): ''' Test turmit pow() method ''' self._rpn('pow') t = Turmit() t._push(2) t._push(4) t.pow() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 16) def test_sqrt(self): ''' Test turmit sqrt() method ''' self._rpn('sqrt', 1) t = Turmit() t._push(42) t.sqrt() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 6) def test_and(self): ''' Test turmit binary and() method ''' self._rpn('bin_and') t = Turmit() t._push(10) t._push(42) t.bin_and() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 10) def test_or(self): ''' Test turmit binary or() method ''' self._rpn('bin_or') t = Turmit() t._push(42) t._push(7) t.bin_or() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 47) def test_xor(self): ''' Test turmit binary xor() method ''' self._rpn('bin_xor') t = Turmit() t._push(42) t._push(11) t.bin_xor() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 33) def test_rshift(self): ''' Test turmit rshift method ''' self._rpn('rshift') t = Turmit() t._push(42) t._push(2) t.rshift() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 10) def test_lshift(self): ''' Test turmit lshift method ''' self._rpn('lshift') t = Turmit() t._push(10) t._push(2) t.lshift() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 40) def test_dup(self): ''' Test turmit dup() method ''' self._rpn('dup', 1) t = Turmit() t._push(42) t.dup() self.assertEqual(t._cur, 1) self.assertEqual(t.shead, 42) def test_pop(self): ''' Test turmit pop() method ''' self._rpn('pop', 1) t = Turmit() t._push(10) t._push(2) t.pop() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 10) t.pop() self.assertEqual(t._cur, len(t._stack) - 1) def test_swp(self): ''' Test turmit swp() method ''' self._rpn('swp', 2) t = Turmit() t._push(42) t._push(1337) t.swp() self.assertEqual(t._cur, 1) self.assertEqual(t.shead, 42) t._pop() self.assertEqual(t.shead, 1337)