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
|
#!/usr/bin/env python3
"""
Generate C testcases based on the given configuration file.
It can be useful for testing many C functions in the same way, like when
testing a wrapper.
"""
import sys
import configparser
import argparse
from string import Template
def listify(x):
if isinstance(x, str):
return [x]
return x
TEST_SUCCESS_TMPL = r"""
int success(void) {
$prep
fiu_disable("$fp");
$call
if (! ($success_cond) ) {
printf("$fp - success condition is false\n");
return -1;
}
return 0;
}
"""
TEST_FAILURE_TMPL = r"""
static int external_cb_was_called = 0;
int external_cb(const char *name, int *failnum,
void **failinfo, unsigned int *flags) {
external_cb_was_called++;
*failinfo = (void *) $errno_on_fail;
return *failnum;
}
int failure(void) {
$prep
fiu_enable_external("$fp", 1, NULL, 0, external_cb);
$call
fiu_disable("$fp");
if (external_cb_was_called != 1) {
printf("$fp - external callback not invoked\n");
return -1;
}
if (! ($errno_cond) ) {
printf("$fp - errno not set appropriately: ");
printf("errno:%d, cond:$errno_cond\n", errno);
return -1;
}
if (! ($failure_cond) ) {
printf("$fp - failure condition is false\n");
return -1;
}
return 0;
}
"""
TEST_MAIN_TMPL = r"""
int main(void) {
int s, f;
s = success();
f = failure();
return s + f;
}
"""
TEST_SKIPPED_TMPL = r"""
int main(void) {
printf("$fp: skipping test\n");
return 0;
}
"""
def generate(options, outfile):
outfile.write("/* AUTOGENERATED FILE - DO NOT EDIT */\n\n")
outfile.write("#include <fiu.h>\n")
outfile.write("#include <fiu-control.h>\n")
outfile.write("#include <stdio.h>\n")
outfile.write("#include <errno.h>\n")
includes = options.get("include", [])
if isinstance(includes, str):
includes = includes.split()
for i in includes:
outfile.write("#include <%s>\n" % i)
else:
outfile.write("\n\n")
if options['if']:
outfile.write("#if %s\n" % options['if'])
if 'errno_on_fail' in options:
options['errno_cond'] = \
'errno == %s' % options['errno_on_fail']
else:
# Default the cond to true, and set failinfo to 0 in case it's
# used.
options['errno_cond'] = '1'
options['errno_on_fail'] = '0'
outfile.write(Template(TEST_SUCCESS_TMPL).substitute(options))
outfile.write(Template(TEST_FAILURE_TMPL).substitute(options))
outfile.write(Template(TEST_MAIN_TMPL).substitute(options))
if options['if']:
outfile.write("#else\n")
outfile.write(Template(TEST_SKIPPED_TMPL).substitute(options))
outfile.write("#endif\n")
def main():
parser = argparse.ArgumentParser(
description ="Generate C testcases")
parser.add_argument("-c", "--conf", metavar = "C",
required = True, type = argparse.FileType('rt'),
help = "configuration file")
parser.add_argument("-o", "--out", metavar = "F",
required = True,
help = "generated file")
args = parser.parse_args()
# Defaults for the optional configuration parameters.
conf_defaults = {
'include': (),
'prep': '',
'if': '',
# These are C conditions that are always true.
'success_cond': '1',
'failure_cond': '1',
# For errno_on_fail, we have a smarter logic so don't do
# anything.
}
conf = configparser.ConfigParser(conf_defaults)
conf.read_file(args.conf)
section = conf.sections()[0]
outfile = open(args.out, 'w')
generate(dict(conf.items(section)), outfile)
if __name__ == "__main__":
main()
|