Genetic Turmit Evolver
python
c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_cturmit.py 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import unittest
  2. import inspect
  3. from cturmit import CTurmit as Turmit
  4. from gte.rpnlib import _op_list
  5. class TurmitTestCase(unittest.TestCase):
  6. def test_init(self):
  7. ''' Test Turmit class __init__ '''
  8. t = Turmit(42)
  9. self.assertEqual(len(t._stack), 42)
  10. self.assertEqual(t.shead, 0)
  11. self.assertEqual(len(t._stack) - 1 , t._cur)
  12. def test_init_args(self):
  13. ''' Test Turmit __init__ arguments '''
  14. t = Turmit()
  15. t = Turmit(prog = 'G R B 44 MOD ADD 43 MOD ADD 42 MOD')
  16. t = Turmit(prog = 'G R B ADD ADD 42 MOD', max_int = 0xFFFF,
  17. stack_size=42)
  18. def test_call(self):
  19. ''' Test Turmit.__call__ '''
  20. t = Turmit(prog = 'G R B ADD ADD 42 MOD', max_int = 0xFFFF,
  21. stack_size=42)
  22. r = t(x=1, y=2, r=255, g=255, b=255)
  23. self.assertEqual(r, 9)
  24. r = t(x=2, y=2, r=255, g=0, b=0)
  25. self.assertEqual(r, 3)
  26. def test_copy(self):
  27. t = Turmit(prog = 'G R B ADD ADD 42 MOD', max_int = 0xFFFF,
  28. stack_size=42)
  29. t2 = Turmit(turmit=t)
  30. r = t2(x=1, y=2, r=255, g=255, b=255)
  31. self.assertEqual(r, 9)
  32. r = t2(x=2, y=2, r=255, g=0, b=0)
  33. self.assertEqual(r, 3)
  34. def test_init_badargs(self):
  35. ''' Test Turmit __init__ bad arguments '''
  36. with self.assertRaises(RuntimeError):
  37. t = Turmit(foobar=42)
  38. with self.assertRaises(ValueError):
  39. t = Turmit(prog='foobar')
  40. class TurmitExprCompileTestCase(unittest.TestCase):
  41. ''' Tests compilatin capabilities of cturmit '''
  42. def test_values(self):
  43. progs = ('1', '0x1', '9', '0x9', '0xf', '0xF', '0x1FFF', '0xFFF1',
  44. '12345');
  45. for prog in progs:
  46. t = Turmit(prog=prog)
  47. def test_var(self):
  48. progs = ('x', 'y', 'r', 'g', 'b')
  49. for prog in progs:
  50. t = Turmit(prog=prog)
  51. class TurmitOperationTestCase(unittest.TestCase):
  52. def test_push(self):
  53. ''' Test Turmit _push() method '''
  54. t = Turmit()
  55. t._push(1)
  56. self.assertEqual(t.shead, 1)
  57. t._push(2)
  58. self.assertEqual(t.shead, 2)
  59. def _rpn(self, rpn, narg = 2):
  60. if rpn not in _op_list:
  61. raise unittest.SkipTest('"%s" not implemented' % rpn)
  62. method, wrapped = _op_list[rpn]
  63. if narg != len(inspect.signature(method).parameters) - 1:
  64. self.fail('Expected %s to take %d arguments, but Turmit.%s() takes \
  65. %d arguments' % (rpn, narg, rpn, len(inspect.signature(method).parameters) - 1))
  66. def test_add(self):
  67. ''' Test turmit add method '''
  68. self._rpn('add')
  69. t = Turmit()
  70. t._push(42)
  71. t._push(8)
  72. t.add()
  73. self.assertEqual(t._cur, 0)
  74. self.assertEqual(t.shead, 50)
  75. t._push(42)
  76. t._push(8)
  77. t.add()
  78. self.assertEqual(t._cur, 1)
  79. self.assertEqual(t.shead, 50)
  80. t.add()
  81. self.assertEqual(t._cur, 0)
  82. self.assertEqual(t.shead, 100)
  83. def test_mem_sz(self):
  84. ''' Test turmit mem_sz() operation '''
  85. self._rpn('mem_sz', 1)
  86. t = Turmit(8)
  87. self.assertEqual(len(t._stack), 8)
  88. t._push(42)
  89. t.mem_sz()
  90. self.assertEqual(len(t._stack), 42)
  91. self.assertEqual(t._cur, 7)
  92. t._push(42)
  93. t._push(8)
  94. t.mem_sz()
  95. self.assertEqual(t._cur, 0)
  96. self.assertEqual(t.shead, 42)
  97. def test_sub(self):
  98. ''' Test turmit sub() method '''
  99. self._rpn('sub')
  100. t = Turmit()
  101. t._push(50)
  102. t._push(8)
  103. t.sub()
  104. self.assertEqual(t._cur, 0)
  105. self.assertEqual(t.shead, 42)
  106. t._push(50)
  107. t._push(8)
  108. t.sub()
  109. self.assertEqual(t._cur, 1)
  110. self.assertEqual(t.shead, 42)
  111. t.sub()
  112. self.assertEqual(t._cur, 0)
  113. self.assertEqual(t.shead, 0)
  114. def test_mul(self):
  115. ''' Test turmit mul() method '''
  116. self._rpn('mul')
  117. t = Turmit()
  118. t._push(7)
  119. t._push(6)
  120. t.mul()
  121. self.assertEqual(t._cur, 0)
  122. self.assertEqual(t.shead, 42)
  123. t._push(7)
  124. t._push(6)
  125. t.mul()
  126. self.assertEqual(t._cur, 1)
  127. self.assertEqual(t.shead, 42)
  128. t.mul()
  129. self.assertEqual(t._cur, 0)
  130. self.assertEqual(t.shead, 42 *42)
  131. def test_div(self):
  132. ''' Test turmit division capabilities '''
  133. self._rpn('div')
  134. t = Turmit()
  135. t._push(43)
  136. t._push(7)
  137. t.div()
  138. self.assertEqual(t._cur, 0)
  139. self.assertEqual(t.shead, 6)
  140. def test_mod(self):
  141. ''' Test turmit mod() method '''
  142. self._rpn('mod')
  143. t = Turmit()
  144. t._push(42)
  145. t._push(4)
  146. t.mod()
  147. self.assertEqual(t._cur, 0)
  148. self.assertEqual(t.shead, 2)
  149. def test_pow(self):
  150. ''' Test turmit pow() method '''
  151. self._rpn('pow')
  152. t = Turmit()
  153. t._push(2)
  154. t._push(4)
  155. t.pow()
  156. self.assertEqual(t._cur, 0)
  157. self.assertEqual(t.shead, 16)
  158. def test_sqrt(self):
  159. ''' Test turmit sqrt() method '''
  160. self._rpn('sqrt', 1)
  161. t = Turmit()
  162. t._push(42)
  163. t.sqrt()
  164. self.assertEqual(t._cur, 0)
  165. self.assertEqual(t.shead, 6)
  166. def test_and(self):
  167. ''' Test turmit binary and() method '''
  168. self._rpn('bin_and')
  169. t = Turmit()
  170. t._push(10)
  171. t._push(42)
  172. t.bin_and()
  173. self.assertEqual(t._cur, 0)
  174. self.assertEqual(t.shead, 10)
  175. def test_or(self):
  176. ''' Test turmit binary or() method '''
  177. self._rpn('bin_or')
  178. t = Turmit()
  179. t._push(42)
  180. t._push(7)
  181. t.bin_or()
  182. self.assertEqual(t._cur, 0)
  183. self.assertEqual(t.shead, 47)
  184. def test_xor(self):
  185. ''' Test turmit binary xor() method '''
  186. self._rpn('bin_xor')
  187. t = Turmit()
  188. t._push(42)
  189. t._push(11)
  190. t.bin_xor()
  191. self.assertEqual(t._cur, 0)
  192. self.assertEqual(t.shead, 33)
  193. def test_rshift(self):
  194. ''' Test turmit rshift method '''
  195. self._rpn('rshift')
  196. t = Turmit()
  197. t._push(42)
  198. t._push(2)
  199. t.rshift()
  200. self.assertEqual(t._cur, 0)
  201. self.assertEqual(t.shead, 10)
  202. def test_lshift(self):
  203. ''' Test turmit lshift method '''
  204. self._rpn('lshift')
  205. t = Turmit()
  206. t._push(10)
  207. t._push(2)
  208. t.lshift()
  209. self.assertEqual(t._cur, 0)
  210. self.assertEqual(t.shead, 40)
  211. def test_dup(self):
  212. ''' Test turmit dup() method '''
  213. self._rpn('dup', 1)
  214. t = Turmit()
  215. t._push(42)
  216. t.dup()
  217. self.assertEqual(t._cur, 1)
  218. self.assertEqual(t.shead, 42)
  219. def test_pop(self):
  220. ''' Test turmit pop() method '''
  221. self._rpn('pop', 1)
  222. t = Turmit()
  223. t._push(10)
  224. t._push(2)
  225. t.pop()
  226. self.assertEqual(t._cur, 0)
  227. self.assertEqual(t.shead, 10)
  228. t.pop()
  229. self.assertEqual(t._cur, len(t._stack) - 1)
  230. def test_swp(self):
  231. ''' Test turmit swp() method '''
  232. self._rpn('swp', 2)
  233. t = Turmit()
  234. t._push(42)
  235. t._push(1337)
  236. t.swp()
  237. self.assertEqual(t._cur, 1)
  238. self.assertEqual(t.shead, 42)
  239. t._pop()
  240. self.assertEqual(t.shead, 1337)