From 50c2e0cec2067ee869e41c702742e40a5a1a94eb Mon Sep 17 00:00:00 2001 From: phk Date: Mon, 11 Aug 2008 08:21:41 +0000 Subject: [PATCH] Remove an spurious include. Fix and extend regression tests while here. Inspired by: solaris patch from Theo Schlossnagle git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@3075 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/lib/libvarnish/num.c | 44 ++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/varnish-cache/lib/libvarnish/num.c b/varnish-cache/lib/libvarnish/num.c index 1447173a..d5d2d360 100644 --- a/varnish-cache/lib/libvarnish/num.c +++ b/varnish-cache/lib/libvarnish/num.c @@ -39,6 +39,11 @@ #include +const char err_miss_num[] = "Missing number"; +const char err_invalid_num[] = "Invalid number"; +const char err_abs_req[] = "Absolute number required"; +const char err_invalid_suff[] = "Invalid suffix"; + const char * str2bytes(const char *p, uintmax_t *r, uintmax_t rel) { @@ -46,11 +51,11 @@ str2bytes(const char *p, uintmax_t *r, uintmax_t rel) char *end; if (p == NULL || *p == '\0') - return ("missing number"); + return (err_miss_num); fval = strtod(p, &end); if (end == p || !isfinite(fval)) - return ("Invalid number"); + return (err_invalid_num); if (*end == '\0') { *r = (uintmax_t)fval; @@ -59,7 +64,7 @@ str2bytes(const char *p, uintmax_t *r, uintmax_t rel) if (end[0] == '%' && end[1] == '\0') { if (rel == 0) - return ("Absolute number required"); + return (err_abs_req); fval *= rel / 100.0; } else { /* accept a space before the multiplier */ @@ -91,15 +96,16 @@ str2bytes(const char *p, uintmax_t *r, uintmax_t rel) fval *= (uintmax_t)1 << 60; ++end; break; - case 'b': case 'B': - ++end; - break; default: break; } + /* [bB] is a generic suffix of no effect */ + if (end[0] == 'b' || end[0] == 'B') + end++; + if (end[0] != '\0') - return ("Invalid suffix"); + return (err_invalid_suff); } *r = (uintmax_t)round(fval); @@ -107,8 +113,9 @@ str2bytes(const char *p, uintmax_t *r, uintmax_t rel) } #ifdef NUM_C_TEST +/* Compile with: "cc -o foo -DNUM_C_TEST -I../.. -I../../include num.c -lm" */ #include -#include +#include #include #include @@ -116,6 +123,7 @@ struct test_case { const char *str; uintmax_t rel; uintmax_t val; + const char *err; } test_cases[] = { { "1", (uintmax_t)0, (uintmax_t)1 }, { "1B", (uintmax_t)0, (uintmax_t)1<<0 }, @@ -150,6 +158,13 @@ struct test_case { { "1%", (uintmax_t)1024, (uintmax_t)10 }, { "2%", (uintmax_t)1024, (uintmax_t)20 }, { "3%", (uintmax_t)1024, (uintmax_t)31 }, + + /* Check the error checks */ + { "", 0, 0, err_miss_num }, + { "m", 0, 0, err_invalid_num }, + { "4%", 0, 0, err_abs_req }, + { "3*", 0, 0, err_invalid_suff }, + /* TODO: add more */ { 0, 0, 0 }, @@ -161,13 +176,18 @@ main(int argc, char *argv[]) struct test_case *tc; uintmax_t val; int ec; + const char *e; (void)argc; for (ec = 0, tc = test_cases; tc->str; ++tc) { - str2bytes(tc->str, &val, tc->rel); - if (val != tc->val) { - printf("%s: str2bytes(\"%s\", %ju) %ju != %ju\n", - *argv, tc->str, tc->rel, val, tc->val); + e = str2bytes(tc->str, &val, tc->rel); + if (e != tc->err) { + printf("%s: str2bytes(\"%s\", %ju) (%s) != (%s)\n", + *argv, tc->str, tc->rel, tc->err, e); + ++ec; + } else if (e == NULL && val != tc->val) { + printf("%s: str2bytes(\"%s\", %ju) %ju != %ju (%s)\n", + *argv, tc->str, tc->rel, val, tc->val, e); ++ec; } } -- 2.39.5