test_copy.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright (c) 2009-2016 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 <string.h>
  8. #include <jansson.h>
  9. #include "util.h"
  10. static void test_copy_simple(void)
  11. {
  12. json_t *value, *copy;
  13. if(json_copy(NULL))
  14. fail("copying NULL doesn't return NULL");
  15. /* true */
  16. value = json_true();
  17. copy = json_copy(value);
  18. if(value != copy)
  19. fail("copying true failed");
  20. json_decref(value);
  21. json_decref(copy);
  22. /* false */
  23. value = json_false();
  24. copy = json_copy(value);
  25. if(value != copy)
  26. fail("copying false failed");
  27. json_decref(value);
  28. json_decref(copy);
  29. /* null */
  30. value = json_null();
  31. copy = json_copy(value);
  32. if(value != copy)
  33. fail("copying null failed");
  34. json_decref(value);
  35. json_decref(copy);
  36. /* string */
  37. value = json_string("foo");
  38. if(!value)
  39. fail("unable to create a string");
  40. copy = json_copy(value);
  41. if(!copy)
  42. fail("unable to copy a string");
  43. if(copy == value)
  44. fail("copying a string doesn't copy");
  45. if(!json_equal(copy, value))
  46. fail("copying a string produces an inequal copy");
  47. if(value->refcount != 1 || copy->refcount != 1)
  48. fail("invalid refcounts");
  49. json_decref(value);
  50. json_decref(copy);
  51. /* integer */
  52. value = json_integer(543);
  53. if(!value)
  54. fail("unable to create an integer");
  55. copy = json_copy(value);
  56. if(!copy)
  57. fail("unable to copy an integer");
  58. if(copy == value)
  59. fail("copying an integer doesn't copy");
  60. if(!json_equal(copy, value))
  61. fail("copying an integer produces an inequal copy");
  62. if(value->refcount != 1 || copy->refcount != 1)
  63. fail("invalid refcounts");
  64. json_decref(value);
  65. json_decref(copy);
  66. /* real */
  67. value = json_real(123e9);
  68. if(!value)
  69. fail("unable to create a real");
  70. copy = json_copy(value);
  71. if(!copy)
  72. fail("unable to copy a real");
  73. if(copy == value)
  74. fail("copying a real doesn't copy");
  75. if(!json_equal(copy, value))
  76. fail("copying a real produces an inequal copy");
  77. if(value->refcount != 1 || copy->refcount != 1)
  78. fail("invalid refcounts");
  79. json_decref(value);
  80. json_decref(copy);
  81. }
  82. static void test_deep_copy_simple(void)
  83. {
  84. json_t *value, *copy;
  85. if(json_deep_copy(NULL))
  86. fail("deep copying NULL doesn't return NULL");
  87. /* true */
  88. value = json_true();
  89. copy = json_deep_copy(value);
  90. if(value != copy)
  91. fail("deep copying true failed");
  92. json_decref(value);
  93. json_decref(copy);
  94. /* false */
  95. value = json_false();
  96. copy = json_deep_copy(value);
  97. if(value != copy)
  98. fail("deep copying false failed");
  99. json_decref(value);
  100. json_decref(copy);
  101. /* null */
  102. value = json_null();
  103. copy = json_deep_copy(value);
  104. if(value != copy)
  105. fail("deep copying null failed");
  106. json_decref(value);
  107. json_decref(copy);
  108. /* string */
  109. value = json_string("foo");
  110. if(!value)
  111. fail("unable to create a string");
  112. copy = json_deep_copy(value);
  113. if(!copy)
  114. fail("unable to deep copy a string");
  115. if(copy == value)
  116. fail("deep copying a string doesn't copy");
  117. if(!json_equal(copy, value))
  118. fail("deep copying a string produces an inequal copy");
  119. if(value->refcount != 1 || copy->refcount != 1)
  120. fail("invalid refcounts");
  121. json_decref(value);
  122. json_decref(copy);
  123. /* integer */
  124. value = json_integer(543);
  125. if(!value)
  126. fail("unable to create an integer");
  127. copy = json_deep_copy(value);
  128. if(!copy)
  129. fail("unable to deep copy an integer");
  130. if(copy == value)
  131. fail("deep copying an integer doesn't copy");
  132. if(!json_equal(copy, value))
  133. fail("deep copying an integer produces an inequal copy");
  134. if(value->refcount != 1 || copy->refcount != 1)
  135. fail("invalid refcounts");
  136. json_decref(value);
  137. json_decref(copy);
  138. /* real */
  139. value = json_real(123e9);
  140. if(!value)
  141. fail("unable to create a real");
  142. copy = json_deep_copy(value);
  143. if(!copy)
  144. fail("unable to deep copy a real");
  145. if(copy == value)
  146. fail("deep copying a real doesn't copy");
  147. if(!json_equal(copy, value))
  148. fail("deep copying a real produces an inequal copy");
  149. if(value->refcount != 1 || copy->refcount != 1)
  150. fail("invalid refcounts");
  151. json_decref(value);
  152. json_decref(copy);
  153. }
  154. static void test_copy_array(void)
  155. {
  156. const char *json_array_text = "[1, \"foo\", 3.141592, {\"foo\": \"bar\"}]";
  157. json_t *array, *copy;
  158. size_t i;
  159. array = json_loads(json_array_text, 0, NULL);
  160. if(!array)
  161. fail("unable to parse an array");
  162. copy = json_copy(array);
  163. if(!copy)
  164. fail("unable to copy an array");
  165. if(copy == array)
  166. fail("copying an array doesn't copy");
  167. if(!json_equal(copy, array))
  168. fail("copying an array produces an inequal copy");
  169. for(i = 0; i < json_array_size(copy); i++)
  170. {
  171. if(json_array_get(array, i) != json_array_get(copy, i))
  172. fail("copying an array modifies its elements");
  173. }
  174. json_decref(array);
  175. json_decref(copy);
  176. }
  177. static void test_deep_copy_array(void)
  178. {
  179. const char *json_array_text = "[1, \"foo\", 3.141592, {\"foo\": \"bar\"}]";
  180. json_t *array, *copy;
  181. size_t i;
  182. array = json_loads(json_array_text, 0, NULL);
  183. if(!array)
  184. fail("unable to parse an array");
  185. copy = json_deep_copy(array);
  186. if(!copy)
  187. fail("unable to deep copy an array");
  188. if(copy == array)
  189. fail("deep copying an array doesn't copy");
  190. if(!json_equal(copy, array))
  191. fail("deep copying an array produces an inequal copy");
  192. for(i = 0; i < json_array_size(copy); i++)
  193. {
  194. if(json_array_get(array, i) == json_array_get(copy, i))
  195. fail("deep copying an array doesn't copy its elements");
  196. }
  197. json_decref(array);
  198. json_decref(copy);
  199. }
  200. static void test_copy_object(void)
  201. {
  202. const char *json_object_text =
  203. "{\"foo\": \"bar\", \"a\": 1, \"b\": 3.141592, \"c\": [1,2,3,4]}";
  204. const char *keys[] = {"foo", "a", "b", "c"};
  205. int i;
  206. json_t *object, *copy;
  207. void *iter;
  208. object = json_loads(json_object_text, 0, NULL);
  209. if(!object)
  210. fail("unable to parse an object");
  211. copy = json_copy(object);
  212. if(!copy)
  213. fail("unable to copy an object");
  214. if(copy == object)
  215. fail("copying an object doesn't copy");
  216. if(!json_equal(copy, object))
  217. fail("copying an object produces an inequal copy");
  218. i = 0;
  219. iter = json_object_iter(object);
  220. while(iter)
  221. {
  222. const char *key;
  223. json_t *value1, *value2;
  224. key = json_object_iter_key(iter);
  225. value1 = json_object_iter_value(iter);
  226. value2 = json_object_get(copy, key);
  227. if(value1 != value2)
  228. fail("copying an object modifies its items");
  229. if (strcmp(key, keys[i]) != 0)
  230. fail("copying an object doesn't preserve key order");
  231. iter = json_object_iter_next(object, iter);
  232. i++;
  233. }
  234. json_decref(object);
  235. json_decref(copy);
  236. }
  237. static void test_deep_copy_object(void)
  238. {
  239. const char *json_object_text =
  240. "{\"foo\": \"bar\", \"a\": 1, \"b\": 3.141592, \"c\": [1,2,3,4]}";
  241. const char *keys[] = {"foo", "a", "b", "c"};
  242. int i;
  243. json_t *object, *copy;
  244. void *iter;
  245. object = json_loads(json_object_text, 0, NULL);
  246. if(!object)
  247. fail("unable to parse an object");
  248. copy = json_deep_copy(object);
  249. if(!copy)
  250. fail("unable to deep copy an object");
  251. if(copy == object)
  252. fail("deep copying an object doesn't copy");
  253. if(!json_equal(copy, object))
  254. fail("deep copying an object produces an inequal copy");
  255. i = 0;
  256. iter = json_object_iter(object);
  257. while(iter)
  258. {
  259. const char *key;
  260. json_t *value1, *value2;
  261. key = json_object_iter_key(iter);
  262. value1 = json_object_iter_value(iter);
  263. value2 = json_object_get(copy, key);
  264. if(value1 == value2)
  265. fail("deep copying an object doesn't copy its items");
  266. if (strcmp(key, keys[i]) != 0)
  267. fail("deep copying an object doesn't preserve key order");
  268. iter = json_object_iter_next(object, iter);
  269. i++;
  270. }
  271. json_decref(object);
  272. json_decref(copy);
  273. }
  274. static void run_tests()
  275. {
  276. test_copy_simple();
  277. test_deep_copy_simple();
  278. test_copy_array();
  279. test_deep_copy_array();
  280. test_copy_object();
  281. test_deep_copy_object();
  282. }