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_rpniter_seq.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/python3
  2. # copyright 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 pickle
  21. import random
  22. import sys
  23. import unittest
  24. try:
  25. import pyrpn
  26. except (ImportError, NameError) as e:
  27. print("error importing pyrpn. try to run make.",
  28. file=sys.stderr)
  29. raise e
  30. class TestRpnIterSequence(unittest.TestCase):
  31. """ Testing RPNIterExpr sequence methods """
  32. def test_len(self):
  33. """ Testing RPNIterExpr.__len__ with various values/constants """
  34. tests = [([pyrpn.const.POS_XY, pyrpn.const.RESULT_RGB, (1024,1024)],
  35. 5),
  36. ([pyrpn.const.POS_LINEAR, pyrpn.const.RESULT_CONST_RGBA, (1024,), (255,0,0,255)],
  37. 1),
  38. ([pyrpn.const.POS_XY, pyrpn.const.RESULT_CONST_RGBA, (640,480), (255,0,0,255)],
  39. 2),
  40. ([pyrpn.const.POS_LINEAR, pyrpn.const.RESULT_RGBA, (1024,)],
  41. 5),
  42. ([pyrpn.const.POS_XDIM, pyrpn.const.RESULT_RGB, (4, 2, 2, 640, 480)],
  43. 7),
  44. ]
  45. for args, expt_len in tests:
  46. rif=pyrpn.RPNIterExpr(*args)
  47. with self.subTest(rif=rif, expt_len=expt_len, args=args):
  48. self.assertEqual(expt_len, len(rif))
  49. class TestRPNIterMapping(unittest.TestCase):
  50. """ Testing RPNIterExpr mapping methods """
  51. def test_keys(self):
  52. """ Testing RPNIterExpr.__getitem__ with various values/constants """
  53. tests = [([pyrpn.const.POS_XY, pyrpn.const.RESULT_RGB, (1024,1024)],
  54. ['X', 'Y', 'R', 'G', 'B']),
  55. ([pyrpn.const.POS_LINEAR, pyrpn.const.RESULT_CONST_RGBA, (1024,), (255,0,0,255)],
  56. ['X']),
  57. ([pyrpn.const.POS_XY, pyrpn.const.RESULT_CONST_RGBA, (640,480), (255,0,0,255)],
  58. ['X', 'Y']),
  59. ([pyrpn.const.POS_LINEAR, pyrpn.const.RESULT_RGBA, (1024,)],
  60. ['X', 'R', 'G', 'B', 'A']),
  61. ([pyrpn.const.POS_XDIM, pyrpn.const.RESULT_RGB, (4, 2, 2, 640, 480)],
  62. ['D0', 'D1', 'D2', 'D3', 'R', 'G', 'B']),
  63. ]
  64. for args, keys in tests:
  65. rif=pyrpn.RPNIterExpr(*args)
  66. for curkey in keys:
  67. with self.subTest(rif=rif, key=curkey):
  68. expr = rif[curkey]
  69. expt = set(keys)
  70. self.assertEqual(set(rif.keys()), expt)
  71. for key, value in rif.items():
  72. with self.subTest(item=(key, value), id1=id(rif[key]),
  73. id2=id(value), rif=rif):
  74. self.assertEqual(rif[key], value)
  75. def test_assignement(self):
  76. """ Testing RPNIterExpr.__setitem__ """
  77. rif = pyrpn.RPNIterExpr(pyrpn.const.POS_XY, pyrpn.const.RESULT_RGB, (1024,1024))
  78. rif['X'] = 'A0 A1 +'
  79. for _ in range(1000):
  80. a, b = [random.randint(0,0xFFFF) for _ in range(2)]
  81. args = (a,b, 0,0,0)
  82. self.assertEqual(a+b, rif['X'].eval(*args))
  83. self.assertEqual(0, rif['Y'].eval(*args))
  84. if __name__ == '__main__':
  85. unittest.main()