blob: cd08d165e26dca85a60af42c150fc373ef0653b3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
|