diff options
author | Michael Smith <mikesmiffy128@gmail.com> | 2021-11-20 03:10:50 +0000 |
---|---|---|
committer | Michael Smith <mikesmiffy128@gmail.com> | 2021-11-20 03:18:08 +0000 |
commit | da6f343032cb01597dc7866e66f091adf3243a62 (patch) | |
tree | 870f8cb8e82bb42202ab92bea03fc6ab35ada7ca /test/kv.test.c |
Initial public snapshot
With code from Bill. Thanks Bill!
Diffstat (limited to 'test/kv.test.c')
-rw-r--r-- | test/kv.test.c | 49 |
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 |