testInternalApis.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #include "testBase.h"
  2. static char *allocRepeatChar(char ch, uint32_t count)
  3. {
  4. char *buf = (char *)malloc((size_t)count + 1U);
  5. TEST_ASSERT_NOT_NULL(buf);
  6. memset(buf, (int)ch, count);
  7. buf[count] = '\0';
  8. return buf;
  9. }
  10. static int gInternalReallocCalls = 0;
  11. static void *internalReallocWrap(void *block, size_t size)
  12. {
  13. gInternalReallocCalls++;
  14. return unityTestRealloc(block, size);
  15. }
  16. static void testInternalStrPtrModeBufSwap(void)
  17. {
  18. const char *key = "k";
  19. uint32_t valueLen = RyanJsonInlineStringSize + 8U;
  20. char *value = allocRepeatChar('x', valueLen);
  21. RyanJson_t node = RyanJsonCreateString(key, value);
  22. TEST_ASSERT_NOT_NULL(node);
  23. TEST_ASSERT_TRUE(RyanJsonIsString(node));
  24. TEST_ASSERT_TRUE_MESSAGE(RyanJsonGetPayloadStrIsPtrByFlag(node), "expected ptr mode");
  25. uint8_t *oldBuf = RyanJsonInternalGetStrPtrModeBuf(node);
  26. TEST_ASSERT_NOT_NULL(oldBuf);
  27. size_t keyLen = strlen(key);
  28. size_t total = keyLen + 1U + valueLen + 1U;
  29. uint8_t *newBuf = (uint8_t *)jsonMalloc(total);
  30. TEST_ASSERT_NOT_NULL(newBuf);
  31. memcpy(newBuf, key, keyLen);
  32. newBuf[keyLen] = '\0';
  33. memset(newBuf + keyLen + 1U, 'y', valueLen);
  34. newBuf[keyLen + 1U + valueLen] = '\0';
  35. RyanJsonInternalSetStrPtrModeBuf(node, newBuf);
  36. TEST_ASSERT_EQUAL_PTR(newBuf, RyanJsonInternalGetStrPtrModeBuf(node));
  37. TEST_ASSERT_EQUAL_PTR(newBuf, RyanJsonInternalGetStrPtrModeBufAt(node, 0));
  38. TEST_ASSERT_EQUAL_PTR(newBuf + keyLen + 1U, RyanJsonInternalGetStrPtrModeBufAt(node, (uint32_t)(keyLen + 1U)));
  39. TEST_ASSERT_EQUAL_STRING(key, RyanJsonGetKey(node));
  40. char *expect = allocRepeatChar('y', valueLen);
  41. TEST_ASSERT_EQUAL_STRING(expect, RyanJsonGetStringValue(node));
  42. free(expect);
  43. jsonFree(oldBuf);
  44. RyanJsonDelete(node);
  45. free(value);
  46. }
  47. static void testInternalExpandReallocUsesReallocHook(void)
  48. {
  49. unityTestLeakScope_t scope = unityTestLeakScopeBegin();
  50. gInternalReallocCalls = 0;
  51. RyanJsonInitHooks(unityTestMalloc, unityTestFree, internalReallocWrap);
  52. uint8_t *buf = (uint8_t *)unityTestMalloc(8U);
  53. TEST_ASSERT_NOT_NULL(buf);
  54. for (uint32_t i = 0; i < 8U; i++)
  55. {
  56. buf[i] = (uint8_t)(0xA0U + i);
  57. }
  58. uint8_t *out = (uint8_t *)RyanJsonInternalExpandRealloc(buf, 8U, 32U);
  59. TEST_ASSERT_NOT_NULL(out);
  60. TEST_ASSERT_EQUAL_INT_MESSAGE(1, gInternalReallocCalls, "realloc hook should be used when provided");
  61. for (uint32_t i = 0; i < 8U; i++)
  62. {
  63. TEST_ASSERT_EQUAL_UINT8((uint8_t)(0xA0U + i), out[i]);
  64. }
  65. RyanJsonFree(out);
  66. RyanJsonInitHooks(unityTestMalloc, unityTestFree, unityTestRealloc);
  67. unityTestLeakScopeEnd(scope, "ExpandRealloc with realloc should not leak");
  68. }
  69. static void testInternalExpandReallocFallbackCopy(void)
  70. {
  71. unityTestLeakScope_t scope = unityTestLeakScopeBegin();
  72. RyanJsonInitHooks(unityTestMalloc, unityTestFree, NULL);
  73. uint8_t *buf = (uint8_t *)unityTestMalloc(12U);
  74. TEST_ASSERT_NOT_NULL(buf);
  75. for (uint32_t i = 0; i < 12U; i++)
  76. {
  77. buf[i] = (uint8_t)(0x5AU ^ i);
  78. }
  79. uint8_t *out = (uint8_t *)RyanJsonInternalExpandRealloc(buf, 12U, 48U);
  80. TEST_ASSERT_NOT_NULL(out);
  81. for (uint32_t i = 0; i < 12U; i++)
  82. {
  83. TEST_ASSERT_EQUAL_UINT8((uint8_t)(0x5AU ^ i), out[i]);
  84. }
  85. RyanJsonFree(out);
  86. RyanJsonInitHooks(unityTestMalloc, unityTestFree, unityTestRealloc);
  87. unityTestLeakScopeEnd(scope, "ExpandRealloc fallback should not leak");
  88. }
  89. static void testInternalNewNodeBoolFlag(void)
  90. {
  91. RyanJsonNodeInfo_t info;
  92. memset(&info, 0, sizeof(info));
  93. info.type = RyanJsonTypeBool;
  94. info.boolIsTrueFlag = RyanJsonTrue;
  95. RyanJson_t node = RyanJsonInternalNewNode(&info);
  96. TEST_ASSERT_NOT_NULL(node);
  97. TEST_ASSERT_TRUE(RyanJsonIsBool(node));
  98. TEST_ASSERT_TRUE(RyanJsonGetBoolValue(node));
  99. RyanJsonDelete(node);
  100. info.boolIsTrueFlag = RyanJsonFalse;
  101. node = RyanJsonInternalNewNode(&info);
  102. TEST_ASSERT_NOT_NULL(node);
  103. TEST_ASSERT_TRUE(RyanJsonIsBool(node));
  104. TEST_ASSERT_FALSE(RyanJsonGetBoolValue(node));
  105. RyanJsonDelete(node);
  106. }
  107. static void testInternalNewNodeNumberPayloadAccess(void)
  108. {
  109. RyanJsonNodeInfo_t info;
  110. memset(&info, 0, sizeof(info));
  111. info.type = RyanJsonTypeNumber;
  112. info.numberIsDoubleFlag = RyanJsonFalse;
  113. RyanJson_t intNode = RyanJsonInternalNewNode(&info);
  114. TEST_ASSERT_NOT_NULL(intNode);
  115. TEST_ASSERT_TRUE(RyanJsonIsInt(intNode));
  116. int32_t intValue = -42;
  117. RyanJsonMemcpy(RyanJsonInternalGetValue(intNode), &intValue, sizeof(intValue));
  118. TEST_ASSERT_EQUAL_INT(intValue, RyanJsonGetIntValue(intNode));
  119. RyanJsonDelete(intNode);
  120. info.numberIsDoubleFlag = RyanJsonTrue;
  121. RyanJson_t dblNode = RyanJsonInternalNewNode(&info);
  122. TEST_ASSERT_NOT_NULL(dblNode);
  123. TEST_ASSERT_TRUE(RyanJsonIsDouble(dblNode));
  124. double dblValue = 3.25;
  125. RyanJsonMemcpy(RyanJsonInternalGetValue(dblNode), &dblValue, sizeof(dblValue));
  126. TEST_ASSERT_TRUE(RyanJsonCompareDouble(dblValue, RyanJsonGetDoubleValue(dblNode)));
  127. RyanJsonDelete(dblNode);
  128. }
  129. static void testInternalCreateContainerWithKeyRoot(void)
  130. {
  131. RyanJson_t obj = RyanJsonInternalCreateObjectAndKey("obj");
  132. TEST_ASSERT_NOT_NULL(obj);
  133. TEST_ASSERT_TRUE(RyanJsonIsObject(obj));
  134. TEST_ASSERT_TRUE(RyanJsonIsKey(obj));
  135. TEST_ASSERT_EQUAL_STRING("obj", RyanJsonGetKey(obj));
  136. TEST_ASSERT_TRUE(RyanJsonAddIntToObject(obj, "a", 1));
  137. TEST_ASSERT_EQUAL_INT(1, RyanJsonGetIntValue(RyanJsonGetObjectByKey(obj, "a")));
  138. RyanJson_t arr = RyanJsonInternalCreateArrayAndKey("arr");
  139. TEST_ASSERT_NOT_NULL(arr);
  140. TEST_ASSERT_TRUE(RyanJsonIsArray(arr));
  141. TEST_ASSERT_TRUE(RyanJsonIsKey(arr));
  142. TEST_ASSERT_EQUAL_STRING("arr", RyanJsonGetKey(arr));
  143. TEST_ASSERT_TRUE(RyanJsonAddIntToArray(arr, 7));
  144. TEST_ASSERT_EQUAL_INT(7, RyanJsonGetIntValue(RyanJsonGetObjectByIndex(arr, 0)));
  145. RyanJsonDelete(obj);
  146. RyanJsonDelete(arr);
  147. }
  148. static void testInternalChangeObjectValueWithPreparedChild(void)
  149. {
  150. RyanJson_t obj = RyanJsonCreateObject();
  151. RyanJson_t child = RyanJsonCreateInt("a", 1);
  152. TEST_ASSERT_NOT_NULL(obj);
  153. TEST_ASSERT_NOT_NULL(child);
  154. child->next = obj;
  155. RyanJsonSetPayloadIsLastByFlag(child, 1);
  156. TEST_ASSERT_TRUE(RyanJsonInternalChangeObjectValue(obj, child));
  157. TEST_ASSERT_EQUAL_PTR(child, RyanJsonGetObjectValue(obj));
  158. TEST_ASSERT_EQUAL_PTR(child, RyanJsonGetObjectByIndex(obj, 0));
  159. RyanJsonDelete(obj);
  160. }
  161. static void testInternalParseDoubleRawNonNullTerminated(void)
  162. {
  163. double number = 0.0;
  164. const uint8_t bufInt[] = {'1', '2', '3', '4'};
  165. TEST_ASSERT_TRUE(RyanJsonInternalParseDoubleRaw(bufInt, (uint32_t)sizeof(bufInt), &number));
  166. TEST_ASSERT_TRUE(RyanJsonCompareDouble(1234.0, number));
  167. const uint8_t bufFrac[] = {'-', '1', '2', '.', '5'};
  168. TEST_ASSERT_TRUE(RyanJsonInternalParseDoubleRaw(bufFrac, (uint32_t)sizeof(bufFrac), &number));
  169. TEST_ASSERT_TRUE(RyanJsonCompareDouble(-12.5, number));
  170. const uint8_t bufExp[] = {'1', 'e', '2'};
  171. TEST_ASSERT_TRUE(RyanJsonInternalParseDoubleRaw(bufExp, (uint32_t)sizeof(bufExp), &number));
  172. TEST_ASSERT_TRUE(RyanJsonCompareDouble(100.0, number));
  173. }
  174. static void testInternalStrEqFastPaths(void)
  175. {
  176. const char *s = "abc";
  177. TEST_ASSERT_TRUE(RyanJsonInternalStrEq(s, s));
  178. TEST_ASSERT_TRUE(RyanJsonInternalStrEq("abc", "abc"));
  179. TEST_ASSERT_FALSE(RyanJsonInternalStrEq("abc", "abD"));
  180. }
  181. static void testInternalListInsertAfterHeadAndTail(void)
  182. {
  183. RyanJson_t obj = RyanJsonCreateObject();
  184. TEST_ASSERT_NOT_NULL(obj);
  185. RyanJson_t first = RyanJsonCreateInt("a", 1);
  186. TEST_ASSERT_NOT_NULL(first);
  187. RyanJsonInternalListInsertAfter(obj, NULL, first);
  188. TEST_ASSERT_EQUAL_PTR(obj, RyanJsonInternalGetParent(first));
  189. TEST_ASSERT_EQUAL_PTR(first, RyanJsonGetObjectValue(obj));
  190. TEST_ASSERT_TRUE_MESSAGE(RyanJsonGetPayloadIsLastByFlag(first), "单节点插入后应为 last");
  191. TEST_ASSERT_NULL_MESSAGE(RyanJsonGetNext(first), "单节点 next 应返回 NULL");
  192. TEST_ASSERT_EQUAL_PTR(obj, first->next);
  193. RyanJson_t second = RyanJsonCreateInt("b", 2);
  194. TEST_ASSERT_NOT_NULL(second);
  195. RyanJsonInternalListInsertAfter(obj, first, second);
  196. TEST_ASSERT_FALSE_MESSAGE(RyanJsonGetPayloadIsLastByFlag(first), "插入尾部后 prev 不应为 last");
  197. TEST_ASSERT_TRUE_MESSAGE(RyanJsonGetPayloadIsLastByFlag(second), "尾节点应标记为 last");
  198. TEST_ASSERT_EQUAL_PTR(second, RyanJsonGetNext(first));
  199. TEST_ASSERT_NULL_MESSAGE(RyanJsonGetNext(second), "尾节点 next 应返回 NULL");
  200. TEST_ASSERT_EQUAL_PTR(obj, second->next);
  201. TEST_ASSERT_EQUAL_PTR(obj, RyanJsonInternalGetParent(second));
  202. RyanJsonDelete(obj);
  203. }
  204. static void testInternalListInsertAfterMiddlePreservesTail(void)
  205. {
  206. RyanJson_t obj = RyanJsonCreateObject();
  207. TEST_ASSERT_NOT_NULL(obj);
  208. RyanJson_t first = RyanJsonCreateInt("a", 1);
  209. RyanJson_t tail = RyanJsonCreateInt("b", 2);
  210. TEST_ASSERT_NOT_NULL(first);
  211. TEST_ASSERT_NOT_NULL(tail);
  212. RyanJsonInternalListInsertAfter(obj, NULL, first);
  213. RyanJsonInternalListInsertAfter(obj, first, tail);
  214. RyanJson_t middle = RyanJsonCreateInt("c", 3);
  215. TEST_ASSERT_NOT_NULL(middle);
  216. RyanJsonInternalListInsertAfter(obj, first, middle);
  217. TEST_ASSERT_EQUAL_PTR(middle, RyanJsonGetNext(first));
  218. TEST_ASSERT_EQUAL_PTR(tail, RyanJsonGetNext(middle));
  219. TEST_ASSERT_FALSE_MESSAGE(RyanJsonGetPayloadIsLastByFlag(middle), "中间节点不应为 last");
  220. TEST_ASSERT_TRUE_MESSAGE(RyanJsonGetPayloadIsLastByFlag(tail), "原尾节点应保持 last");
  221. TEST_ASSERT_EQUAL_PTR(obj, RyanJsonInternalGetParent(middle));
  222. TEST_ASSERT_EQUAL_PTR(obj, RyanJsonInternalGetParent(tail));
  223. RyanJsonDelete(obj);
  224. }
  225. static void testInternalInitHooksArgumentGuards(void)
  226. {
  227. TEST_ASSERT_FALSE_MESSAGE(RyanJsonInitHooks(NULL, unityTestFree, unityTestRealloc), "InitHooks(NULL malloc) 应失败");
  228. TEST_ASSERT_FALSE_MESSAGE(RyanJsonInitHooks(unityTestMalloc, NULL, unityTestRealloc), "InitHooks(NULL free) 应失败");
  229. TEST_ASSERT_TRUE_MESSAGE(RyanJsonInitHooks(unityTestMalloc, unityTestFree, unityTestRealloc), "恢复默认 hooks 应成功");
  230. }
  231. static void testInternalGetValueForPublicKeyedNumbers(void)
  232. {
  233. RyanJson_t intNode = RyanJsonCreateInt("k", -7);
  234. TEST_ASSERT_NOT_NULL(intNode);
  235. int32_t intValue = 0;
  236. RyanJsonMemcpy(&intValue, RyanJsonInternalGetValue(intNode), sizeof(intValue));
  237. TEST_ASSERT_EQUAL_INT(-7, intValue);
  238. RyanJsonDelete(intNode);
  239. RyanJson_t doubleNode = RyanJsonCreateDouble("d", -8.75);
  240. TEST_ASSERT_NOT_NULL(doubleNode);
  241. double doubleValue = 0.0;
  242. RyanJsonMemcpy(&doubleValue, RyanJsonInternalGetValue(doubleNode), sizeof(doubleValue));
  243. TEST_ASSERT_TRUE(RyanJsonCompareDouble(-8.75, doubleValue));
  244. RyanJsonDelete(doubleNode);
  245. }
  246. static void testInternalKeyLenEncodingHelpers(void)
  247. {
  248. // RyanJsonInternalCalcLenBytes:keyLen -> 字节数映射
  249. TEST_ASSERT_EQUAL_UINT8(1, RyanJsonInternalCalcLenBytes(0));
  250. TEST_ASSERT_EQUAL_UINT8(1, RyanJsonInternalCalcLenBytes(UINT8_MAX));
  251. TEST_ASSERT_EQUAL_UINT8(2, RyanJsonInternalCalcLenBytes((uint32_t)UINT8_MAX + 1U));
  252. TEST_ASSERT_EQUAL_UINT8(2, RyanJsonInternalCalcLenBytes(UINT16_MAX));
  253. TEST_ASSERT_EQUAL_UINT8(3, RyanJsonInternalCalcLenBytes((uint32_t)UINT16_MAX + 1U));
  254. // RyanJsonInternalDecodeKeyLenField:编码值 3 表示 4 字节,其余保持不变
  255. TEST_ASSERT_EQUAL_UINT8(0, RyanJsonInternalDecodeKeyLenField(0));
  256. TEST_ASSERT_EQUAL_UINT8(1, RyanJsonInternalDecodeKeyLenField(1));
  257. TEST_ASSERT_EQUAL_UINT8(2, RyanJsonInternalDecodeKeyLenField(2));
  258. TEST_ASSERT_EQUAL_UINT8(4, RyanJsonInternalDecodeKeyLenField(3));
  259. }
  260. static void testInternalGetKeyLenForDifferentSizes(void)
  261. {
  262. RyanJson_t root = RyanJsonCreateObject();
  263. TEST_ASSERT_NOT_NULL(root);
  264. TEST_ASSERT_TRUE(RyanJsonAddIntToObject(root, "", 1));
  265. RyanJson_t empty = RyanJsonGetObjectByKey(root, "");
  266. TEST_ASSERT_NOT_NULL(empty);
  267. TEST_ASSERT_EQUAL_UINT32(0U, RyanJsonInternalGetKeyLen(empty));
  268. TEST_ASSERT_TRUE(RyanJsonAddIntToObject(root, "abc", 2));
  269. RyanJson_t shortNode = RyanJsonGetObjectByKey(root, "abc");
  270. TEST_ASSERT_NOT_NULL(shortNode);
  271. TEST_ASSERT_EQUAL_UINT32(3U, RyanJsonInternalGetKeyLen(shortNode));
  272. uint32_t longLen = (uint32_t)UINT8_MAX + 5U;
  273. char *longKey = (char *)malloc(longLen + 1U);
  274. TEST_ASSERT_NOT_NULL(longKey);
  275. memset(longKey, 'k', longLen);
  276. longKey[longLen] = '\0';
  277. TEST_ASSERT_TRUE(RyanJsonAddIntToObject(root, longKey, 3));
  278. RyanJson_t longNode = RyanJsonGetObjectByKey(root, longKey);
  279. TEST_ASSERT_NOT_NULL(longNode);
  280. TEST_ASSERT_EQUAL_UINT32(longLen, RyanJsonInternalGetKeyLen(longNode));
  281. free(longKey);
  282. RyanJsonDelete(root);
  283. }
  284. static void testInternalInlineStringSizeFormula(void)
  285. {
  286. // 验证 InlineStringSize 与当前平台布局公式保持一致,避免静态配置回归。
  287. uint32_t base = (uint32_t)(2U * sizeof(void *) + (uint32_t)(RyanJsonMallocHeaderSize / 2U));
  288. uint32_t expected = (uint32_t)RyanJsonAlign(base, RyanJsonMallocAlign) - (uint32_t)RyanJsonFlagSize;
  289. uint32_t actual = (uint32_t)RyanJsonInlineStringSize;
  290. TEST_ASSERT_EQUAL_UINT32_MESSAGE(expected, actual, "InlineStringSize 公式不一致");
  291. TEST_ASSERT_TRUE_MESSAGE(((actual + (uint32_t)RyanJsonFlagSize) % (uint32_t)RyanJsonMallocAlign) == 0U,
  292. "InlineStringSize 与对齐粒度不匹配");
  293. TEST_ASSERT_TRUE_MESSAGE(actual > 0U, "InlineStringSize 应为正数");
  294. }
  295. void testInternalApisRunner(void)
  296. {
  297. UnitySetTestFile(__FILE__);
  298. RUN_TEST(testInternalStrPtrModeBufSwap);
  299. RUN_TEST(testInternalExpandReallocUsesReallocHook);
  300. RUN_TEST(testInternalExpandReallocFallbackCopy);
  301. RUN_TEST(testInternalInitHooksArgumentGuards);
  302. RUN_TEST(testInternalNewNodeBoolFlag);
  303. RUN_TEST(testInternalNewNodeNumberPayloadAccess);
  304. RUN_TEST(testInternalGetValueForPublicKeyedNumbers);
  305. RUN_TEST(testInternalKeyLenEncodingHelpers);
  306. RUN_TEST(testInternalGetKeyLenForDifferentSizes);
  307. RUN_TEST(testInternalInlineStringSizeFormula);
  308. RUN_TEST(testInternalCreateContainerWithKeyRoot);
  309. RUN_TEST(testInternalChangeObjectValueWithPreparedChild);
  310. RUN_TEST(testInternalParseDoubleRawNonNullTerminated);
  311. RUN_TEST(testInternalStrEqFastPaths);
  312. RUN_TEST(testInternalListInsertAfterHeadAndTail);
  313. RUN_TEST(testInternalListInsertAfterMiddlePreservesTail);
  314. }