1
0

run_tests.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import os
  2. import sys
  3. import time
  4. import subprocess
  5. def test_file(filepath, cpython=False):
  6. if cpython:
  7. return os.system("python " + filepath) == 0
  8. if sys.platform == 'win32':
  9. return os.system("main.exe " + filepath) == 0
  10. else:
  11. return os.system("./main " + filepath) == 0
  12. def test_dir(path):
  13. print("Testing directory:", path)
  14. for filename in sorted(os.listdir(path)):
  15. if not filename.endswith('.py'):
  16. continue
  17. filepath = os.path.join(path, filename)
  18. print("> " + filepath, flush=True)
  19. if path == 'benchmarks/':
  20. _0 = time.time()
  21. if not test_file(filepath, cpython=True):
  22. print('cpython run failed')
  23. continue
  24. _1 = time.time()
  25. if not test_file(filepath): exit(1)
  26. _2 = time.time()
  27. print(f' cpython: {_1 - _0:.6f}s (100%)')
  28. print(f' pocketpy: {_2 - _1:.6f}s ({(_2 - _1) / (_1 - _0) * 100:.2f}%)')
  29. else:
  30. if not test_file(filepath):
  31. print('-' * 50)
  32. print("TEST FAILED!")
  33. exit(1)
  34. # print("TEST FAILED! Press any key to continue...")
  35. # input()
  36. print('CPython:', str(sys.version).replace('\n', ''))
  37. print('System:', '64-bit' if sys.maxsize > 2**32 else '32-bit')
  38. def test_repl():
  39. print("[REPL Test Enabled]")
  40. if sys.platform in ['linux', 'darwin']:
  41. cmd = './main'
  42. else:
  43. cmd = None
  44. if cmd is not None:
  45. res = subprocess.run([cmd], encoding='utf-8', input=r'''
  46. def add(a, b):
  47. return a + b
  48. class A:
  49. def __init__(self, x):
  50. self.x = x
  51. def get(self):
  52. return self.x
  53. print('ans_1:', add(1, 2))
  54. print('ans_2:', A('abc').get())
  55. exit()
  56. ''', capture_output=True, check=True)
  57. res.check_returncode()
  58. # assert 'ans_1: 3' in res.stdout, res.stdout
  59. if 'ans_1: 3' not in res.stdout:
  60. print(res.stdout)
  61. exit(1)
  62. # assert 'ans_2: abc' in res.stdout, res.stdout
  63. if 'ans_2: abc' not in res.stdout:
  64. print(res.stdout)
  65. exit(1)
  66. if len(sys.argv) == 2:
  67. assert 'benchmark' in sys.argv[1]
  68. test_dir('benchmarks/')
  69. else:
  70. test_dir('tests/')
  71. test_repl()
  72. print("ALL TESTS PASSED")