test_examples.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import os
  2. import sys
  3. from pathlib import Path
  4. import importlib.util
  5. import pytest
  6. import tempfile
  7. import shutil
  8. EXAMPLES_SCRIPTS_PATH = os.path.join(Path(__file__).parent.parent, "examples", "pke")
  9. def importhelper(path, modulename):
  10. spec = importlib.util.spec_from_file_location(
  11. modulename, os.path.join(path, modulename + ".py")
  12. )
  13. module = importlib.util.module_from_spec(spec)
  14. sys.modules[modulename] = module
  15. spec.loader.exec_module(module)
  16. return module
  17. @pytest.mark.parametrize(
  18. "raw_modulename",
  19. [
  20. "simple-ckks-bootstrapping.py",
  21. "simple-integers-serial-bgvrns.py",
  22. "function-evaluation.py",
  23. "advanced-real-numbers-128.py",
  24. "simple-integers-bgvrns.py",
  25. "simple-integers-serial.py",
  26. "polynomial-evaluation.py",
  27. "scheme-switching.py",
  28. "tckks-interactive-mp-bootstrapping.py",
  29. "advanced-real-numbers.py",
  30. "threshold-fhe-5p.py",
  31. "simple-integers.py",
  32. "simple-real-numbers-serial.py",
  33. "iterative-ckks-bootstrapping.py",
  34. "tckks-interactive-mp-bootstrapping-Chebyschev.py",
  35. "simple-real-numbers.py",
  36. "threshold-fhe.py",
  37. "pre-buffer.py",
  38. ],
  39. )
  40. def test_run_scripts(raw_modulename):
  41. with tempfile.TemporaryDirectory() as td:
  42. os.mkdir(td + "/demoData")
  43. modulename_py = raw_modulename.replace("-", "_")
  44. shutil.copyfile(
  45. os.path.join(EXAMPLES_SCRIPTS_PATH, raw_modulename),
  46. os.path.join(td, modulename_py),
  47. )
  48. sys.path.insert(0, td)
  49. modulename = modulename_py.split(".")[0]
  50. print(f"-*- running module {modulename} -*-")
  51. module = importhelper(td, modulename)
  52. module.main()