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_turmit.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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_class(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_push(self):
  13. ''' Test Turmit _push() method '''
  14. t = Turmit()
  15. t._push(1)
  16. self.assertEqual(t.shead, 1)
  17. t._push(2)
  18. self.assertEqual(t.shead, 2)
  19. def _rpn(self, rpn, narg = 2):
  20. if rpn not in _op_list:
  21. raise unittest.SkipTest('"%s" not implemented' % rpn)
  22. method, wrapped = _op_list[rpn]
  23. if narg != len(inspect.signature(method).parameters) - 1:
  24. self.fail('Expected %s to take %d arguments, but Turmit.%s() takes \
  25. %d arguments' % (rpn, narg, rpn, len(inspect.signature(method).parameters) - 1))
  26. def test_add(self):
  27. ''' Test turmit add method '''
  28. self._rpn('add')
  29. t = Turmit()
  30. t._push(42)
  31. t._push(8)
  32. t.add()
  33. self.assertEqual(t._cur, 0)
  34. self.assertEqual(t.shead, 50)
  35. t._push(42)
  36. t._push(8)
  37. t.add()
  38. self.assertEqual(t._cur, 1)
  39. self.assertEqual(t.shead, 50)
  40. t.add()
  41. self.assertEqual(t._cur, 0)
  42. self.assertEqual(t.shead, 100)
  43. def test_mem_sz(self):
  44. ''' Test turmit mem_sz() operation '''
  45. self._rpn('mem_sz', 1)
  46. t = Turmit(8)
  47. self.assertEqual(len(t._stack), 8)
  48. t._push(42)
  49. t.mem_sz()
  50. self.assertEqual(len(t._stack), 42)
  51. self.assertEqual(t._cur, 7)
  52. t._push(42)
  53. t._push(8)
  54. t.mem_sz()
  55. self.assertEqual(t._cur, 0)
  56. self.assertEqual(t.shead, 42)
  57. def test_sub(self):
  58. ''' Test turmit sub() method '''
  59. self._rpn('sub')
  60. t = Turmit()
  61. t._push(8)
  62. t._push(50)
  63. t.sub()
  64. self.assertEqual(t._cur, 0)
  65. self.assertEqual(t.shead, 42)
  66. t._push(8)
  67. t._push(50)
  68. t.sub()
  69. self.assertEqual(t._cur, 1)
  70. self.assertEqual(t.shead, 42)
  71. t.sub()
  72. self.assertEqual(t._cur, 0)
  73. self.assertEqual(t.shead, 0)
  74. def test_mul(self):
  75. ''' Test turmit mul() method '''
  76. self._rpn('mul')
  77. t = Turmit()
  78. t._push(7)
  79. t._push(6)
  80. t.mul()
  81. self.assertEqual(t._cur, 0)
  82. self.assertEqual(t.shead, 42)
  83. t._push(7)
  84. t._push(6)
  85. t.mul()
  86. self.assertEqual(t._cur, 1)
  87. self.assertEqual(t.shead, 42)
  88. t.mul()
  89. self.assertEqual(t._cur, 0)
  90. self.assertEqual(t.shead, 42 *42)
  91. def test_mod(self):
  92. ''' Test turmit mod() method '''
  93. self._rpn('mod')
  94. t = Turmit()
  95. t._push(2)
  96. t._push(42)
  97. t.mod()
  98. self.assertEqual(t._cur, 0)
  99. self.assertEqual(t.shead, 2)
  100. def test_pow(self):
  101. ''' Test turmit pow() method '''
  102. self._rpn('pow')
  103. t = Turmit()
  104. t._push(4)
  105. t._push(2)
  106. t.pow()
  107. self.assertEqual(t._cur, 0)
  108. self.assertEqual(t.shead, 16)
  109. def test_sqrt(self):
  110. ''' Test turmit sqrt() method '''
  111. self._rpn('sqrt', 1)
  112. t = Turmit()
  113. t._push(42)
  114. t.sqrt()
  115. self.assertEqual(t._cur, 0)
  116. self.assertEqual(t.shead, 6)
  117. def test_and(self):
  118. ''' Test turmit binary and() method '''
  119. self._rpn('bin_and')
  120. t = Turmit()
  121. t._push(42)
  122. t._push(10)
  123. t.bin_and()
  124. self.assertEqual(t._cur, 0)
  125. self.assertEqual(t.shead, 10)
  126. def test_or(self):
  127. ''' Test turmit binary or() method '''
  128. self._rpn('bin_or')
  129. t = Turmit()
  130. t._push(42)
  131. t._push(7)
  132. t.bin_or()
  133. self.assertEqual(t._cur, 0)
  134. self.assertEqual(t.shead, 47)
  135. def test_xor(self):
  136. ''' Test turmit binary xor() method '''
  137. self._rpn('bin_xor')
  138. t = Turmit()
  139. t._push(42)
  140. t._push(11)
  141. t.bin_xor()
  142. self.assertEqual(t._cur, 0)
  143. self.assertEqual(t.shead, 33)
  144. def test_rshift(self):
  145. ''' Test turmit rshift method '''
  146. self._rpn('rshift')
  147. t = Turmit()
  148. t._push(2)
  149. t._push(42)
  150. t.rshift()
  151. self.assertEqual(t._cur, 0)
  152. self.assertEqual(t.shead, 10)
  153. def test_lshift(self):
  154. ''' Test turmit lshift method '''
  155. self._rpn('lshift')
  156. t = Turmit()
  157. t._push(2)
  158. t._push(10)
  159. t.lshift()
  160. self.assertEqual(t._cur, 0)
  161. self.assertEqual(t.shead, 40)
  162. def test_dup(self):
  163. ''' Test turmit dup() method '''
  164. self._rpn('dup', 1)
  165. t = Turmit()
  166. t._push(42)
  167. t.dup()
  168. self.assertEqual(t._cur, 1)
  169. self.assertEqual(t.shead, 42)
  170. def test_pop(self):
  171. ''' Test turmit pop() method '''
  172. self._rpn('pop', 0)
  173. t = Turmit()
  174. t._push(10)
  175. t._push(2)
  176. t.pop()
  177. self.assertEqual(t._cur, 0)
  178. self.asertEqual(t.shead, 10)
  179. t.pop()
  180. self.assertEqual(t._cur, len(t._stack) - 1)