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