RFC8259JsonTest.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <time.h>
  6. #include <sys/stat.h>
  7. #include <dirent.h>
  8. // #include <intrin.h>
  9. #include "RyanJson.h"
  10. #include "cJSON.h"
  11. #include "yyjson.h"
  12. #include "valloc.h"
  13. // #define PrintfStrCmpEnable
  14. typedef int (*jsonParseData)(char *file_name, char *data, uint32_t len);
  15. static void *yy_malloc(void *ctx, size_t size)
  16. {
  17. return v_malloc(size);
  18. }
  19. static void *yy_realloc(void *ctx, void *ptr, size_t old_size, size_t size)
  20. {
  21. return v_realloc(ptr, size);
  22. }
  23. static void yy_free(void *ctx, void *ptr)
  24. {
  25. v_free(ptr);
  26. }
  27. static yyjson_alc yyalc = {
  28. yy_malloc,
  29. yy_realloc,
  30. yy_free,
  31. NULL};
  32. /* Read a file, parse, render back, etc. */
  33. int testFile(const char *path, jsonParseData jsonParseDataHandle)
  34. {
  35. DIR *dir = NULL;
  36. struct dirent *entry;
  37. int idx = 0, alc = 0;
  38. char **names = NULL, **names_tmp;
  39. int path_len = strlen(path);
  40. int count = 0;
  41. int used_count = 0;
  42. if (!path || !path_len || !(dir = opendir(path)))
  43. goto fail;
  44. while ((entry = readdir(dir)))
  45. {
  46. char *name = (char *)entry->d_name;
  47. if (!name || !strlen(name))
  48. continue;
  49. if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
  50. continue;
  51. char aaa[300] = {0};
  52. snprintf(aaa, sizeof(aaa), "%s/%s", path, name);
  53. FILE *f = fopen(aaa, "rb");
  54. if (f == NULL)
  55. goto fail;
  56. fseek(f, 0, SEEK_END);
  57. long len = ftell(f);
  58. fseek(f, 0, SEEK_SET);
  59. char *data = (char *)malloc(len + 10);
  60. fread(data, 1, len, f);
  61. data[len] = '\0';
  62. fclose(f);
  63. int status = 0;
  64. status = jsonParseDataHandle(name, data, len);
  65. used_count++;
  66. if (0 == strncmp("y_", name, 2))
  67. {
  68. if (0 == status)
  69. count++;
  70. else
  71. printf("应该成功,但是失败: %s, len: %ld\n", data, len);
  72. }
  73. else if (0 == strncmp("n_", name, 2))
  74. {
  75. if (0 != status)
  76. count++;
  77. else
  78. printf("应该失败,但是成功: %s, len: %ld\n", data, len);
  79. }
  80. else if (0 == strncmp("i_", name, 2))
  81. {
  82. count++;
  83. }
  84. int area = 0, use = 0;
  85. v_mcheck(&area, &use);
  86. if (use != (len + 10))
  87. {
  88. free(data);
  89. printf("内存泄漏 %s len: %ld\r\n", data, len);
  90. printf("内存泄漏 %x len: %ld\r\n", data, len);
  91. printf("内存泄漏 %c len: %ld\r\n", data, len);
  92. printf("|||----------->>> area = %d, size = %d\r\n", area, use);
  93. break;
  94. }
  95. free(data);
  96. }
  97. closedir(dir);
  98. printf("RFC 8259 JSON: (%d/%d)\r\n", count, used_count);
  99. return 0;
  100. fail:
  101. if (dir)
  102. closedir(dir);
  103. return -1;
  104. }
  105. /**
  106. * @brief RyanJson 测试程序
  107. *
  108. * @param file_name
  109. * @param data
  110. * @param len
  111. * @return int
  112. */
  113. int RyanJsonParseData(char *file_name, char *data, uint32_t len)
  114. {
  115. if (strcmp(file_name, "n_structure_100000_opening_arrays.json") == 0 ||
  116. strcmp(file_name, "n_structure_open_array_object.json") == 0 ||
  117. strcmp(file_name, "n_structure_100000_opening_arrays.json") == 0)
  118. return -1;
  119. RyanJson_t json = RyanJsonParseOptions(data, len, RyanJsonTrue, NULL);
  120. if (NULL == json)
  121. return -1;
  122. #ifdef PrintfStrCmpEnable
  123. char *str = RyanJsonPrint(json, 60, RyanJsonFalse, NULL);
  124. if (NULL == str)
  125. {
  126. printf("反序列化失败: [%s]\n", data);
  127. goto err;
  128. }
  129. RyanJsonMinify(data);
  130. if (0 != strcmp(data, str))
  131. printf("数据不一致 -- 原始: %s -- 序列化: %s\n", data, str);
  132. RyanJsonFree(str);
  133. #endif
  134. RyanJsonDelete(json);
  135. return 0;
  136. err:
  137. RyanJsonDelete(json);
  138. return -1;
  139. }
  140. /**
  141. * @brief cJson测试程序
  142. *
  143. * @param file_name
  144. * @param data
  145. * @param len
  146. * @return int
  147. */
  148. int cJSONParseData(char *file_name, char *data, uint32_t len)
  149. {
  150. if (strcmp(file_name, "n_structure_100000_opening_arrays.json") == 0 ||
  151. strcmp(file_name, "n_structure_open_array_object.json") == 0 ||
  152. strcmp(file_name, "n_structure_100000_opening_arrays.json") == 0)
  153. return -1;
  154. cJSON *json = cJSON_ParseWithLengthOpts(data, len + sizeof(""), NULL, RyanJsonTrue);
  155. if (NULL == json)
  156. return -1;
  157. #ifdef PrintfStrCmpEnable
  158. char *str = cJSON_PrintBuffered(json, 60, RyanJsonFalse);
  159. if (NULL == str)
  160. {
  161. printf("反序列化失败: [%s]\n", data);
  162. goto err;
  163. }
  164. cJSON_Minify(data);
  165. if (0 != strcmp(data, str))
  166. printf("-- 原始: %s -- 序列化: %s\n", data, str);
  167. cJSON_free(str);
  168. #endif
  169. cJSON_Delete(json);
  170. return 0;
  171. err:
  172. cJSON_Delete(json);
  173. return -1;
  174. }
  175. /**
  176. * @brief cJson测试程序
  177. *
  178. * @param file_name
  179. * @param data
  180. * @param len
  181. * @return int
  182. */
  183. int yyjsonParseData(char *file_name, char *data, uint32_t len)
  184. {
  185. if (strcmp(file_name, "n_structure_100000_opening_arrays.json") == 0 ||
  186. strcmp(file_name, "n_structure_open_array_object.json") == 0 ||
  187. strcmp(file_name, "n_structure_100000_opening_arrays.json") == 0)
  188. return -1;
  189. yyjson_doc *doc = yyjson_read_opts(data, len, YYJSON_READ_NOFLAG, &yyalc, NULL);
  190. if (NULL == doc)
  191. return -1;
  192. #ifdef PrintfStrCmpEnable
  193. char *str = yyjson_write_opts(doc, YYJSON_WRITE_NOFLAG, &yyalc, NULL, NULL);
  194. if (NULL == str)
  195. {
  196. printf("反序列化失败: [%s]\n", data);
  197. goto err;
  198. }
  199. cJSON_Minify(data);
  200. if (0 != strcmp(data, str))
  201. printf("-- 原始: %s -- 序列化: %s\n", data, str);
  202. free(str);
  203. #endif
  204. yyjson_doc_free(doc);
  205. return 0;
  206. err:
  207. yyjson_doc_free(doc);
  208. return -1;
  209. }
  210. // RFC 8259 JSON Test Suite
  211. // https://github.com/nst/JSONTestSuite
  212. int RFC8259JsonTest(void)
  213. {
  214. int result = 0;
  215. RyanJsonInitHooks(v_malloc, v_free, v_realloc);
  216. cJSON_Hooks hooks = {
  217. .malloc_fn = v_malloc,
  218. .free_fn = v_free};
  219. cJSON_InitHooks(&hooks);
  220. printf("开始 RFC 8259 JSON 测试");
  221. printf("\r\n--------------------------- RFC8259 RyanJson --------------------------\r\n");
  222. result = testFile("./RyanJsonExample/RFC8259JsonData", RyanJsonParseData);
  223. if (0 != result)
  224. {
  225. printf("%s:%d RyanJson RFC8259JsonTest fail\r\n", __FILE__, __LINE__);
  226. goto err;
  227. }
  228. printf("\r\n--------------------------- RFC8259 cJSON --------------------------\r\n");
  229. result = testFile("./RyanJsonExample/RFC8259JsonData", cJSONParseData);
  230. if (0 != result)
  231. {
  232. printf("%s:%d cJSON RFC8259JsonTest fail\r\n", __FILE__, __LINE__);
  233. goto err;
  234. }
  235. printf("\r\n--------------------------- RFC8259 yyjson --------------------------\r\n");
  236. result = testFile("./RyanJsonExample/RFC8259JsonData", yyjsonParseData);
  237. if (0 != result)
  238. {
  239. printf("%s:%d yyjson RFC8259JsonTest fail\r\n", __FILE__, __LINE__);
  240. goto err;
  241. }
  242. displayMem();
  243. return 0;
  244. err:
  245. displayMem();
  246. return -1;
  247. }