-
Notifications
You must be signed in to change notification settings - Fork 2
/
invert_assertions.py
90 lines (71 loc) · 2.21 KB
/
invert_assertions.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
import re
import os
import sys
def invert_single_assertion(file_string,match,replacement,assertion):
"""Invert an assertion by replacing the old one, using the regex match"""
repl_string = assertion + '(' + match.group(1) + replacement + match.group(3) + ')'
result = file_string[:match.start()] + repl_string + file_string[match.end():]
return re.sub('@expect verified', '@expect error', result)
def fail_suffix(i):
"""Filename suffix for the ith failing file"""
if i == 0:
return '_fail'
else:
return '_fail_' + str(i+1)
def fail_filename(orig_filename,i):
"""Filename for a failing regression"""
parts = orig_filename.split('.')
parts[-2] += fail_suffix(i)
return '.'.join(parts)
def invert_assertions(filename,original="==",replacement="!=",assertion="assert"):
"""Inverts each assertion in a file, writing each one seperately to a new file."""
orig_file = ""
with open(filename, "r") as f:
orig_file = f.read()
regex = assertion + r'\((.*)(' + original + r')(.*)\)'
matches = re.finditer(regex,orig_file)
i = 0
for match in matches:
new_file_str = invert_single_assertion(orig_file,match,replacement,assertion)
with open(fail_filename(filename,i), "w") as f:
f.write(new_file_str)
i += 1
def main():
if len(sys.argv) < 2:
print("Usage: \npython invert_assertions.py [folder_name]")
sys.exit()
reg_list = [
'hello',
'compute',
'function',
'forloop',
'fib',
'compound',
'array',
'pointer',
'method',
'dynamic',
'inout',
'overload',
]
folder = sys.argv[1]
os.chdir(folder)
for src_file in os.listdir('.'): # current directory, which we just changed into
if not os.path.isfile('./' + src_file):
continue
parts = src_file.split('.')
reg_name = parts[-2]
if reg_name in reg_list:
orig = "=="
repl = "!="
sert = "assert"
if parts[-1] in ['f', 'f90', 'f95', 'for', 'f03']: #fortran
repl = "/="
elif parts[-1] in ['rs']: #rust
sert = r'assert!'
if parts[-2] == 'hello':
orig = "true"
repl = "false"
invert_assertions(src_file,orig,repl,sert)
if __name__ == '__main__':
main()