testForEach.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "testBase.h"
  2. static void testForEachEdgeCases(void)
  3. {
  4. RyanJson_t item = NULL;
  5. // 遍历 NULL 对象 (应该安全跳过循环)
  6. int32_t count = 0;
  7. RyanJsonArrayForEach(NULL, item)
  8. {
  9. count++;
  10. }
  11. TEST_ASSERT_EQUAL_INT_MESSAGE(0, count, "遍历 NULL Array 应不执行循环");
  12. count = 0;
  13. RyanJsonObjectForEach(NULL, item)
  14. {
  15. count++;
  16. }
  17. TEST_ASSERT_EQUAL_INT_MESSAGE(0, count, "遍历 NULL Object 应不执行循环");
  18. // 遍历非容器对象 (应该同上)
  19. RyanJson_t num = RyanJsonCreateInt("num", 1);
  20. count = 0;
  21. RyanJsonArrayForEach(num, item)
  22. {
  23. count++;
  24. }
  25. TEST_ASSERT_EQUAL_INT_MESSAGE(0, count, "遍历非容器 Array 应不执行循环");
  26. count = 0;
  27. RyanJsonObjectForEach(num, item)
  28. {
  29. count++;
  30. }
  31. TEST_ASSERT_EQUAL_INT_MESSAGE(0, count, "遍历非容器 Object 应不执行循环");
  32. RyanJsonDelete(num);
  33. // 循环中断测试 (break)
  34. RyanJson_t arr = RyanJsonCreateArray();
  35. RyanJsonAddIntToArray(arr, 1);
  36. RyanJsonAddIntToArray(arr, 2);
  37. RyanJsonAddIntToArray(arr, 3);
  38. count = 0;
  39. RyanJsonArrayForEach(arr, item)
  40. {
  41. count++;
  42. if (RyanJsonGetIntValue(item) == 2) { break; }
  43. }
  44. TEST_ASSERT_EQUAL_INT_MESSAGE(2, count, "循环 break 测试失败");
  45. RyanJsonDelete(arr);
  46. }
  47. static void testForEachIterativeTraversals(void)
  48. {
  49. char jsonstr[] = "{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,\"item\":"
  50. "{\"inter\":16,\"double\":16."
  51. "89,\"string\":\"hello\","
  52. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16.89,"
  53. "16.89,16.89,16.89],"
  54. "\"arrayString\":[\"hello\",\"hello\","
  55. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  56. "\"double\":16.89,\"string\":"
  57. "\"hello\",\"boolTrue\":true,"
  58. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  59. "\"boolFalse\":false,\"null\":null}]}";
  60. RyanJson_t json = RyanJsonParse(jsonstr);
  61. TEST_ASSERT_NOT_NULL_MESSAGE(json, "解析 Json 失败");
  62. RyanJson_t item = NULL;
  63. // 遍历 arrayDouble 数组测试
  64. RyanJsonArrayForEach(RyanJsonGetObjectToKey(json, "arrayDouble"), item)
  65. {
  66. TEST_ASSERT_TRUE_MESSAGE(RyanJsonIsDouble(item), "数组元素不是浮点数类型");
  67. TEST_ASSERT_TRUE_MESSAGE(RyanJsonCompareDouble(16.89, RyanJsonGetDoubleValue(item)), "数组元素值不正确");
  68. }
  69. // 遍历 arrayInt 数组测试
  70. RyanJsonArrayForEach(RyanJsonGetObjectToKey(json, "arrayInt"), item)
  71. {
  72. TEST_ASSERT_TRUE_MESSAGE(RyanJsonIsInt(item), "数组元素不是整数类型");
  73. TEST_ASSERT_EQUAL_INT_MESSAGE(16, RyanJsonGetIntValue(item), "数组元素值不正确");
  74. }
  75. // 遍历 item 对象测试
  76. RyanJsonObjectForEach(RyanJsonGetObjectToKey(json, "item"), item)
  77. {
  78. TEST_ASSERT_NOT_NULL_MESSAGE(RyanJsonGetKey(item), "对象键值为空");
  79. char *str = RyanJsonPrint(item, 128, RyanJsonTrue, NULL);
  80. TEST_ASSERT_NOT_NULL_MESSAGE(str, "遍历项打印失败");
  81. RyanJsonFree(str);
  82. }
  83. RyanJsonDelete(json);
  84. }
  85. void testForEachRunner(void)
  86. {
  87. UnitySetTestFile(__FILE__);
  88. RUN_TEST(testForEachEdgeCases);
  89. RUN_TEST(testForEachIterativeTraversals);
  90. }