summaryrefslogtreecommitdiffhomepage
path: root/test/kv.test.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/kv.test.c')
-rw-r--r--test/kv.test.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/test/kv.test.c b/test/kv.test.c
new file mode 100644
index 0000000..cd08d16
--- /dev/null
+++ b/test/kv.test.c
@@ -0,0 +1,49 @@
+/* This file is dedicated to the public domain. */
+
+{.desc = "the KeyValues parser"};
+
+// undef conflicting macros
+#undef ERROR // windows.h
+#undef OUT // "
+#undef EOF // stdio.h
+#include "../src/kv.c"
+
+#include "../src/intdefs.h"
+#include "../src/noreturn.h"
+
+static noreturn die(const struct kv_parser *kvp) {
+ fprintf(stderr, "parse error: %d:%d: %s\n", kvp->line, kvp->col,
+ kvp->errmsg);
+ exit(1);
+}
+
+static void tokcb(enum kv_token type, const char *p, uint len,
+ void *ctxt) {
+ // nop - we're just testing the tokeniser
+}
+
+static const char data[] =
+"KeyValues {\n\tKey/1\tVal1! \tKey2\nVal2// comment\n\"String Key\"// also comment\nVal3 Key4{ Key5 \"Value Five\" } // one more\n\t\n}"
+;
+static const int sz = sizeof(data) - 1;
+
+TEST("parsing should work with any buffer size", 0) {
+ for (int chunksz = 3; chunksz <= sz; ++chunksz) {
+ struct kv_parser kvp = {0};
+ // sending data in chunks to test reentrancy
+ for (int chunk = 0; chunk * chunksz < sz; ++chunk) {
+ int thischunk = chunksz;
+ if (chunk * chunksz + thischunk > sz) {
+ thischunk = sz - chunk * chunksz;
+ }
+ kv_parser_feed(&kvp, data + chunk * chunksz, thischunk,
+ tokcb, 0);
+ if (kvp.state == KV_PARSER_ERROR) die(&kvp);
+ }
+ kv_parser_done(&kvp);
+ if (kvp.state == KV_PARSER_ERROR) die(&kvp);
+ }
+ return true;
+}
+
+// vi: sw=4 ts=4 noet tw=80 cc=80