testCompareMutation.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include "testBase.h"
  2. static void testCompareMutationIgnoreValueChanges(void)
  3. {
  4. // 复杂链路:
  5. // Parse -> Duplicate -> ChangeInt/ChangeString -> Compare/CompareOnlyKey。
  6. // 目标:验证值变更不影响 CompareOnlyKey,但 Compare 必须失败。
  7. RyanJson_t root = RyanJsonParse("{\"obj\":{\"a\":1,\"b\":\"x\"},\"arr\":[1,\"x\"]}");
  8. TEST_ASSERT_NOT_NULL(root);
  9. RyanJson_t copy = RyanJsonDuplicate(root);
  10. TEST_ASSERT_NOT_NULL(copy);
  11. TEST_ASSERT_TRUE(RyanJsonChangeIntValue(RyanJsonGetObjectToKey(copy, "obj", "a"), 9));
  12. TEST_ASSERT_TRUE(RyanJsonChangeStringValue(RyanJsonGetObjectToKey(copy, "obj", "b"), "y"));
  13. RyanJson_t arr = RyanJsonGetObjectToKey(copy, "arr");
  14. TEST_ASSERT_NOT_NULL(arr);
  15. TEST_ASSERT_TRUE(RyanJsonChangeIntValue(RyanJsonGetObjectByIndex(arr, 0), 2));
  16. TEST_ASSERT_TRUE(RyanJsonChangeStringValue(RyanJsonGetObjectByIndex(arr, 1), "z"));
  17. TEST_ASSERT_FALSE(RyanJsonCompare(root, copy));
  18. TEST_ASSERT_TRUE(RyanJsonCompareOnlyKey(root, copy));
  19. RyanJsonDelete(copy);
  20. RyanJsonDelete(root);
  21. }
  22. static void testCompareMutationFailOnTypeChange(void)
  23. {
  24. // 复杂链路:
  25. // Parse -> Duplicate -> ReplaceByKey(类型变化) -> CompareOnlyKey。
  26. // 目标:验证类型变化会导致 CompareOnlyKey 失败。
  27. RyanJson_t root = RyanJsonParse("{\"a\":1,\"b\":true}");
  28. TEST_ASSERT_NOT_NULL(root);
  29. RyanJson_t copy = RyanJsonDuplicate(root);
  30. TEST_ASSERT_NOT_NULL(copy);
  31. TEST_ASSERT_TRUE(RyanJsonReplaceByKey(copy, "a", RyanJsonCreateString("a", "1")));
  32. TEST_ASSERT_FALSE(RyanJsonCompareOnlyKey(root, copy));
  33. RyanJsonDelete(copy);
  34. RyanJsonDelete(root);
  35. }
  36. static void testCompareMutationFailOnSizeChange(void)
  37. {
  38. // 复杂链路:
  39. // Parse -> Duplicate -> DetachByIndex(Array) -> CompareOnlyKey。
  40. // 目标:验证容器尺寸变化会导致 CompareOnlyKey 失败。
  41. RyanJson_t root = RyanJsonParse("{\"arr\":[1,2,3]}");
  42. TEST_ASSERT_NOT_NULL(root);
  43. RyanJson_t copy = RyanJsonDuplicate(root);
  44. TEST_ASSERT_NOT_NULL(copy);
  45. RyanJson_t arr = RyanJsonGetObjectToKey(copy, "arr");
  46. TEST_ASSERT_NOT_NULL(arr);
  47. RyanJson_t detached = RyanJsonDetachByIndex(arr, 1);
  48. TEST_ASSERT_NOT_NULL(detached);
  49. RyanJsonDelete(detached);
  50. TEST_ASSERT_FALSE(RyanJsonCompareOnlyKey(root, copy));
  51. RyanJsonDelete(copy);
  52. RyanJsonDelete(root);
  53. }
  54. static void testCompareMutationDuplicateCountMismatch(void)
  55. {
  56. // 复杂链路:
  57. // Parse(重复 key) -> Duplicate -> DeleteByKey -> CompareOnlyKey。
  58. // 目标:验证重复 key 数量变化会导致 CompareOnlyKey 失败。
  59. RyanJson_t root = RyanJsonParse("{\"a\":1,\"a\":2,\"b\":3}");
  60. if (NULL == root)
  61. {
  62. // strict 控制组:无重复 key 时也应识别结构差异。
  63. RyanJson_t strictRoot = RyanJsonParse("{\"a\":1,\"b\":3}");
  64. TEST_ASSERT_NOT_NULL(strictRoot);
  65. RyanJson_t strictCopy = RyanJsonDuplicate(strictRoot);
  66. TEST_ASSERT_NOT_NULL(strictCopy);
  67. TEST_ASSERT_TRUE(RyanJsonDeleteByKey(strictCopy, "a"));
  68. TEST_ASSERT_FALSE(RyanJsonCompareOnlyKey(strictRoot, strictCopy));
  69. RyanJsonDelete(strictCopy);
  70. RyanJsonDelete(strictRoot);
  71. return;
  72. }
  73. RyanJson_t copy = RyanJsonDuplicate(root);
  74. TEST_ASSERT_NOT_NULL(copy);
  75. TEST_ASSERT_TRUE(RyanJsonDeleteByKey(copy, "a"));
  76. TEST_ASSERT_FALSE(RyanJsonCompareOnlyKey(root, copy));
  77. RyanJsonDelete(copy);
  78. RyanJsonDelete(root);
  79. }
  80. static void testCompareMutationFailOnKeyRename(void)
  81. {
  82. // 复杂链路:
  83. // Parse -> Duplicate -> ChangeKey -> CompareOnlyKey。
  84. // 目标:验证 key 变化会导致 CompareOnlyKey 失败。
  85. RyanJson_t root = RyanJsonParse("{\"a\":1}");
  86. TEST_ASSERT_NOT_NULL(root);
  87. RyanJson_t copy = RyanJsonDuplicate(root);
  88. TEST_ASSERT_NOT_NULL(copy);
  89. TEST_ASSERT_TRUE(RyanJsonChangeKey(RyanJsonGetObjectByKey(copy, "a"), "b"));
  90. TEST_ASSERT_FALSE(RyanJsonCompareOnlyKey(root, copy));
  91. RyanJsonDelete(copy);
  92. RyanJsonDelete(root);
  93. }
  94. static void testCompareMutationDuplicateReorderWithValueChange(void)
  95. {
  96. // 复杂链路:
  97. // Parse(重复 key) -> Duplicate -> DetachByKey -> ChangeIntValue -> Insert(尾部) -> Compare/CompareOnlyKey。
  98. // 目标:验证重复 key 重新排列且值变化时,CompareOnlyKey 仍为 True。
  99. RyanJson_t root = RyanJsonParse("{\"a\":1,\"a\":2,\"b\":3}");
  100. if (NULL == root)
  101. {
  102. // strict 控制组:无重复 key,值变化时 CompareOnlyKey 仍应为 True。
  103. RyanJson_t strictRoot = RyanJsonParse("{\"a\":1,\"b\":3}");
  104. TEST_ASSERT_NOT_NULL(strictRoot);
  105. RyanJson_t strictCopy = RyanJsonDuplicate(strictRoot);
  106. TEST_ASSERT_NOT_NULL(strictCopy);
  107. TEST_ASSERT_TRUE(RyanJsonChangeIntValue(RyanJsonGetObjectByKey(strictCopy, "a"), 9));
  108. TEST_ASSERT_FALSE(RyanJsonCompare(strictRoot, strictCopy));
  109. TEST_ASSERT_TRUE(RyanJsonCompareOnlyKey(strictRoot, strictCopy));
  110. RyanJsonDelete(strictCopy);
  111. RyanJsonDelete(strictRoot);
  112. return;
  113. }
  114. RyanJson_t copy = RyanJsonDuplicate(root);
  115. TEST_ASSERT_NOT_NULL(copy);
  116. RyanJson_t moved = RyanJsonDetachByKey(copy, "a");
  117. TEST_ASSERT_NOT_NULL(moved);
  118. TEST_ASSERT_TRUE(RyanJsonChangeIntValue(moved, 9));
  119. TEST_ASSERT_TRUE(RyanJsonInsert(copy, 99, moved));
  120. TEST_ASSERT_FALSE(RyanJsonCompare(root, copy));
  121. TEST_ASSERT_TRUE(RyanJsonCompareOnlyKey(root, copy));
  122. RyanJsonDelete(copy);
  123. RyanJsonDelete(root);
  124. }
  125. static void testCompareMutationDuplicateCountMismatchAfterInsert(void)
  126. {
  127. // 复杂链路:
  128. // Parse(重复 key) -> Duplicate -> Insert(增加重复 key) -> CompareOnlyKey。
  129. // 目标:验证重复 key 数量变化会导致 CompareOnlyKey 失败。
  130. RyanJson_t root = RyanJsonParse("{\"a\":1,\"a\":2,\"b\":3}");
  131. if (NULL == root)
  132. {
  133. // strict 控制组:新增 key 导致结构变化。
  134. RyanJson_t strictRoot = RyanJsonParse("{\"a\":1,\"b\":3}");
  135. TEST_ASSERT_NOT_NULL(strictRoot);
  136. RyanJson_t strictCopy = RyanJsonDuplicate(strictRoot);
  137. TEST_ASSERT_NOT_NULL(strictCopy);
  138. TEST_ASSERT_TRUE(RyanJsonInsert(strictCopy, 99, RyanJsonCreateInt("c", 5)));
  139. TEST_ASSERT_FALSE(RyanJsonCompareOnlyKey(strictRoot, strictCopy));
  140. RyanJsonDelete(strictCopy);
  141. RyanJsonDelete(strictRoot);
  142. return;
  143. }
  144. RyanJson_t copy = RyanJsonDuplicate(root);
  145. TEST_ASSERT_NOT_NULL(copy);
  146. TEST_ASSERT_TRUE(RyanJsonInsert(copy, 0, RyanJsonCreateInt("a", 9)));
  147. TEST_ASSERT_FALSE(RyanJsonCompareOnlyKey(root, copy));
  148. RyanJsonDelete(copy);
  149. RyanJsonDelete(root);
  150. }
  151. static void testCompareMutationAfterAddItemWrap(void)
  152. {
  153. // 复杂链路:
  154. // Parse -> Duplicate -> AddItemToObject(Array) -> Compare/CompareOnlyKey。
  155. // 目标:验证包装新增结构后,CompareOnlyKey 只关注结构与 key。
  156. // 说明:期望文档刻意使用不同值,以确保 Compare=false、CompareOnlyKey=true。
  157. RyanJson_t root = RyanJsonParse("{\"a\":{}}");
  158. TEST_ASSERT_NOT_NULL(root);
  159. RyanJson_t copy = RyanJsonDuplicate(root);
  160. TEST_ASSERT_NOT_NULL(copy);
  161. RyanJson_t arr = RyanJsonCreateArray();
  162. TEST_ASSERT_NOT_NULL(arr);
  163. TEST_ASSERT_TRUE(RyanJsonInsert(arr, 0, RyanJsonCreateInt(NULL, 1)));
  164. TEST_ASSERT_TRUE(RyanJsonAddItemToObject(copy, "b", arr));
  165. RyanJson_t expect = RyanJsonParse("{\"a\":{},\"b\":[0]}");
  166. TEST_ASSERT_NOT_NULL(expect);
  167. TEST_ASSERT_FALSE(RyanJsonCompare(copy, expect));
  168. TEST_ASSERT_TRUE(RyanJsonCompareOnlyKey(copy, expect));
  169. RyanJsonDelete(expect);
  170. RyanJsonDelete(copy);
  171. RyanJsonDelete(root);
  172. }
  173. static void testCompareMutationWrappedArrayValueChurn(void)
  174. {
  175. // 复杂链路:
  176. // Parse -> AddItemToObject(Array) -> Duplicate -> ChangeIntValue -> CompareOnlyKey。
  177. // 目标:验证包装 Array 值变化不影响 CompareOnlyKey。
  178. // 说明:期望文档刻意使用不同值,以确保 Compare=false、CompareOnlyKey=true。
  179. RyanJson_t root = RyanJsonParse("{\"a\":{}}");
  180. TEST_ASSERT_NOT_NULL(root);
  181. RyanJson_t work = RyanJsonDuplicate(root);
  182. TEST_ASSERT_NOT_NULL(work);
  183. RyanJson_t arr = RyanJsonCreateArray();
  184. TEST_ASSERT_NOT_NULL(arr);
  185. TEST_ASSERT_TRUE(RyanJsonInsert(arr, 0, RyanJsonCreateInt(NULL, 1)));
  186. TEST_ASSERT_TRUE(RyanJsonInsert(arr, 1, RyanJsonCreateInt(NULL, 2)));
  187. TEST_ASSERT_TRUE(RyanJsonAddItemToObject(work, "b", arr));
  188. RyanJson_t arrNode = RyanJsonGetObjectByKey(work, "b");
  189. TEST_ASSERT_NOT_NULL(arrNode);
  190. TEST_ASSERT_TRUE(RyanJsonChangeIntValue(RyanJsonGetObjectByIndex(arrNode, 0), 7));
  191. RyanJson_t expect = RyanJsonParse("{\"a\":{},\"b\":[0,0]}");
  192. TEST_ASSERT_NOT_NULL(expect);
  193. TEST_ASSERT_FALSE(RyanJsonCompare(work, expect));
  194. TEST_ASSERT_TRUE(RyanJsonCompareOnlyKey(work, expect));
  195. RyanJsonDelete(expect);
  196. RyanJsonDelete(work);
  197. RyanJsonDelete(root);
  198. }
  199. static void testCompareMutationReplaceScalarKeepsOnlyKey(void)
  200. {
  201. // 复杂链路:
  202. // Parse -> Duplicate -> ReplaceByKey(标量) -> CompareOnlyKey。
  203. // 目标:验证标量替换仅改变值,不影响 CompareOnlyKey。
  204. RyanJson_t root = RyanJsonParse("{\"a\":1}");
  205. TEST_ASSERT_NOT_NULL(root);
  206. RyanJson_t copy = RyanJsonDuplicate(root);
  207. TEST_ASSERT_NOT_NULL(copy);
  208. TEST_ASSERT_TRUE(RyanJsonReplaceByKey(copy, "a", RyanJsonCreateInt("a", 9)));
  209. TEST_ASSERT_FALSE(RyanJsonCompare(root, copy));
  210. TEST_ASSERT_TRUE(RyanJsonCompareOnlyKey(root, copy));
  211. RyanJsonDelete(copy);
  212. RyanJsonDelete(root);
  213. }
  214. static void testCompareMutationNestedArraySizeMismatch(void)
  215. {
  216. // 复杂链路:
  217. // Parse -> CompareOnlyKey(失败) -> DeleteByIndex(修复长度) -> CompareOnlyKey(成功)。
  218. // 目标:验证 CompareOnlyKey 会检查嵌套 Array 长度,不因值变化而误判。
  219. RyanJson_t left = RyanJsonParse("{\"arr\":[[1,2],{\"k\":1}]}");
  220. RyanJson_t right = RyanJsonParse("{\"arr\":[[1,2,3],{\"k\":9}]}");
  221. TEST_ASSERT_NOT_NULL(left);
  222. TEST_ASSERT_NOT_NULL(right);
  223. TEST_ASSERT_FALSE_MESSAGE(RyanJsonCompareOnlyKey(left, right), "嵌套 Array 长度不一致时 CompareOnlyKey 应返回 False");
  224. TEST_ASSERT_FALSE_MESSAGE(RyanJsonCompare(left, right), "嵌套 Array 长度不一致时 Compare 应返回 False");
  225. RyanJson_t inner = RyanJsonGetObjectToIndex(RyanJsonGetObjectToKey(right, "arr"), 0);
  226. TEST_ASSERT_NOT_NULL(inner);
  227. TEST_ASSERT_TRUE_MESSAGE(RyanJsonDeleteByIndex(inner, 0), "修复嵌套 Array 长度失败");
  228. TEST_ASSERT_EQUAL_UINT32(2U, RyanJsonGetArraySize(inner));
  229. TEST_ASSERT_TRUE_MESSAGE(RyanJsonCompareOnlyKey(left, right), "修复长度后 CompareOnlyKey 应返回 True");
  230. TEST_ASSERT_FALSE_MESSAGE(RyanJsonCompare(left, right), "值不同但结构一致时 Compare 应返回 False");
  231. RyanJsonDelete(right);
  232. RyanJsonDelete(left);
  233. }
  234. void testCompareMutationRunner(void)
  235. {
  236. UnitySetTestFile(__FILE__);
  237. RUN_TEST(testCompareMutationIgnoreValueChanges);
  238. RUN_TEST(testCompareMutationFailOnTypeChange);
  239. RUN_TEST(testCompareMutationFailOnSizeChange);
  240. RUN_TEST(testCompareMutationNestedArraySizeMismatch);
  241. RUN_TEST(testCompareMutationDuplicateCountMismatch);
  242. RUN_TEST(testCompareMutationFailOnKeyRename);
  243. RUN_TEST(testCompareMutationDuplicateReorderWithValueChange);
  244. RUN_TEST(testCompareMutationDuplicateCountMismatchAfterInsert);
  245. RUN_TEST(testCompareMutationAfterAddItemWrap);
  246. RUN_TEST(testCompareMutationWrappedArrayValueChurn);
  247. RUN_TEST(testCompareMutationReplaceScalarKeepsOnlyKey);
  248. }