tests.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include "test.h"
  6. #include "testutil.h"
  7. #include <rtthread.h>
  8. #include <finsh.h>
  9. int test_empty(void)
  10. {
  11. check(parse("{}", 1, 1,
  12. JSMN_OBJECT, 0, 2, 0));
  13. check(parse("[]", 1, 1,
  14. JSMN_ARRAY, 0, 2, 0));
  15. check(parse("[{},{}]", 3, 3,
  16. JSMN_ARRAY, 0, 7, 2,
  17. JSMN_OBJECT, 1, 3, 0,
  18. JSMN_OBJECT, 4, 6, 0));
  19. return 0;
  20. }
  21. int test_object(void)
  22. {
  23. check(parse("{\"a\":0}", 3, 3,
  24. JSMN_OBJECT, 0, 7, 1,
  25. JSMN_STRING, "a", 1,
  26. JSMN_PRIMITIVE, "0"));
  27. check(parse("{\"a\":[]}", 3, 3,
  28. JSMN_OBJECT, 0, 8, 1,
  29. JSMN_STRING, "a", 1,
  30. JSMN_ARRAY, 5, 7, 0));
  31. check(parse("{\"a\":{},\"b\":{}}", 5, 5,
  32. JSMN_OBJECT, -1, -1, 2,
  33. JSMN_STRING, "a", 1,
  34. JSMN_OBJECT, -1, -1, 0,
  35. JSMN_STRING, "b", 1,
  36. JSMN_OBJECT, -1, -1, 0));
  37. check(parse("{\n \"Day\": 26,\n \"Month\": 9,\n \"Year\": 12\n }", 7, 7,
  38. JSMN_OBJECT, -1, -1, 3,
  39. JSMN_STRING, "Day", 1,
  40. JSMN_PRIMITIVE, "26",
  41. JSMN_STRING, "Month", 1,
  42. JSMN_PRIMITIVE, "9",
  43. JSMN_STRING, "Year", 1,
  44. JSMN_PRIMITIVE, "12"));
  45. check(parse("{\"a\": 0, \"b\": \"c\"}", 5, 5,
  46. JSMN_OBJECT, -1, -1, 2,
  47. JSMN_STRING, "a", 1,
  48. JSMN_PRIMITIVE, "0",
  49. JSMN_STRING, "b", 1,
  50. JSMN_STRING, "c", 0));
  51. #ifdef JSMN_STRICT
  52. check(parse("{\"a\"\n0}", JSMN_ERROR_INVAL, 3));
  53. check(parse("{\"a\", 0}", JSMN_ERROR_INVAL, 3));
  54. check(parse("{\"a\": {2}}", JSMN_ERROR_INVAL, 3));
  55. check(parse("{\"a\": {2: 3}}", JSMN_ERROR_INVAL, 3));
  56. check(parse("{\"a\": {\"a\": 2 3}}", JSMN_ERROR_INVAL, 5));
  57. /* FIXME */
  58. /*check(parse("{\"a\"}", JSMN_ERROR_INVAL, 2));*/
  59. /*check(parse("{\"a\": 1, \"b\"}", JSMN_ERROR_INVAL, 4));*/
  60. /*check(parse("{\"a\",\"b\":1}", JSMN_ERROR_INVAL, 4));*/
  61. /*check(parse("{\"a\":1,}", JSMN_ERROR_INVAL, 4));*/
  62. /*check(parse("{\"a\":\"b\":\"c\"}", JSMN_ERROR_INVAL, 4));*/
  63. /*check(parse("{,}", JSMN_ERROR_INVAL, 4));*/
  64. #endif
  65. return 0;
  66. }
  67. int test_array(void)
  68. {
  69. /* FIXME */
  70. /*check(parse("[10}", JSMN_ERROR_INVAL, 3));*/
  71. /*check(parse("[1,,3]", JSMN_ERROR_INVAL, 3)*/
  72. check(parse("[10]", 2, 2,
  73. JSMN_ARRAY, -1, -1, 1,
  74. JSMN_PRIMITIVE, "10"));
  75. check(parse("{\"a\": 1]", JSMN_ERROR_INVAL, 3));
  76. /* FIXME */
  77. /*check(parse("[\"a\": 1]", JSMN_ERROR_INVAL, 3));*/
  78. return 0;
  79. }
  80. int test_primitive(void)
  81. {
  82. check(parse("{\"boolVar\" : true }", 3, 3,
  83. JSMN_OBJECT, -1, -1, 1,
  84. JSMN_STRING, "boolVar", 1,
  85. JSMN_PRIMITIVE, "true"));
  86. check(parse("{\"boolVar\" : false }", 3, 3,
  87. JSMN_OBJECT, -1, -1, 1,
  88. JSMN_STRING, "boolVar", 1,
  89. JSMN_PRIMITIVE, "false"));
  90. check(parse("{\"nullVar\" : null }", 3, 3,
  91. JSMN_OBJECT, -1, -1, 1,
  92. JSMN_STRING, "nullVar", 1,
  93. JSMN_PRIMITIVE, "null"));
  94. check(parse("{\"intVar\" : 12}", 3, 3,
  95. JSMN_OBJECT, -1, -1, 1,
  96. JSMN_STRING, "intVar", 1,
  97. JSMN_PRIMITIVE, "12"));
  98. check(parse("{\"floatVar\" : 12.345}", 3, 3,
  99. JSMN_OBJECT, -1, -1, 1,
  100. JSMN_STRING, "floatVar", 1,
  101. JSMN_PRIMITIVE, "12.345"));
  102. return 0;
  103. }
  104. int test_string(void)
  105. {
  106. check(parse("{\"strVar\" : \"hello world\"}", 3, 3,
  107. JSMN_OBJECT, -1, -1, 1,
  108. JSMN_STRING, "strVar", 1,
  109. JSMN_STRING, "hello world", 0));
  110. check(parse("{\"strVar\" : \"escapes: \\/\\r\\n\\t\\b\\f\\\"\\\\\"}", 3, 3,
  111. JSMN_OBJECT, -1, -1, 1,
  112. JSMN_STRING, "strVar", 1,
  113. JSMN_STRING, "escapes: \\/\\r\\n\\t\\b\\f\\\"\\\\", 0));
  114. check(parse("{\"strVar\": \"\"}", 3, 3,
  115. JSMN_OBJECT, -1, -1, 1,
  116. JSMN_STRING, "strVar", 1,
  117. JSMN_STRING, "", 0));
  118. check(parse("{\"a\":\"\\uAbcD\"}", 3, 3,
  119. JSMN_OBJECT, -1, -1, 1,
  120. JSMN_STRING, "a", 1,
  121. JSMN_STRING, "\\uAbcD", 0));
  122. check(parse("{\"a\":\"str\\u0000\"}", 3, 3,
  123. JSMN_OBJECT, -1, -1, 1,
  124. JSMN_STRING, "a", 1,
  125. JSMN_STRING, "str\\u0000", 0));
  126. check(parse("{\"a\":\"\\uFFFFstr\"}", 3, 3,
  127. JSMN_OBJECT, -1, -1, 1,
  128. JSMN_STRING, "a", 1,
  129. JSMN_STRING, "\\uFFFFstr", 0));
  130. check(parse("{\"a\":[\"\\u0280\"]}", 4, 4,
  131. JSMN_OBJECT, -1, -1, 1,
  132. JSMN_STRING, "a", 1,
  133. JSMN_ARRAY, -1, -1, 1,
  134. JSMN_STRING, "\\u0280", 0));
  135. check(parse("{\"a\":\"str\\uFFGFstr\"}", JSMN_ERROR_INVAL, 3));
  136. check(parse("{\"a\":\"str\\u@FfF\"}", JSMN_ERROR_INVAL, 3));
  137. check(parse("{{\"a\":[\"\\u028\"]}", JSMN_ERROR_INVAL, 4));
  138. return 0;
  139. }
  140. int test_partial_string(void)
  141. {
  142. int i;
  143. int r;
  144. jsmn_parser p;
  145. jsmntok_t tok[5];
  146. const char *js = "{\"x\": \"va\\\\ue\", \"y\": \"value y\"}";
  147. jsmn_init(&p);
  148. for (i = 1; i <= strlen(js); i++)
  149. {
  150. r = jsmn_parse(&p, js, i, tok, sizeof(tok) / sizeof(tok[0]));
  151. if (i == strlen(js))
  152. {
  153. check(r == 5);
  154. check(tokeq(js, tok, 5,
  155. JSMN_OBJECT, -1, -1, 2,
  156. JSMN_STRING, "x", 1,
  157. JSMN_STRING, "va\\\\ue", 0,
  158. JSMN_STRING, "y", 1,
  159. JSMN_STRING, "value y", 0));
  160. }
  161. else
  162. {
  163. check(r == JSMN_ERROR_PART);
  164. }
  165. }
  166. return 0;
  167. }
  168. int test_partial_array(void)
  169. {
  170. #ifdef JSMN_STRICT
  171. int r;
  172. int i;
  173. jsmn_parser p;
  174. jsmntok_t tok[10];
  175. const char *js = "[ 1, true, [123, \"hello\"]]";
  176. jsmn_init(&p);
  177. for (i = 1; i <= strlen(js); i++)
  178. {
  179. r = jsmn_parse(&p, js, i, tok, sizeof(tok) / sizeof(tok[0]));
  180. if (i == strlen(js))
  181. {
  182. check(r == 6);
  183. check(tokeq(js, tok, 6,
  184. JSMN_ARRAY, -1, -1, 3,
  185. JSMN_PRIMITIVE, "1",
  186. JSMN_PRIMITIVE, "true",
  187. JSMN_ARRAY, -1, -1, 2,
  188. JSMN_PRIMITIVE, "123",
  189. JSMN_STRING, "hello", 0));
  190. }
  191. else
  192. {
  193. check(r == JSMN_ERROR_PART);
  194. }
  195. }
  196. #endif
  197. return 0;
  198. }
  199. int test_array_nomem(void)
  200. {
  201. int i;
  202. int r;
  203. jsmn_parser p;
  204. jsmntok_t toksmall[10], toklarge[10];
  205. const char *js;
  206. js = " [ 1, true, [123, \"hello\"]]";
  207. for (i = 0; i < 6; i++)
  208. {
  209. jsmn_init(&p);
  210. memset(toksmall, 0, sizeof(toksmall));
  211. memset(toklarge, 0, sizeof(toklarge));
  212. r = jsmn_parse(&p, js, strlen(js), toksmall, i);
  213. check(r == JSMN_ERROR_NOMEM);
  214. memcpy(toklarge, toksmall, sizeof(toksmall));
  215. r = jsmn_parse(&p, js, strlen(js), toklarge, 10);
  216. check(r >= 0);
  217. check(tokeq(js, toklarge, 4,
  218. JSMN_ARRAY, -1, -1, 3,
  219. JSMN_PRIMITIVE, "1",
  220. JSMN_PRIMITIVE, "true",
  221. JSMN_ARRAY, -1, -1, 2,
  222. JSMN_PRIMITIVE, "123",
  223. JSMN_STRING, "hello", 0));
  224. }
  225. return 0;
  226. }
  227. int test_unquoted_keys(void)
  228. {
  229. #ifndef JSMN_STRICT
  230. int r;
  231. jsmn_parser p;
  232. jsmntok_t tok[10];
  233. const char *js;
  234. jsmn_init(&p);
  235. js = "key1: \"value\"\nkey2 : 123";
  236. r = jsmn_parse(&p, js, strlen(js), tok, 10);
  237. check(r >= 0);
  238. check(tokeq(js, tok, 4,
  239. JSMN_PRIMITIVE, "key1",
  240. JSMN_STRING, "value", 0,
  241. JSMN_PRIMITIVE, "key2",
  242. JSMN_PRIMITIVE, "123"));
  243. #endif
  244. return 0;
  245. }
  246. int test_issue_22(void)
  247. {
  248. int r;
  249. jsmn_parser p;
  250. jsmntok_t tokens[128];
  251. const char *js;
  252. js = "{ \"height\":10, \"layers\":[ { \"data\":[6,6], \"height\":10, "
  253. "\"name\":\"Calque de Tile 1\", \"opacity\":1, \"type\":\"tilelayer\", "
  254. "\"visible\":true, \"width\":10, \"x\":0, \"y\":0 }], "
  255. "\"orientation\":\"orthogonal\", \"properties\": { }, \"tileheight\":32, "
  256. "\"tilesets\":[ { \"firstgid\":1, \"image\":\"..\\/images\\/tiles.png\", "
  257. "\"imageheight\":64, \"imagewidth\":160, \"margin\":0, \"name\":\"Tiles\", "
  258. "\"properties\":{}, \"spacing\":0, \"tileheight\":32, \"tilewidth\":32 }], "
  259. "\"tilewidth\":32, \"version\":1, \"width\":10 }";
  260. jsmn_init(&p);
  261. r = jsmn_parse(&p, js, strlen(js), tokens, 128);
  262. check(r >= 0);
  263. return 0;
  264. }
  265. int test_issue_27(void)
  266. {
  267. const char *js =
  268. "{ \"name\" : \"Jack\", \"age\" : 27 } { \"name\" : \"Anna\", ";
  269. check(parse(js, JSMN_ERROR_PART, 8));
  270. return 0;
  271. }
  272. int test_input_length(void)
  273. {
  274. const char *js;
  275. int r;
  276. jsmn_parser p;
  277. jsmntok_t tokens[10];
  278. js = "{\"a\": 0}garbage";
  279. jsmn_init(&p);
  280. r = jsmn_parse(&p, js, 8, tokens, 10);
  281. check(r == 3);
  282. check(tokeq(js, tokens, 3,
  283. JSMN_OBJECT, -1, -1, 1,
  284. JSMN_STRING, "a", 1,
  285. JSMN_PRIMITIVE, "0"));
  286. return 0;
  287. }
  288. int test_count(void)
  289. {
  290. jsmn_parser p;
  291. const char *js;
  292. js = "{}";
  293. jsmn_init(&p);
  294. check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 1);
  295. js = "[]";
  296. jsmn_init(&p);
  297. check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 1);
  298. js = "[[]]";
  299. jsmn_init(&p);
  300. check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 2);
  301. js = "[[], []]";
  302. jsmn_init(&p);
  303. check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 3);
  304. js = "[[], []]";
  305. jsmn_init(&p);
  306. check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 3);
  307. js = "[[], [[]], [[], []]]";
  308. jsmn_init(&p);
  309. check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 7);
  310. js = "[\"a\", [[], []]]";
  311. jsmn_init(&p);
  312. check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 5);
  313. js = "[[], \"[], [[]]\", [[]]]";
  314. jsmn_init(&p);
  315. check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 5);
  316. js = "[1, 2, 3]";
  317. jsmn_init(&p);
  318. check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 4);
  319. js = "[1, 2, [3, \"a\"], null]";
  320. jsmn_init(&p);
  321. check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 7);
  322. return 0;
  323. }
  324. int test_nonstrict(void)
  325. {
  326. #ifndef JSMN_STRICT
  327. const char *js;
  328. js = "a: 0garbage";
  329. check(parse(js, 2, 2,
  330. JSMN_PRIMITIVE, "a",
  331. JSMN_PRIMITIVE, "0garbage"));
  332. js = "Day : 26\nMonth : Sep\n\nYear: 12";
  333. check(parse(js, 6, 6,
  334. JSMN_PRIMITIVE, "Day",
  335. JSMN_PRIMITIVE, "26",
  336. JSMN_PRIMITIVE, "Month",
  337. JSMN_PRIMITIVE, "Sep",
  338. JSMN_PRIMITIVE, "Year",
  339. JSMN_PRIMITIVE, "12"));
  340. //nested {s don't cause a parse error.
  341. js = "\"key {1\": 1234";
  342. check(parse(js, 2, 2,
  343. JSMN_STRING, "key {1", 1,
  344. JSMN_PRIMITIVE, "1234"));
  345. #endif
  346. return 0;
  347. }
  348. int test_unmatched_brackets(void)
  349. {
  350. const char *js;
  351. js = "\"key 1\": 1234}";
  352. check(parse(js, JSMN_ERROR_INVAL, 2));
  353. js = "{\"key 1\": 1234";
  354. check(parse(js, JSMN_ERROR_PART, 3));
  355. js = "{\"key 1\": 1234}}";
  356. check(parse(js, JSMN_ERROR_INVAL, 3));
  357. js = "\"key 1\"}: 1234";
  358. check(parse(js, JSMN_ERROR_INVAL, 3));
  359. js = "{\"key {1\": 1234}";
  360. check(parse(js, 3, 3,
  361. JSMN_OBJECT, 0, 16, 1,
  362. JSMN_STRING, "key {1", 1,
  363. JSMN_PRIMITIVE, "1234"));
  364. js = "{{\"key 1\": 1234}";
  365. check(parse(js, JSMN_ERROR_PART, 4));
  366. return 0;
  367. }
  368. int testfunc(void)
  369. {
  370. test(test_empty, "test for a empty JSON objects/arrays");
  371. test(test_object, "test for a JSON objects");
  372. test(test_array, "test for a JSON arrays");
  373. test(test_primitive, "test primitive JSON data types");
  374. test(test_string, "test string JSON data types");
  375. test(test_partial_string, "test partial JSON string parsing");
  376. test(test_partial_array, "test partial array reading");
  377. test(test_array_nomem, "test array reading with a smaller number of tokens");
  378. test(test_unquoted_keys, "test unquoted keys (like in JavaScript)");
  379. test(test_input_length, "test strings that are not null-terminated");
  380. test(test_issue_22, "test issue #22");
  381. test(test_issue_27, "test issue #27");
  382. test(test_count, "test tokens count estimation");
  383. test(test_nonstrict, "test for non-strict mode");
  384. test(test_unmatched_brackets, "test for unmatched brackets");
  385. printf("\nPASSED: %d\nFAILED: %d\n", test_passed, test_failed);
  386. return (test_failed > 0);
  387. }
  388. MSH_CMD_EXPORT(testfunc, test example);