1 from HTMLgen import TemplateDocument
2 import sys, re, tempfile, os
4 """See InstanceGenerator for a description of this file"""
7 # Directory that the generated files should be placed into
10 # Start of the test function table definition
11 testfuntableheader = """
17 # End of the test function table definition
18 testfuntablefooter = """}
21 # Code to generate the suite function
32 """Removes all white space from the start and the end of a string.
33 Like java.lang.String.trim"""
34 ret = chomp(re.sub(r'^\s+', '', a))
38 """Removes all white space from the end of a string.
40 return re.sub(r'\s+$', '', a)
43 """Creates a directory if it doesn't exist"""
44 if not os.path.isdir(path):
47 class InstanceGenerator:
48 """Test case iteration generator.
49 Takes the template given as the first argument, pulls out all the meta
50 iteration information, and generates an instance for each combination
51 of the names and types.
53 See doc/test_suite_spec.tex for more information on the template file
56 def __init__(self, inname):
58 # Initalise the replacements hash.
59 # Map of name to values.
60 self.replacements = { }
61 # Initalise the function list hash.
63 # Emit the suite wrapper into a temporary file
64 self.tmpname = tempfile.mktemp()
65 (self.dirname, self.filename) = os.path.split(self.inname)
66 (self.basename, self.ext) = os.path.splitext (self.filename)
68 def permute(self, basepath, keys, trans = {}):
69 """Permutes across all of the names. For each value, recursivly creates
70 a mangled form of the name, this value, and all the combinations of
71 the remaining values. At the tail of the recursion when one full
72 combination is built, generates an instance of the test case from
75 # End of the recursion.
76 # Set the runtime substitutions.
77 trans['testcase'] = re.sub(r'\\', r'\\\\', basepath)
78 # Create the instance from the template
79 T = TemplateDocument(self.tmpname)
80 T.substitutions = trans
81 T.write(basepath + self.ext)
83 # Pull off this key, then recursivly iterate through the rest.
85 for part in self.replacements[key]:
87 # Turn a empty string into something decent for a filename
90 # Remove any bad characters from the filename.
91 part = re.sub(r'\s+', r'_', part)
92 # The slice operator (keys[1:]) creates a copy of the list missing the
94 # Can't use '-' as a seperator due to the mcs51 assembler.
95 self.permute(basepath + '_' + key + '_' + part, keys[1:], trans)
97 def writetemplate(self):
98 """Given a template file and a temporary name writes out a verbatim copy
99 of the source file and adds the suite table and functions."""
100 fout = open(self.tmpname, 'w')
102 for line in self.lines:
105 # Emmit the suite table
106 fout.write(testfuntableheader)
109 for fun in self.functions:
110 # Turn the function definition into a pointer
111 fun = re.sub(r'\(\w+\)', '', fun)
112 fout.write(" __prints(\"Running " + fun + "\\n\");\n");
113 fout.write(' ' + fun + "();\n")
116 fout.write(testfuntablefooter)
117 fout.write("\nconst int __numCases = " + str(n) + ";\n")
118 fout.write(testfunsuite);
123 """Read in all of the input file."""
124 fin = open(self.inname)
125 self.lines = fin.readlines()
129 # Start off in the header.
132 # Iterate over the source file and pull out the meta data.
133 for line in self.lines:
136 # If we are still in the header, see if this is a substitution line
138 # A substitution line has a ':' in it
139 if re.search(r':', line) != None:
140 # Split out the name from the values
141 (name, rawvalues) = re.split(r':', line)
142 # Split the values at the commas
143 values = re.split(r',', rawvalues)
147 # Trim all the values
148 values = map(trim, values)
150 self.replacements[name] = values
151 elif re.search(r'\*/', line) != None:
152 # Hit the end of the comments
158 # Pull out any test function names
159 if re.search(r'^test\w+\(\w+\)', line) != None:
160 self.functions.append(line)
163 """Main function. Generates all of the instances."""
168 # Create the output directory if it doesn't exist
172 self.permute(os.path.join(outdir, self.basename), self.replacements.keys())
174 # Remove the temporary file
175 os.remove(self.tmpname)
178 # Check and parse the command line arguments
179 if len(sys.argv) < 3:
180 print "usage: generate-cases.py template.c outdir"
183 # Input name is the first arg.
185 s = InstanceGenerator(sys.argv[1])
188 if __name__ == '__main__':