X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fopt%2Ftest%2Frun-helpers.c;h=10b241908add534cd0f816457214debe28be1fea;hb=5d628b63760d38f7d5094141d019f4ab83546690;hp=8cf229cc6acc230fd6e32bd794938f24c967d141;hpb=ac9d55d8c5af9697be8c4dd4f27de61e3cb8bf95;p=ccan diff --git a/ccan/opt/test/run-helpers.c b/ccan/opt/test/run-helpers.c index 8cf229cc..10b24190 100644 --- a/ccan/opt/test/run-helpers.c +++ b/ccan/opt/test/run-helpers.c @@ -19,9 +19,6 @@ static int saved_fprintf(FILE *ignored, const char *fmt, ...); #define vfprintf(f, fmt, ap) saved_vprintf(fmt, ap) static int saved_vprintf(const char *fmt, va_list ap); -#define malloc(size) saved_malloc(size) -static void *saved_malloc(size_t size); - #include #include #include @@ -65,17 +62,22 @@ static int saved_fprintf(FILE *ignored, const char *fmt, ...) return ret; } -#undef malloc -static void *last_allocation; -static void *saved_malloc(size_t size) +static void set_args(int *argc, char ***argv, ...) { - return last_allocation = malloc(size); + va_list ap; + *argv = malloc(sizeof(**argv) * 20); + + va_start(ap, argv); + for (*argc = 0; + ((*argv)[*argc] = va_arg(ap, char*)) != NULL; + (*argc)++); + va_end(ap); } /* Test helpers. */ int main(int argc, char *argv[]) { - plan_tests(452); + plan_tests(454); /* opt_set_bool */ { @@ -120,7 +122,7 @@ int main(int argc, char *argv[]) } /* opt_set_charp */ { - char *arg = (char *)"wrong"; + char *arg = cast_const(char *, "wrong"); reset_options(); opt_register_arg("-a", opt_set_charp, NULL, &arg, "All"); ok1(parse_args(&argc, &argv, "-a", "string", NULL)); @@ -985,11 +987,7 @@ int main(int argc, char *argv[]) /* parse_args allocates argv */ free(argv); - argc = 2; - argv = malloc(sizeof(argv[0]) * 3); - argv[0] = (char *)"thisprog"; - argv[1] = (char *)"-a"; - argv[2] = NULL; + set_args(&argc, &argv, "thisprog", "-a", NULL); exitval = setjmp(exited); if (exitval == 0) { @@ -997,6 +995,8 @@ int main(int argc, char *argv[]) fail("opt_show_version_and_exit returned?"); } else { ok1(exitval - 1 == 0); + /* We should have freed table!. */ + ok1(opt_table == NULL); } ok1(strcmp(output, "1.2.3\n") == 0); free(output); @@ -1011,11 +1011,7 @@ int main(int argc, char *argv[]) opt_register_noarg("-a", opt_usage_and_exit, "[args]", ""); - argc = 2; - argv = malloc(sizeof(argv[0]) * 3); - argv[0] = (char *)"thisprog"; - argv[1] = (char *)"-a"; - argv[2] = NULL; + set_args(&argc, &argv, "thisprog", "-a", NULL); exitval = setjmp(exited); if (exitval == 0) { @@ -1023,14 +1019,14 @@ int main(int argc, char *argv[]) fail("opt_usage_and_exit returned?"); } else { ok1(exitval - 1 == 0); + /* We should have freed table!. */ + ok1(opt_table == NULL); } ok1(strstr(output, "[args]")); ok1(strstr(output, argv[0])); - ok1(strstr(output, "[-a]")); + ok1(strstr(output, "\n-a")); free(output); free(argv); - /* It exits without freeing usage string. */ - free(last_allocation); output = NULL; } @@ -1150,11 +1146,7 @@ int main(int argc, char *argv[]) opt_register_noarg("-a", opt_usage_and_exit, "[args]", ""); - argc = 2; - argv = malloc(sizeof(argv[0]) * 3); - argv[0] = (char *)"thisprog"; - argv[1] = (char *)"--garbage"; - argv[2] = NULL; + set_args(&argc, &argv, "thisprog", "--garbage", NULL); ok1(!opt_parse(&argc, argv, opt_log_stderr)); ok1(!strcmp(output, "thisprog: --garbage: unrecognized option\n")); @@ -1169,11 +1161,7 @@ int main(int argc, char *argv[]) reset_options(); opt_register_noarg("-a", opt_usage_and_exit, "[args]", ""); - argc = 2; - argv = malloc(sizeof(argv[0]) * 3); - argv[0] = (char *)"thisprog"; - argv[1] = (char *)"--garbage"; - argv[2] = NULL; + set_args(&argc, &argv, "thisprog", "--garbage", NULL); exitval = setjmp(exited); if (exitval == 0) { opt_parse(&argc, argv, opt_log_stderr_exit);