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_pyrpn.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. if __name__ == '__main__':
  110. unittest.main()