Genetic Turmit Evolver
python
c
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

test_cturmit.py 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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(max_int=0xFFFF)
  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. t._push(8)
  115. t._push(50)
  116. t.sub()
  117. self.assertEqual(t._cur, 1)
  118. self.assertEqual(t.shead, 0xFFFF + (8 - 50))
  119. def test_mul(self):
  120. ''' Test turmit mul() method '''
  121. self._rpn('mul')
  122. t = Turmit()
  123. t._push(7)
  124. t._push(6)
  125. t.mul()
  126. self.assertEqual(t._cur, 0)
  127. self.assertEqual(t.shead, 42)
  128. t._push(7)
  129. t._push(6)
  130. t.mul()
  131. self.assertEqual(t._cur, 1)
  132. self.assertEqual(t.shead, 42)
  133. t.mul()
  134. self.assertEqual(t._cur, 0)
  135. self.assertEqual(t.shead, 42 *42)
  136. def test_div(self):
  137. ''' Test turmit division capabilities '''
  138. self._rpn('div')
  139. t = Turmit()
  140. t._push(43)
  141. t._push(7)
  142. t.div()
  143. self.assertEqual(t._cur, 0)
  144. self.assertEqual(t.shead, 6)
  145. def test_mod(self):
  146. ''' Test turmit mod() method '''
  147. self._rpn('mod')
  148. t = Turmit()
  149. t._push(42)
  150. t._push(4)
  151. t.mod()
  152. self.assertEqual(t._cur, 0)
  153. self.assertEqual(t.shead, 2)
  154. def test_pow(self):
  155. ''' Test turmit pow() method '''
  156. self._rpn('pow')
  157. t = Turmit()
  158. t._push(2)
  159. t._push(4)
  160. t.pow()
  161. self.assertEqual(t._cur, 0)
  162. self.assertEqual(t.shead, 16)
  163. def test_sqrt(self):
  164. ''' Test turmit sqrt() method '''
  165. self._rpn('sqrt', 1)
  166. t = Turmit()
  167. t._push(42)
  168. t.sqrt()
  169. self.assertEqual(t._cur, 0)
  170. self.assertEqual(t.shead, 6)
  171. def test_and(self):
  172. ''' Test turmit binary and() method '''
  173. self._rpn('bin_and')
  174. t = Turmit()
  175. t._push(10)
  176. t._push(42)
  177. t.bin_and()
  178. self.assertEqual(t._cur, 0)
  179. self.assertEqual(t.shead, 10)
  180. def test_or(self):
  181. ''' Test turmit binary or() method '''
  182. self._rpn('bin_or')
  183. t = Turmit()
  184. t._push(42)
  185. t._push(7)
  186. t.bin_or()
  187. self.assertEqual(t._cur, 0)
  188. self.assertEqual(t.shead, 47)
  189. def test_xor(self):
  190. ''' Test turmit binary xor() method '''
  191. self._rpn('bin_xor')
  192. t = Turmit()
  193. t._push(42)
  194. t._push(11)
  195. t.bin_xor()
  196. self.assertEqual(t._cur, 0)
  197. self.assertEqual(t.shead, 33)
  198. def test_rshift(self):
  199. ''' Test turmit rshift method '''
  200. self._rpn('rshift')
  201. t = Turmit()
  202. t._push(42)
  203. t._push(2)
  204. t.rshift()
  205. self.assertEqual(t._cur, 0)
  206. self.assertEqual(t.shead, 10)
  207. def test_lshift(self):
  208. ''' Test turmit lshift method '''
  209. self._rpn('lshift')
  210. t = Turmit()
  211. t._push(10)
  212. t._push(2)
  213. t.lshift()
  214. self.assertEqual(t._cur, 0)
  215. self.assertEqual(t.shead, 40)
  216. def test_dup(self):
  217. ''' Test turmit dup() method '''
  218. self._rpn('dup', 1)
  219. t = Turmit()
  220. t._push(42)
  221. t.dup()
  222. self.assertEqual(t._cur, 1)
  223. self.assertEqual(t.shead, 42)
  224. def test_pop(self):
  225. ''' Test turmit pop() method '''
  226. self._rpn('pop', 1)
  227. t = Turmit()
  228. t._push(10)
  229. t._push(2)
  230. t.pop()
  231. self.assertEqual(t._cur, 0)
  232. self.assertEqual(t.shead, 10)
  233. t.pop()
  234. self.assertEqual(t._cur, len(t._stack) - 1)
  235. def test_swp(self):
  236. ''' Test turmit swp() method '''
  237. self._rpn('swp', 2)
  238. t = Turmit()
  239. t._push(42)
  240. t._push(1337)
  241. t.swp()
  242. self.assertEqual(t._cur, 1)
  243. self.assertEqual(t.shead, 42)
  244. t._pop()
  245. self.assertEqual(t.shead, 1337)