unit_test.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /*
  2. * Copyright (c) 2004-2013 Sergey Lyubka <[email protected]>
  3. * Copyright (c) 2013 Cesanta Software Limited
  4. * All rights reserved
  5. *
  6. * This library is dual-licensed: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation. For the terms of this
  9. * license, see <http: *www.gnu.org/licenses/>.
  10. *
  11. * You are free to use this library under the terms of the GNU General
  12. * Public License, but WITHOUT ANY WARRANTY; without even the implied
  13. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU General Public License for more details.
  15. *
  16. * Alternatively, you can license this library under a commercial
  17. * license, as set out in <http://cesanta.com/products.html>.
  18. */
  19. /*
  20. * To unit test on Mac system, do
  21. *
  22. * g++ unit_test.c -o unit_test -W -Wall -g -O0 -fprofile-arcs -ftest-coverage
  23. * clang unit_test.c -o unit_test -W -Wall -g -O0 -fprofile-arcs -ftest-coverage
  24. * ./unit_test
  25. * gcov -a unit_test.c
  26. */
  27. #include "frozen.c"
  28. #include <float.h>
  29. #include <math.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. const char *tok_type_names[] = {
  34. "INVALID", "STRING", "NUMBER", "TRUE", "FALSE",
  35. "NULL", "OBJECT_START", "OBJECT_END", "ARRAY_START", "ARRAY_END",
  36. };
  37. #define FAIL(str, line) \
  38. do { \
  39. fprintf(stderr, "Fail on line %d: [%s]\n", line, str); \
  40. return str; \
  41. } while (0)
  42. #define ASSERT(expr) \
  43. do { \
  44. static_num_tests++; \
  45. if (!(expr)) FAIL(#expr, __LINE__); \
  46. } while (0)
  47. #define MEMORY_CAPACITY 1024
  48. static char buffer[MEMORY_CAPACITY];
  49. static size_t buffer_size = 0;
  50. static
  51. void *epic_alloc(size_t size)
  52. {
  53. trace_assert(buffer_size + size <= MEMORY_CAPACITY);
  54. void *result = buffer + buffer_size;
  55. buffer_size += size;
  56. return result;
  57. }
  58. static void epic_free(void *ptr) {}
  59. Allocator allocator = {
  60. .alloc = epic_alloc,
  61. .free = epic_free
  62. };
  63. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  64. #define RUN_TEST(test) \
  65. do { \
  66. const char *msg = test(); \
  67. if (msg) return msg; \
  68. printf("Consumed %d bytes of memory\n", buffer_size); \
  69. buffer_size = 0; \
  70. } while (0)
  71. static int static_num_tests = 0;
  72. static const char *test_errors(void) {
  73. /* clang-format off */
  74. static const char *invalid_tests[] = {
  75. "p", "a:3", "\x01", "{:",
  76. " { 1", "{a:\"\n\"}", "{a:1x}", "{a:1e}",
  77. "{a:.1}", "{a:0.}", "{a:0.e}", "{a:0.e1}",
  78. "{a:0.1e}", "{a:\"\\u\" } ", "{a:\"\\yx\"}", "{a:\"\\u111r\"}",
  79. NULL};
  80. static const char *incomplete_tests[] = {"",
  81. " \r\n\t",
  82. "{",
  83. " { a",
  84. "{a:",
  85. "{a:\"",
  86. " { a : \"xx",
  87. "{a:12",
  88. "{a:\"\\uf",
  89. "{a:\"\\uff",
  90. "{a:\"\\ufff",
  91. "{a:\"\\uffff",
  92. "{a:\"\\uffff\"",
  93. "{a:\"\\uffff\" ,",
  94. "{a:n",
  95. "{a:nu",
  96. "{a:nul",
  97. "{a:null",
  98. NULL};
  99. /* clang-format on */
  100. static const struct {
  101. const char *str;
  102. int expected_len;
  103. } success_tests[] = {{"{}", 2},
  104. /* 2, 3, 4 byte utf-8 chars */
  105. {"{a:\"\xd0\xb1\xe3\x81\xaf\xf0\xa2\xb3\x82\"}", 15},
  106. {"{a:\"\\u0006\"}", 12},
  107. {" { } ", 4},
  108. {"{a:1}", 5},
  109. {"{a:1.23}", 8},
  110. {"{a:1e23}", 8},
  111. {"{a:1.23e2}", 10},
  112. {"{a:-123}", 8},
  113. {"{a:-1.3}", 8},
  114. {"{a:-1.3e-2}", 11},
  115. {"{a:\"\"}", 6},
  116. {"{a:\" \\n\\t\\r\"}", 13},
  117. {" {a:[1]} 123456", 8},
  118. {" {a:[]} 123456", 7},
  119. {" {a:[1,2]} 123456", 10},
  120. {"{a:1,b:2} xxxx", 9},
  121. {"{a:1,b:{},c:[{}]} xxxx", 17},
  122. {"{a:true,b:[false,null]} xxxx", 23},
  123. {"[1.23, 3, 5]", 12},
  124. {"[13, {\"a\":\"hi there\"}, 5]", 25},
  125. {NULL, 0}};
  126. const char *s1 =
  127. " { a: 1, b: \"hi there\", c: true, d: false, "
  128. " e : null, f: [ 1, -2, 3], g: { \"1\": [], h: [ 7 ] } } ";
  129. int i;
  130. ASSERT(json_walk(NULL, 0, NULL, 0) == JSON_STRING_INVALID);
  131. for (i = 0; invalid_tests[i] != NULL; i++) {
  132. ASSERT(json_walk(invalid_tests[i], strlen(invalid_tests[i]), NULL, NULL) ==
  133. JSON_STRING_INVALID);
  134. }
  135. for (i = 0; incomplete_tests[i] != NULL; i++) {
  136. ASSERT(json_walk(incomplete_tests[i], strlen(incomplete_tests[i]), NULL,
  137. NULL) == JSON_STRING_INCOMPLETE);
  138. }
  139. for (i = 0; success_tests[i].str != NULL; i++) {
  140. ASSERT(json_walk(success_tests[i].str, strlen(success_tests[i].str), NULL,
  141. NULL) == success_tests[i].expected_len);
  142. }
  143. ASSERT(json_walk("{}", 2, NULL, NULL) == 2);
  144. ASSERT(json_walk(s1, strlen(s1), NULL, 0) > 0);
  145. return NULL;
  146. }
  147. struct my_struct {
  148. int a, b;
  149. };
  150. static int print_my_struct(struct json_out *out, va_list *ap) {
  151. struct my_struct *p = va_arg(*ap, struct my_struct *);
  152. return json_printf(out, "{a: %d, b: %d}", p->a, p->b);
  153. }
  154. static const char *test_json_printf(void) {
  155. char buf[200] = "";
  156. {
  157. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  158. const char *result = "42 42";
  159. json_printf(&out, "%ld %d", 42, 42);
  160. ASSERT(strcmp(buf, result) == 0);
  161. }
  162. /* platform specific compatibility where it matters */
  163. {
  164. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  165. const char *result = "16045690985373621933 42";
  166. json_printf(&out, "%" UINT64_FMT " %d", 0xdeadbeeffee1deadUL, 42);
  167. ASSERT(strcmp(buf, result) == 0);
  168. }
  169. {
  170. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  171. const char *result = "12 42";
  172. size_t foo = 12;
  173. json_printf(&out, "%lu %d", foo, 42);
  174. ASSERT(strcmp(buf, result) == 0);
  175. }
  176. /* people live in the future today, %llu works even on recent windozes */
  177. {
  178. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  179. const char *result = "16045690985373621933 42";
  180. json_printf(&out, "%llu %d", 0xdeadbeeffee1deadUL, 42);
  181. ASSERT(strcmp(buf, result) == 0);
  182. }
  183. {
  184. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  185. const char *result = "12 42";
  186. size_t foo = 12;
  187. json_printf(&out, "%zu %d", foo, 42);
  188. ASSERT(strcmp(buf, result) == 0);
  189. }
  190. {
  191. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  192. const char *result = "{\"foo\": 123, \"x\": [false, true], \"y\": \"hi\"}";
  193. json_printf(&out, "{%Q: %d, x: [%B, %B], y: %Q}", "foo", 123, 0, -1, "hi");
  194. ASSERT(strcmp(buf, result) == 0);
  195. }
  196. {
  197. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  198. int arr[] = {-2387, 943478};
  199. json_printf(&out, "%M", json_printf_array, arr, sizeof(arr), sizeof(arr[0]),
  200. "%d");
  201. ASSERT(strcmp(buf, "[-2387, 943478]") == 0);
  202. }
  203. {
  204. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  205. double arr[] = {9.32156, 3.1415926};
  206. json_printf(&out, "%M", json_printf_array, arr, sizeof(arr), sizeof(arr[0]),
  207. "%.2lf");
  208. ASSERT(strcmp(buf, "[9.32, 3.14]") == 0);
  209. }
  210. {
  211. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  212. unsigned short arr[] = {65535, 777};
  213. const char *result = "{\"a\": [-1, 777], \"b\": 37}";
  214. json_printf(&out, "{a: %M, b: %d}", json_printf_array, arr, sizeof(arr),
  215. sizeof(arr[0]), "%hd", 37);
  216. ASSERT(strcmp(buf, result) == 0);
  217. }
  218. {
  219. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  220. const char *arr[] = {"hi", "there", NULL};
  221. const char *result = "[\"hi\", \"there\", null]";
  222. json_printf(&out, "%M", json_printf_array, arr, sizeof(arr), sizeof(arr[0]),
  223. "%Q");
  224. ASSERT(strcmp(buf, result) == 0);
  225. }
  226. {
  227. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  228. const char *result = "{\"a\": \"\\\"\\\\\\r\\nя\\t\\u0002\"}";
  229. json_printf(&out, "{a: %Q}", "\"\\\r\nя\t\x02");
  230. ASSERT(strcmp(buf, result) == 0);
  231. }
  232. {
  233. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  234. struct my_struct mys = {1, 2};
  235. const char *result = "{\"foo\": {\"a\": 1, \"b\": 2}, \"bar\": 3}";
  236. json_printf(&out, "{foo: %M, bar: %d}", print_my_struct, &mys, 3);
  237. ASSERT(strcmp(buf, result) == 0);
  238. }
  239. {
  240. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  241. out.u.buf.size = 3;
  242. memset(buf, 0, sizeof(buf));
  243. ASSERT(json_printf(&out, "{%d}", 123) == 5);
  244. ASSERT(memcmp(buf, "{1\x00\x00\x00", 5) == 0);
  245. }
  246. {
  247. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  248. const char *result = "\"foo\"";
  249. out.u.buf.size = 6;
  250. memset(buf, 0, sizeof(buf));
  251. ASSERT(json_printf(&out, "%.*Q", 3, "foobar") == 5);
  252. ASSERT(memcmp(buf, result, 5) == 0);
  253. }
  254. {
  255. /*
  256. * Check long string (which forces frozen to allocate a temporary buffer
  257. * from heap)
  258. */
  259. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  260. const char *result =
  261. "{\"foo\": "
  262. "\"12345678901234567890123456789012345678901234567890123456789012345678"
  263. "90123456789012345678901234567890\"}";
  264. const char *s =
  265. "\"123456789012345678901234567890123456789012345678901234567890"
  266. "1234567890123456789012345678901234567890\"";
  267. json_printf(&out, "{foo: %s}", s);
  268. ASSERT(strcmp(buf, result) == 0);
  269. }
  270. {
  271. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  272. const char *fmt = "{a: \"%s\"}", *result = "{\"a\": \"b\"}";
  273. memset(buf, 0, sizeof(buf));
  274. ASSERT(json_printf(&out, fmt, "b") > 0);
  275. ASSERT(strcmp(buf, result) == 0);
  276. }
  277. {
  278. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  279. memset(buf, 0, sizeof(buf));
  280. ASSERT(json_printf(&out, "%.*s %d", 2, "abc", 5) > 0);
  281. ASSERT(strcmp(buf, "ab 5") == 0);
  282. }
  283. {
  284. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  285. const char *result = "\"a_b0\": 1";
  286. memset(buf, 0, sizeof(buf));
  287. ASSERT(json_printf(&out, "a_b0: %d", 1) > 0);
  288. ASSERT(strcmp(buf, result) == 0);
  289. }
  290. #if JSON_ENABLE_BASE64
  291. {
  292. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  293. const char *result = "\"YTI=\"";
  294. memset(buf, 0, sizeof(buf));
  295. ASSERT(json_printf(&out, "%V", "a2", 2) > 0);
  296. ASSERT(strcmp(buf, result) == 0);
  297. }
  298. {
  299. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  300. const char *result = "\"ACABIAIgYWJj\"";
  301. memset(buf, 0, sizeof(buf));
  302. ASSERT(json_printf(&out, "%V", "\x00 \x01 \x02 abc", 9) > 0);
  303. ASSERT(strcmp(buf, result) == 0);
  304. }
  305. #endif /* JSON_ENABLE_BASE64 */
  306. #if JSON_ENABLE_HEX
  307. {
  308. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  309. const char *result = "\"002001200220616263\"";
  310. memset(buf, 0, sizeof(buf));
  311. ASSERT(json_printf(&out, "%H", 9, "\x00 \x01 \x02 abc") > 0);
  312. ASSERT(strcmp(buf, result) == 0);
  313. }
  314. #endif /* JSON_ENABLE_HEX */
  315. {
  316. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  317. memset(buf, 0, sizeof(buf));
  318. ASSERT(json_printf(&out, "%c", 0x53) > 0);
  319. ASSERT(strcmp(buf, "S") == 0);
  320. }
  321. {
  322. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  323. const char *result = "<\"array\">0f";
  324. memset(buf, 0, sizeof(buf));
  325. ASSERT(json_printf(&out, "<array>%02x", 15) > 0);
  326. ASSERT(strcmp(buf, result) == 0);
  327. }
  328. {
  329. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  330. double arr[] = {9.32156, 3.1415926};
  331. #if !defined(_MSC_VER) || _MSC_VER >= 1700
  332. const char *result = "[9.32e+00, 3.14e+00]"; // Modern compilers
  333. #else
  334. const char *result = "[9.32e+000, 3.14e+000]"; // Old VC98 compiler
  335. #endif
  336. json_printf(&out, "%M", json_printf_array, arr, sizeof(arr), sizeof(arr[0]),
  337. "%.2e");
  338. ASSERT(strcmp(buf, result) == 0);
  339. }
  340. {
  341. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  342. double arr[] = {9.32156, 3.1415926};
  343. json_printf(&out, "%M", json_printf_array, arr, sizeof(arr), sizeof(arr[0]),
  344. "%.4g");
  345. ASSERT(strcmp(buf, "[9.322, 3.142]") == 0);
  346. }
  347. return NULL;
  348. }
  349. static const char *test_system(void) {
  350. char buf[2020];
  351. uint64_t u = (uint64_t) 0xdeadbeeffee1dead;
  352. int64_t d = (int64_t) u;
  353. int res;
  354. snprintf(buf, sizeof(buf), "%" UINT64_FMT, u);
  355. ASSERT(strcmp(buf, "16045690985373621933") == 0);
  356. snprintf(buf, sizeof(buf), "%" INT64_FMT, d);
  357. ASSERT(strcmp(buf, "-2401053088335929683") == 0);
  358. res = snprintf(buf, 3, "foo");
  359. ASSERT(res == 3);
  360. ASSERT(buf[0] == 'f');
  361. ASSERT(buf[1] == 'o');
  362. ASSERT(buf[2] == '\0');
  363. return NULL;
  364. }
  365. static void cb(void *data, const char *name, size_t name_len, const char *path,
  366. const struct json_token *token) {
  367. char *buf = (char *) data;
  368. const char *snull = "<null>";
  369. sprintf(buf + strlen(buf), "name:'%.*s', path:'%s', type:%s, val:'%.*s'\n",
  370. (int) (name != NULL ? name_len : strlen(snull)),
  371. name != NULL ? name : snull, path, tok_type_names[token->type],
  372. (int) (token->ptr != NULL ? token->len : (int) strlen(snull)),
  373. token->ptr != NULL ? token->ptr : snull);
  374. }
  375. static const char *test_callback_api(void) {
  376. const char *s =
  377. "{\"c\":[\"foo\", \"bar\", {\"a\":9, \"b\": \"x\"}], "
  378. "\"mynull\": null, \"mytrue\": true, \"myfalse\": false}";
  379. const char *result =
  380. "name:'<null>', path:'', type:OBJECT_START, val:'<null>'\n"
  381. "name:'c', path:'.c', type:ARRAY_START, val:'<null>'\n"
  382. "name:'0', path:'.c[0]', type:STRING, val:'foo'\n"
  383. "name:'1', path:'.c[1]', type:STRING, val:'bar'\n"
  384. "name:'2', path:'.c[2]', type:OBJECT_START, val:'<null>'\n"
  385. "name:'a', path:'.c[2].a', type:NUMBER, val:'9'\n"
  386. "name:'b', path:'.c[2].b', type:STRING, val:'x'\n"
  387. "name:'<null>', path:'.c[2]', type:OBJECT_END, val:'{\"a\":9, \"b\": "
  388. "\"x\"}'\n"
  389. "name:'<null>', path:'.c', type:ARRAY_END, val:'[\"foo\", \"bar\", "
  390. "{\"a\":9, \"b\": \"x\"}]'\n"
  391. "name:'mynull', path:'.mynull', type:NULL, val:'null'\n"
  392. "name:'mytrue', path:'.mytrue', type:TRUE, val:'true'\n"
  393. "name:'myfalse', path:'.myfalse', type:FALSE, val:'false'\n"
  394. "name:'<null>', path:'', type:OBJECT_END, val:'{\"c\":[\"foo\", \"bar\", "
  395. "{\"a\":9, \"b\": \"x\"}], \"mynull\": null, \"mytrue\": true, "
  396. "\"myfalse\": false}'\n";
  397. char buf[4096] = "";
  398. ASSERT(json_walk(s, strlen(s), cb, buf) == (int) strlen(s));
  399. ASSERT(strcmp(buf, result) == 0);
  400. return NULL;
  401. }
  402. /*
  403. * Tests with the path which is longer than JSON_MAX_PATH_LEN (at the moment,
  404. * 60)
  405. */
  406. static const char *test_callback_api_long_path(void) {
  407. const char *s =
  408. "{\"MyWZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  409. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  410. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  411. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  412. "ZZZZZZZZZZZZZZZZZvf\": {}, \"jYP-27917287424p\": {}}";
  413. const char *result =
  414. "name:'<null>', path:'', type:OBJECT_START, val:'<null>'\n"
  415. "name:'"
  416. "MyWZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  417. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  418. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  419. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZvf', "
  420. "path:'."
  421. "MyWZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  422. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  423. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  424. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ', "
  425. "type:OBJECT_START, val:'<null>'\n"
  426. "name:'<null>', "
  427. "path:'."
  428. "MyWZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  429. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  430. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  431. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ', "
  432. "type:OBJECT_END, val:'{}'\n"
  433. "name:'jYP-27917287424p', path:'.jYP-27917287424p', type:OBJECT_START, "
  434. "val:'<null>'\n"
  435. "name:'<null>', path:'.jYP-27917287424p', type:OBJECT_END, val:'{}'\n"
  436. "name:'<null>', path:'', type:OBJECT_END, "
  437. "val:'{"
  438. "\"MyWZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  439. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  440. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  441. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
  442. "ZZZZZZZZZZZZZZZZvf\": {}, \"jYP-27917287424p\": {}}'\n";
  443. char buf[4096] = "";
  444. ASSERT(json_walk(s, strlen(s), cb, buf) == (int) strlen(s));
  445. ASSERT(strcmp(buf, result) == 0);
  446. return NULL;
  447. }
  448. static void scan_array(const char *str, int len, void *user_data) {
  449. struct json_token t;
  450. int i;
  451. char *buf = (char *) user_data;
  452. for (i = 0; json_scanf_array_elem(str, len, ".x", i, &t) > 0; i++) {
  453. sprintf(buf + strlen(buf), "%d[%.*s] ", i, t.len, t.ptr);
  454. }
  455. }
  456. static const char *test_scanf(void) {
  457. char buf[100] = "";
  458. int a = 0, b = 0;
  459. char *d = NULL;
  460. const char *str =
  461. "{ a: 1234, b : true, \"c\": {x: [17, 78, -20]}, d: \"hi%20there\" }";
  462. ASSERT(json_scanf(str, strlen(str), "{a: %d, b: %B, c: [%M], d: %Q}", &a, &b,
  463. &scan_array, buf, &d) == 4);
  464. ASSERT(a == 1234);
  465. ASSERT(b == 1);
  466. ASSERT(strcmp(buf, "0[17] 1[78] 2[-20] ") == 0);
  467. ASSERT(d != NULL);
  468. ASSERT(strcmp(d, "hi%20there") == 0);
  469. allocator.free(d);
  470. {
  471. /* Test errors */
  472. const char *str = "{foo:1, bar:[2,3,4]}";
  473. size_t i;
  474. ASSERT(json_walk(str, strlen(str), NULL, NULL) == (int) strlen(str));
  475. for (i = 1; i < strlen(str); i++) {
  476. ASSERT(json_walk(str, i, NULL, NULL) == JSON_STRING_INCOMPLETE);
  477. }
  478. }
  479. {
  480. /* Test that paths are utf8 */
  481. const char *str = "{\"ы\": 123}";
  482. int x = 0;
  483. ASSERT(json_scanf(str, strlen(str), "{ы: %d}", &x) == 1);
  484. ASSERT(x == 123);
  485. }
  486. {
  487. /* Test that paths are utf8 */
  488. const char *str = "{a: 123, b: [1,2,3]}";
  489. struct json_token t;
  490. memset(&t, 0, sizeof(t));
  491. ASSERT(json_scanf(str, strlen(str), "{b: %T}", &t) == 1);
  492. ASSERT(t.type == JSON_TYPE_ARRAY_END);
  493. ASSERT(t.len == 7);
  494. ASSERT(strncmp(t.ptr, "[1,2,3]", t.len) == 0);
  495. }
  496. {
  497. /* Test zero termination */
  498. char *s = NULL;
  499. const char *str = "{a: \"foo\", b:123}";
  500. ASSERT(json_scanf(str, strlen(str), "{a: %Q}", &s) == 1);
  501. ASSERT(s != NULL);
  502. ASSERT(s[3] == '\0');
  503. allocator.free(s);
  504. }
  505. {
  506. /* Test for scalar value being a root element */
  507. int n = 0;
  508. const char *str = " true ";
  509. ASSERT(json_scanf(str, strlen(str), " %B ", &n) == 1);
  510. ASSERT(n == 1);
  511. }
  512. {
  513. /* Test array of objects */
  514. const char *str = " { \"a\": [ {\"b\": 123}, {\"b\": 345} ]} ";
  515. int i, value, len = strlen(str), values[] = {123, 345};
  516. struct json_token t;
  517. /* Scan each array element into a token */
  518. for (i = 0; json_scanf_array_elem(str, len, ".a", i, &t) > 0; i++) {
  519. /* Now scan each token */
  520. ASSERT(t.type == JSON_TYPE_OBJECT_END);
  521. ASSERT(json_scanf(t.ptr, t.len, "{b: %d}", &value) == 1);
  522. ASSERT((size_t) i < sizeof(values) / sizeof(values[0]));
  523. ASSERT(values[i] == value);
  524. }
  525. ASSERT(i == 2);
  526. }
  527. {
  528. const char *str = "{a : [\"foo\", \"\", \"a\"] }";
  529. struct json_token t;
  530. ASSERT(json_scanf_array_elem(str, strlen(str), ".a", 0, &t) == 3);
  531. ASSERT(json_scanf_array_elem(str, strlen(str), ".a", 1, &t) == 0);
  532. ASSERT(json_scanf_array_elem(str, strlen(str), ".a", 2, &t) == 1);
  533. ASSERT(json_scanf_array_elem(str, strlen(str), ".a", 3, &t) == -1);
  534. }
  535. {
  536. const char *str = "{a : \"foo\\b\\f\\n\\r\\t\\\\\" }";
  537. char *result;
  538. ASSERT(json_scanf(str, strlen(str), "{a: %Q}", &result) == 1);
  539. ASSERT(strcmp(result, "foo\b\f\n\r\t\\") == 0);
  540. allocator.free(result);
  541. ASSERT(json_scanf(str, 9, "{a: %Q}", &result) == 0);
  542. }
  543. {
  544. const char *str = "{a : \"привет\" }";
  545. char *result;
  546. ASSERT(json_scanf(str, strlen(str), "{a: %Q}", &result) == 1);
  547. ASSERT(strcmp(result, "привет") == 0);
  548. allocator.free(result);
  549. }
  550. #if JSON_ENABLE_BASE64
  551. {
  552. const char *str = "{a : \"YTI=\" }";
  553. int len;
  554. char *result;
  555. ASSERT(json_scanf(str, strlen(str), "{a: %V}", &result, &len) == 1);
  556. ASSERT(len == 2);
  557. ASSERT(strcmp(result, "a2") == 0);
  558. allocator.free(result);
  559. }
  560. {
  561. const char *str = "{a : \"0L/RgNC40LLQtdGC0Ys=\" }";
  562. int len;
  563. char *result;
  564. ASSERT(json_scanf(str, strlen(str), "{a: %V}", &result, &len) == 1);
  565. ASSERT(len == 14);
  566. ASSERT(strcmp(result, "приветы") == 0);
  567. allocator.free(result);
  568. }
  569. #endif /* JSON_ENABLE_BASE64 */
  570. #if JSON_ENABLE_HEX
  571. {
  572. const char *str = "{a : \"61626320\" }";
  573. int len = 0;
  574. char *result = NULL;
  575. ASSERT(json_scanf(str, strlen(str), "{a: %H}", &len, &result) == 1);
  576. ASSERT(len == 4);
  577. ASSERT(strcmp(result, "abc ") == 0);
  578. allocator.free(result);
  579. }
  580. #endif /* JSON_ENABLE_HEX */
  581. {
  582. const char *str = "{a : null }";
  583. char *result = (char *) 123;
  584. ASSERT(json_scanf(str, strlen(str), "{a: %Q}", &result) == 0);
  585. ASSERT(result == NULL);
  586. allocator.free(result);
  587. }
  588. {
  589. int a = 0;
  590. bool b = false;
  591. int c = 0xFFFFFFFF;
  592. const char *str = "{\"b\":true,\"c\":false,\"a\":2}";
  593. ASSERT(json_scanf(str, strlen(str), "{a:%d, b:%B, c:%B}", &a, &b, &c) == 3);
  594. ASSERT(a == 2);
  595. ASSERT(b == true);
  596. if (sizeof(bool) == 1) {
  597. ASSERT((char) c == false);
  598. } else {
  599. ASSERT(c == false);
  600. }
  601. }
  602. {
  603. const char *str = "{ fa: 1, fb: 2.34, fc: 5.67 }";
  604. const char *fmt = "{fa: %f, fb: %f, fc: %lf}";
  605. float fa = 0.0, fb = 0.0;
  606. double fc = 0.0;
  607. #if !JSON_MINIMAL
  608. float a = 1.0f, b = 2.34f;
  609. double c = 5.67;
  610. ASSERT(json_scanf(str, strlen(str), fmt, &fa, &fb, &fc) == 3);
  611. ASSERT(fa == a);
  612. ASSERT(fb == b);
  613. ASSERT(fabs(fc - c) < FLT_EPSILON);
  614. #else
  615. ASSERT(json_scanf(str, strlen(str), fmt, &fa, &fb, &fc) == 0);
  616. #endif
  617. }
  618. {
  619. int v = -1;
  620. long lv = -1;
  621. const char *s = "{\"v\": 0x12, \"lv\": 0x34}";
  622. ASSERT(json_scanf("0x", 2, "%i", &v) == 0); // Incomplete string
  623. ASSERT(json_scanf("0xe", 3, "%i", &v) == 1);
  624. ASSERT(v == 0xe);
  625. ASSERT(json_scanf("12", 2, "%i", &v) == 1);
  626. ASSERT(v == 12);
  627. // %d and %ld accept hex
  628. ASSERT(json_scanf(s, strlen(s), "{lv:%ld, v:%d}", &lv, &v) == 2);
  629. ASSERT(v == 0x12);
  630. ASSERT(lv == 0x34);
  631. }
  632. {
  633. unsigned int v = 0;
  634. unsigned long lv = 0;
  635. const char *s = "{\"v\": 0x12, \"lv\": 0x34}";
  636. // %u and %lu accept hex
  637. ASSERT(json_scanf(s, strlen(s), "{lv:%lu, v:%u}", &lv, &v) == 2);
  638. ASSERT(v == 0x12);
  639. ASSERT(lv == 0x34);
  640. }
  641. return NULL;
  642. }
  643. static const char *test_json_unescape(void) {
  644. char buf[1];
  645. ASSERT(json_unescape("foo", 3, NULL, 0) == 3);
  646. ASSERT(json_unescape("foo\\", 4, NULL, 0) == JSON_STRING_INCOMPLETE);
  647. ASSERT(json_unescape("foo\\x", 5, NULL, 0) == JSON_STRING_INVALID);
  648. ASSERT(json_unescape("\\ueeee", 5, NULL, 0) == JSON_STRING_INCOMPLETE);
  649. ASSERT(json_unescape("\\ueeee", 6, NULL, 0) == JSON_STRING_INVALID);
  650. // Simple one-byte escapes should work
  651. ASSERT(json_unescape("\\u0026", 2, NULL, 0) == JSON_STRING_INCOMPLETE);
  652. ASSERT(json_unescape("\\u0026", 3, NULL, 0) == JSON_STRING_INCOMPLETE);
  653. ASSERT(json_unescape("\\u0026", 4, NULL, 0) == JSON_STRING_INCOMPLETE);
  654. ASSERT(json_unescape("\\u0026", 5, NULL, 0) == JSON_STRING_INCOMPLETE);
  655. ASSERT(json_unescape("\\u0026", 6, buf, sizeof(buf)) == 1);
  656. ASSERT(buf[0] == '&');
  657. return NULL;
  658. }
  659. static void cb2(void *data, const char *name, size_t name_len, const char *path,
  660. const struct json_token *token) {
  661. struct json_token *pt = (struct json_token *) data;
  662. pt->ptr = token->ptr;
  663. pt->len = token->len;
  664. (void) path;
  665. (void) name_len;
  666. (void) name;
  667. }
  668. static const char *test_parse_string(void) {
  669. const char *str = " \" foo\\bar\"";
  670. const int str_len = strlen(str);
  671. struct json_token t;
  672. struct frozen f;
  673. memset(&f, 0, sizeof(f));
  674. f.end = str + str_len;
  675. f.cur = str;
  676. f.callback_data = (void *) &t;
  677. f.callback = cb2;
  678. ASSERT(json_parse_string(&f) == 0);
  679. ASSERT(strncmp(t.ptr, " foo\\bar", t.len) == 0);
  680. return NULL;
  681. }
  682. static const char *test_eos(void) {
  683. const char *s = "{\"a\": 12345}";
  684. size_t n = 999;
  685. char *buf = (char *) allocator.alloc(n);
  686. int s_len = strlen(s), a = 0;
  687. ASSERT(buf != NULL);
  688. memset(buf, 'x', n);
  689. memcpy(buf, s, s_len);
  690. ASSERT(json_scanf(buf, n, "{a:%d}", &a) == 1);
  691. ASSERT(a == 12345);
  692. allocator.free(buf);
  693. return NULL;
  694. }
  695. static int compare_file(const char *file_name, const char *s) {
  696. int res = -1;
  697. char *p = json_fread(file_name);
  698. if (p == NULL) return res;
  699. res = strcmp(p, s);
  700. allocator.free(p);
  701. return res == 0;
  702. }
  703. static const char *test_fprintf(void) {
  704. const char *fname = "a.json";
  705. const char *result = "{\"a\":123}\n";
  706. char *p;
  707. ASSERT(json_fprintf(fname, "{a:%d}", 123) > 0);
  708. p = json_fread(fname);
  709. ASSERT(p != NULL);
  710. ASSERT(strcmp(p, result) == 0);
  711. allocator.free(p);
  712. remove(fname);
  713. ASSERT(json_fread(fname) == NULL);
  714. return NULL;
  715. }
  716. static const char *test_json_setf(void) {
  717. char buf[200];
  718. const char *s1 = "{ \"a\": 123, \"b\": [ 1 ], \"c\": true }";
  719. {
  720. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  721. const char *s2 = "{ \"a\": 7, \"b\": [ 1 ], \"c\": true }";
  722. int res = json_setf(s1, strlen(s1), &out, ".a", "%d", 7);
  723. ASSERT(res == 1);
  724. ASSERT(strcmp(buf, s2) == 0);
  725. }
  726. {
  727. /* Add Key with length > 1 */
  728. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  729. const char *s2 =
  730. "{ \"a\": 123, \"b\": [ 1 ], \"c\": true,\"foo\":{\"bar\":42} }";
  731. int res = json_setf(s1, strlen(s1), &out, ".foo.bar", "%d", 42);
  732. ASSERT(res == 0);
  733. ASSERT(strcmp(buf, s2) == 0);
  734. }
  735. {
  736. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  737. const char *s2 = "{ \"a\": 123, \"b\": false, \"c\": true }";
  738. int res = json_setf(s1, strlen(s1), &out, ".b", "%B", 0);
  739. ASSERT(res == 1);
  740. ASSERT(strcmp(buf, s2) == 0);
  741. }
  742. {
  743. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  744. const char *s2 = "{ \"a\": 123, \"b\": [ 2 ], \"c\": true }";
  745. int res = json_setf(s1, strlen(s1), &out, ".b[0]", "%d", 2);
  746. ASSERT(res == 1);
  747. ASSERT(strcmp(buf, s2) == 0);
  748. }
  749. {
  750. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  751. const char *s2 = "{ \"b\": [ 1 ], \"c\": true }";
  752. int res = json_setf(s1, strlen(s1), &out, ".a", NULL);
  753. ASSERT(res == 1);
  754. ASSERT(strcmp(buf, s2) == 0);
  755. }
  756. {
  757. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  758. const char *s2 = "{ \"a\": 123, \"b\": [ 1 ] }";
  759. int res = json_setf(s1, strlen(s1), &out, ".c", NULL);
  760. ASSERT(res == 1);
  761. ASSERT(strcmp(buf, s2) == 0);
  762. }
  763. {
  764. /* Delete non-existent key */
  765. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  766. const char *s1 = "{\"a\":1}";
  767. int res = json_setf(s1, strlen(s1), &out, ".d", NULL);
  768. ASSERT(res == 0);
  769. ASSERT(strcmp(buf, s1) == 0);
  770. }
  771. {
  772. /* Delete non-existent key, spaces in obj */
  773. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  774. int res = json_setf(s1, strlen(s1), &out, ".d", NULL);
  775. ASSERT(res == 0);
  776. ASSERT(strcmp(buf, s1) == 0);
  777. }
  778. {
  779. /* Change the whole JSON object */
  780. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  781. const char *s2 = "123";
  782. int res = json_setf(s1, strlen(s1), &out, "", "%d", 123);
  783. ASSERT(res == 1);
  784. ASSERT(strcmp(buf, s2) == 0);
  785. }
  786. {
  787. /* Add missing keys */
  788. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  789. const char *s2 =
  790. "{ \"a\": 123, \"b\": [ 1 ], \"c\": true,\"d\":{\"e\":8} }";
  791. int res = json_setf(s1, strlen(s1), &out, ".d.e", "%d", 8);
  792. ASSERT(res == 0);
  793. ASSERT(strcmp(buf, s2) == 0);
  794. }
  795. {
  796. /* Append to arrays */
  797. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  798. const char *s2 = "{ \"a\": 123, \"b\": [ 1,2 ], \"c\": true }";
  799. int res = json_setf(s1, strlen(s1), &out, ".b[]", "%d", 2);
  800. ASSERT(res == 0);
  801. ASSERT(strcmp(buf, s2) == 0);
  802. }
  803. {
  804. /* Delete from array */
  805. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  806. const char *s2 = "{ \"a\": 123, \"b\": [ ], \"c\": true }";
  807. int res = json_setf(s1, strlen(s1), &out, ".b[0]", NULL);
  808. ASSERT(res == 1);
  809. ASSERT(strcmp(buf, s2) == 0);
  810. }
  811. {
  812. /* Create array and push value */
  813. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  814. const char *s2 = "{ \"a\": 123, \"b\": [ 1 ], \"c\": true,\"d\":[3] }";
  815. int res = json_setf(s1, strlen(s1), &out, ".d[]", "%d", 3);
  816. // printf("[%s]\n[%s]\n", buf, s2);
  817. ASSERT(res == 0);
  818. ASSERT(strcmp(buf, s2) == 0);
  819. }
  820. return NULL;
  821. }
  822. static const char *test_prettify(void) {
  823. const char *fname = "a.json";
  824. char buf[200];
  825. {
  826. const char *s1 = "{ \"a\": 1, \"b\":2,\"c\":[null,\"aa\",{},true]}";
  827. struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
  828. const char *s2 =
  829. "{\n \"a\": 1,\n \"b\": 2,\n \"c\": [\n null,\n \"aa\",\n "
  830. "{},\n true\n ]\n}";
  831. ASSERT(json_prettify(s1, strlen(s1), &out) > 0);
  832. ASSERT(strcmp(buf, s2) == 0);
  833. }
  834. {
  835. remove(fname);
  836. ASSERT(json_prettify_file(fname) == -1);
  837. }
  838. {
  839. ASSERT(compare_file(fname, "") == -1);
  840. json_fprintf(fname, "::");
  841. ASSERT(json_prettify_file(fname) == JSON_STRING_INVALID);
  842. ASSERT(compare_file(fname, "::\n"));
  843. remove(fname);
  844. }
  845. {
  846. ASSERT(compare_file(fname, "") == -1);
  847. json_fprintf(fname, "{");
  848. ASSERT(json_prettify_file(fname) == JSON_STRING_INCOMPLETE);
  849. ASSERT(compare_file(fname, "{\n"));
  850. remove(fname);
  851. }
  852. {
  853. ASSERT(compare_file(fname, "") == -1);
  854. json_fprintf(fname, "%d", 123);
  855. ASSERT(compare_file(fname, "123\n"));
  856. ASSERT(json_prettify_file(fname) > 0);
  857. ASSERT(compare_file(fname, "123\n"));
  858. remove(fname);
  859. }
  860. {
  861. const char *s = "{\n \"a\": 123\n}\n";
  862. ASSERT(compare_file(fname, "") == -1);
  863. json_fprintf(fname, "{a:%d}", 123);
  864. ASSERT(json_prettify_file(fname) > 0);
  865. ASSERT(compare_file(fname, s));
  866. (void) remove(fname);
  867. }
  868. return NULL;
  869. }
  870. static const char *test_json_next(void) {
  871. struct json_token key, val;
  872. char buf[100];
  873. {
  874. /* Traverse an object */
  875. void *h = NULL;
  876. int i = 0;
  877. const char *s = "{ \"a\": [], \"b\": [ 1, {} ], \"c\": true }";
  878. int len = strlen(s);
  879. const char *results[] = {"[a] -> [[]]", "[b] -> [[ 1, {} ]]",
  880. "[c] -> [true]"};
  881. while ((h = json_next_key(s, len, h, "", &key, &val)) != NULL) {
  882. snprintf(buf, sizeof(buf), "[%.*s] -> [%.*s]", key.len, key.ptr, val.len,
  883. val.ptr);
  884. ASSERT(strcmp(results[i], buf) == 0);
  885. i++;
  886. }
  887. ASSERT(i == 3);
  888. }
  889. {
  890. /* Traverse an array */
  891. void *h = NULL;
  892. int i = 0, idx;
  893. const char *s = "{ \"a\": [], \"b\": [ 1, {} ], \"c\": true }";
  894. int len = strlen(s);
  895. const char *results[] = {"[0] -> [1]", "[1] -> [{}]"};
  896. while ((h = json_next_elem(s, len, h, ".b", &idx, &val)) != NULL) {
  897. snprintf(buf, sizeof(buf), "[%d] -> [%.*s]", idx, val.len, val.ptr);
  898. ASSERT(strcmp(results[i], buf) == 0);
  899. i++;
  900. }
  901. ASSERT(i == 2);
  902. }
  903. {
  904. /* Traverse more complex object */
  905. const char *s = "{ \"a\": 123, \"b\": { \"c\": true, \"d\": 1234 } }";
  906. void *h = NULL;
  907. int i = 0;
  908. int len = strlen(s);
  909. const char *results[] = {"[c] -> [true]", "[d] -> [1234]"};
  910. while ((h = json_next_key(s, len, h, ".b", &key, &val)) != NULL) {
  911. snprintf(buf, sizeof(buf), "[%.*s] -> [%.*s]", key.len, key.ptr, val.len,
  912. val.ptr);
  913. ASSERT(strcmp(results[i], buf) == 0);
  914. i++;
  915. }
  916. ASSERT(i == 2);
  917. }
  918. return NULL;
  919. }
  920. static const char *test_json_printf_hex(void) {
  921. char *s = json_asprintf("%H", 3, "abc");
  922. #if JSON_ENABLE_HEX
  923. const char *r = "\"616263\"";
  924. ASSERT(strcmp(s, r) == 0);
  925. #else
  926. ASSERT(s == NULL);
  927. #endif
  928. allocator.free(s);
  929. return NULL;
  930. }
  931. static const char *test_json_printf_base64(void) {
  932. char *s = json_asprintf("{a:%d,b:%V}", 77, "hi", 2);
  933. #if JSON_ENABLE_BASE64
  934. const char *r = "{\"a\":77,\"b\":\"aGk=\"}";
  935. #else
  936. const char *r = "{\"a\":77,\"b\":}";
  937. #endif
  938. ASSERT(strcmp(s, r) == 0);
  939. allocator.free(s);
  940. return NULL;
  941. }
  942. static const char *run_all_tests(void) {
  943. // RUN_TEST(test_json_printf_hex);
  944. // RUN_TEST(test_json_printf_base64);
  945. RUN_TEST(test_json_next);
  946. RUN_TEST(test_prettify);
  947. RUN_TEST(test_eos);
  948. RUN_TEST(test_scanf);
  949. RUN_TEST(test_errors);
  950. RUN_TEST(test_json_printf);
  951. RUN_TEST(test_system);
  952. RUN_TEST(test_callback_api);
  953. RUN_TEST(test_callback_api_long_path);
  954. RUN_TEST(test_json_unescape);
  955. RUN_TEST(test_parse_string);
  956. RUN_TEST(test_fprintf);
  957. RUN_TEST(test_json_setf);
  958. return NULL;
  959. }
  960. int main(void) {
  961. allocator.alloc = epic_alloc;
  962. allocator.free = epic_free;
  963. const char *fail_msg = run_all_tests();
  964. printf("%s, tests run: %d\n", fail_msg ? "FAIL" : "PASS", static_num_tests);
  965. return fail_msg == NULL ? EXIT_SUCCESS : EXIT_FAILURE;
  966. }