run_tests.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import os
  2. import sys
  3. import time
  4. def test_file(filepath, cpython=False):
  5. if cpython:
  6. with open(filepath, 'rt') as f:
  7. text = f.read().replace('++', '+=1').replace('--', '-=1')
  8. with open("tmp.py", 'wt') as f:
  9. f.write(text)
  10. x = os.system("python3 tmp.py") == 0
  11. os.remove("tmp.py")
  12. return x
  13. if sys.platform == 'win32':
  14. return os.system("pocketpy.exe " + filepath) == 0
  15. else:
  16. return os.system("./pocketpy " + filepath) == 0
  17. def test_dir(path):
  18. print("Testing directory:", path)
  19. for filename in os.listdir(path):
  20. if not filename.endswith('.py'):
  21. continue
  22. filepath = os.path.join(path, filename)
  23. print("> " + filepath, flush=True)
  24. if path == 'benchmarks/':
  25. _0 = time.time()
  26. if not test_file(filepath, cpython=True): exit(1)
  27. _1 = time.time()
  28. if not test_file(filepath): exit(1)
  29. _2 = time.time()
  30. print(f' cpython: {_1 - _0:.6f}s (100%)')
  31. print(f' pocketpy: {_2 - _1:.6f}s ({(_2 - _1) / (_1 - _0) * 100:.2f}%)')
  32. else:
  33. if not test_file(filepath): exit(1)
  34. if len(sys.argv) == 2:
  35. assert 'benchmark' in sys.argv[1]
  36. d = 'benchmarks/'
  37. else:
  38. d = 'tests/'
  39. test_dir(d)
  40. print("ALL TESTS PASSED")