Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_json_jsonAppendString.c207 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 7/* Wrapper provided in the module for testing the static function */8extern void test_jsonAppendString(JsonString *p, const char *zIn, u32 N);9 10/* Helpers to init/cleanup a JsonString without calling static functions */11static void jsInit(JsonString *p){12  memset(p, 0, sizeof(*p));13  p->zBuf = p->zSpace;14  p->nAlloc = (u32)sizeof(p->zSpace);15  p->nUsed = 0;16  p->bStatic = 1;17  p->pCtx = 0;18  p->eErr = 0;19}20static void jsTerm(JsonString *p){21  if( p->zBuf && !p->bStatic ){22    sqlite3RCStrUnref(p->zBuf);23  }24  memset(p, 0, sizeof(*p));25}26static const char* jsCString(JsonString *p){27  /* Ensure null-termination for assertions */28  if( p->zBuf ){29    p->zBuf[p->nUsed] = 0;30  }31  return p->zBuf ? p->zBuf : "";32}33 34/* Unity fixtures */35void setUp(void) {36  /* no-op */37}38void tearDown(void) {39  /* no-op */40}41 42/* Utility to build a quoted string of length n consisting of ch */43static char *makeExpectedQuotedRepeat(char ch, int n){44  char *z = (char*)sqlite3_malloc(n + 3);45  TEST_ASSERT_NOT_NULL(z);46  z[0] = '"';47  for(int i=0; i<n; i++) z[i+1] = ch;48  z[n+1] = '"';49  z[n+2] = 0;50  return z;51}52 53/* Tests */54 55void test_jsonAppendString_basic_ascii(void){56  JsonString js;57  jsInit(&js);58 59  const char *in = "abc";60  test_jsonAppendString(&js, in, (u32)3);61  const char *out = jsCString(&js);62 63  TEST_ASSERT_EQUAL_INT(0, js.eErr);64  TEST_ASSERT_EQUAL_UINT(5, js.nUsed); /* "abc" is 5 chars with quotes */65  TEST_ASSERT_EQUAL_STRING("\"abc\"", out);66 67  jsTerm(&js);68}69 70void test_jsonAppendString_escape_quote_backslash_singlequote(void){71  JsonString js;72  jsInit(&js);73 74  char in[] = { '"', '\\', '\'', 0 };75  test_jsonAppendString(&js, in, (u32)3);76  const char *out = jsCString(&js);77 78  /* Expect: "\"\\\"\\\\'\"" */79  TEST_ASSERT_EQUAL_INT(0, js.eErr);80  TEST_ASSERT_EQUAL_STRING("\"\\\"\\\\'\"", out);81 82  jsTerm(&js);83}84 85void test_jsonAppendString_control_chars_and_hex(void){86  JsonString js;87  jsInit(&js);88 89  char in[] = { '\b', '\f', '\n', '\r', '\t', 1, 0 };90  test_jsonAppendString(&js, in, (u32)6);91  const char *out = jsCString(&js);92 93  /* \b \f \n \r \t map to short forms; 0x01 maps to \u0001 */94  TEST_ASSERT_EQUAL_INT(0, js.eErr);95  TEST_ASSERT_EQUAL_STRING("\"\\b\\f\\n\\r\\t\\u0001\"", out);96 97  jsTerm(&js);98}99 100void test_jsonAppendString_embedded_nul(void){101  JsonString js;102  jsInit(&js);103 104  char in[] = { 'A', 0, 'B' };105  test_jsonAppendString(&js, in, (u32)3);106  const char *out = jsCString(&js);107 108  TEST_ASSERT_EQUAL_INT(0, js.eErr);109  TEST_ASSERT_EQUAL_STRING("\"A\\u0000B\"", out);110 111  jsTerm(&js);112}113 114void test_jsonAppendString_zero_length_input(void){115  JsonString js;116  jsInit(&js);117 118  const char *in = "abcdef";119  test_jsonAppendString(&js, in, (u32)0);120  const char *out = jsCString(&js);121 122  TEST_ASSERT_EQUAL_INT(0, js.eErr);123  TEST_ASSERT_EQUAL_STRING("\"\"", out);124 125  jsTerm(&js);126}127 128void test_jsonAppendString_null_input_pointer(void){129  JsonString js;130  jsInit(&js);131 132  /* Should be a no-op */133  test_jsonAppendString(&js, NULL, (u32)5);134  const char *out = jsCString(&js);135 136  TEST_ASSERT_EQUAL_INT(0, js.eErr);137  TEST_ASSERT_EQUAL_UINT(0, js.nUsed);138  TEST_ASSERT_EQUAL_STRING("", out);139 140  jsTerm(&js);141}142 143void test_jsonAppendString_length_less_than_strlen(void){144  JsonString js;145  jsInit(&js);146 147  const char *in = "abcde";148  test_jsonAppendString(&js, in, (u32)3);149  const char *out = jsCString(&js);150 151  TEST_ASSERT_EQUAL_INT(0, js.eErr);152  TEST_ASSERT_EQUAL_STRING("\"abc\"", out);153 154  jsTerm(&js);155}156 157void test_jsonAppendString_large_growth(void){158  JsonString js;159  jsInit(&js);160 161  const int N = 5000;162  char *in = (char*)sqlite3_malloc(N);163  TEST_ASSERT_NOT_NULL(in);164  memset(in, 'x', N);165 166  test_jsonAppendString(&js, in, (u32)N);167  const char *out = jsCString(&js);168 169  /* Build expected */170  char *expected = makeExpectedQuotedRepeat('x', N);171 172  TEST_ASSERT_EQUAL_INT(0, js.eErr);173  TEST_ASSERT_EQUAL_UINT((u32)(N + 2), js.nUsed);174  TEST_ASSERT_EQUAL_STRING(expected, out);175 176  sqlite3_free(in);177  sqlite3_free(expected);178  jsTerm(&js);179}180 181void test_jsonAppendString_appends_to_existing_buffer(void){182  JsonString js;183  jsInit(&js);184 185  test_jsonAppendString(&js, "first", (u32)5);186  test_jsonAppendString(&js, "second", (u32)6);187  const char *out = jsCString(&js);188 189  TEST_ASSERT_EQUAL_INT(0, js.eErr);190  TEST_ASSERT_EQUAL_STRING("\"first\"\"second\"", out);191 192  jsTerm(&js);193}194 195int main(void){196  UNITY_BEGIN();197  RUN_TEST(test_jsonAppendString_basic_ascii);198  RUN_TEST(test_jsonAppendString_escape_quote_backslash_singlequote);199  RUN_TEST(test_jsonAppendString_control_chars_and_hex);200  RUN_TEST(test_jsonAppendString_embedded_nul);201  RUN_TEST(test_jsonAppendString_zero_length_input);202  RUN_TEST(test_jsonAppendString_null_input_pointer);203  RUN_TEST(test_jsonAppendString_length_less_than_strlen);204  RUN_TEST(test_jsonAppendString_large_growth);205  RUN_TEST(test_jsonAppendString_appends_to_existing_buffer);206  return UNITY_END();207}