-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrun_tests.py
122 lines (91 loc) · 3.43 KB
/
run_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
################################################################################
# Copyright (c) 2021 AVL List GmbH and others
#
# This program and the accompanying materials are made available under the
# terms of the Apache Software License 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: Apache-2.0
################################################################################
import argparse
import os
import shutil
import stat
import subprocess
import sys
import time
import numpy as np
def _parse_args():
parser = argparse.ArgumentParser(
description="Runs examples and compares results with the reference files"
)
parser.add_argument("executable", help="path to the OpenMCx executable")
parser.add_argument("examples", help="path to the folder with the test examples")
args = parser.parse_args()
return args
def _clear_readonly_bit(func, path, exc_info):
os.chmod(path, stat.S_IWRITE)
func(path)
def _run_tests(exe, test_dir):
exe_abs_path = os.path.abspath(exe)
test_dir_abs_path = os.path.abspath(test_dir)
test_examples = [example for example in os.listdir(test_dir_abs_path) if os.path.isfile(os.path.join(test_dir_abs_path, example, "model.ssd"))]
num_tests = len(test_examples)
num_failed = 0
work_dir = os.path.join(os.getcwd(), "workdir")
if os.path.exists(work_dir):
shutil.rmtree(work_dir, onerror=_clear_readonly_bit)
num_tries = 10
while True:
try:
os.makedirs(work_dir)
break
except OSError:
num_tries -= 1
if num_tries == 0:
raise
time.sleep(0.1)
print("Number of found test examples: {}".format(num_tests))
for example in test_examples:
cwd = os.path.join(work_dir, example)
os.makedirs(cwd)
os.chdir(cwd)
print("Running {}".format(example))
input_file = os.path.join(test_dir_abs_path, example, "model.ssd")
ret_val = subprocess.Popen([exe_abs_path, '-v', input_file]).wait()
if ret_val != 0:
num_failed += 1
print("\tFAIL")
continue
# check reference files
test_passed = True
ref_dir = os.path.join(test_dir_abs_path, example, "reference")
for res in os.listdir(ref_dir):
if not res.endswith(".csv"):
continue
res_path = os.path.join("results", res)
ref_path = os.path.join(ref_dir, res)
if not os.path.exists(res_path):
print("Results {} are missing".format(res))
test_passed = False
res_data = np.genfromtxt(res_path, delimiter=',', skip_header=3)
ref_data = np.genfromtxt(ref_path, delimiter=',', skip_header=3)
diff_data = ref_data - res_data
for i, j in np.ndindex(diff_data.shape):
if abs(diff_data[i, j]) > 1e-8:
print("Results {} do not match".format(res))
test_passed = False
break
if test_passed:
print("\tSUCCESS")
else:
num_failed += 1
print("\tFAIL")
print("Tests ran: {}".format(num_tests))
print("Tests failed: {}".format(num_failed))
return num_failed
def main():
args = _parse_args()
return _run_tests(args.executable, args.examples)
if __name__ == "__main__":
sys.exit(main())