summaryrefslogtreecommitdiff
path: root/support/regression/cases/generate-cases.py
blob: 2fb924b930d17203bee545650c804926af21f740 (plain)
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
from HTMLgen import TemplateDocument
import sys, re, tempfile, os

"""See InstanceGenerator for a description of this file"""

# Globals
# Directory that the generated files should be placed into
outdir = sys.argv[2]

# Start of the test function table definition
testfuntableheader = """
void
__runSuite(void)
{
"""

# End of the test function table definition
testfuntablefooter = """}
"""

# Code to generate the suite function
testfunsuite = """
__code const char *
__getSuiteName(void)
{
  return "{testcase}";
}
""" 

# Utility functions
def createdir(path):
    """Creates a directory if it doesn't exist"""
    if not os.path.isdir(path):
        os.mkdir(path)

class InstanceGenerator:
    """Test case iteration generator.
    Takes the template given as the first argument, pulls out all the meta
    iteration information, and generates an instance for each combination
    of the names and types.

    See doc/test_suite_spec.tex for more information on the template file
    format."""

    def __init__(self, inname):
        self.inname = inname
        # Initalise the replacements hash.
        # Map of name to values.
        self.replacements = []
        # Initalise the function list hash.
        self.functions = []
        # Emit the suite wrapper into a temporary file
        self.tmpname = tempfile.mktemp()
        (self.dirname, self.filename) = os.path.split(self.inname)
        (self.basename, self.ext) = os.path.splitext (self.filename)
        if self.ext == ".in":
            (self.basename, self.ext) = os.path.splitext (self.filename[:-3])

    def permute(self, basename, repl, trans = {}):
        """Permutes across all of the names.  For each value, recursively creates
        a mangled form of the name, this value, and all the combinations of
        the remaining values.  At the tail of the recursion when one full
        combination is built, generates an instance of the test case from
        the template."""
        basepath = os.path.join(outdir, basename)
        if len(repl) == 0:
            # End of the recursion.
            # Set the runtime substitutions.
            trans['testcase'] = re.sub(r'\\', r'\\\\', basename)
            # Create the instance from the template
            T = TemplateDocument(self.tmpname)
            T.substitutions = trans
            T.write(basepath + self.ext)
        else:
            # Pull off this key, then recursively iterate through the rest.
            key = repl[0][0]
            for part in repl[0][1]:
                trans[key] = part
                # Turn a empty string into something decent for a filename
                if not part:
                    part = 'none'
                # Remove any bad characters from the filename.
                part = re.sub(r'\s+', r'_', part)
                # The slice operator (_[1:]) creates a copy of the list missing the
                # first element.
                # Can't use '-' as a seperator due to the mcs51 assembler.
                self.permute(basename + '_' + key + '_' + part, repl[1:], trans)

    def writetemplate(self):
        """Given a template file and a temporary name writes out a verbatim copy
        of the source file and adds the suite table and functions."""
        if sys.version_info[0]<3:
            fout = open(self.tmpname, 'w')
        else:
            fout = open(self.tmpname, 'w', encoding="latin-1")

        for line in self.lines:
            fout.write(line)

        # Emmit the suite table
        fout.write(testfuntableheader)

        n = 0;
        for fun in self.functions:
            # Turn the function definition into a function call
            fout.write("  __prints(\"Running " + fun + "\\n\");\n");
            fout.write('  ' + fun + "();\n")
            n += 1;

        fout.write(testfuntablefooter)
        fout.write("\nconst int __numCases = " + str(n) + ";\n")
        fout.write(testfunsuite);
        
        fout.close()
        return n

    def readfile(self):
        """Read in all of the input file."""
        if sys.version_info[0]<3:
            fin = open(self.inname)
        else:
            fin = open(self.inname, encoding="latin-1")
        self.lines = fin.readlines()
        fin.close()

    def parse(self):
        # Start off in the header.
        inheader = 1;

        # Iterate over the source file and pull out the meta data.
        for line in self.lines:
            line = line.strip()

            # If we are still in the header, see if this is a substitution line
            if inheader:
                # A substitution line has a ':' in it
                if re.search(r':', line) != None:
                    # Split out the name from the values
                    (name, rawvalues) = re.split(r':', line)
                    # Split the values at the commas
                    values = re.split(r',', rawvalues)
                    
                    # Trim the name
                    name = name.strip()
                    # Trim all the values
                    values = [value.strip() for value in values]
                    
                    self.replacements.append((name, values))
                elif re.search(r'\*/', line) != None:
                    # Hit the end of the comments
                    inheader = 0;
                else:
                    # Do nothing.
                    None
            else:
                # Pull out any test function names
                m = re.match(r'^(?:\W*void\W+)?\W*(test\w*)\W*\(\W*void\W*\)', line)
                if m != None:
                    self.functions.append(m.group(1))

    def generate(self):
        """Main function.  Generates all of the instances."""
        self.readfile()
        self.parse()
        if self.writetemplate() == 0:
            sys.stderr.write("Empty function list in " + self.inname + "!\n")

        # Create the output directory if it doesn't exist
        createdir(outdir)

        # Generate
        self.permute(self.basename, self.replacements)

        # Remove the temporary file
        os.remove(self.tmpname)

def main():
    # Check and parse the command line arguments
    if len(sys.argv) < 3:
        print("usage: generate-cases.py template.c outdir")
        sys.exit(-1)
        
    # Input name is the first arg.

    s = InstanceGenerator(sys.argv[1])
    s.generate()

if __name__ == '__main__':
    main()