Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_json_jsonObjectCompute.c112 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3 4#include <stdlib.h>5#include <string.h>6#include <stdint.h>7 8/* The target function is static in the module; call the test wrapper. */9void test_jsonObjectCompute(sqlite3_context *ctx, int isFinal);10 11static sqlite3 *gDb = NULL;12 13/* Scalar function that invokes test_jsonObjectCompute with provided isFinal. */14static void udf_call_jsonObjectCompute(sqlite3_context *ctx, int argc, sqlite3_value **argv){15  UNUSED_PARAMETER(argc);16  int isFinal = sqlite3_value_int(argv[0]);17  test_jsonObjectCompute(ctx, isFinal);18}19 20/* Helper: prepare, step, and fetch first column as text with subtype and type. */21static void run_query_single_text(const char *zSql, int bindInt0,22                                  const unsigned char **pzText, int *pnBytes,23                                  unsigned int *pSubtype, int *pType){24  sqlite3_stmt *pStmt = 0;25  int rc = sqlite3_prepare_v2(gDb, zSql, -1, &pStmt, 0);26  TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "prepare failed");27  rc = sqlite3_bind_int(pStmt, 1, bindInt0);28  TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "bind failed");29  rc = sqlite3_step(pStmt);30  TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_ROW, rc, "step did not yield row");31  if( pType ) *pType = sqlite3_column_type(pStmt, 0);32  if( pSubtype ) *pSubtype = sqlite3_column_subtype(pStmt, 0);33  if( pzText ) *pzText = (const unsigned char*)sqlite3_column_text(pStmt, 0);34  if( pnBytes ) *pnBytes = sqlite3_column_bytes(pStmt, 0);35  rc = sqlite3_finalize(pStmt);36  TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "finalize failed");37}38 39void setUp(void) {40  int rc = sqlite3_initialize();41  TEST_ASSERT_TRUE(rc==SQLITE_OK || rc==SQLITE_MISUSE);42  rc = sqlite3_open_v2(":memory:", &gDb, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL);43  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);44 45  /* Register two scalar entry points with different user_data flags. */46  /* Plain: user_data = 0 */47  rc = sqlite3_create_function(gDb, "joc_plain", 1, SQLITE_UTF8, (void*)(intptr_t)0,48                               udf_call_jsonObjectCompute, 0, 0);49  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);50 51  /* Blob-flagged: user_data non-zero (bit pattern irrelevant when pStr==NULL) */52  rc = sqlite3_create_function(gDb, "joc_blob", 1, SQLITE_UTF8, (void*)(intptr_t)0x7fffffff,53                               udf_call_jsonObjectCompute, 0, 0);54  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);55}56 57void tearDown(void) {58  if( gDb ){ sqlite3_close(gDb); gDb = NULL; }59}60 61/* Basic: No aggregate context, isFinal=0, user_data=0. Expect "{}" text with JSON subtype. */62void test_jsonObjectCompute_nullContext_isFinal0_plain(void){63  const unsigned char *z;64  int n; unsigned int st; int typ;65  run_query_single_text("SELECT joc_plain(?)", 0, &z, &n, &st, &typ);66  TEST_ASSERT_EQUAL_INT(SQLITE_TEXT, typ);67  TEST_ASSERT_EQUAL_INT(JSON_SUBTYPE, (int)st);68  TEST_ASSERT_EQUAL_INT(2, n);69  TEST_ASSERT_EQUAL_STRING("{}", (const char*)z);70}71 72/* No aggregate context, isFinal=1, user_data=0. Expect same as above. */73void test_jsonObjectCompute_nullContext_isFinal1_plain(void){74  const unsigned char *z;75  int n; unsigned int st; int typ;76  run_query_single_text("SELECT joc_plain(?)", 1, &z, &n, &st, &typ);77  TEST_ASSERT_EQUAL_INT(SQLITE_TEXT, typ);78  TEST_ASSERT_EQUAL_INT(JSON_SUBTYPE, (int)st);79  TEST_ASSERT_EQUAL_INT(2, n);80  TEST_ASSERT_EQUAL_STRING("{}", (const char*)z);81}82 83/* No aggregate context, isFinal=0, user_data non-zero (JSON_BLOB-like). Still returns text "{}". */84void test_jsonObjectCompute_nullContext_isFinal0_blobflag(void){85  const unsigned char *z;86  int n; unsigned int st; int typ;87  run_query_single_text("SELECT joc_blob(?)", 0, &z, &n, &st, &typ);88  TEST_ASSERT_EQUAL_INT(SQLITE_TEXT, typ);89  TEST_ASSERT_EQUAL_INT(JSON_SUBTYPE, (int)st);90  TEST_ASSERT_EQUAL_INT(2, n);91  TEST_ASSERT_EQUAL_STRING("{}", (const char*)z);92}93 94/* No aggregate context, isFinal=1, user_data non-zero (JSON_BLOB-like). Still returns text "{}". */95void test_jsonObjectCompute_nullContext_isFinal1_blobflag(void){96  const unsigned char *z;97  int n; unsigned int st; int typ;98  run_query_single_text("SELECT joc_blob(?)", 1, &z, &n, &st, &typ);99  TEST_ASSERT_EQUAL_INT(SQLITE_TEXT, typ);100  TEST_ASSERT_EQUAL_INT(JSON_SUBTYPE, (int)st);101  TEST_ASSERT_EQUAL_INT(2, n);102  TEST_ASSERT_EQUAL_STRING("{}", (const char*)z);103}104 105int main(void){106  UNITY_BEGIN();107  RUN_TEST(test_jsonObjectCompute_nullContext_isFinal0_plain);108  RUN_TEST(test_jsonObjectCompute_nullContext_isFinal1_plain);109  RUN_TEST(test_jsonObjectCompute_nullContext_isFinal0_blobflag);110  RUN_TEST(test_jsonObjectCompute_nullContext_isFinal1_blobflag);111  return UNITY_END();112}