RyanJsonBaseTest.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "RyanJsonBaseTest.h"
  2. static RyanJsonBool_e likeReferenceTest()
  3. {
  4. // char *str = NULL;
  5. // char jsonstr[] =
  6. // "{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,\"item\":{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16.89,16.89,16.89,16.89],\"arrayString\":[\"hello\",\"hello\",\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null}]}";
  7. // RyanJson_t json = RyanJsonParse(jsonstr);
  8. // RyanJson_t item = NULL;
  9. // // RyanJson_t adfasdf = RyanJsonDuplicate(RyanJsonGetObjectToKey(json, "item"));
  10. // // RyanJsonAddItemToObject(json, "test", adfasdf);
  11. // // 这里做你想做的事,这里我选择打印出来
  12. // // str = RyanJsonPrint(json, 50, RyanJsonTrue, NULL);
  13. // // printf("item %s \r\n", str);
  14. // // RyanJsonFree(str);
  15. // for (int i = 0; i < 1; i++)
  16. // {
  17. // // 分离test对象
  18. // item = RyanJsonDetachByKey(json, "item");
  19. // // if (RyanJsonIsKey(item))
  20. // // RyanJsonFree(RyanJsonGetKey(item));
  21. // // RyanJsonFree(item);
  22. // }
  23. // RyanJsonAddItemToObject(json, "item", item);
  24. // str = RyanJsonPrint(json, 50, RyanJsonTrue, NULL);
  25. // printf("item %s \r\n", str);
  26. // RyanJsonFree(str);
  27. // RyanJsonDelete(json);
  28. return 0;
  29. }
  30. uint64_t platformUptimeMs(void)
  31. {
  32. struct timespec ts;
  33. // CLOCK_MONOTONIC: 单调递增,不受系统时间修改影响,适合做耗时统计
  34. clock_gettime(CLOCK_MONOTONIC, &ts);
  35. return (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
  36. }
  37. RyanJsonBool_e RyanJsonBaseTest(void)
  38. {
  39. int32_t result = 0;
  40. RyanJsonInitHooks(v_malloc, v_free, v_realloc);
  41. uint32_t testRunCount = 0;
  42. uint64_t funcStartMs;
  43. #define runTestWithLogAndTimer(fun) \
  44. do \
  45. { \
  46. testRunCount++; \
  47. printf("┌── [TEST %d] 开始执行: " #fun "()\r\n", testRunCount); \
  48. funcStartMs = platformUptimeMs(); \
  49. result = fun(); \
  50. printf("└── [TEST %" PRIu32 "] 结束执行: 返回值 = %" PRId32 " %s | 耗时: %" PRIu64 " ms\x1b[0m\r\n\r\n", testRunCount, \
  51. result, (result == RyanJsonTrue) ? "✅" : "❌", (platformUptimeMs() - funcStartMs)); \
  52. RyanJsonCheckCodeNoReturn(RyanJsonTrue == result, { goto __exit; }); \
  53. } while (0)
  54. runTestWithLogAndTimer(RyanJsonBaseTestChangeJson); // JSON 修改功能的条件覆盖测试
  55. runTestWithLogAndTimer(RyanJsonBaseTestCompareJson); // 节点比较与一致性验证
  56. runTestWithLogAndTimer(RyanJsonBaseTestCreateJson); // 节点创建与结构正确性检查
  57. runTestWithLogAndTimer(RyanJsonBaseTestDeleteJson); // JSON 删除功能的条件覆盖测试
  58. runTestWithLogAndTimer(RyanJsonBaseTestDetachJson); // 节点分离操作的条件覆盖测试
  59. runTestWithLogAndTimer(RyanJsonBaseTestDuplicateJson); // 节点复制的深拷贝与浅拷贝验证
  60. runTestWithLogAndTimer(RyanJsonBaseTestForEachJson); // 节点遍历与迭代稳定性测试
  61. runTestWithLogAndTimer(RyanJsonBaseTestLoadJson); // JSON 文本解析与加载能力验证
  62. runTestWithLogAndTimer(RyanJsonBaseTestReplaceJson); // 节点替换功能的条件覆盖测试
  63. // result = likeReferenceTest(); // 模仿 引用类型实现 示例
  64. // if (0 != result)
  65. // {
  66. // printf("%s:%d loadJsonTest fail\r\n", __FILE__, __LINE__);
  67. // return RyanJsonFalse;
  68. // }
  69. displayMem();
  70. return RyanJsonTrue;
  71. __exit:
  72. displayMem();
  73. return RyanJsonFalse;
  74. }