summaryrefslogtreecommitdiff
path: root/support/regression/cases/mkdrv.py
blob: ea814c750174713bd1dcb35003f96400117838bd (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
from HTMLgen import TemplateDocument
import sys, re, os

"""
Build a test driver for various C tests without main function,
Derived from generate-cases, and far too complicated for the task.
"""

# Globals
# Directory that the generated files should be placed into
infile = sys.argv[1]
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 "%s";
}
""" 

# 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.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 writetemplate(self, fn):
        """Given a template file and a temporary name writes out a verbatim copy
        of the source file and adds the suite table and functions."""

        """print("writing", fn)"""
        if sys.version_info[0]<3:
            fout = open(fn, 'w')
        else:
            fout = open(fn, 'w', encoding="latin-1")

        fout.write('#include "')
        fout.write(infile)
        fout.write('"\n')

        # 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 % ( self.basename + self.ext ));

        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[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()
        outfn = sys.argv[2]
        if self.writetemplate(outfn) == 0:
            sys.stderr.write("Empty function list in " + self.inname + "!\n")

def main():
    # Check and parse the command line arguments
    if len(sys.argv) < 3:
        print("usage: mkdrv.py infile outfile")
        sys.exit(-1)

    # Input name is the first arg.

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

if __name__ == '__main__':
    main()