From da6f343032cb01597dc7866e66f091adf3243a62 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Sat, 20 Nov 2021 03:10:50 +0000 Subject: Initial public snapshot With code from Bill. Thanks Bill! --- test/kv.test.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 test/kv.test.c (limited to 'test/kv.test.c') 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 -- cgit v1.2.3