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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/python3
  2. # Copyright 2020, 2023 Weber Yann
  3. #
  4. # This file is part of rpnifs.
  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 copy
  20. import sys
  21. import random
  22. import math
  23. import pickle
  24. import unittest
  25. from unittest import skip
  26. IMAX = (1<<64)-1
  27. try:
  28. import pyrpn
  29. except (ImportError, NameError) as e:
  30. print("Error importing pyrpn. Try to run make.",
  31. file=sys.stderr)
  32. raise e
  33. class Test0RpnModule(unittest.TestCase):
  34. def test_init(self):
  35. """ RPNExpr instanciation """
  36. expr = pyrpn.RPNExpr("42", 2)
  37. def test_init_badargs(self):
  38. """ RPNExpr instanciation with bad arguments """
  39. badargs = [(), ('ab+',), ('ab+',300), (42, 42), ('ab', '42')]
  40. for badarg in badargs:
  41. with self.subTest(badargs=badarg):
  42. with self.assertRaises((ValueError, TypeError)):
  43. expr = pyrpn.RPNExpr(*badarg)
  44. def test_init_loop(self):
  45. """ Testing pyrpn.RPNExpr multiple instanciation """
  46. exprs = []
  47. for i in range(256):
  48. expr = pyrpn.RPNExpr("42", 2)
  49. def test_init_args(self):
  50. """ Testing pyrpn.RPNExpr instanciation with arguments """
  51. for argc in range(0, 256):
  52. with self.subTest("RPNExpr('42', %d)" % argc):
  53. expr = pyrpn.RPNExpr("42", argc)
  54. def test_init_stack_sz(self):
  55. """ Instanciate RPNExpr specifiing a stack_size """
  56. for argc in range(0,256, 8):
  57. for sz in range(4, 256, 2):
  58. with self.subTest("RPNExpr('42', %d,%d)" % (argc, sz)):
  59. expr = pyrpn.RPNExpr("42", argc, sz)
  60. def test_op_dict(self):
  61. """ Testing RPNExpr.get_ops() method """
  62. known_ops = dict([
  63. ('add', '+'),
  64. ('sub', '-'),
  65. ('mul', '*'),
  66. ('div', '/'),
  67. ('mod', '%'),
  68. ('neg', '!'),
  69. ('not', '~'),
  70. ('and', '&'),
  71. ('or', '|'),
  72. ('xor', '^'),
  73. ('>>', 'r'),
  74. ('<<', 'l'),
  75. ('xchg', 'x'),
  76. ('dup', 'd'),
  77. ('pop', 'p'),
  78. ])
  79. for long, short in pyrpn.get_ops().items():
  80. self.assertIn(long, known_ops)
  81. self.assertEqual(short, known_ops[long])
  82. def testing_rand_expr_len(self):
  83. """ Testing if RPNExpr.random_expr() returns expected expression """
  84. for _ in range(100):
  85. elen = random.randint(1, 50)
  86. rnd_expr = pyrpn.random_expr(20, elen)
  87. spl = [s for s in rnd_expr.split(' ') if len(s.strip())]
  88. with self.subTest(expected_tokens=elen, tokens_count=len(spl), expr=rnd_expr):
  89. self.assertEqual(len(spl), elen)
  90. def test_rand_expr(self):
  91. """ Testing RPNExpr.random_expr() """
  92. result = {}
  93. counters = {'vals': 0, 'args': 0}
  94. all_count = 0
  95. for sz in range(1,64,3):
  96. for argc in range(1,128):
  97. rnd_expr = pyrpn.random_expr(argc, sz)
  98. #print(repr(rnd_expr))
  99. for tok in rnd_expr.split(' '):
  100. all_count += 1
  101. if len(tok) == 0:
  102. continue
  103. try:
  104. itok = int(tok, 0)
  105. counters['vals'] += 1
  106. continue
  107. except Exception:
  108. pass
  109. if tok[0] == 'A':
  110. counters['args'] += 1
  111. else:
  112. if tok not in counters:
  113. counters[tok] = 0
  114. counters[tok] += 1
  115. all_ops = len(pyrpn.get_ops()) + 1
  116. entropy = 1-sum([(n/all_count)**2
  117. for _, n in counters.items()])
  118. self.assertGreater(entropy, 1-(1/all_ops), "Low entropy !")
  119. if __name__ == '__main__':
  120. unittest.main()