Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_mem0_sqlite3MemSetDefault.c118 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <stdlib.h>4#include <string.h>5 6void setUp(void) {7  /* Ensure SQLite is shut down so sqlite3_config() will accept changes */8  sqlite3_shutdown();9}10 11void tearDown(void) {12  /* Leave SQLite shut down to avoid interference across tests */13  sqlite3_shutdown();14}15 16#ifdef SQLITE_ZERO_MALLOC17/* Forward declaration in case not provided by headers */18extern void sqlite3MemSetDefault(void);19#endif20 21#ifdef SQLITE_ZERO_MALLOC22 23static void assertNoopMethodsBehavior(const sqlite3_mem_methods *m){24  /* xInit should return SQLITE_OK and be callable with pAppData */25  int rc = m->xInit(m->pAppData);26  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);27 28  /* xMalloc should always return NULL */29  void *p = m->xMalloc(128);30  TEST_ASSERT_NULL(p);31 32  /* xRealloc should always return NULL regardless of inputs */33  void *p2 = m->xRealloc((void*)0x1, 256);34  TEST_ASSERT_NULL(p2);35 36  /* xSize should return 0 regardless of pointer */37  int sz0 = m->xSize(NULL);38  int sz1 = m->xSize((void*)0xDEADBEEF);39  TEST_ASSERT_EQUAL_INT(0, sz0);40  TEST_ASSERT_EQUAL_INT(0, sz1);41 42  /* xRoundup should return the input unchanged */43  TEST_ASSERT_EQUAL_INT(0, m->xRoundup(0));44  TEST_ASSERT_EQUAL_INT(1, m->xRoundup(1));45  TEST_ASSERT_EQUAL_INT(7, m->xRoundup(7));46  TEST_ASSERT_EQUAL_INT(8, m->xRoundup(8));47  TEST_ASSERT_EQUAL_INT(31, m->xRoundup(31));48  TEST_ASSERT_EQUAL_INT(1024, m->xRoundup(1024));49 50  /* xShutdown should be callable without effect */51  m->xShutdown(m->pAppData);52}53 54void test_sqlite3MemSetDefault_sets_nonnull_function_pointers(void) {55  sqlite3MemSetDefault();56 57  const sqlite3_mem_methods *m = &sqlite3GlobalConfig.m;58 59  TEST_ASSERT_NOT_NULL(m->xMalloc);60  TEST_ASSERT_NOT_NULL(m->xFree);61  TEST_ASSERT_NOT_NULL(m->xRealloc);62  TEST_ASSERT_NOT_NULL(m->xSize);63  TEST_ASSERT_NOT_NULL(m->xRoundup);64  TEST_ASSERT_NOT_NULL(m->xInit);65  TEST_ASSERT_NOT_NULL(m->xShutdown);66 67  /* pAppData is set to 0 in mem0.c defaultMethods */68  TEST_ASSERT_NULL(m->pAppData);69}70 71void test_sqlite3MemSetDefault_methods_exhibit_noop_behavior(void) {72  sqlite3MemSetDefault();73 74  const sqlite3_mem_methods *m = &sqlite3GlobalConfig.m;75  assertNoopMethodsBehavior(m);76}77 78void test_sqlite3MemSetDefault_idempotent_multiple_calls(void) {79  sqlite3MemSetDefault();80  sqlite3_mem_methods m1 = sqlite3GlobalConfig.m;81 82  /* Call again; function pointers should remain the same and behavior unchanged */83  sqlite3MemSetDefault();84  sqlite3_mem_methods m2 = sqlite3GlobalConfig.m;85 86  TEST_ASSERT_EQUAL_PTR(m1.xMalloc,   m2.xMalloc);87  TEST_ASSERT_EQUAL_PTR(m1.xFree,     m2.xFree);88  TEST_ASSERT_EQUAL_PTR(m1.xRealloc,  m2.xRealloc);89  TEST_ASSERT_EQUAL_PTR(m1.xSize,     m2.xSize);90  TEST_ASSERT_EQUAL_PTR(m1.xRoundup,  m2.xRoundup);91  TEST_ASSERT_EQUAL_PTR(m1.xInit,     m2.xInit);92  TEST_ASSERT_EQUAL_PTR(m1.xShutdown, m2.xShutdown);93  TEST_ASSERT_EQUAL_PTR(m1.pAppData,  m2.pAppData);94 95  assertNoopMethodsBehavior(&m2);96}97 98#else /* !SQLITE_ZERO_MALLOC */99 100void test_sqlite3MemSetDefault_skipped_without_SQLITE_ZERO_MALLOC(void) {101  TEST_IGNORE_MESSAGE("SQLITE_ZERO_MALLOC not defined; sqlite3MemSetDefault not available in this build.");102}103 104#endif /* SQLITE_ZERO_MALLOC */105 106int main(void) {107  UNITY_BEGIN();108 109#ifdef SQLITE_ZERO_MALLOC110  RUN_TEST(test_sqlite3MemSetDefault_sets_nonnull_function_pointers);111  RUN_TEST(test_sqlite3MemSetDefault_methods_exhibit_noop_behavior);112  RUN_TEST(test_sqlite3MemSetDefault_idempotent_multiple_calls);113#else114  RUN_TEST(test_sqlite3MemSetDefault_skipped_without_SQLITE_ZERO_MALLOC);115#endif116 117  return UNITY_END();118}