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 8.5 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.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 such as DocBook.\","
  34. " \"GlossSeeAlso\": [\"GML\", \"XML\"]"
  35. " },"
  36. " \"GlossSee\": \"markup\""
  37. " }"
  38. " }"
  39. " }"
  40. " }"
  41. "}";
  42. static const char *json_str2 =
  43. "{\"menu\": {"
  44. " \"header\": \"SVG Viewer\","
  45. " \"items\": ["
  46. " {\"id\": \"Open\"},"
  47. " {\"id\": \"OpenNew\", \"label\": \"Open New\"},"
  48. " null,"
  49. " {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},"
  50. " {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},"
  51. " {\"id\": \"OriginalView\", \"label\": \"Original View\"},"
  52. " null,"
  53. " {\"id\": \"Quality\", \"another_null\": null},"
  54. " {\"id\": \"Pause\"},"
  55. " {\"id\": \"Mute\"},"
  56. " null,"
  57. " {\"id\": \"Find\", \"label\": \"Find...\"},"
  58. " {\"id\": \"FindAgain\", \"label\": \"Find Again\"},"
  59. " {\"id\": \"Copy\"},"
  60. " {\"id\": \"CopyAgain\", \"label\": \"Copy Again\"},"
  61. " {\"id\": \"CopySVG\", \"label\": \"Copy SVG\"},"
  62. " {\"id\": \"ViewSVG\", \"label\": \"View SVG\"},"
  63. " {\"id\": \"ViewSource\", \"label\": \"View Source\"},"
  64. " {\"id\": \"SaveAs\", \"label\": \"Save As\"},"
  65. " null,"
  66. " {\"id\": \"Help\"},"
  67. " {\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}"
  68. " ]"
  69. "}}";
  70. static const char *json_str3 =
  71. "{\"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, json_object **dst)
  90. {
  91. int rc;
  92. rc = json_c_shallow_copy_default(src, parent, key, index, dst);
  93. if (rc < 0)
  94. return rc;
  95. if (key != NULL && strcmp(key, "with_serializer") == 0)
  96. {
  97. printf("CALLED: my_shallow_copy on with_serializer object\n");
  98. void *userdata = json_object_get_userdata(src);
  99. json_object_set_serializer(*dst, my_custom_serializer, userdata, NULL);
  100. return 2;
  101. }
  102. return rc;
  103. }
  104. int main(int argc, char **argv)
  105. {
  106. struct json_object *src1, *src2, *src3;
  107. struct json_object *dst1 = NULL, *dst2 = NULL, *dst3 = NULL;
  108. int benchmark = 0;
  109. if (argc > 1 && strcmp(argv[1], "--benchmark") == 0)
  110. {
  111. benchmark = 1;
  112. }
  113. src1 = json_tokener_parse(json_str1);
  114. src2 = json_tokener_parse(json_str2);
  115. src3 = json_tokener_parse(json_str3);
  116. assert(src1 != NULL);
  117. assert(src1 != NULL);
  118. assert(src3 != NULL);
  119. printf("PASSED - loaded input data\n");
  120. /* do this 3 times to make sure overwriting it works */
  121. assert(0 == json_object_deep_copy(src1, &dst1, NULL));
  122. assert(0 == json_object_deep_copy(src2, &dst2, NULL));
  123. assert(0 == json_object_deep_copy(src3, &dst3, NULL));
  124. printf("PASSED - all json_object_deep_copy() returned succesful\n");
  125. assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
  126. assert(errno == EINVAL);
  127. assert(-1 == json_object_deep_copy(src2, &dst2, NULL));
  128. assert(errno == EINVAL);
  129. assert(-1 == json_object_deep_copy(src3, &dst3, NULL));
  130. assert(errno == EINVAL);
  131. printf("PASSED - all json_object_deep_copy() returned EINVAL for non-null pointer\n");
  132. assert(1 == json_object_equal(src1, dst1));
  133. assert(1 == json_object_equal(src2, dst2));
  134. assert(1 == json_object_equal(src3, dst3));
  135. printf("PASSED - all json_object_equal() tests returned succesful\n");
  136. assert(0 == strcmp(json_object_to_json_string_ext(src1, JSON_C_TO_STRING_PRETTY),
  137. json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY)));
  138. assert(0 == strcmp(json_object_to_json_string_ext(src2, JSON_C_TO_STRING_PRETTY),
  139. json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY)));
  140. assert(0 == strcmp(json_object_to_json_string_ext(src3, JSON_C_TO_STRING_PRETTY),
  141. json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY)));
  142. printf("PASSED - comparison of string output\n");
  143. json_object_get(dst1);
  144. assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
  145. assert(errno == EINVAL);
  146. json_object_put(dst1);
  147. printf("PASSED - trying to overrwrite an object that has refcount > 1");
  148. printf("\nPrinting JSON objects for visual inspection\n");
  149. printf("------------------------------------------------\n");
  150. printf(" JSON1\n");
  151. printf("%s\n", json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY));
  152. printf("------------------------------------------------\n");
  153. printf("------------------------------------------------\n");
  154. printf(" JSON2\n");
  155. printf("%s\n", json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY));
  156. printf("------------------------------------------------\n");
  157. printf("------------------------------------------------\n");
  158. printf(" JSON3\n");
  159. printf("------------------------------------------------\n");
  160. printf("%s\n", json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY));
  161. printf("------------------------------------------------\n");
  162. json_object_put(dst1);
  163. json_object_put(dst2);
  164. json_object_put(dst3);
  165. printf("\nTesting deep_copy with a custom serializer set\n");
  166. json_object *with_serializer = json_object_new_string("notemitted");
  167. char udata[] = "dummy userdata";
  168. json_object_set_serializer(with_serializer, my_custom_serializer, udata, NULL);
  169. json_object_object_add(src1, "with_serializer", with_serializer);
  170. dst1 = NULL;
  171. /* With a custom serializer in use, a custom shallow_copy function must also be used */
  172. assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
  173. assert(0 == json_object_deep_copy(src1, &dst1, my_shallow_copy));
  174. json_object *dest_with_serializer = json_object_object_get(dst1, "with_serializer");
  175. assert(dest_with_serializer != NULL);
  176. char *dst_userdata = json_object_get_userdata(dest_with_serializer);
  177. assert(strcmp(dst_userdata, "dummy userdata") == 0);
  178. const char *special_output = json_object_to_json_string(dest_with_serializer);
  179. assert(strcmp(special_output, "OTHER") == 0);
  180. printf("\ndeep_copy with custom serializer worked OK.\n");
  181. json_object_put(dst1);
  182. if (benchmark)
  183. {
  184. do_benchmark(src2);
  185. }
  186. json_object_put(src1);
  187. json_object_put(src2);
  188. json_object_put(src3);
  189. return 0;
  190. }
  191. static void do_benchmark(json_object *src2)
  192. {
  193. json_object *dst2 = NULL;
  194. int ii;
  195. /**
  196. * The numbers that I got are:
  197. * BENCHMARK - 1000000 iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took 71 seconds
  198. * BENCHMARK - 1000000 iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took 29 seconds
  199. */
  200. int iterations = 1000000;
  201. time_t start = time(NULL);
  202. start = time(NULL);
  203. for (ii = 0; ii < iterations; ii++) {
  204. dst2 = json_tokener_parse(json_object_get_string(src2));
  205. json_object_put(dst2);
  206. }
  207. printf("BENCHMARK - %d iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took %d seconds\n", iterations, (int)(time(NULL) - start));
  208. start = time(NULL);
  209. dst2 = NULL;
  210. for (ii = 0; ii < iterations; ii++) {
  211. json_object_deep_copy(src2, &dst2, NULL);
  212. json_object_put(dst2);
  213. dst2 = NULL;
  214. }
  215. printf("BENCHMARK - %d iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took %d seconds\n", iterations, (int)(time(NULL) - start));
  216. }