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 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 longs expressions (from 256 to 65536 op )"""
  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. sys.stderr.write('.')
  53. sys.stderr.flush()
  54. args = [random.randint(0,IMAX) for _ in range(argc)]
  55. expr = pyrpn.RPNExpr(pyrpn.random_expr(argc, i), argc)
  56. del(expr)
  57. def test_very_long(self):
  58. """ Compile 3 very long expression (5242K op) """
  59. argc = 4
  60. codelen = 0x500000
  61. import time
  62. for i in range(3):
  63. args = [random.randint(0,IMAX) for _ in range(argc)]
  64. sys.stderr.write('Generate')
  65. sys.stderr.flush()
  66. expr_str = pyrpn.random_expr(argc, codelen)
  67. sys.stderr.write('d Compile')
  68. sys.stderr.flush()
  69. expr = pyrpn.RPNExpr(expr_str, argc)
  70. sys.stderr.write('d ')
  71. sys.stderr.flush()
  72. del(expr)
  73. def test_pickling(self):
  74. """ Pickle & unpickle tests """
  75. argc = 4
  76. for _ in range(512):
  77. expr = pyrpn.RPNExpr(pyrpn.random_expr(2,10), argc)
  78. pik = pickle.dumps(expr)
  79. new_expr = pickle.loads(pik)
  80. self.assertEqual(str(expr), str(new_expr))
  81. args = [random.randint(0,IMAX) for _ in range(argc)]
  82. self.assertEqual(expr.eval(*args), new_expr.eval(*args))
  83. if __name__ == '__main__':
  84. unittest.main()