From: Rusty Russell Date: Tue, 5 Oct 2010 07:00:30 +0000 (+1030) Subject: opt: remove unused debug function and code, test a few more corner cases. X-Git-Url: https://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=f20e4f235ac89f3901f248f91c3ac5a33088349d opt: remove unused debug function and code, test a few more corner cases. --- diff --git a/ccan/opt/opt.c b/ccan/opt/opt.c index aa85325d..a5180706 100644 --- a/ccan/opt/opt.c +++ b/ccan/opt/opt.c @@ -41,8 +41,6 @@ static const char *first_opt(unsigned *i, unsigned *len) static const char *next_opt(const char *p, unsigned *i, unsigned *len) { - if (!p) - (*i)++; for (; *i < opt_count; (*i)++) { if (opt_table[*i].flags == OPT_SUBTABLE) continue; @@ -259,13 +257,6 @@ static void parse_fail(void (*errlog)(const char *fmt, ...), strcspn(longopt, "/"), longopt, problem); } -void dump_optstate(void); -void dump_optstate(void) -{ - printf("opterr = %i, optind = %i, optopt = %i, optarg = %s\n", - opterr, optind, optopt, optarg); -} - /* Parse your arguments. */ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...)) { diff --git a/ccan/opt/test/run-helpers.c b/ccan/opt/test/run-helpers.c index f8041ead..48b7cd8b 100644 --- a/ccan/opt/test/run-helpers.c +++ b/ccan/opt/test/run-helpers.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "utils.h" /* We don't actually want it to exit... */ @@ -48,7 +49,7 @@ static int saved_printf(const char *fmt, ...) /* Test helpers. */ int main(int argc, char *argv[]) { - plan_tests(88); + plan_tests(96); /* opt_set_bool */ { @@ -66,6 +67,9 @@ int main(int argc, char *argv[]) ok1(!arg); ok1(parse_args(&argc, &argv, "-b", "true", NULL)); ok1(arg); + ok1(!parse_args(&argc, &argv, "-b", "unknown", NULL)); + ok1(arg); + ok1(strstr(err_output, ": -b: Invalid argument 'unknown'")); } /* opt_set_invbool */ { @@ -84,6 +88,9 @@ int main(int argc, char *argv[]) ok1(arg); ok1(parse_args(&argc, &argv, "-b", "true", NULL)); ok1(!arg); + ok1(!parse_args(&argc, &argv, "-b", "unknown", NULL)); + ok1(!arg); + ok1(strstr(err_output, ": -b: Invalid argument 'unknown'")); } /* opt_set_charp */ { @@ -122,6 +129,17 @@ int main(int argc, char *argv[]) ok1(arg == 0); ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL)); ok1(!parse_args(&argc, &argv, "-a", "4294967296", NULL)); + if (ULONG_MAX == UINT_MAX) { + pass("Can't test overflow"); + pass("Can't test error message"); + } else { + char buf[30]; + sprintf(buf, "%lu", ULONG_MAX); + ok1(!parse_args(&argc, &argv, "-a", buf, NULL)); + ok1(strstr(err_output, ": -a: value '") + && strstr(err_output, buf) + && strstr(err_output, "' does not fit into an integer")); + } } /* opt_set_longval */ {