testEqualityInt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "testBase.h"
  2. #define IntList \
  3. /* 零值测试 */ \
  4. X(0) \
  5. X(-0) \
  6. /* 正负边界 */ \
  7. X(1) \
  8. X(-1) \
  9. X(2) \
  10. X(-2) \
  11. /* 常见小整数 */ \
  12. X(10) \
  13. X(-10) \
  14. X(100) \
  15. X(-100) \
  16. X(255) \
  17. X(-255) \
  18. X(256) \
  19. X(-256) \
  20. /* 常见数值 */ \
  21. X(1000) \
  22. X(-1000) \
  23. X(9999) \
  24. X(-9999) \
  25. X(12345) \
  26. X(-12345) \
  27. X(65535) \
  28. X(-65535) \
  29. X(65536) \
  30. X(-65536) \
  31. /* 大整数 */ \
  32. X(100000) \
  33. X(-100000) \
  34. X(1000000) \
  35. X(-1000000) \
  36. X(10000000) \
  37. X(-10000000) \
  38. X(100000000) \
  39. X(-100000000) \
  40. X(1000000000) \
  41. X(-1000000000) \
  42. /* 8位边界 */ \
  43. X(127) \
  44. X(-128) \
  45. /* 16位边界 */ \
  46. X(32767) \
  47. X(-32768) \
  48. /* 32位边界 */ \
  49. X(2147483647) \
  50. X(-2147483648) \
  51. /* 特殊模式 */ \
  52. X(1234567890) \
  53. X(-1234567890) \
  54. X(123456789) \
  55. X(-123456789) \
  56. /* 2的幂次 */ \
  57. X(2) \
  58. X(4) \
  59. X(8) \
  60. X(16) \
  61. X(32) \
  62. X(64) \
  63. X(128) \
  64. X(512) \
  65. X(1024) \
  66. X(2048) \
  67. X(4096) \
  68. X(8192) \
  69. X(16384) \
  70. X(32768) \
  71. X(65536) \
  72. X(131072) \
  73. X(262144) \
  74. X(524288) \
  75. X(1048576) \
  76. X(2097152) \
  77. X(4194304) \
  78. X(8388608) \
  79. X(16777216) \
  80. X(33554432) \
  81. X(67108864) \
  82. X(134217728) \
  83. X(268435456) \
  84. X(536870912) \
  85. X(1073741824)
  86. static const int32_t IntValueTable[] = {
  87. #define X(a) a,
  88. IntList
  89. #undef X
  90. };
  91. static const char *IntStringTable[] = {
  92. #define X(a) "{\"int32_t\":" #a "}",
  93. IntList
  94. #undef X
  95. };
  96. static const char *IntStringTable2[] = {
  97. #define X(a) #a,
  98. IntList
  99. #undef X
  100. };
  101. static const char *IntStringTable3[] = {
  102. #define X(a) "[" #a "]",
  103. IntList
  104. #undef X
  105. };
  106. /**
  107. * @brief 整数类型边界与一致性测试
  108. */
  109. void testEqualityIntEdgeCases(void)
  110. {
  111. // NULL 输入
  112. TEST_ASSERT_FALSE_MESSAGE(RyanJsonIsInt(NULL), "RyanJsonIsInt(NULL) 应返回 false");
  113. // 类型混淆测试
  114. RyanJson_t dbl = RyanJsonCreateDouble("dbl", 123.456);
  115. TEST_ASSERT_TRUE_MESSAGE(RyanJsonIsNumber(dbl), "RyanJsonIsNumber(Double) 应返回 true");
  116. TEST_ASSERT_FALSE_MESSAGE(RyanJsonIsInt(dbl), "RyanJsonIsInt(Double) 应返回 false");
  117. RyanJsonDelete(dbl);
  118. RyanJson_t str = RyanJsonCreateString("str", "123");
  119. TEST_ASSERT_FALSE_MESSAGE(RyanJsonIsInt(str), "RyanJsonIsInt(String) 应返回 false");
  120. RyanJsonDelete(str);
  121. RyanJson_t obj = RyanJsonCreateObject();
  122. TEST_ASSERT_FALSE_MESSAGE(RyanJsonIsInt(obj), "RyanJsonIsInt(Object) 应返回 false");
  123. RyanJsonDelete(obj);
  124. RyanJson_t boolNode = RyanJsonCreateBool("bool", RyanJsonTrue);
  125. TEST_ASSERT_FALSE_MESSAGE(RyanJsonIsInt(boolNode), "RyanJsonIsInt(Bool) 应返回 false");
  126. RyanJsonDelete(boolNode);
  127. }
  128. static void testEqualityIntTypeBoundaries(void)
  129. {
  130. // int32_t 边界内:应为 int
  131. RyanJson_t intMax = RyanJsonParse("2147483647");
  132. TEST_ASSERT_NOT_NULL(intMax);
  133. TEST_ASSERT_TRUE(RyanJsonIsInt(intMax));
  134. TEST_ASSERT_EQUAL_INT32(2147483647, RyanJsonGetIntValue(intMax));
  135. RyanJsonDelete(intMax);
  136. RyanJson_t intMin = RyanJsonParse("-2147483648");
  137. TEST_ASSERT_NOT_NULL(intMin);
  138. TEST_ASSERT_TRUE(RyanJsonIsInt(intMin));
  139. TEST_ASSERT_EQUAL_INT32(INT32_MIN, RyanJsonGetIntValue(intMin));
  140. RyanJsonDelete(intMin);
  141. // 超出 int32_t 范围:应退化为 double
  142. RyanJson_t overMax = RyanJsonParse("2147483648");
  143. TEST_ASSERT_NOT_NULL(overMax);
  144. TEST_ASSERT_TRUE(RyanJsonIsDouble(overMax));
  145. TEST_ASSERT_FALSE(RyanJsonIsInt(overMax));
  146. TEST_ASSERT_TRUE(RyanJsonCompareDouble(2147483648.0, RyanJsonGetDoubleValue(overMax)));
  147. RyanJsonDelete(overMax);
  148. RyanJson_t belowMin = RyanJsonParse("-2147483649");
  149. TEST_ASSERT_NOT_NULL(belowMin);
  150. TEST_ASSERT_TRUE(RyanJsonIsDouble(belowMin));
  151. TEST_ASSERT_FALSE(RyanJsonIsInt(belowMin));
  152. TEST_ASSERT_TRUE(RyanJsonCompareDouble(-2147483649.0, RyanJsonGetDoubleValue(belowMin)));
  153. RyanJsonDelete(belowMin);
  154. // 指数/小数语义:数值等于整数也应按 double 处理
  155. RyanJson_t expInt = RyanJsonParse("1e0");
  156. TEST_ASSERT_NOT_NULL(expInt);
  157. TEST_ASSERT_TRUE(RyanJsonIsDouble(expInt));
  158. TEST_ASSERT_FALSE(RyanJsonIsInt(expInt));
  159. RyanJsonDelete(expInt);
  160. RyanJson_t fracInt = RyanJsonParse("1.0");
  161. TEST_ASSERT_NOT_NULL(fracInt);
  162. TEST_ASSERT_TRUE(RyanJsonIsDouble(fracInt));
  163. TEST_ASSERT_FALSE(RyanJsonIsInt(fracInt));
  164. RyanJsonDelete(fracInt);
  165. // -0 保持 int 路径
  166. RyanJson_t negZero = RyanJsonParse("-0");
  167. TEST_ASSERT_NOT_NULL(negZero);
  168. TEST_ASSERT_TRUE(RyanJsonIsInt(negZero));
  169. TEST_ASSERT_EQUAL_INT32(0, RyanJsonGetIntValue(negZero));
  170. RyanJsonDelete(negZero);
  171. }
  172. static void testEqualityIntTableCommon(const char *const *stringTable, RyanJsonBool_e withKey)
  173. {
  174. for (uint32_t i = 0; i < sizeof(IntValueTable) / sizeof(IntValueTable[0]); i++)
  175. {
  176. const char *jsonIntStr = stringTable[i];
  177. RyanJson_t jsonRoot = RyanJsonParse(jsonIntStr);
  178. TEST_ASSERT_NOT_NULL_MESSAGE(jsonRoot, jsonIntStr);
  179. RyanJson_t valueNode = withKey ? RyanJsonGetObjectToKey(jsonRoot, "int32_t") : jsonRoot;
  180. TEST_ASSERT_TRUE_MESSAGE(RyanJsonIsInt(valueNode),
  181. withKey ? "Key 'int32_t' not found or not int32_t" : "Root node not int32_t");
  182. // 验证解析后的数值是否正确
  183. int32_t intValue = RyanJsonGetIntValue(valueNode);
  184. TEST_ASSERT_EQUAL_INT32_MESSAGE(IntValueTable[i], intValue, jsonIntStr);
  185. // 往返校验:序列化后再次解析,整数值应保持一致
  186. char *serializedStr = RyanJsonPrint(jsonRoot, 128, RyanJsonFalse, NULL);
  187. TEST_ASSERT_NOT_NULL_MESSAGE(serializedStr, "序列化失败");
  188. RyanJsonDelete(jsonRoot);
  189. RyanJson_t roundtripJson = RyanJsonParse(serializedStr);
  190. RyanJsonFree(serializedStr);
  191. TEST_ASSERT_NOT_NULL(roundtripJson);
  192. RyanJson_t roundtripValueNode = withKey ? RyanJsonGetObjectToKey(roundtripJson, "int32_t") : roundtripJson;
  193. TEST_ASSERT_TRUE(RyanJsonIsInt(roundtripValueNode));
  194. int32_t roundtripValue = RyanJsonGetIntValue(roundtripValueNode);
  195. TEST_ASSERT_EQUAL_INT32_MESSAGE(IntValueTable[i], roundtripValue, "往返测试数值不匹配");
  196. RyanJsonDelete(roundtripJson);
  197. }
  198. }
  199. static void testEqualityIntTable(void)
  200. {
  201. testEqualityIntTableCommon(IntStringTable, RyanJsonTrue);
  202. }
  203. static void testEqualityIntTable2(void)
  204. {
  205. testEqualityIntTableCommon(IntStringTable2, RyanJsonFalse);
  206. }
  207. static void testEqualityIntArrayTable(const char *const *stringTable)
  208. {
  209. for (uint32_t i = 0; i < sizeof(IntValueTable) / sizeof(IntValueTable[0]); i++)
  210. {
  211. const char *jsonIntStr = stringTable[i];
  212. RyanJson_t jsonRoot = RyanJsonParse(jsonIntStr);
  213. TEST_ASSERT_NOT_NULL_MESSAGE(jsonRoot, jsonIntStr);
  214. TEST_ASSERT_TRUE_MESSAGE(RyanJsonIsArray(jsonRoot), "Root node not array");
  215. TEST_ASSERT_EQUAL_INT_MESSAGE(1, RyanJsonGetSize(jsonRoot), "Array size not 1");
  216. RyanJson_t valueNode = RyanJsonGetObjectByIndex(jsonRoot, 0);
  217. TEST_ASSERT_TRUE_MESSAGE(RyanJsonIsInt(valueNode), "Array[0] not int32_t");
  218. int32_t intValue = RyanJsonGetIntValue(valueNode);
  219. TEST_ASSERT_EQUAL_INT32_MESSAGE(IntValueTable[i], intValue, jsonIntStr);
  220. // 往返校验:序列化后再次解析,整数值应保持一致
  221. char *serializedStr = RyanJsonPrint(jsonRoot, 128, RyanJsonFalse, NULL);
  222. TEST_ASSERT_NOT_NULL_MESSAGE(serializedStr, "序列化失败");
  223. RyanJsonDelete(jsonRoot);
  224. RyanJson_t roundtripJson = RyanJsonParse(serializedStr);
  225. RyanJsonFree(serializedStr);
  226. TEST_ASSERT_NOT_NULL(roundtripJson);
  227. TEST_ASSERT_TRUE(RyanJsonIsArray(roundtripJson));
  228. TEST_ASSERT_EQUAL_INT_MESSAGE(1, RyanJsonGetSize(roundtripJson), "Roundtrip array size not 1");
  229. RyanJson_t roundtripValueNode = RyanJsonGetObjectByIndex(roundtripJson, 0);
  230. TEST_ASSERT_TRUE(RyanJsonIsInt(roundtripValueNode));
  231. int32_t roundtripValue = RyanJsonGetIntValue(roundtripValueNode);
  232. TEST_ASSERT_EQUAL_INT32_MESSAGE(IntValueTable[i], roundtripValue, "往返测试数值不匹配");
  233. RyanJsonDelete(roundtripJson);
  234. }
  235. }
  236. static void testEqualityIntTable3(void)
  237. {
  238. testEqualityIntArrayTable(IntStringTable3);
  239. }
  240. void testEqualityIntRunner(void)
  241. {
  242. UnitySetTestFile(__FILE__);
  243. RUN_TEST(testEqualityIntEdgeCases);
  244. RUN_TEST(testEqualityIntTypeBoundaries);
  245. RUN_TEST(testEqualityIntTable);
  246. RUN_TEST(testEqualityIntTable2);
  247. RUN_TEST(testEqualityIntTable3);
  248. }