RyanJsonBaseTestEqualityInt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "RyanJsonBaseTest.h"
  2. #define IntList \
  3. /* ========== 零值测试 ========== */ \
  4. X(0) \
  5. /* ========== 正负边界 ========== */ \
  6. X(1) \
  7. X(-1) \
  8. X(2) \
  9. X(-2) \
  10. /* ========== 常见小整数 ========== */ \
  11. X(10) \
  12. X(-10) \
  13. X(100) \
  14. X(-100) \
  15. X(255) \
  16. X(-255) \
  17. X(256) \
  18. X(-256) \
  19. /* ========== 常见数值 ========== */ \
  20. X(1000) \
  21. X(-1000) \
  22. X(9999) \
  23. X(-9999) \
  24. X(12345) \
  25. X(-12345) \
  26. X(65535) \
  27. X(-65535) \
  28. X(65536) \
  29. X(-65536) \
  30. /* ========== 大整数 ========== */ \
  31. X(100000) \
  32. X(-100000) \
  33. X(1000000) \
  34. X(-1000000) \
  35. X(10000000) \
  36. X(-10000000) \
  37. X(100000000) \
  38. X(-100000000) \
  39. X(1000000000) \
  40. X(-1000000000) \
  41. /* ========== 8位边界 ========== */ \
  42. X(127) \
  43. X(-128) \
  44. /* ========== 16位边界 ========== */ \
  45. X(32767) \
  46. X(-32768) \
  47. /* ========== 32位边界 ========== */ \
  48. X(2147483647) \
  49. X(-2147483648) \
  50. /* ========== 特殊模式 ========== */ \
  51. X(1234567890) \
  52. X(-1234567890) \
  53. X(123456789) \
  54. X(-123456789) \
  55. /* ========== 2的幂次 ========== */ \
  56. X(2) \
  57. X(4) \
  58. X(8) \
  59. X(16) \
  60. X(32) \
  61. X(64) \
  62. X(128) \
  63. X(512) \
  64. X(1024) \
  65. X(2048) \
  66. X(4096) \
  67. X(8192) \
  68. X(16384) \
  69. X(32768) \
  70. X(65536) \
  71. X(131072) \
  72. X(262144) \
  73. X(524288) \
  74. X(1048576) \
  75. X(2097152) \
  76. X(4194304) \
  77. X(8388608) \
  78. X(16777216) \
  79. X(33554432) \
  80. X(67108864) \
  81. X(134217728) \
  82. X(268435456) \
  83. X(536870912) \
  84. X(1073741824)
  85. static const int32_t IntValueTable[] = {
  86. #define X(a) a,
  87. IntList
  88. #undef X
  89. };
  90. static const char *IntStringTable[] = {
  91. #define X(a) "{\"int\":" #a "}",
  92. IntList
  93. #undef X
  94. };
  95. // 整数一致性测试
  96. RyanJsonBool_e RyanJsonBaseTestEqualityInt(void)
  97. {
  98. for (uint32_t i = 0; i < sizeof(IntValueTable) / sizeof(IntValueTable[0]); i++)
  99. {
  100. const char *jsonIntStr = IntStringTable[i];
  101. RyanJson_t jsonRoot = RyanJsonParse(jsonIntStr);
  102. RyanJsonCheckCode(NULL != jsonRoot, {
  103. jsonLog("str: %s", jsonIntStr);
  104. goto err;
  105. });
  106. RyanJsonCheckReturnFalse(RyanJsonIsInt(RyanJsonGetObjectToKey(jsonRoot, "int")));
  107. // 验证解析后的数值是否正确
  108. int32_t intValue = RyanJsonGetIntValue(RyanJsonGetObjectToKey(jsonRoot, "int"));
  109. RyanJsonCheckCode(intValue == IntValueTable[i], {
  110. jsonLog("str: %s, expected: %" PRId32 ", got: %" PRId32, jsonIntStr, IntValueTable[i], intValue);
  111. RyanJsonDelete(jsonRoot);
  112. goto err;
  113. });
  114. // 验证序列化后再解析,然后判断int是否一致(往返测试)
  115. char *serializedStr = RyanJsonPrint(jsonRoot, 128, RyanJsonFalse, NULL);
  116. RyanJsonDelete(jsonRoot);
  117. RyanJson_t roundtripJson = RyanJsonParse(serializedStr);
  118. RyanJsonFree(serializedStr);
  119. RyanJsonCheckReturnFalse(NULL != roundtripJson);
  120. RyanJsonCheckReturnFalse(RyanJsonIsInt(RyanJsonGetObjectToKey(roundtripJson, "int")));
  121. int32_t roundtripValue = RyanJsonGetIntValue(RyanJsonGetObjectToKey(roundtripJson, "int"));
  122. RyanJsonCheckCode(roundtripValue == IntValueTable[i], {
  123. jsonLog("roundtrip failed: expected: %" PRId32 ", got: %" PRId32, IntValueTable[i], roundtripValue);
  124. RyanJsonDelete(roundtripJson);
  125. goto err;
  126. });
  127. RyanJsonDelete(roundtripJson);
  128. }
  129. return RyanJsonTrue;
  130. err:
  131. return RyanJsonFalse;
  132. }