-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathtstrout.py
222 lines (162 loc) · 6.09 KB
/
tstrout.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#! /usr/bin/env python
# -*- encoding: utf8 -*-
from __future__ import print_function
import os
import sys
import re
TOKEN_BASE0 = 10000
TOKEN_NAME = TOKEN_BASE0 + 1
TOKEN_SPACE = TOKEN_BASE0 + 2
TOKEN_OPERATOR = TOKEN_BASE0 + 3
def tokenize(expr_or_stmt):
tokens_regexps = [
(TOKEN_SPACE, re.compile("[ \t\n\r]+") ),
(TOKEN_NAME, re.compile("[_A-Za-z][_A-Za-z0-9]*") ),
(TOKEN_OPERATOR, re.compile("[-+*/&|]") ),
]
tokens = list()
working_val = expr_or_stmt[:]
while len(working_val) > 0:
print(working_val, len(working_val))
for tok, regexp in tokens_regexps:
matched = regexp.match(working_val)
if matched is None:
continue
if tok != TOKEN_SPACE:
tokens.append((tok, working_val[matched.start():matched.end()]))
working_val = working_val[matched.end():]
if tok == TOKEN_SPACE:
#print("Current length", len(working_val))
continue
break
print(matched, dir(matched))
if matched.pos == 0:
print(matched.endpos)
print(matched.pos)
print(matched.expand)
else:
raise ValueError, repr(working_val)
return tokens
def cplusplus_dtype(arg):
tokens = tokenize(arg)
#print(tokens)
pre_all = tokens[0][1]
name = tokens[-1][1]
if len(tokens) == 2:
pre_each, post_each = "", ""
elif len(tokens) == 3:
pre_each = tokens[1][1]
post_each = ""
else:
raise ValueError, repr(tokens)
return ((pre_all, pre_each, post_each), name)
SHIFTWIDTH = 4
def code_write(fd, indent, s):
fd.write(indent*SHIFTWIDTH*" " + s)
def code_include(fd, filename, system_header=False):
if system_header:
ch_open = "<"
ch_close = ">"
else:
ch_open = '"'
ch_close = '"'
fd.write("#include " + ch_open)
fd.write(filename)
fd.write(ch_close + "\n\n")
def generate_tester(cplusplus_filename):
directory, fname = os.path.split(cplusplus_filename)
routname, extension = fname.split(".")
#
# read C++ file with implementation
fd = open(cplusplus_filename)
data = fd.read()
fd.close()
# find begining of arguments' string
dfn_loc = data.find(routname + "(")
dfn_open_brace_loc = data.find("{", dfn_loc)
# find end of arguments' string
idx = data.find(")", dfn_loc, dfn_open_brace_loc)
while idx > 0:
new_idx = data.find(")", idx+1, dfn_open_brace_loc)
if new_idx == -1: # no other ")" found or the end reached
break
if new_idx > 0: # another ")" found
idx = new_idx
args_str = data[dfn_loc + len(routname) + 1:idx].strip()
#print(args_str)
# parse arguments' string
args = list()
for arg in args_str.split(","):
dtype, name = cplusplus_dtype(arg)
args.append((dtype, name))
# tester routine source file
tester_filename = "t_" + routname + "." + "cc"
tpath = os.path.join(directory, tester_filename)
tfd = open(tpath, "w")
# system headers
code_include(tfd, "cstring", True)
code_include(tfd, "cstdio", True)
# tester routine header
header_filename = os.path.join(directory, routname + ".hpp")
if os.path.exists(header_filename):
code_include(tfd, routname + ".hpp")
indent = 0
# help routine
code_write(tfd, indent, "void\nshow_help(const char *progname) {\n")
indent += 1
code_write(tfd, indent, 'printf("Tester for {}() function with the following arguments:\\n");\n'.format(routname))
for arg in args:
code_write(tfd, indent, "printf({}--{}=<{}>\\n{});\n".format('"', arg[1], arg[0], '"'))
indent -= 1
code_write(tfd, indent, "}\n")
# main() function starts here
code_write(tfd, indent, "int\nmain(int argc, char *argv[]) {\n")
indent += 1
# declare each argument
for arg in args:
# test for passed-by-reference: don't print "&" for these arguments
code_write(tfd, indent, "{} {}{}{}".format(arg[0][0], arg[0][1] if arg[0][1] != "&" else "", arg[1], arg[0][2]))
if arg[0][1] == "*":
tfd.write(" = new {}()".format(arg[0][0])) # call default constractor
tfd.write(";\n")
# if not arguments given, then show help text (cannot use --help because there might be argument called help)
code_write(tfd, indent, "if (argc <= 1 || argv[1] == NULL) {\n")
code_write(tfd, indent+1, "show_help(argv[0]);\n")
code_write(tfd, indent, "}\n")
# loop over command line arguments
code_write(tfd, indent, "for(int i=1; i<argc && argv[i]; ++i) {\n")
indent += 1
for arg in args:
if arg[0][0] in ("int", "local_int_t"):
cmd_arg = "--" + arg[1] + "="
code_write(tfd, indent, "if (strncmp(argv[i], {}{}{}, {})==0) {}\n".format('"', cmd_arg, '"', len(cmd_arg), "{"))
indent += 1
# parse the value of the command argument and store it the C++ variable
code_write(tfd, indent, "if (sscanf(argv[i]+{}, \"%d\", &{}) != 1)\n".format(len(arg[1])+2+1, arg[1]))
indent += 1
# assign 0 to the C++ variable
code_write(tfd, indent, "{} = 0;\n".format(arg[1]))
indent -= 1
indent -= 1
code_write(tfd, indent, "}\n")
indent -= 1
code_write(tfd, indent, "}\n")
code_write(tfd, indent, "{}(".format(routname)) # start the call to the tested function
sep = ""
for arg in args:
tfd.write(sep)
tfd.write(arg[1])
sep = ","
tfd.write(");\n") # end the call to the tested function
for arg in args:
if arg[0][1] == "&": # if this is an argument passed by reference
code_write(tfd, indent, "printf(\"{}=%g\\n\", (double){});\n".format(arg[1], arg[1]))
code_write(tfd, indent, "return 0;\n") # return 0 from main()
indent -= 1
code_write(tfd, indent, "}\n") # end of main()
tfd.close()
def main(argv):
generate_tester(argv[1])
return 0
if "__main__" == __name__:
sys.exit(main(sys.argv))