1 from HTMLgen
import TemplateDocument
5 Build a test driver for various C tests without main function,
6 Derived from generate-cases, and far too complicated for the task.
10 # Directory that the generated files should be placed into
14 # Start of the test function table definition
15 testfuntableheader
= """
21 # End of the test function table definition
22 testfuntablefooter
= """}
25 # Code to generate the suite function
36 """Creates a directory if it doesn't exist"""
37 if not os
.path
.isdir(path
):
40 class InstanceGenerator
:
41 """Test case iteration generator.
42 Takes the template given as the first argument, pulls out all the meta
43 iteration information, and generates an instance for each combination
44 of the names and types.
46 See doc/test_suite_spec.tex for more information on the template file
49 def __init__(self
, inname
):
51 # Initalise the replacements hash.
52 # Map of name to values.
53 self
.replacements
= { }
54 # Initalise the function list hash.
56 # Emit the suite wrapper into a temporary file
57 (self
.dirname
, self
.filename
) = os
.path
.split(self
.inname
)
58 (self
.basename
, self
.ext
) = os
.path
.splitext (self
.filename
)
60 (self
.basename
, self
.ext
) = os
.path
.splitext (self
.filename
[:-3])
62 def writetemplate(self
, fn
):
63 """Given a template file and a temporary name writes out a verbatim copy
64 of the source file and adds the suite table and functions."""
66 """print("writing", fn)"""
67 if sys
.version_info
[0]<3:
70 fout
= open(fn
, 'w', encoding
="latin-1")
72 fout
.write('#include "')
76 # Emmit the suite table
77 fout
.write(testfuntableheader
)
80 for fun
in self
.functions
:
81 # Turn the function definition into a function call
82 fout
.write(" __prints(\"Running " + fun
+ "\\n\");\n");
83 fout
.write(' ' + fun
+ "();\n")
86 fout
.write(testfuntablefooter
)
87 fout
.write("\nconst int __numCases = " + str(n
) + ";\n")
88 fout
.write(testfunsuite
% ( self
.basename
+ self
.ext
));
94 """Read in all of the input file."""
95 if sys
.version_info
[0]<3:
96 fin
= open(self
.inname
)
98 fin
= open(self
.inname
, encoding
="latin-1")
99 self
.lines
= fin
.readlines()
103 # Start off in the header.
106 # Iterate over the source file and pull out the meta data.
107 for line
in self
.lines
:
110 # If we are still in the header, see if this is a substitution line
112 # A substitution line has a ':' in it
113 if re
.search(r
':', line
) != None:
114 # Split out the name from the values
115 (name
, rawvalues
) = re
.split(r
':', line
)
116 # Split the values at the commas
117 values
= re
.split(r
',', rawvalues
)
121 # Trim all the values
122 values
= [value
.strip() for value
in values
]
124 self
.replacements
[name
] = values
125 elif re
.search(r
'\*/', line
) != None:
126 # Hit the end of the comments
132 # Pull out any test function names
133 m
= re
.match(r
'^(?:\W*void\W+)?\W*(test\w*)\W*\(\W*void\W*\)', line
)
135 self
.functions
.append(m
.group(1))
138 """Main function. Generates all of the instances."""
142 if self
.writetemplate(outfn
) == 0:
143 sys
.stderr
.write("Empty function list in " + self
.inname
+ "!\n")
146 # Check and parse the command line arguments
147 if len(sys
.argv
) < 3:
148 print("usage: mkdrv.py infile outfile")
151 # Input name is the first arg.
153 s
= InstanceGenerator(sys
.argv
[1])
156 if __name__
== '__main__':