You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_deep_copy.c 9.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include <stddef.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #ifdef NDEBUG
  6. #undef NDEBUG
  7. #endif
  8. #include <assert.h>
  9. #include <errno.h>
  10. #include <time.h>
  11. #include "json.h"
  12. #include "printbuf.h"
  13. static void do_benchmark(json_object *src1);
  14. static const char *json_str1 =
  15. "{"
  16. " \"glossary\": {"
  17. " \"title\": \"example glossary\","
  18. " \"GlossDiv\": {"
  19. " \"number\": 16446744073709551615,"
  20. " \"title\": \"S\","
  21. " \"null_obj\": null, "
  22. " \"exixt\": false,"
  23. " \"quantity\":20,"
  24. " \"univalent\":19.8,"
  25. " \"GlossList\": {"
  26. " \"GlossEntry\": {"
  27. " \"ID\": \"SGML\","
  28. " \"SortAs\": \"SGML\","
  29. " \"GlossTerm\": \"Standard Generalized Markup Language\","
  30. " \"Acronym\": \"SGML\","
  31. " \"Abbrev\": \"ISO 8879:1986\","
  32. " \"GlossDef\": {"
  33. " \"para\": \"A meta-markup language, used to create markup languages "
  34. "such as DocBook.\","
  35. " \"GlossSeeAlso\": [\"GML\", \"XML\"]"
  36. " },"
  37. " \"GlossSee\": \"markup\""
  38. " }"
  39. " }"
  40. " }"
  41. " }"
  42. "}";
  43. static const char *json_str2 =
  44. "{\"menu\": {"
  45. " \"header\": \"SVG Viewer\","
  46. " \"items\": ["
  47. " {\"id\": \"Open\"},"
  48. " {\"id\": \"OpenNew\", \"label\": \"Open New\"},"
  49. " null,"
  50. " {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},"
  51. " {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},"
  52. " {\"id\": \"OriginalView\", \"label\": \"Original View\"},"
  53. " null,"
  54. " {\"id\": \"Quality\", \"another_null\": null},"
  55. " {\"id\": \"Pause\"},"
  56. " {\"id\": \"Mute\"},"
  57. " null,"
  58. " {\"id\": \"Find\", \"label\": \"Find...\"},"
  59. " {\"id\": \"FindAgain\", \"label\": \"Find Again\"},"
  60. " {\"id\": \"Copy\"},"
  61. " {\"id\": \"CopyAgain\", \"label\": \"Copy Again\"},"
  62. " {\"id\": \"CopySVG\", \"label\": \"Copy SVG\"},"
  63. " {\"id\": \"ViewSVG\", \"label\": \"View SVG\"},"
  64. " {\"id\": \"ViewSource\", \"label\": \"View Source\"},"
  65. " {\"id\": \"SaveAs\", \"label\": \"Save As\"},"
  66. " null,"
  67. " {\"id\": \"Help\"},"
  68. " {\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}"
  69. " ]"
  70. "}}";
  71. static const char *json_str3 = "{\"menu\": {"
  72. " \"id\": \"file\","
  73. " \"value\": \"File\","
  74. " \"popup\": {"
  75. " \"menuitem\": ["
  76. " {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},"
  77. " {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},"
  78. " {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}"
  79. " ]"
  80. " }"
  81. "}}";
  82. json_object_to_json_string_fn my_custom_serializer;
  83. int my_custom_serializer(struct json_object *jso, struct printbuf *pb, int level, int flags)
  84. {
  85. sprintbuf(pb, "OTHER");
  86. return 0;
  87. }
  88. json_c_shallow_copy_fn_len my_shallow_copy;
  89. int my_shallow_copy(json_object *src, json_object *parent, const struct json_key *key, size_t index,
  90. json_object **dst)
  91. {
  92. int rc;
  93. rc = json_c_shallow_copy_default_len(src, parent, key, index, dst);
  94. if (rc < 0)
  95. return rc;
  96. if (key != NULL && json_key_size(key) == 15 &&
  97. strcmp(json_key_data(key), "with_serializer") == 0)
  98. {
  99. printf("CALLED: my_shallow_copy on with_serializer object\n");
  100. void *userdata = json_object_get_userdata(src);
  101. json_object_set_serializer(*dst, my_custom_serializer, userdata, NULL);
  102. return 2;
  103. }
  104. return rc;
  105. }
  106. int main(int argc, char **argv)
  107. {
  108. struct json_object *src1, *src2, *src3;
  109. struct json_object *dst1 = NULL, *dst2 = NULL, *dst3 = NULL;
  110. int benchmark = 0;
  111. if (argc > 1 && strcmp(argv[1], "--benchmark") == 0)
  112. {
  113. benchmark = 1;
  114. }
  115. src1 = json_tokener_parse(json_str1);
  116. src2 = json_tokener_parse(json_str2);
  117. src3 = json_tokener_parse(json_str3);
  118. assert(src1 != NULL);
  119. assert(src2 != NULL);
  120. assert(src3 != NULL);
  121. printf("PASSED - loaded input data\n");
  122. /* do this 3 times to make sure overwriting it works */
  123. assert(0 == json_object_deep_copy(src1, &dst1, NULL));
  124. assert(0 == json_object_deep_copy(src2, &dst2, NULL));
  125. assert(0 == json_object_deep_copy(src3, &dst3, NULL));
  126. printf("PASSED - all json_object_deep_copy() returned succesful\n");
  127. assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
  128. assert(errno == EINVAL);
  129. assert(-1 == json_object_deep_copy(src2, &dst2, NULL));
  130. assert(errno == EINVAL);
  131. assert(-1 == json_object_deep_copy(src3, &dst3, NULL));
  132. assert(errno == EINVAL);
  133. printf("PASSED - all json_object_deep_copy() returned EINVAL for non-null pointer\n");
  134. assert(1 == json_object_equal(src1, dst1));
  135. assert(1 == json_object_equal(src2, dst2));
  136. assert(1 == json_object_equal(src3, dst3));
  137. printf("PASSED - all json_object_equal() tests returned succesful\n");
  138. assert(0 == strcmp(json_object_to_json_string_ext(src1, JSON_C_TO_STRING_PRETTY),
  139. json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY)));
  140. assert(0 == strcmp(json_object_to_json_string_ext(src2, JSON_C_TO_STRING_PRETTY),
  141. json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY)));
  142. assert(0 == strcmp(json_object_to_json_string_ext(src3, JSON_C_TO_STRING_PRETTY),
  143. json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY)));
  144. printf("PASSED - comparison of string output\n");
  145. json_object_get(dst1);
  146. assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
  147. assert(errno == EINVAL);
  148. json_object_put(dst1);
  149. printf("PASSED - trying to overrwrite an object that has refcount > 1");
  150. printf("\nPrinting JSON objects for visual inspection\n");
  151. printf("------------------------------------------------\n");
  152. printf(" JSON1\n");
  153. printf("%s\n", json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY));
  154. printf("------------------------------------------------\n");
  155. printf("------------------------------------------------\n");
  156. printf(" JSON2\n");
  157. printf("%s\n", json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY));
  158. printf("------------------------------------------------\n");
  159. printf("------------------------------------------------\n");
  160. printf(" JSON3\n");
  161. printf("------------------------------------------------\n");
  162. printf("%s\n", json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY));
  163. printf("------------------------------------------------\n");
  164. json_object_put(dst1);
  165. json_object_put(dst2);
  166. json_object_put(dst3);
  167. printf("\nTesting deep_copy with a custom serializer set\n");
  168. json_object *with_serializer = json_object_new_string("notemitted");
  169. char udata[] = "dummy userdata";
  170. json_object_set_serializer(with_serializer, my_custom_serializer, udata, NULL);
  171. json_object_object_add(src1, "with_serializer", with_serializer);
  172. dst1 = NULL;
  173. /* With a custom serializer in use, a custom shallow_copy function must also be used */
  174. assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
  175. assert(0 == json_object_deep_copy_len(src1, &dst1, my_shallow_copy));
  176. json_object *dest_with_serializer = json_object_object_get(dst1, "with_serializer");
  177. assert(dest_with_serializer != NULL);
  178. char *dst_userdata = json_object_get_userdata(dest_with_serializer);
  179. assert(strcmp(dst_userdata, "dummy userdata") == 0);
  180. const char *special_output = json_object_to_json_string(dest_with_serializer);
  181. assert(strcmp(special_output, "OTHER") == 0);
  182. printf("\ndeep_copy with custom serializer worked OK.\n");
  183. json_object_put(dst1);
  184. if (benchmark)
  185. {
  186. do_benchmark(src2);
  187. }
  188. json_object_put(src1);
  189. json_object_put(src2);
  190. json_object_put(src3);
  191. return 0;
  192. }
  193. static void do_benchmark(json_object *src2)
  194. {
  195. json_object *dst2 = NULL;
  196. int ii;
  197. /**
  198. * The numbers that I got are:
  199. * BENCHMARK - 1000000 iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took 71 seconds
  200. * BENCHMARK - 1000000 iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took 29 seconds
  201. */
  202. int iterations = 1000000;
  203. time_t start = time(NULL);
  204. start = time(NULL);
  205. for (ii = 0; ii < iterations; ii++)
  206. {
  207. dst2 = json_tokener_parse(json_object_get_string(src2));
  208. json_object_put(dst2);
  209. }
  210. printf("BENCHMARK - %d iterations of 'dst2 = "
  211. "json_tokener_parse(json_object_get_string(src2))' took %d seconds\n",
  212. iterations, (int)(time(NULL) - start));
  213. start = time(NULL);
  214. dst2 = NULL;
  215. for (ii = 0; ii < iterations; ii++)
  216. {
  217. json_object_deep_copy(src2, &dst2, NULL);
  218. json_object_put(dst2);
  219. dst2 = NULL;
  220. }
  221. printf("BENCHMARK - %d iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took %d "
  222. "seconds\n",
  223. iterations, (int)(time(NULL) - start));
  224. }