X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fopt%2Ftest%2Frun-helpers.c;h=589962fe4d8fadaffe6dc4cafd2764b2b5838a22;hb=b3ae89992ecaffed31dcc2e912539c289478801a;hp=d0dc52d642eada4ee6303aed9626a04dcf205c92;hpb=bbdf3ef3c2c14e515388c6146fd00557cee905a1;p=ccan diff --git a/ccan/opt/test/run-helpers.c b/ccan/opt/test/run-helpers.c index d0dc52d6..589962fe 100644 --- a/ccan/opt/test/run-helpers.c +++ b/ccan/opt/test/run-helpers.c @@ -27,13 +27,6 @@ static void *saved_malloc(size_t size); #include #include -static void reset_options(void) -{ - free(opt_table); - opt_table = NULL; - opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0; -} - static char *output = NULL; static int saved_vprintf(const char *fmt, va_list ap) @@ -79,6 +72,18 @@ static void *saved_malloc(size_t size) return last_allocation = malloc(size); } +static void set_args(int *argc, char ***argv, ...) +{ + 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[]) { @@ -399,7 +404,7 @@ int main(int argc, char *argv[]) } else fail("FIXME: Handle other long long int" - " sizes (specifically %lu bytes)", + " sizes (specifically %zu bytes)", sizeof(long long)); } /* opt_set_ulonglongval_bi */ @@ -566,19 +571,19 @@ int main(int argc, char *argv[]) char buf[OPT_SHOW_LEN+2] = { 0 }; buf[OPT_SHOW_LEN] = '!'; i = 7777; - opt_show_ulonglongval_bi(buf, &i); + opt_show_ulonglongval_bi(buf, (unsigned long long *)&i); ok1(strcmp(buf, "7777") == 0); ok1(buf[OPT_SHOW_LEN] == '!'); i = 10240000 * k; - opt_show_ulonglongval_bi(buf, &i); + opt_show_ulonglongval_bi(buf, (unsigned long long *)&i); ok1(strcmp(buf, "10000M") == 0); ok1(buf[OPT_SHOW_LEN] == '!'); i = 5 * P; - opt_show_ulonglongval_bi(buf, &i); + opt_show_ulonglongval_bi(buf, (unsigned long long *)&i); ok1(strcmp(buf, "5P") == 0); ok1(buf[OPT_SHOW_LEN] == '!'); i = 1024 * P; - opt_show_ulonglongval_bi(buf, &i); + opt_show_ulonglongval_bi(buf, (unsigned long long *)&i); ok1(strcmp(buf, "1E") == 0); ok1(buf[OPT_SHOW_LEN] == '!'); } @@ -652,7 +657,7 @@ int main(int argc, char *argv[]) /* opt_set_ulongval_si */ { - unsigned long long int arg = 1000; + unsigned long int arg = 1000; reset_options(); opt_register_arg("-a", opt_set_ulongval_si, NULL, @@ -677,7 +682,7 @@ int main(int argc, char *argv[]) ok1(arg == 1 * G); ok1(!parse_args(&argc, &argv, "-a", "-1G", NULL)); ok1(parse_args(&argc, &argv, "-a", "4G", NULL)); - ok1(arg == 4000000000); + ok1(arg == 4000000000U); if (sizeof(long) == 4){ ok1(!parse_args(&argc, &argv, "-a", "4294967296", NULL)); ok1(!parse_args(&argc, &argv, "-a", "4295M", NULL)); @@ -784,7 +789,7 @@ int main(int argc, char *argv[]) } else fail("FIXME: Handle other long long int" - " sizes (specifically %lu bytes)", + " sizes (specifically %zu bytes)", sizeof(long long)); } @@ -950,19 +955,19 @@ int main(int argc, char *argv[]) char buf[OPT_SHOW_LEN+2] = { 0 }; buf[OPT_SHOW_LEN] = '!'; i = 7777; - opt_show_ulonglongval_si(buf, &i); + opt_show_ulonglongval_si(buf, (unsigned long long *)&i); ok1(strcmp(buf, "7777") == 0); ok1(buf[OPT_SHOW_LEN] == '!'); i = 10240000 * k; - opt_show_ulonglongval_si(buf, &i); + opt_show_ulonglongval_si(buf, (unsigned long long *)&i); ok1(strcmp(buf, "10240M") == 0); ok1(buf[OPT_SHOW_LEN] == '!'); i = 5 * P; - opt_show_ulonglongval_si(buf, &i); + opt_show_ulonglongval_si(buf, (unsigned long long *)&i); ok1(strcmp(buf, "5P") == 0); ok1(buf[OPT_SHOW_LEN] == '!'); i = 1000 * P; - opt_show_ulonglongval_si(buf, &i); + opt_show_ulonglongval_si(buf, (unsigned long long *)&i); ok1(strcmp(buf, "1E") == 0); ok1(buf[OPT_SHOW_LEN] == '!'); } @@ -992,11 +997,7 @@ int main(int argc, char *argv[]) /* parse_args allocates argv */ free(argv); - argc = 2; - argv = malloc(sizeof(argv[0]) * 3); - argv[0] = "thisprog"; - argv[1] = "-a"; - argv[2] = NULL; + set_args(&argc, &argv, "thisprog", "-a", NULL); exitval = setjmp(exited); if (exitval == 0) { @@ -1018,11 +1019,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] = "thisprog"; - argv[1] = "-a"; - argv[2] = NULL; + set_args(&argc, &argv, "thisprog", "-a", NULL); exitval = setjmp(exited); if (exitval == 0) { @@ -1157,11 +1154,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] = "thisprog"; - argv[1] = "--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")); @@ -1176,11 +1169,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] = "thisprog"; - argv[1] = "--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);