Fast IFS using RPN notation
python
c
x86-64
nasm
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.

tests_rpn.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #!/usr/bin/python3
  2. # Copyright 2020 Weber Yann
  3. #
  4. # This file is part of geneifs.
  5. #
  6. # geneifs is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # geneifs is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with geneifs. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import sys
  20. import random
  21. import math
  22. import pickle
  23. import unittest
  24. from unittest import skip
  25. IMAX = (1<<64)-1
  26. try:
  27. import pyrpn
  28. except (ImportError, NameError) as e:
  29. print("Error importing pyrpn. Try to run make.",
  30. file=sys.stderr)
  31. raise e
  32. class Test0RpnModule(unittest.TestCase):
  33. def test_init(self):
  34. """ RPNExpr instanciation """
  35. expr = pyrpn.RPNExpr("42", 2)
  36. def test_init_badargs(self):
  37. """ RPNExpr instanciation with bad arguments """
  38. badargs = [('', 2), (), ('ab+',), ('ab+',300), (42, 42), ('ab', '42')]
  39. for badarg in badargs:
  40. with self.assertRaises((ValueError, TypeError)):
  41. expr = pyrpn.RPNExpr(*badarg)
  42. def test_init_loop(self):
  43. """ Testing pyrpn.RPNExpr multiple instanciation """
  44. exprs = []
  45. for i in range(256):
  46. expr = pyrpn.RPNExpr("42", 2)
  47. def test_init_args(self):
  48. """ Testing pyrpn.RPNExpr instanciation with arguments """
  49. for argc in range(0, 256):
  50. with self.subTest("RPNExpr('42', %d)" % argc):
  51. expr = pyrpn.RPNExpr("42", argc)
  52. def test_init_stack_sz(self):
  53. """ Instanciate RPNExpr specifiing a stack_size """
  54. for argc in range(0,256, 8):
  55. for sz in range(4, 256, 2):
  56. with self.subTest("RPNExpr('42', %d,%d)" % (argc, sz)):
  57. expr = pyrpn.RPNExpr("42", argc, sz)
  58. def test_op_dict(self):
  59. """ Testing RPNExpr.get_ops() method """
  60. known_ops = dict([
  61. ('add', '+'),
  62. ('sub', '-'),
  63. ('mul', '*'),
  64. ('div', '/'),
  65. ('mod', '%'),
  66. ('neg', '!'),
  67. ('not', '~'),
  68. ('and', '&'),
  69. ('or', '|'),
  70. ('xor', '^'),
  71. ('>>', 'r'),
  72. ('<<', 'l'),
  73. ('xchg', 'x'),
  74. ('dup', 'd'),
  75. ('pop', 'p'),
  76. ])
  77. for long, short in pyrpn.get_ops().items():
  78. self.assertIn(long, known_ops)
  79. self.assertEqual(short, known_ops[long])
  80. def test_rand_expr(self):
  81. """ Testing RPNExpr.random_expr() """
  82. result = {}
  83. counters = {'vals': 0, 'args': 0}
  84. all_count = 0
  85. for sz in range(1,64,3):
  86. for argc in range(1,128):
  87. rnd_expr = pyrpn.random_expr(argc, sz)
  88. #print(repr(rnd_expr))
  89. for tok in rnd_expr.split(' '):
  90. all_count += 1
  91. if len(tok) == 0:
  92. continue
  93. try:
  94. itok = int(tok, 0)
  95. counters['vals'] += 1
  96. continue
  97. except Exception:
  98. pass
  99. if tok[0] == 'A':
  100. counters['args'] += 1
  101. else:
  102. if tok not in counters:
  103. counters[tok] = 0
  104. counters[tok] += 1
  105. all_ops = len(pyrpn.get_ops()) + 2
  106. entropy = 1-sum([(n/all_count)**2
  107. for _, n in counters.items()])
  108. self.assertGreater(entropy, 1-(1/all_ops), "Low entropy !")
  109. class TestRpnCompile(unittest.TestCase):
  110. def test_basic(self):
  111. """ Compile 6 7 * """
  112. expr = pyrpn.RPNExpr("6 7 *", 2)
  113. def test_base_error(self):
  114. """ Compile error a b + """
  115. with self.assertRaises(ValueError):
  116. expr = pyrpn.RPNExpr("a b +", 3)
  117. def test_various_compile(self):
  118. """ Compile various expressions """
  119. exprs = (("42 2 + * /", 0),
  120. ("A1 A2 A0 * + /", 3),
  121. )
  122. for expr, argc in exprs:
  123. res = pyrpn.RPNExpr(expr, argc)
  124. def test_long_code(self):
  125. """ Compile long expressions """
  126. for i in range(64,256,2):
  127. for j in range(256,4):
  128. exprs = ' '.join(['+' for _ in range(i)])
  129. expr = pyrpn.RPNExpr(exprs, j)
  130. def test_pickling(self):
  131. """ Pickle & unpickle tests """
  132. argc = 4
  133. for _ in range(512):
  134. expr = pyrpn.RPNExpr(pyrpn.random_expr(2,10), argc)
  135. pik = pickle.dumps(expr)
  136. new_expr = pickle.loads(pik)
  137. self.assertEqual(str(expr), str(new_expr))
  138. args = [random.randint(0,IMAX) for _ in range(argc)]
  139. self.assertEqual(expr.eval(*args), new_expr.eval(*args))
  140. class TestRpnEval(unittest.TestCase):
  141. def test_arithm(self):
  142. """ Tests arithmetic ops """
  143. ops = [
  144. ('+', '+'),
  145. ('-', '-'),
  146. ('*', '*'),
  147. ('/', '//'),
  148. ('%', '%'),
  149. ('&', '&'),
  150. ('|', '|'),
  151. ('^', '^'),
  152. #('l', '<<'),
  153. #('r', '>>')
  154. ]
  155. for rpn_op, pyop in ops:
  156. with self.subTest('Testing op %s (%s)' % (rpn_op, pyop)):
  157. sys.stderr.write('.')
  158. sys.stderr.flush()
  159. for i in range(0x1000):
  160. op1, op2 = random.randint(0,IMAX), random.randint(1,IMAX)
  161. pyexpr = '%d %s %d' % (op1, pyop, op2)
  162. pyval = eval(pyexpr) % (1<<64)
  163. rpn_expr = "%d %d %s" % (op1, op2, rpn_op)
  164. expr = pyrpn.RPNExpr(rpn_expr, 0)
  165. res = expr.eval()
  166. self.assertEqual(res, pyval,
  167. "TEST#%d %s != %s" % (i, rpn_expr, pyexpr))
  168. def test_not(self):
  169. """ Test unary op """
  170. ops = [('~', '~%d'), ('!', '-%d')]
  171. for rpn_op, pystr in ops:
  172. with self.subTest("Testing op '%sX' (%s)" % (rpn_op, pystr)):
  173. sys.stderr.write('.')
  174. sys.stderr.flush()
  175. for i in range(0x1000):
  176. op1 = random.randint(0, IMAX)
  177. pyexpr = pystr % op1
  178. pyval = eval(pyexpr) % (1<<64)
  179. rpn_expr = '%d %s' % (op1, rpn_op)
  180. expr = pyrpn.RPNExpr(rpn_expr, 0)
  181. res = expr.eval()
  182. self.assertEqual(res, pyval,
  183. "TEST#%d %s != %s" % (i, rpn_expr, pyexpr))
  184. def test_div_zero(self):
  185. """ Test division by zeros """
  186. tests = ['A0 0 /', 'A0 0 %']
  187. for test in tests:
  188. with self.subTest('Testing division by zero using %s' % test):
  189. for i in range(10):
  190. expr = pyrpn.RPNExpr(test, 1)
  191. res = expr.eval(random.randint(0, IMAX))
  192. self.assertEqual(res, 0)
  193. def test_stack_init(self):
  194. """ testing that stack is initialized to 0 """
  195. rpn = ' '.join(['+' for _ in range(15)])
  196. for stack_size in range(4, 128):
  197. with self.subTest('Stack with size %d initialized to 0' % stack_size):
  198. for argc in range(0,256,16):
  199. expr = pyrpn.RPNExpr(rpn, argc, stack_size)
  200. r = expr.eval(*[random.randint(0, IMAX)
  201. for _ in range(argc)])
  202. self.assertEqual(r, 0,
  203. '"+ + + +..." should be 0 but %d returned with %d args' % (r, argc))
  204. def test_lshift_limit(self):
  205. """ 2 << 0x10000 == 0 ? """
  206. expr = pyrpn.RPNExpr("2 %d <<" % 0x100000, 0)
  207. res = expr.eval()
  208. self.assertEqual(res, 0)
  209. def test_rshift_limit(self):
  210. """ (1<<64)-1 >> 0x10000 == 0 ? """
  211. expr = pyrpn.RPNExpr("%d %d >>" % ((1<<64)-1, 0x100000), 0)
  212. res = expr.eval()
  213. self.assertEqual(res, 0)
  214. def test_airthm_extended(self):
  215. """ Extended arithmetic tests """
  216. exprs = (
  217. ('A0 A1 +', '{0} + {1}', 2),
  218. ('A0 A0 A1 + +', '{0} + {1} + {0}', 2),
  219. ('A1 A0 A0 A0 A0 A0 A0 A0 A0 + + + + + + +', '{0} * 8', 2),
  220. ('+', '0', 2),
  221. ('-', '0', 2),
  222. ('A0 A1 -', '{0} - {1}', 2),
  223. ('A0 A0 A1 - -', '{0} - ({0} - {1})', 2),
  224. ('A0 0 A0 - -', '({0} - (0 - {0})) ', 2),
  225. ('*', '0', 2),
  226. ('A0 A1 *', '{0} * {1}', 2),
  227. ('A0 A0 A1 * *', '{0} * {0} * {1}', 2),
  228. ('A0 A0 A0 * *', '{0} * {0} * {0}', 2),
  229. ('A0 A1 x /', '{1} // {0}', 2),
  230. ('A0 A1 A0 pop /', '{0} // {1}', 2),
  231. ('A0 A1 dup +', '{1} + {1}', 2),
  232. )
  233. for rpn, pye, argc in exprs:
  234. expr = pyrpn.RPNExpr(rpn, argc, 8)
  235. for _ in range(0x300):
  236. args = tuple([random.randint(0,255) for _ in range(argc)])
  237. with self.subTest('%s == %s %r' % (rpn, pye, args)):
  238. pyexpr = pye.format(*args)
  239. try:
  240. respy = eval(pyexpr) % (1<<64)
  241. except ZeroDivisionError:
  242. respy = 0
  243. res = expr.eval(*args)
  244. self.assertEqual(res, respy,
  245. '%s%r != %s' % (rpn, args, pyexpr))
  246. class SequentialTestLoader(unittest.TestLoader):
  247. def getTestCaseNames(self, testCaseClass):
  248. test_names = super().getTestCaseNames(testCaseClass)
  249. testcase_methods = list(testCaseClass.__dict__.keys())
  250. test_names.sort(key=testcase_methods.index)
  251. return test_names
  252. if __name__ == '__main__':
  253. unittest.main(testLoader=SequentialTestLoader())