test: Use complete prototypes in func field of struct test

Function declarators with empty parentheses is an obsolescent feature in
C99, and it is not clear to me that the standard allows assigning
assigning a function pointer declared in this way to a function declared
in prototype-format.

In any case, using a union for the functions is just as simple and
enforces that we pass the correct types to the functions.
This commit is contained in:
Michael Forney 2017-03-07 23:32:44 -08:00 committed by Laslo Hunhold
parent 30fd43d7f3
commit 72b49a065b

73
test.c
View File

@ -115,47 +115,50 @@ binary_nt(char *s1, char *s2)
struct test { struct test {
char *name; char *name;
int (*func)(); union {
int (*u)(char *);
int (*b)(char *, char *);
} func;
}; };
static struct test unary[] = { static struct test unary[] = {
{ "-b", unary_b }, { "-b", { .u = unary_b } },
{ "-c", unary_c }, { "-c", { .u = unary_c } },
{ "-d", unary_d }, { "-d", { .u = unary_d } },
{ "-e", unary_e }, { "-e", { .u = unary_e } },
{ "-f", unary_f }, { "-f", { .u = unary_f } },
{ "-g", unary_g }, { "-g", { .u = unary_g } },
{ "-h", unary_h }, { "-h", { .u = unary_h } },
{ "-k", unary_k }, { "-k", { .u = unary_k } },
{ "-L", unary_h }, { "-L", { .u = unary_h } },
{ "-n", unary_n }, { "-n", { .u = unary_n } },
{ "-p", unary_p }, { "-p", { .u = unary_p } },
{ "-r", unary_r }, { "-r", { .u = unary_r } },
{ "-S", unary_S }, { "-S", { .u = unary_S } },
{ "-s", unary_s }, { "-s", { .u = unary_s } },
{ "-t", unary_t }, { "-t", { .u = unary_t } },
{ "-u", unary_u }, { "-u", { .u = unary_u } },
{ "-w", unary_w }, { "-w", { .u = unary_w } },
{ "-x", unary_x }, { "-x", { .u = unary_x } },
{ "-z", unary_z }, { "-z", { .u = unary_z } },
{ NULL, NULL }, { NULL },
}; };
static struct test binary[] = { static struct test binary[] = {
{ "=" , binary_se }, { "=" , { .b = binary_se } },
{ "!=" , binary_sn }, { "!=" , { .b = binary_sn } },
{ "-eq", binary_eq }, { "-eq", { .b = binary_eq } },
{ "-ne", binary_ne }, { "-ne", { .b = binary_ne } },
{ "-gt", binary_gt }, { "-gt", { .b = binary_gt } },
{ "-ge", binary_ge }, { "-ge", { .b = binary_ge } },
{ "-lt", binary_lt }, { "-lt", { .b = binary_lt } },
{ "-le", binary_le }, { "-le", { .b = binary_le } },
{ "-ef", binary_ef }, { "-ef", { .b = binary_ef } },
{ "-ot", binary_ot }, { "-ot", { .b = binary_ot } },
{ "-nt", binary_nt }, { "-nt", { .b = binary_nt } },
{ NULL, NULL }, { NULL },
}; };
static struct test * static struct test *
@ -191,7 +194,7 @@ twoarg(char *argv[])
return !onearg(argv + 1); return !onearg(argv + 1);
if ((t = find_test(unary, *argv))) if ((t = find_test(unary, *argv)))
return t->func(argv[1]); return t->func.u(argv[1]);
enprintf(2, "bad unary test %s\n", argv[0]); enprintf(2, "bad unary test %s\n", argv[0]);
@ -204,7 +207,7 @@ threearg(char *argv[])
struct test *t = find_test(binary, argv[1]); struct test *t = find_test(binary, argv[1]);
if (t) if (t)
return t->func(argv[0], argv[2]); return t->func.b(argv[0], argv[2]);
if (!strcmp(argv[0], "!")) if (!strcmp(argv[0], "!"))
return !twoarg(argv + 1); return !twoarg(argv + 1);