Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_json_jsonAppendControlChar.c169 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdlib.h>5#include <stdio.h>6 7extern void test_jsonAppendControlChar(JsonString *p, u8 c);8 9static void jsInit(JsonString *p){10  memset(p, 0, sizeof(*p));11  p->zBuf = p->zSpace;12  p->nAlloc = (u32)sizeof(p->zSpace);13  p->nUsed = 0;14  /* Other fields not required by the target function are left zeroed */15}16 17void setUp(void) {18  /* Setup code here, or leave empty */19}20 21void tearDown(void) {22  /* Cleanup code here, or leave empty */23}24 25/* Helper: build expected \u00xx string for a control character c */26static void buildExpectedUnicode(u8 c, char out[7]){27  static const char *hex = "0123456789abcdef";28  out[0] = '\\';29  out[1] = 'u';30  out[2] = '0';31  out[3] = '0';32  out[4] = hex[(c>>4)&0x0f];33  out[5] = hex[c&0x0f];34  out[6] = '\0';35}36 37/* Test the five special escapes: \b, \t, \n, \f, \r */38void test_jsonAppendControlChar_special_escapes(void){39  u8 inputs[] = { 8, 9, 10, 12, 13 };40  const char letters[] = { 'b', 't', 'n', 'f', 'r' };41  for(unsigned i=0; i<sizeof(inputs)/sizeof(inputs[0]); i++){42    JsonString js;43    jsInit(&js);44    test_jsonAppendControlChar(&js, inputs[i]);45    /* Add a terminator for comparison */46    js.zBuf[js.nUsed] = '\0';47 48    char expected[3];49    expected[0] = '\\';50    expected[1] = letters[i];51    expected[2] = '\0';52 53    TEST_ASSERT_EQUAL_UINT32_MESSAGE(2u, js.nUsed, "Expected 2-byte escape length");54    TEST_ASSERT_EQUAL_STRING(expected, js.zBuf);55  }56}57 58/* Test a selection of non-special controls produce the \u00xx form */59void test_jsonAppendControlChar_unicode_escapes_selected(void){60  u8 tests[] = { 0x00, 0x01, 0x02, 0x07, 0x0B, 0x0E, 0x0F, 0x10, 0x1F };61  for(unsigned i=0; i<sizeof(tests)/sizeof(tests[0]); i++){62    u8 c = tests[i];63    /* Skip specials to ensure we test the \u00xx path only */64    if( c==8 || c==9 || c==10 || c==12 || c==13 ) continue;65 66    JsonString js;67    jsInit(&js);68    test_jsonAppendControlChar(&js, c);69    js.zBuf[js.nUsed] = '\0';70 71    char expected[7];72    buildExpectedUnicode(c, expected);73 74    TEST_ASSERT_EQUAL_UINT32_MESSAGE(6u, js.nUsed, "Expected 6-byte unicode escape length");75    TEST_ASSERT_EQUAL_STRING(expected, js.zBuf);76  }77}78 79/* Test full range 0..31, verifying each appended segment matches expected */80void test_jsonAppendControlChar_full_range_appends(void){81  JsonString js;82  jsInit(&js);83 84  for(u8 c=0; c<32; c++){85    u32 start = js.nUsed;86    test_jsonAppendControlChar(&js, c);87    u32 end = js.nUsed;88    TEST_ASSERT_MESSAGE(end > start, "nUsed must increase");89 90    char expected[7];91    size_t expLen = 0;92    if( c==8 ){ expected[0]='\\'; expected[1]='b'; expLen=2; }93    else if( c==9 ){ expected[0]='\\'; expected[1]='t'; expLen=2; }94    else if( c==10 ){ expected[0]='\\'; expected[1]='n'; expLen=2; }95    else if( c==12 ){ expected[0]='\\'; expected[1]='f'; expLen=2; }96    else if( c==13 ){ expected[0]='\\'; expected[1]='r'; expLen=2; }97    else{98      buildExpectedUnicode(c, expected);99      expLen = 6;100    }101 102    TEST_ASSERT_EQUAL_UINT32_MESSAGE((u32)(start + expLen), end, "Incorrect nUsed increment");103    TEST_ASSERT_EQUAL_INT_MESSAGE(0, memcmp(&js.zBuf[start], expected, expLen),104                                  "Appended bytes mismatch");105  }106}107 108/* Boundary test: nUsed == nAlloc - 7, then append a 6-byte unicode sequence */109void test_jsonAppendControlChar_boundary_near_full_unicode(void){110  JsonString js;111  jsInit(&js);112 113  /* Ensure there is at least 7 bytes remaining per function assert */114  TEST_ASSERT_TRUE_MESSAGE(js.nAlloc >= 7, "JsonString buffer too small for test");115  js.nUsed = js.nAlloc - 7;116 117  u32 start = js.nUsed;118  u8 c = 0x00; /* non-special -> unicode escape */119  test_jsonAppendControlChar(&js, c);120 121  TEST_ASSERT_EQUAL_UINT32((u32)(start + 6), js.nUsed);122 123  char expected[7];124  buildExpectedUnicode(c, expected);125  TEST_ASSERT_EQUAL_INT(0, memcmp(&js.zBuf[start], expected, 6));126}127 128/* Boundary test: nUsed == nAlloc - 7, then append a 2-byte special sequence */129void test_jsonAppendControlChar_boundary_near_full_special(void){130  JsonString js;131  jsInit(&js);132 133  TEST_ASSERT_TRUE_MESSAGE(js.nAlloc >= 7, "JsonString buffer too small for test");134  js.nUsed = js.nAlloc - 7;135 136  u32 start = js.nUsed;137  u8 c = 0x0A; /* '\n' -> special escape */138  test_jsonAppendControlChar(&js, c);139 140  TEST_ASSERT_EQUAL_UINT32((u32)(start + 2), js.nUsed);141 142  char expected[3] = {'\\','n','\0'};143  TEST_ASSERT_EQUAL_INT(0, memcmp(&js.zBuf[start], expected, 2));144}145 146/* Verify nUsed increments correctly for both paths */147void test_jsonAppendControlChar_nUsed_increment(void){148  JsonString js;149 150  jsInit(&js);151  u32 before = js.nUsed;152  test_jsonAppendControlChar(&js, 0x05); /* unicode path -> 6 */153  TEST_ASSERT_EQUAL_UINT32(before + 6, js.nUsed);154 155  before = js.nUsed;156  test_jsonAppendControlChar(&js, 0x09); /* '\t' -> special path -> 2 */157  TEST_ASSERT_EQUAL_UINT32(before + 2, js.nUsed);158}159 160int main(void) {161  UNITY_BEGIN();162  RUN_TEST(test_jsonAppendControlChar_special_escapes);163  RUN_TEST(test_jsonAppendControlChar_unicode_escapes_selected);164  RUN_TEST(test_jsonAppendControlChar_full_range_appends);165  RUN_TEST(test_jsonAppendControlChar_boundary_near_full_unicode);166  RUN_TEST(test_jsonAppendControlChar_boundary_near_full_special);167  RUN_TEST(test_jsonAppendControlChar_nUsed_increment);168  return UNITY_END();169}