Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / support / regression / cases / mkdrv.py
blobea814c750174713bd1dcb35003f96400117838bd
1 from HTMLgen import TemplateDocument
2 import sys, re, os
4 """
5 Build a test driver for various C tests without main function,
6 Derived from generate-cases, and far too complicated for the task.
7 """
9 # Globals
10 # Directory that the generated files should be placed into
11 infile = sys.argv[1]
12 outdir = sys.argv[2]
14 # Start of the test function table definition
15 testfuntableheader = """
16 void
17 __runSuite(void)
19 """
21 # End of the test function table definition
22 testfuntablefooter = """}
23 """
25 # Code to generate the suite function
26 testfunsuite = """
27 __code const char *
28 __getSuiteName(void)
30 return "%s";
32 """
34 # Utility functions
35 def createdir(path):
36 """Creates a directory if it doesn't exist"""
37 if not os.path.isdir(path):
38 os.mkdir(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
47 format."""
49 def __init__(self, inname):
50 self.inname = inname
51 # Initalise the replacements hash.
52 # Map of name to values.
53 self.replacements = { }
54 # Initalise the function list hash.
55 self.functions = []
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)
59 if self.ext == ".in":
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:
68 fout = open(fn, 'w')
69 else:
70 fout = open(fn, 'w', encoding="latin-1")
72 fout.write('#include "')
73 fout.write(infile)
74 fout.write('"\n')
76 # Emmit the suite table
77 fout.write(testfuntableheader)
79 n = 0;
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")
84 n += 1;
86 fout.write(testfuntablefooter)
87 fout.write("\nconst int __numCases = " + str(n) + ";\n")
88 fout.write(testfunsuite % ( self.basename + self.ext ));
90 fout.close()
91 return n
93 def readfile(self):
94 """Read in all of the input file."""
95 if sys.version_info[0]<3:
96 fin = open(self.inname)
97 else:
98 fin = open(self.inname, encoding="latin-1")
99 self.lines = fin.readlines()
100 fin.close()
102 def parse(self):
103 # Start off in the header.
104 inheader = 1;
106 # Iterate over the source file and pull out the meta data.
107 for line in self.lines:
108 line = line.strip()
110 # If we are still in the header, see if this is a substitution line
111 if inheader:
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)
119 # Trim the name
120 name = name.strip()
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
127 inheader = 0;
128 else:
129 # Do nothing.
130 None
131 else:
132 # Pull out any test function names
133 m = re.match(r'^(?:\W*void\W+)?\W*(test\w*)\W*\(\W*void\W*\)', line)
134 if m != None:
135 self.functions.append(m.group(1))
137 def generate(self):
138 """Main function. Generates all of the instances."""
139 self.readfile()
140 self.parse()
141 outfn = sys.argv[2]
142 if self.writetemplate(outfn) == 0:
143 sys.stderr.write("Empty function list in " + self.inname + "!\n")
145 def main():
146 # Check and parse the command line arguments
147 if len(sys.argv) < 3:
148 print("usage: mkdrv.py infile outfile")
149 sys.exit(-1)
151 # Input name is the first arg.
153 s = InstanceGenerator(sys.argv[1])
154 s.generate()
156 if __name__ == '__main__':
157 main()