Genetic Turmit Evolver
python
c
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

test_turmit.py 7.1KB

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