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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <errno.h>
  7. #include <time.h>
  8. #include "json.h"
  9. static const char *json_str1 =
  10. "{"
  11. " \"glossary\": {"
  12. " \"title\": \"example glossary\","
  13. " \"GlossDiv\": {"
  14. " \"title\": \"S\","
  15. " \"null_obj\": null, "
  16. " \"GlossList\": {"
  17. " \"GlossEntry\": {"
  18. " \"ID\": \"SGML\","
  19. " \"SortAs\": \"SGML\","
  20. " \"GlossTerm\": \"Standard Generalized Markup Language\","
  21. " \"Acronym\": \"SGML\","
  22. " \"Abbrev\": \"ISO 8879:1986\","
  23. " \"GlossDef\": {"
  24. " \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\","
  25. " \"GlossSeeAlso\": [\"GML\", \"XML\"]"
  26. " },"
  27. " \"GlossSee\": \"markup\""
  28. " }"
  29. " }"
  30. " }"
  31. " }"
  32. "}";
  33. static const char *json_str2 =
  34. "{\"menu\": {"
  35. " \"header\": \"SVG Viewer\","
  36. " \"items\": ["
  37. " {\"id\": \"Open\"},"
  38. " {\"id\": \"OpenNew\", \"label\": \"Open New\"},"
  39. " null,"
  40. " {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},"
  41. " {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},"
  42. " {\"id\": \"OriginalView\", \"label\": \"Original View\"},"
  43. " null,"
  44. " {\"id\": \"Quality\", \"another_null\": null},"
  45. " {\"id\": \"Pause\"},"
  46. " {\"id\": \"Mute\"},"
  47. " null,"
  48. " {\"id\": \"Find\", \"label\": \"Find...\"},"
  49. " {\"id\": \"FindAgain\", \"label\": \"Find Again\"},"
  50. " {\"id\": \"Copy\"},"
  51. " {\"id\": \"CopyAgain\", \"label\": \"Copy Again\"},"
  52. " {\"id\": \"CopySVG\", \"label\": \"Copy SVG\"},"
  53. " {\"id\": \"ViewSVG\", \"label\": \"View SVG\"},"
  54. " {\"id\": \"ViewSource\", \"label\": \"View Source\"},"
  55. " {\"id\": \"SaveAs\", \"label\": \"Save As\"},"
  56. " null,"
  57. " {\"id\": \"Help\"},"
  58. " {\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}"
  59. " ]"
  60. "}}";
  61. static const char *json_str3 =
  62. "{\"menu\": {"
  63. " \"id\": \"file\","
  64. " \"value\": \"File\","
  65. " \"popup\": {"
  66. " \"menuitem\": ["
  67. " {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},"
  68. " {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},"
  69. " {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}"
  70. " ]"
  71. " }"
  72. "}}";
  73. int main(int argc, char **argv)
  74. {
  75. struct json_object *src1, *src2, *src3;
  76. struct json_object *dst1 = NULL, *dst2 = NULL, *dst3 = NULL;
  77. src1 = json_tokener_parse(json_str1);
  78. src2 = json_tokener_parse(json_str2);
  79. src3 = json_tokener_parse(json_str3);
  80. assert(src1 != NULL);
  81. assert(src1 != NULL);
  82. assert(src3 != NULL);
  83. printf("PASSED - loaded input data\n");
  84. /* do this 3 times to make sure overwriting it works */
  85. assert(0 == json_object_deep_copy(src1, &dst1));
  86. assert(0 == json_object_deep_copy(src2, &dst2));
  87. assert(0 == json_object_deep_copy(src3, &dst3));
  88. printf("PASSED - all json_object_deep_copy() returned succesful\n");
  89. assert(-1 == json_object_deep_copy(src1, &dst1));
  90. assert(errno == EINVAL);
  91. assert(-1 == json_object_deep_copy(src2, &dst2));
  92. assert(errno == EINVAL);
  93. assert(-1 == json_object_deep_copy(src3, &dst3));
  94. assert(errno == EINVAL);
  95. printf("PASSED - all json_object_deep_copy() returned EINVAL for non-null pointer\n");
  96. assert(1 == json_object_equal(src1, dst1));
  97. assert(1 == json_object_equal(src2, dst2));
  98. assert(1 == json_object_equal(src3, dst3));
  99. printf("PASSED - all json_object_equal() tests returned succesful\n");
  100. assert(0 == strcmp(json_object_to_json_string_ext(src1, JSON_C_TO_STRING_PRETTY),
  101. json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY)));
  102. assert(0 == strcmp(json_object_to_json_string_ext(src2, JSON_C_TO_STRING_PRETTY),
  103. json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY)));
  104. assert(0 == strcmp(json_object_to_json_string_ext(src3, JSON_C_TO_STRING_PRETTY),
  105. json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY)));
  106. printf("PASSED - comparison of string output\n");
  107. json_object_get(dst1);
  108. assert(-1 == json_object_deep_copy(src1, &dst1));
  109. assert(errno == EINVAL);
  110. printf("PASSED - trying to overrwrite an object that has refcount > 1");
  111. printf("\nPrinting JSON objects for visual inspection\n");
  112. printf("------------------------------------------------\n");
  113. printf(" JSON1\n");
  114. printf("%s", json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY));
  115. printf("------------------------------------------------\n");
  116. printf("------------------------------------------------\n");
  117. printf(" JSON2\n");
  118. printf("%s", json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY));
  119. printf("------------------------------------------------\n");
  120. printf("------------------------------------------------\n");
  121. printf(" JSON3\n");
  122. printf("------------------------------------------------\n");
  123. printf("%s", json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY));
  124. printf("------------------------------------------------\n");
  125. json_object_put(dst1);
  126. json_object_put(dst2);
  127. json_object_put(dst3);
  128. #if BENCHMARK
  129. /**
  130. * The numbers that I got are:
  131. * BENCHMARK - 1000000 iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took 20 seconds
  132. * BENCHMARK - 1000000 iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took 7 seconds
  133. */
  134. int iterations = 1000000;
  135. time_t start = time(NULL);
  136. for (i = 0; i < iterations; i++) {
  137. dst2 = json_tokener_parse(json_object_get_string(src2));
  138. json_object_put(dst2);
  139. }
  140. printf("BENCHMARK - %d iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took %d seconds\n", iterations, (int)(time(NULL) - start));
  141. start = time(NULL);
  142. for (i = 0; i < iterations; i++) {
  143. json_object_deep_copy(src2, &dst2);
  144. }
  145. json_object_put(dst2);
  146. printf("BENCHMARK - %d iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took %d seconds\n", iterations, (int)(time(NULL) - start));
  147. #endif
  148. return 0;
  149. }