aboutsummaryrefslogtreecommitdiffstats
path: root/tests/make_catalog.py
blob: 96f9b23e570dc7bd6518c6d2f0751074586d8960 (plain) (blame)
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
#!/usr/bin/env python

import re
import sys

class TestCase(object):
    def __init__(self, function, plugins, desc):
        self.function = function
        self.plugins  = plugins
        self.desc     = desc

    def __str__(self):
        return 'TESTCASE(%s, %s, "%s")' % \
            (self.function, " | ".join(self.plugins), self.desc)


class Finder(object):
    def __init__(self):
        self.found = []
        self.regex = re.compile(
            "TESTCASE\\("
            "\\s*([a-zA-z_0-9]+)\\s*,"    # function name
            "\\s*([- |a-zA-z_0-9]+)\\s*," # plugins
            "\\s*\"([^\"]*)\"\\s*"        # description
            "\\)"
            , re.MULTILINE)

    def search_file(self, fname):
        f = open(fname, "r")
        src = ''.join(f)
        f.close()
        matches = self.regex.findall(src)
        del src

        for m in matches:
            name    = m[0]
            plugins = m[1].split('|')
            desc    = m[2]
            plugins = [p.strip() for p in plugins]
            self.found.append(TestCase(name, plugins, desc))


def search_files(args=sys.argv[1:]):
    f = Finder()
    for fname in args:
        try:
            f.search_file(fname)
        except IOError, msg:
            sys.stderr.write("%s: %s\n" % (fname, msg))
            sys.exit(1)
    return f.found


def create_tc_tables(out=sys.stdout):
    def _(o):
        out.write("%s\n" % str(o))

    tests   = search_files()

    plugins = set()
    for tc in tests:
        for p in tc.plugins:
            plugins.add(p)

    plugins.discard('ALL')
    plugins.discard('LITMUS')

    _('#include "tests.h"')

    for tc in tests:
        _('void test_%s(void);' % tc.function)

    _('struct testcase test_catalog[] = {')
    for tc in tests:
        _('\t{test_%s, "%s"},' % (tc.function, tc.desc))
    _('};')

    for p in plugins:
        count = 0
        _('int %s_TESTS[] = {' % p)
        for (i, tc) in enumerate(tests):
            if p in tc.plugins or \
                    'ALL' in tc.plugins or \
                    'LITMUS' in tc.plugins and p != 'LINUX':
                _('\t%d,' % i)
                count += 1
        _('};')
        _('#define NUM_%s_TESTS %d' % (p, count))

    _('struct testsuite testsuite[] = {')
    for p in plugins:
        _('\t{"%s", %s_TESTS, NUM_%s_TESTS},' % (p.replace('_', '-'), p, p))
    _('};')
    _('#define NUM_PLUGINS %s' % len(plugins))

if __name__ == '__main__':
    create_tc_tables()