kv.test.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* This file is dedicated to the public domain. */
  2. {.desc = "the KeyValues parser"};
  3. // undef conflicting macros
  4. #undef ERROR // windows.h
  5. #undef OUT // "
  6. #undef EOF // stdio.h
  7. #include "../src/kv.c"
  8. #include "../src/intdefs.h"
  9. #include "../src/noreturn.h"
  10. static noreturn die(const struct kv_parser *kvp) {
  11. fprintf(stderr, "parse error: %d:%d: %s\n", kvp->line, kvp->col,
  12. kvp->errmsg);
  13. exit(1);
  14. }
  15. static void tokcb(enum kv_token type, const char *p, uint len,
  16. void *ctxt) {
  17. // nop - we're just testing the tokeniser
  18. }
  19. static const char data[] =
  20. "KeyValues {\n\
  21. Key/1 Val1![conditional]\n\
  22. Key2\n\
  23. Val2// comment\n\
  24. \"String Key\" // also comment\n\
  25. Val3 Key4 [conditional!]{ Key5 \"Value Five\" } // one more\n\
  26. } \n\
  27. ";
  28. static const int sz = sizeof(data) - 1;
  29. TEST("parsing should work with any buffer size") {
  30. for (int chunksz = 3; chunksz <= sz; ++chunksz) {
  31. struct kv_parser kvp = {0};
  32. // sending data in chunks to test reentrancy
  33. for (int chunk = 0; chunk * chunksz < sz; ++chunk) {
  34. int thischunk = chunksz;
  35. if (chunk * chunksz + thischunk > sz) {
  36. thischunk = sz - chunk * chunksz;
  37. }
  38. if (!kv_parser_feed(&kvp, data + chunk * chunksz, thischunk,
  39. tokcb, 0)) {
  40. die(&kvp);
  41. }
  42. }
  43. if (!kv_parser_done(&kvp)) die(&kvp);
  44. }
  45. return true;
  46. }
  47. // vi: sw=4 ts=4 noet tw=80 cc=80