summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Mark Bell <jmb@netsurf-browser.org>2008-11-24 00:56:48 +0000
committerJohn Mark Bell <jmb@netsurf-browser.org>2008-11-24 00:56:48 +0000
commitd8b5dd586db230db3f09bedba9bfc0bdb254fe57 (patch)
treecc03414893089bec421e755efb949a76d470fe6e
parentf235d82dccb499f1af06e7ef3e1d7b9b57c12014 (diff)
downloadlibcss-d8b5dd586db230db3f09bedba9bfc0bdb254fe57.tar.gz
libcss-d8b5dd586db230db3f09bedba9bfc0bdb254fe57.tar.bz2
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
-rw-r--r--include/libcss/types.h3
-rw-r--r--src/utils/utils.h5
-rw-r--r--test/data/number/number.dat102
-rw-r--r--test/number.c12
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 <stdbool.h>
-#include <inttypes.h>
+#include <stdint.h>
+#include <stdlib.h>
/** 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);
}