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_compile.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 TestRpnCompile(unittest.TestCase):
  33. def test_basic(self):
  34. """ Compile 6 7 * """
  35. expr = pyrpn.RPNExpr("6 7 *", 2)
  36. def test_base_error(self):
  37. """ Compile error a b + """
  38. with self.assertRaises(ValueError):
  39. expr = pyrpn.RPNExpr("a b +", 3)
  40. def test_various_compile(self):
  41. """ Compile various expressions """
  42. exprs = (("42 2 + * /", 0),
  43. ("A1 A2 A0 * + /", 3),
  44. )
  45. for expr, argc in exprs:
  46. res = pyrpn.RPNExpr(expr, argc)
  47. def test_long_code(self):
  48. """ Compile long expressions """
  49. for i in range(0x100,0x10000,0x500):
  50. with self.subTest('Testing expression with %X ops' % i):
  51. for argc in range(1,32, 8):
  52. args = [random.randint(0,IMAX) for _ in range(argc)]
  53. expr = pyrpn.RPNExpr(pyrpn.random_expr(argc, i), argc)
  54. del(expr)
  55. def test_very_long(self):
  56. """ Compile a very long expression """
  57. argc = 4
  58. codelen = 0x500000
  59. import time
  60. for i in range(3):
  61. args = [random.randint(0,IMAX) for _ in range(argc)]
  62. expr_str = pyrpn.random_expr(argc, codelen)
  63. expr = pyrpn.RPNExpr(expr_str, argc)
  64. del(expr)
  65. def test_pickling(self):
  66. """ Pickle & unpickle tests """
  67. argc = 4
  68. for _ in range(512):
  69. expr = pyrpn.RPNExpr(pyrpn.random_expr(2,10), argc)
  70. pik = pickle.dumps(expr)
  71. new_expr = pickle.loads(pik)
  72. self.assertEqual(str(expr), str(new_expr))
  73. args = [random.randint(0,IMAX) for _ in range(argc)]
  74. self.assertEqual(expr.eval(*args), new_expr.eval(*args))
  75. if __name__ == '__main__':
  76. unittest.main()