Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_dbstat_statOpen.c93 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdlib.h>5 6/* Prototype of the wrapper provided in the module */7int test_statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);8 9static void *alloc_dummy_vtab(size_t sz){10  /* Allocate a zeroed block large enough to safely contain StatTable fields.11     We choose a conservative size to avoid any out-of-bounds reads. */12  if( sz == 0 ){13    sz = 256;14  }15  void *p = sqlite3_malloc64(sz);16  if( p ) memset(p, 0, sz);17  return p;18}19 20void setUp(void) {21  /* No setup required */22}23 24void tearDown(void) {25  /* No teardown required */26}27 28void test_statOpen_basic_success(void) {29  sqlite3_vtab *pVTab = (sqlite3_vtab*)alloc_dummy_vtab(256);30  TEST_ASSERT_NOT_NULL_MESSAGE(pVTab, "Failed to allocate dummy vtab memory");31 32  sqlite3_vtab_cursor *pCur = NULL;33  int rc = test_statOpen(pVTab, &pCur);34 35  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);36  TEST_ASSERT_NOT_NULL(pCur);37  /* Verify base.pVtab points to the supplied vtab */38  TEST_ASSERT_EQUAL_PTR(pVTab, pCur->pVtab);39 40  /* Cleanup */41  sqlite3_free(pCur);42  sqlite3_free(pVTab);43}44 45void test_statOpen_two_independent_cursors(void) {46  sqlite3_vtab *pVTab = (sqlite3_vtab*)alloc_dummy_vtab(256);47  TEST_ASSERT_NOT_NULL(pVTab);48 49  sqlite3_vtab_cursor *pCur1 = NULL;50  sqlite3_vtab_cursor *pCur2 = NULL;51 52  int rc1 = test_statOpen(pVTab, &pCur1);53  int rc2 = test_statOpen(pVTab, &pCur2);54 55  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc1);56  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc2);57  TEST_ASSERT_NOT_NULL(pCur1);58  TEST_ASSERT_NOT_NULL(pCur2);59 60  /* Both cursors should reference the same vtab base pointer, but be distinct allocations */61  TEST_ASSERT_EQUAL_PTR(pVTab, pCur1->pVtab);62  TEST_ASSERT_EQUAL_PTR(pVTab, pCur2->pVtab);63  TEST_ASSERT_NOT_EQUAL(pCur1, pCur2);64 65  /* Cleanup */66  sqlite3_free(pCur1);67  sqlite3_free(pCur2);68  sqlite3_free(pVTab);69}70 71void test_statOpen_multiple_open_close_cycle(void) {72  sqlite3_vtab *pVTab = (sqlite3_vtab*)alloc_dummy_vtab(256);73  TEST_ASSERT_NOT_NULL(pVTab);74 75  for (int i = 0; i < 10; i++) {76    sqlite3_vtab_cursor *pCur = NULL;77    int rc = test_statOpen(pVTab, &pCur);78    TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);79    TEST_ASSERT_NOT_NULL(pCur);80    TEST_ASSERT_EQUAL_PTR(pVTab, pCur->pVtab);81    sqlite3_free(pCur);82  }83 84  sqlite3_free(pVTab);85}86 87int main(void) {88  UNITY_BEGIN();89  RUN_TEST(test_statOpen_basic_success);90  RUN_TEST(test_statOpen_two_independent_cursors);91  RUN_TEST(test_statOpen_multiple_open_close_cycle);92  return UNITY_END();93}