import unittest import inspect from gte.turmit import Turmit from gte.rpnlib import _op_list class TurmitTestCase(unittest.TestCase): def test_init_class(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_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(8) t._push(50) t.sub() self.assertEqual(t._cur, 0) self.assertEqual(t.shead, 42) t._push(8) t._push(50) 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_mod(self): ''' Test turmit mod() method ''' self._rpn('mod') t = Turmit() t._push(2) t._push(42) 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(4) t._push(2) 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(42) t._push(10) 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(2) t._push(42) 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(2) t._push(10) 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', 0) t = Turmit() t._push(10) t._push(2) t.pop() self.assertEqual(t._cur, 0) self.asertEqual(t.shead, 10) t.pop() self.assertEqual(t._cur, len(t._stack) - 1)