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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. " \"exist\": 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 my_shallow_copy;
  89. int my_shallow_copy(json_object *src, json_object *parent, const char *key, size_t index,
  90. json_object **dst)
  91. {
  92. int rc;
  93. rc = json_c_shallow_copy_default(src, parent, key, index, dst);
  94. if (rc < 0)
  95. return rc;
  96. if (key != NULL && strcmp(key, "with_serializer") == 0)
  97. {
  98. printf("CALLED: my_shallow_copy on with_serializer object\n");
  99. void *userdata = json_object_get_userdata(src);
  100. json_object_set_serializer(*dst, my_custom_serializer, userdata, NULL);
  101. return 2;
  102. }
  103. return rc;
  104. }
  105. int main(int argc, char **argv)
  106. {
  107. struct json_object *src1, *src2, *src3;
  108. struct json_object *dst1 = NULL, *dst2 = NULL, *dst3 = NULL;
  109. int benchmark = 0;
  110. if (argc > 1 && strcmp(argv[1], "--benchmark") == 0)
  111. {
  112. benchmark = 1;
  113. }
  114. src1 = json_tokener_parse(json_str1);
  115. src2 = json_tokener_parse(json_str2);
  116. src3 = json_tokener_parse(json_str3);
  117. assert(src1 != NULL);
  118. assert(src2 != NULL);
  119. assert(src3 != NULL);
  120. printf("PASSED - loaded input data\n");
  121. /* do this 3 times to make sure overwriting it works */
  122. assert(0 == json_object_deep_copy(src1, &dst1, NULL));
  123. assert(0 == json_object_deep_copy(src2, &dst2, NULL));
  124. assert(0 == json_object_deep_copy(src3, &dst3, NULL));
  125. printf("PASSED - all json_object_deep_copy() returned successful\n");
  126. assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
  127. assert(errno == EINVAL);
  128. assert(-1 == json_object_deep_copy(src2, &dst2, NULL));
  129. assert(errno == EINVAL);
  130. assert(-1 == json_object_deep_copy(src3, &dst3, NULL));
  131. assert(errno == EINVAL);
  132. printf("PASSED - all json_object_deep_copy() returned EINVAL for non-null pointer\n");
  133. assert(1 == json_object_equal(src1, dst1));
  134. assert(1 == json_object_equal(src2, dst2));
  135. assert(1 == json_object_equal(src3, dst3));
  136. printf("PASSED - all json_object_equal() tests returned successful\n");
  137. assert(0 == strcmp(json_object_to_json_string_ext(src1, JSON_C_TO_STRING_PRETTY),
  138. json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY)));
  139. assert(0 == strcmp(json_object_to_json_string_ext(src2, JSON_C_TO_STRING_PRETTY),
  140. json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY)));
  141. assert(0 == strcmp(json_object_to_json_string_ext(src3, JSON_C_TO_STRING_PRETTY),
  142. json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY)));
  143. printf("PASSED - comparison of string output\n");
  144. json_object_get(dst1);
  145. assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
  146. assert(errno == EINVAL);
  147. json_object_put(dst1);
  148. printf("PASSED - trying to overrwrite an object that has refcount > 1");
  149. printf("\nPrinting JSON objects for visual inspection\n");
  150. printf("------------------------------------------------\n");
  151. printf(" JSON1\n");
  152. printf("%s\n", json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY));
  153. printf("------------------------------------------------\n");
  154. printf("------------------------------------------------\n");
  155. printf(" JSON2\n");
  156. printf("%s\n", json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY));
  157. printf("------------------------------------------------\n");
  158. printf("------------------------------------------------\n");
  159. printf(" JSON3\n");
  160. printf("------------------------------------------------\n");
  161. printf("%s\n", json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY));
  162. printf("------------------------------------------------\n");
  163. json_object_put(dst1);
  164. json_object_put(dst2);
  165. json_object_put(dst3);
  166. printf("\nTesting deep_copy with a custom serializer set\n");
  167. json_object *with_serializer = json_object_new_string("notemitted");
  168. char udata[] = "dummy userdata";
  169. json_object_set_serializer(with_serializer, my_custom_serializer, udata, NULL);
  170. json_object_object_add(src1, "with_serializer", with_serializer);
  171. dst1 = NULL;
  172. /* With a custom serializer in use, a custom shallow_copy function must also be used */
  173. assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
  174. assert(0 == json_object_deep_copy(src1, &dst1, my_shallow_copy));
  175. json_object *dest_with_serializer = json_object_object_get(dst1, "with_serializer");
  176. assert(dest_with_serializer != NULL);
  177. char *dst_userdata = json_object_get_userdata(dest_with_serializer);
  178. assert(strcmp(dst_userdata, "dummy userdata") == 0);
  179. const char *special_output = json_object_to_json_string(dest_with_serializer);
  180. assert(strcmp(special_output, "OTHER") == 0);
  181. printf("\ndeep_copy with custom serializer worked OK.\n");
  182. json_object_put(dst1);
  183. if (benchmark)
  184. {
  185. do_benchmark(src2);
  186. }
  187. json_object_put(src1);
  188. json_object_put(src2);
  189. json_object_put(src3);
  190. return 0;
  191. }
  192. static void do_benchmark(json_object *src2)
  193. {
  194. json_object *dst2 = NULL;
  195. int ii;
  196. /**
  197. * The numbers that I got are:
  198. * BENCHMARK - 1000000 iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took 71 seconds
  199. * BENCHMARK - 1000000 iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took 29 seconds
  200. */
  201. int iterations = 1000000;
  202. time_t start = time(NULL);
  203. start = time(NULL);
  204. for (ii = 0; ii < iterations; ii++)
  205. {
  206. dst2 = json_tokener_parse(json_object_get_string(src2));
  207. json_object_put(dst2);
  208. }
  209. printf("BENCHMARK - %d iterations of 'dst2 = "
  210. "json_tokener_parse(json_object_get_string(src2))' took %d seconds\n",
  211. iterations, (int)(time(NULL) - start));
  212. start = time(NULL);
  213. dst2 = NULL;
  214. for (ii = 0; ii < iterations; ii++)
  215. {
  216. json_object_deep_copy(src2, &dst2, NULL);
  217. json_object_put(dst2);
  218. dst2 = NULL;
  219. }
  220. printf("BENCHMARK - %d iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took %d "
  221. "seconds\n",
  222. iterations, (int)(time(NULL) - start));
  223. }