123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- import unittest
- import inspect
-
- from cturmit import CTurmit as 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 = 'G R B 44 MOD ADD 43 MOD ADD 42 MOD')
- t = Turmit(prog = 'G R B ADD ADD 42 MOD', max_int = 0xFFFF,
- stack_size=42)
-
- def test_call(self):
- ''' Test Turmit.__call__ '''
- t = Turmit(prog = 'G R B ADD ADD 42 MOD', max_int = 0xFFFF,
- 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_copy(self):
- t = Turmit(prog = 'G R B ADD ADD 42 MOD', max_int = 0xFFFF,
- stack_size=42)
- t2 = Turmit(turmit=t)
- r = t2(x=1, y=2, r=255, g=255, b=255)
- self.assertEqual(r, 9)
- r = t2(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(prog='foobar')
-
-
- class TurmitExprCompileTestCase(unittest.TestCase):
- ''' Tests compilatin capabilities of cturmit '''
-
- def test_values(self):
- progs = ('1', '0x1', '9', '0x9', '0xf', '0xF', '0x1FFF', '0xFFF1',
- '12345');
- for prog in progs:
- t = Turmit(prog=prog)
-
- def test_var(self):
- progs = ('x', 'y', 'r', 'g', 'b')
- for prog in progs:
- t = Turmit(prog=prog)
-
-
- 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(max_int=0xFFFF)
-
- 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)
-
- t._push(8)
- t._push(50)
- t.sub()
- self.assertEqual(t._cur, 1)
- self.assertEqual(t.shead, 0xFFFF + (8 - 50))
-
- 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)
|