run_tests.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import os
  2. import sys
  3. import time
  4. class WorkDir:
  5. def __init__(self, next):
  6. self.prev = os.getcwd()
  7. self.next = next
  8. def __enter__(self):
  9. os.chdir(self.next)
  10. def __exit__(self, *args, **kwargs):
  11. os.chdir(self.prev)
  12. def test_file(filepath, cpython=False):
  13. if cpython:
  14. x, y = os.path.split(filepath)
  15. with WorkDir(x):
  16. return os.system("python3 " + y) == 0
  17. if sys.platform == 'win32':
  18. return os.system("main.exe " + filepath) == 0
  19. else:
  20. return os.system("./main " + filepath) == 0
  21. def test_dir(path):
  22. print("Testing directory:", path)
  23. for filename in sorted(os.listdir(path)):
  24. if not filename.endswith('.py'):
  25. continue
  26. filepath = os.path.join(path, filename)
  27. print("> " + filepath, flush=True)
  28. if path == 'benchmarks/':
  29. _0 = time.time()
  30. if not test_file(filepath, cpython=True):
  31. print('cpython run failed')
  32. continue
  33. _1 = time.time()
  34. if not test_file(filepath): exit(1)
  35. _2 = time.time()
  36. print(f' cpython: {_1 - _0:.6f}s (100%)')
  37. print(f' pocketpy: {_2 - _1:.6f}s ({(_2 - _1) / (_1 - _0) * 100:.2f}%)')
  38. else:
  39. if not test_file(filepath):
  40. print('-' * 50)
  41. print("TEST FAILED! Press any key to continue...")
  42. input()
  43. print('CPython:', str(sys.version).replace('\n', ''))
  44. print('System:', '64-bit' if sys.maxsize > 2**32 else '32-bit')
  45. if len(sys.argv) == 2:
  46. assert 'benchmark' in sys.argv[1]
  47. d = 'benchmarks/'
  48. else:
  49. d = 'tests/'
  50. test_dir(d)
  51. print("ALL TESTS PASSED")