From d8b5dd586db230db3f09bedba9bfc0bdb254fe57 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Mon, 24 Nov 2008 00:56:48 +0000 Subject: Fix number parsing and make test code automatically determine correctness. More test data, which covers everything. Fix includes in libcss/types.h svn path=/trunk/libcss/; revision=5764 --- include/libcss/types.h | 3 +- src/utils/utils.h | 5 ++- test/data/number/number.dat | 102 ++++++++++++++++++++++++++++++++++++++++---- test/number.c | 12 ++++-- 4 files changed, 107 insertions(+), 15 deletions(-) diff --git a/include/libcss/types.h b/include/libcss/types.h index 0a2df82..4cb8fbc 100644 --- a/include/libcss/types.h +++ b/include/libcss/types.h @@ -9,7 +9,8 @@ #define libcss_types_h_ #include -#include +#include +#include /** Source of charset information, in order of importance * A client-dictated charset will override all others. diff --git a/src/utils/utils.h b/src/utils/utils.h index eead21d..f1ab6d7 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -106,6 +106,7 @@ static inline fixed number_from_css_string(const css_string *string, len--; } fracpart = ((1 << 10) * fracpart + pwr/2) / pwr; + /* Extra paranoid clamp to maximum fractional part */ if (fracpart >= (1 << 10)) fracpart = (1 << 10) - 1; } @@ -113,13 +114,13 @@ static inline fixed number_from_css_string(const css_string *string, /* If the intpart is larger than we can represent, * then clamp to the maximum value we can store. */ if (intpart >= (1 << 21)) { - intpart = (sign == -1) ? (1 << 21) : (1 << 21) - 1; fracpart = (1 << 10) - 1; + intpart = (1 << 21) - 1; } *consumed = ptr - string->ptr; - return FMULI((intpart << 10) | fracpart, sign); + return FMULI(((intpart << 10) | fracpart), sign); } #endif diff --git a/test/data/number/number.dat b/test/data/number/number.dat index 0aeb6d0..a8c9cae 100644 --- a/test/data/number/number.dat +++ b/test/data/number/number.dat @@ -1,48 +1,134 @@ #data 1 #expected -1 +1.000 #reset #data .0 #expected -.0 +0.000 #reset #data .5 #expected -.5 +0.500 #reset #data .999 #expected -.999 +0.999 #reset #data 2097151 #expected -2097151 +2097151.000 #reset +# Test INT_MAX + 1. Note that, in converting the result to float, +# we'll end up with INT_MAX + 1 as the output. #data 2097152 #expected -2097151.999 +2097152.000 #reset #data --2097152 +-1 +#expected +-1.000 +#reset + +#data +-.0 +#expected +0.000 +#reset + +#data +-.5 #expected +-0.500 +#reset + +#data +-.999 +#expected +-0.999 +#reset + +#data +-2097151 +#expected +-2097151.000 +#reset + +#data -2097152 +#expected +-2097152.000 #reset #data -2097153 #expected --2097152.999 +-2097152.000 +#reset + +#data +-x +#expected +0.000 +#reset + +#data ++x +#expected +0.000 +#reset + +#data +x +#expected +0.000 +#reset + +#data +1.x +#expected +1.000 +#reset + +#data +.x +#expected +0.000 +#reset + +#data +- +#expected +0.000 +#reset + +#data ++ +#expected +0.000 +#reset + +#data +0.12345 +#expected +0.123 +#reset + +#data +0.12367 +#expected +0.124 #reset diff --git a/test/number.c b/test/number.c index 87dcbcd..7e793a2 100644 --- a/test/number.c +++ b/test/number.c @@ -52,7 +52,7 @@ int main(int argc, char **argv) /* and run final test */ if (ctx.bufused > 0) - run_test(ctx.buf, ctx.bufused, ctx.exp, ctx.explen); + run_test(ctx.buf, ctx.bufused - 1, ctx.exp, ctx.explen); free(ctx.buf); @@ -69,7 +69,7 @@ bool handle_line(const char *data, size_t datalen, void *pw) if (ctx->inexp) { /* This marks end of testcase, so run it */ - run_test(ctx->buf, ctx->bufused, + run_test(ctx->buf, ctx->bufused - 1, ctx->exp, ctx->explen); ctx->buf[0] = '\0'; @@ -110,13 +110,17 @@ void run_test(const uint8_t *data, size_t len, const char *exp, size_t explen) css_string in = { (uint8_t *) data, len }; size_t consumed; fixed result; + char buf[256]; UNUSED(exp); UNUSED(explen); result = number_from_css_string(&in, &consumed); - /** \todo some kind of verification of the result */ - printf("%d\n", result); + snprintf(buf, sizeof buf, "%.3f", FIXTOFLT(result)); + + printf("got: %s expected: %.*s\n", buf, (int) explen, exp); + + assert(strncmp(buf, exp, explen) == 0); } -- cgit v1.2.3