test_object.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Copyright (c) 2009-2012 Petri Lehtinen <petri@digip.org>
  3. *
  4. * Jansson is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license. See LICENSE for details.
  6. */
  7. #include <jansson.h>
  8. #include <string.h>
  9. #include "util.h"
  10. static void test_clear()
  11. {
  12. json_t *object, *ten;
  13. object = json_object();
  14. ten = json_integer(10);
  15. if(!object)
  16. fail("unable to create object");
  17. if(!ten)
  18. fail("unable to create integer");
  19. if(json_object_set(object, "a", ten) ||
  20. json_object_set(object, "b", ten) ||
  21. json_object_set(object, "c", ten) ||
  22. json_object_set(object, "d", ten) ||
  23. json_object_set(object, "e", ten))
  24. fail("unable to set value");
  25. if(json_object_size(object) != 5)
  26. fail("invalid size");
  27. json_object_clear(object);
  28. if(json_object_size(object) != 0)
  29. fail("invalid size after clear");
  30. json_decref(ten);
  31. json_decref(object);
  32. }
  33. static void test_update()
  34. {
  35. json_t *object, *other, *nine, *ten;
  36. object = json_object();
  37. other = json_object();
  38. nine = json_integer(9);
  39. ten = json_integer(10);
  40. if(!object || !other)
  41. fail("unable to create object");
  42. if(!nine || !ten)
  43. fail("unable to create integer");
  44. /* update an empty object with an empty object */
  45. if(json_object_update(object, other))
  46. fail("unable to update an emtpy object with an empty object");
  47. if(json_object_size(object) != 0)
  48. fail("invalid size after update");
  49. if(json_object_size(other) != 0)
  50. fail("invalid size for updater after update");
  51. /* update an empty object with a nonempty object */
  52. if(json_object_set(other, "a", ten) ||
  53. json_object_set(other, "b", ten) ||
  54. json_object_set(other, "c", ten) ||
  55. json_object_set(other, "d", ten) ||
  56. json_object_set(other, "e", ten))
  57. fail("unable to set value");
  58. if(json_object_update(object, other))
  59. fail("unable to update an empty object");
  60. if(json_object_size(object) != 5)
  61. fail("invalid size after update");
  62. if(json_object_get(object, "a") != ten ||
  63. json_object_get(object, "b") != ten ||
  64. json_object_get(object, "c") != ten ||
  65. json_object_get(object, "d") != ten ||
  66. json_object_get(object, "e") != ten)
  67. fail("update works incorrectly");
  68. /* perform the same update again */
  69. if(json_object_update(object, other))
  70. fail("unable to update an empty object");
  71. if(json_object_size(object) != 5)
  72. fail("invalid size after update");
  73. if(json_object_get(object, "a") != ten ||
  74. json_object_get(object, "b") != ten ||
  75. json_object_get(object, "c") != ten ||
  76. json_object_get(object, "d") != ten ||
  77. json_object_get(object, "e") != ten)
  78. fail("update works incorrectly");
  79. /* update a nonempty object with a nonempty object with both old
  80. and new keys */
  81. if(json_object_clear(other))
  82. fail("clear failed");
  83. if(json_object_set(other, "a", nine) ||
  84. json_object_set(other, "b", nine) ||
  85. json_object_set(other, "f", nine) ||
  86. json_object_set(other, "g", nine) ||
  87. json_object_set(other, "h", nine))
  88. fail("unable to set value");
  89. if(json_object_update(object, other))
  90. fail("unable to update a nonempty object");
  91. if(json_object_size(object) != 8)
  92. fail("invalid size after update");
  93. if(json_object_get(object, "a") != nine ||
  94. json_object_get(object, "b") != nine ||
  95. json_object_get(object, "f") != nine ||
  96. json_object_get(object, "g") != nine ||
  97. json_object_get(object, "h") != nine)
  98. fail("update works incorrectly");
  99. json_decref(nine);
  100. json_decref(ten);
  101. json_decref(other);
  102. json_decref(object);
  103. }
  104. static void test_conditional_updates()
  105. {
  106. json_t *object, *other;
  107. object = json_pack("{sisi}", "foo", 1, "bar", 2);
  108. other = json_pack("{sisi}", "foo", 3, "baz", 4);
  109. if(json_object_update_existing(object, other))
  110. fail("json_object_update_existing failed");
  111. if(json_object_size(object) != 2)
  112. fail("json_object_update_existing added new items");
  113. if(json_integer_value(json_object_get(object, "foo")) != 3)
  114. fail("json_object_update_existing failed to update existing key");
  115. if(json_integer_value(json_object_get(object, "bar")) != 2)
  116. fail("json_object_update_existing updated wrong key");
  117. json_decref(object);
  118. object = json_pack("{sisi}", "foo", 1, "bar", 2);
  119. if(json_object_update_missing(object, other))
  120. fail("json_object_update_missing failed");
  121. if(json_object_size(object) != 3)
  122. fail("json_object_update_missing didn't add new items");
  123. if(json_integer_value(json_object_get(object, "foo")) != 1)
  124. fail("json_object_update_missing updated existing key");
  125. if(json_integer_value(json_object_get(object, "bar")) != 2)
  126. fail("json_object_update_missing updated wrong key");
  127. if(json_integer_value(json_object_get(object, "baz")) != 4)
  128. fail("json_object_update_missing didn't add new items");
  129. json_decref(object);
  130. json_decref(other);
  131. }
  132. static void test_circular()
  133. {
  134. json_t *object1, *object2;
  135. object1 = json_object();
  136. object2 = json_object();
  137. if(!object1 || !object2)
  138. fail("unable to create object");
  139. /* the simple case is checked */
  140. if(json_object_set(object1, "a", object1) == 0)
  141. fail("able to set self");
  142. /* create circular references */
  143. if(json_object_set(object1, "a", object2) ||
  144. json_object_set(object2, "a", object1))
  145. fail("unable to set value");
  146. /* circularity is detected when dumping */
  147. if(json_dumps(object1, 0) != NULL)
  148. fail("able to dump circulars");
  149. /* decref twice to deal with the circular references */
  150. json_decref(object1);
  151. json_decref(object2);
  152. json_decref(object1);
  153. }
  154. static void test_set_nocheck()
  155. {
  156. json_t *object, *string;
  157. object = json_object();
  158. string = json_string("bar");
  159. if(!object)
  160. fail("unable to create object");
  161. if(!string)
  162. fail("unable to create string");
  163. if(json_object_set_nocheck(object, "foo", string))
  164. fail("json_object_set_nocheck failed");
  165. if(json_object_get(object, "foo") != string)
  166. fail("json_object_get after json_object_set_nocheck failed");
  167. /* invalid UTF-8 in key */
  168. if(json_object_set_nocheck(object, "a\xefz", string))
  169. fail("json_object_set_nocheck failed for invalid UTF-8");
  170. if(json_object_get(object, "a\xefz") != string)
  171. fail("json_object_get after json_object_set_nocheck failed");
  172. if(json_object_set_new_nocheck(object, "bax", json_integer(123)))
  173. fail("json_object_set_new_nocheck failed");
  174. if(json_integer_value(json_object_get(object, "bax")) != 123)
  175. fail("json_object_get after json_object_set_new_nocheck failed");
  176. /* invalid UTF-8 in key */
  177. if(json_object_set_new_nocheck(object, "asdf\xfe", json_integer(321)))
  178. fail("json_object_set_new_nocheck failed for invalid UTF-8");
  179. if(json_integer_value(json_object_get(object, "asdf\xfe")) != 321)
  180. fail("json_object_get after json_object_set_new_nocheck failed");
  181. json_decref(string);
  182. json_decref(object);
  183. }
  184. static void test_iterators()
  185. {
  186. json_t *object, *foo, *bar, *baz;
  187. void *iter;
  188. if(json_object_iter(NULL))
  189. fail("able to iterate over NULL");
  190. if(json_object_iter_next(NULL, NULL))
  191. fail("able to increment an iterator on a NULL object");
  192. object = json_object();
  193. foo = json_string("foo");
  194. bar = json_string("bar");
  195. baz = json_string("baz");
  196. if(!object || !foo || !bar || !bar)
  197. fail("unable to create values");
  198. if(json_object_iter_next(object, NULL))
  199. fail("able to increment a NULL iterator");
  200. if(json_object_set(object, "a", foo) ||
  201. json_object_set(object, "b", bar) ||
  202. json_object_set(object, "c", baz))
  203. fail("unable to populate object");
  204. iter = json_object_iter(object);
  205. if(!iter)
  206. fail("unable to get iterator");
  207. if(strcmp(json_object_iter_key(iter), "a"))
  208. fail("iterating failed: wrong key");
  209. if(json_object_iter_value(iter) != foo)
  210. fail("iterating failed: wrong value");
  211. iter = json_object_iter_next(object, iter);
  212. if(!iter)
  213. fail("unable to increment iterator");
  214. if(strcmp(json_object_iter_key(iter), "b"))
  215. fail("iterating failed: wrong key");
  216. if(json_object_iter_value(iter) != bar)
  217. fail("iterating failed: wrong value");
  218. iter = json_object_iter_next(object, iter);
  219. if(!iter)
  220. fail("unable to increment iterator");
  221. if(strcmp(json_object_iter_key(iter), "c"))
  222. fail("iterating failed: wrong key");
  223. if(json_object_iter_value(iter) != baz)
  224. fail("iterating failed: wrong value");
  225. if(json_object_iter_next(object, iter) != NULL)
  226. fail("able to iterate over the end");
  227. if(json_object_iter_at(object, "foo"))
  228. fail("json_object_iter_at() succeeds for non-existent key");
  229. iter = json_object_iter_at(object, "b");
  230. if(!iter)
  231. fail("json_object_iter_at() fails for an existing key");
  232. if(strcmp(json_object_iter_key(iter), "b"))
  233. fail("iterating failed: wrong key");
  234. if(json_object_iter_value(iter) != bar)
  235. fail("iterating failed: wrong value");
  236. iter = json_object_iter_next(object, iter);
  237. if(!iter)
  238. fail("unable to increment iterator");
  239. if(strcmp(json_object_iter_key(iter), "c"))
  240. fail("iterating failed: wrong key");
  241. if(json_object_iter_value(iter) != baz)
  242. fail("iterating failed: wrong value");
  243. if(json_object_iter_set(object, iter, bar))
  244. fail("unable to set value at iterator");
  245. if(strcmp(json_object_iter_key(iter), "c"))
  246. fail("json_object_iter_key() fails after json_object_iter_set()");
  247. if(json_object_iter_value(iter) != bar)
  248. fail("json_object_iter_value() fails after json_object_iter_set()");
  249. if(json_object_get(object, "c") != bar)
  250. fail("json_object_get() fails after json_object_iter_set()");
  251. json_decref(object);
  252. json_decref(foo);
  253. json_decref(bar);
  254. json_decref(baz);
  255. }
  256. static void test_misc()
  257. {
  258. json_t *object, *string, *other_string, *value;
  259. object = json_object();
  260. string = json_string("test");
  261. other_string = json_string("other");
  262. if(!object)
  263. fail("unable to create object");
  264. if(!string || !other_string)
  265. fail("unable to create string");
  266. if(json_object_get(object, "a"))
  267. fail("value for nonexisting key");
  268. if(json_object_set(object, "a", string))
  269. fail("unable to set value");
  270. if(!json_object_set(object, NULL, string))
  271. fail("able to set NULL key");
  272. if(!json_object_set(object, "a", NULL))
  273. fail("able to set NULL value");
  274. /* invalid UTF-8 in key */
  275. if(!json_object_set(object, "a\xefz", string))
  276. fail("able to set invalid unicode key");
  277. value = json_object_get(object, "a");
  278. if(!value)
  279. fail("no value for existing key");
  280. if(value != string)
  281. fail("got different value than what was added");
  282. /* "a", "lp" and "px" collide in a five-bucket hashtable */
  283. if(json_object_set(object, "b", string) ||
  284. json_object_set(object, "lp", string) ||
  285. json_object_set(object, "px", string))
  286. fail("unable to set value");
  287. value = json_object_get(object, "a");
  288. if(!value)
  289. fail("no value for existing key");
  290. if(value != string)
  291. fail("got different value than what was added");
  292. if(json_object_set(object, "a", other_string))
  293. fail("unable to replace an existing key");
  294. value = json_object_get(object, "a");
  295. if(!value)
  296. fail("no value for existing key");
  297. if(value != other_string)
  298. fail("got different value than what was set");
  299. if(!json_object_del(object, "nonexisting"))
  300. fail("able to delete a nonexisting key");
  301. if(json_object_del(object, "px"))
  302. fail("unable to delete an existing key");
  303. if(json_object_del(object, "a"))
  304. fail("unable to delete an existing key");
  305. if(json_object_del(object, "lp"))
  306. fail("unable to delete an existing key");
  307. /* add many keys to initiate rehashing */
  308. if(json_object_set(object, "a", string))
  309. fail("unable to set value");
  310. if(json_object_set(object, "lp", string))
  311. fail("unable to set value");
  312. if(json_object_set(object, "px", string))
  313. fail("unable to set value");
  314. if(json_object_set(object, "c", string))
  315. fail("unable to set value");
  316. if(json_object_set(object, "d", string))
  317. fail("unable to set value");
  318. if(json_object_set(object, "e", string))
  319. fail("unable to set value");
  320. if(json_object_set_new(object, "foo", json_integer(123)))
  321. fail("unable to set new value");
  322. value = json_object_get(object, "foo");
  323. if(!json_is_integer(value) || json_integer_value(value) != 123)
  324. fail("json_object_set_new works incorrectly");
  325. if(!json_object_set_new(object, NULL, json_integer(432)))
  326. fail("able to set_new NULL key");
  327. if(!json_object_set_new(object, "foo", NULL))
  328. fail("able to set_new NULL value");
  329. json_decref(string);
  330. json_decref(other_string);
  331. json_decref(object);
  332. }
  333. static void test_preserve_order()
  334. {
  335. json_t *object;
  336. char *result;
  337. const char *expected = "{\"foobar\": 1, \"bazquux\": 6, \"lorem ipsum\": 3, \"sit amet\": 5, \"helicopter\": 7}";
  338. object = json_object();
  339. json_object_set_new(object, "foobar", json_integer(1));
  340. json_object_set_new(object, "bazquux", json_integer(2));
  341. json_object_set_new(object, "lorem ipsum", json_integer(3));
  342. json_object_set_new(object, "dolor", json_integer(4));
  343. json_object_set_new(object, "sit amet", json_integer(5));
  344. /* changing a value should preserve the order */
  345. json_object_set_new(object, "bazquux", json_integer(6));
  346. /* deletion shouldn't change the order of others */
  347. json_object_del(object, "dolor");
  348. /* add a new item just to make sure */
  349. json_object_set_new(object, "helicopter", json_integer(7));
  350. result = json_dumps(object, JSON_PRESERVE_ORDER);
  351. if(strcmp(expected, result) != 0) {
  352. fprintf(stderr, "%s != %s", expected, result);
  353. fail("JSON_PRESERVE_ORDER doesn't work");
  354. }
  355. free(result);
  356. json_decref(object);
  357. }
  358. static void test_foreach()
  359. {
  360. const char *key;
  361. json_t *object1, *object2, *value;
  362. object1 = json_pack("{sisisi}", "foo", 1, "bar", 2, "baz", 3);
  363. object2 = json_object();
  364. json_object_foreach(object1, key, value)
  365. json_object_set(object2, key, value);
  366. if(!json_equal(object1, object2))
  367. fail("json_object_foreach failed to iterate all key-value pairs");
  368. json_decref(object1);
  369. json_decref(object2);
  370. }
  371. static void run_tests()
  372. {
  373. test_misc();
  374. test_clear();
  375. test_update();
  376. test_conditional_updates();
  377. test_circular();
  378. test_set_nocheck();
  379. test_iterators();
  380. test_preserve_order();
  381. test_foreach();
  382. }
粤ICP备19079148号