summaryrefslogtreecommitdiff
path: root/test/number.c
diff options
context:
space:
mode:
authorJohn Mark Bell <jmb@netsurf-browser.org>2008-11-23 10:29:16 +0000
committerJohn Mark Bell <jmb@netsurf-browser.org>2008-11-23 10:29:16 +0000
commitf235d82dccb499f1af06e7ef3e1d7b9b57c12014 (patch)
tree12db32dbd27a0eb28fa24be089b2d1aa80a3f1b1 /test/number.c
parenta3b7632e61981a730a8883a13f36f19be75647f1 (diff)
downloadlibcss-f235d82dccb499f1af06e7ef3e1d7b9b57c12014.tar.gz
libcss-f235d82dccb499f1af06e7ef3e1d7b9b57c12014.tar.bz2
Some kind of testsuite for number parsing.
It appears that negative values are broken, at least. svn path=/trunk/libcss/; revision=5763
Diffstat (limited to 'test/number.c')
-rw-r--r--test/number.c122
1 files changed, 122 insertions, 0 deletions
diff --git a/test/number.c b/test/number.c
new file mode 100644
index 0000000..87dcbcd
--- /dev/null
+++ b/test/number.c
@@ -0,0 +1,122 @@
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "utils/utils.h"
+
+#include "testutils.h"
+
+typedef struct line_ctx {
+ size_t buflen;
+ size_t bufused;
+ uint8_t *buf;
+
+ size_t explen;
+ char exp[256];
+
+ bool indata;
+ bool inexp;
+} line_ctx;
+
+static bool handle_line(const char *data, size_t datalen, void *pw);
+static void run_test(const uint8_t *data, size_t len,
+ const char *exp, size_t explen);
+
+int main(int argc, char **argv)
+{
+ line_ctx ctx;
+
+ if (argc != 3) {
+ printf("Usage: %s <aliases_file> <filename>\n", argv[0]);
+ return 1;
+ }
+
+ ctx.buflen = parse_filesize(argv[2]);
+ if (ctx.buflen == 0)
+ return 1;
+
+ ctx.buf = malloc(ctx.buflen);
+ if (ctx.buf == NULL) {
+ printf("Failed allocating %u bytes\n",
+ (unsigned int) ctx.buflen);
+ return 1;
+ }
+
+ ctx.buf[0] = '\0';
+ ctx.bufused = 0;
+ ctx.explen = 0;
+ ctx.indata = false;
+ ctx.inexp = false;
+
+ assert(parse_testfile(argv[2], handle_line, &ctx) == true);
+
+ /* and run final test */
+ if (ctx.bufused > 0)
+ run_test(ctx.buf, ctx.bufused, ctx.exp, ctx.explen);
+
+ free(ctx.buf);
+
+ printf("PASS\n");
+
+ return 0;
+}
+
+bool handle_line(const char *data, size_t datalen, void *pw)
+{
+ line_ctx *ctx = (line_ctx *) pw;
+
+ if (data[0] == '#') {
+ if (ctx->inexp) {
+ /* This marks end of testcase, so run it */
+
+ run_test(ctx->buf, ctx->bufused,
+ ctx->exp, ctx->explen);
+
+ ctx->buf[0] = '\0';
+ ctx->bufused = 0;
+
+ ctx->explen = 0;
+ }
+
+ if (ctx->indata && strncasecmp(data+1, "expected", 8) == 0) {
+ ctx->indata = false;
+ ctx->inexp = true;
+ } else if (!ctx->indata) {
+ ctx->indata = (strncasecmp(data+1, "data", 4) == 0);
+ ctx->inexp = (strncasecmp(data+1, "expected", 8) == 0);
+ } else {
+ memcpy(ctx->buf + ctx->bufused, data, datalen);
+ ctx->bufused += datalen;
+ }
+ } else {
+ if (ctx->indata) {
+ memcpy(ctx->buf + ctx->bufused, data, datalen);
+ ctx->bufused += datalen;
+ }
+ if (ctx->inexp) {
+ if (data[datalen - 1] == '\n')
+ datalen -= 1;
+
+ memcpy(ctx->exp, data, datalen);
+ ctx->explen = datalen;
+ }
+ }
+
+ return true;
+}
+
+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;
+
+ UNUSED(exp);
+ UNUSED(explen);
+
+ result = number_from_css_string(&in, &consumed);
+
+ /** \todo some kind of verification of the result */
+ printf("%d\n", result);
+}
+