Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_pcache1_pcache1Init.c104 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <stdlib.h>4#include <string.h>5 6/* External wrapper provided for testing the static function */7extern int test_pcache1Init(void *NotUsed);8 9/* Track if we've already invoked pcache1Init to avoid double-init10   (which would trigger an assert in debug builds). */11static int gDidInit = 0;12 13/* Saved original sqlite3GlobalConfig fields we modify */14static void *gSaved_pPage = NULL;15static int gSaved_nPage = 0;16static int gSaved_bCoreMutex = 0;17 18/* Helper to set up a controlled sqlite3GlobalConfig environment.19   Saves old values so they can be restored after the test. */20static void configure_global_config(void *pPage, int nPage, int bCoreMutex){21  gSaved_pPage = sqlite3GlobalConfig.pPage;22  gSaved_nPage = sqlite3GlobalConfig.nPage;23  gSaved_bCoreMutex = sqlite3GlobalConfig.bCoreMutex;24 25  sqlite3GlobalConfig.pPage = pPage;26  sqlite3GlobalConfig.nPage = nPage;27  sqlite3GlobalConfig.bCoreMutex = bCoreMutex;28}29 30static void restore_global_config(void){31  sqlite3GlobalConfig.pPage = gSaved_pPage;32  sqlite3GlobalConfig.nPage = gSaved_nPage;33  sqlite3GlobalConfig.bCoreMutex = gSaved_bCoreMutex;34}35 36void setUp(void) {37  /* No per-test automatic initialization. */38}39 40void tearDown(void) {41  /* No per-test automatic cleanup. */42}43 44/* Test that pcache1Init:45   - Returns SQLITE_OK.46   - Ignores the NotUsed pointer (we pass non-NULL).47   - Does not modify sqlite3GlobalConfig fields it reads. */48void test_pcache1Init_success_and_no_config_mutation(void) {49  /* Set a specific global configuration before init. */50  void *const desired_pPage = NULL;  /* ensure pPage==NULL */51  int   const desired_nPage = 7;     /* some positive page count */52  int   const desired_bCoreMutex = 0;/* single-threaded core mutex off */53 54  configure_global_config(desired_pPage, desired_nPage, desired_bCoreMutex);55 56  /* Pass a non-NULL NotUsed pointer to ensure it is indeed ignored. */57  int dummyArg = 12345;58  int rc = test_pcache1Init(&dummyArg);59  gDidInit = 1;60 61  TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "pcache1Init should return SQLITE_OK");62 63  /* Verify sqlite3GlobalConfig fields were not altered by initialization. */64  TEST_ASSERT_EQUAL_PTR(desired_pPage, sqlite3GlobalConfig.pPage);65  TEST_ASSERT_EQUAL_INT(desired_nPage, sqlite3GlobalConfig.nPage);66  TEST_ASSERT_EQUAL_INT(desired_bCoreMutex, sqlite3GlobalConfig.bCoreMutex);67 68  restore_global_config();69}70 71/* Ensure we do not accidentally call pcache1Init twice.72   If the first test ran, assert that we consider the subsystem initialized.73   If not, perform a single initialization now to still validate return code. */74void test_pcache1Init_not_called_twice(void) {75  if (gDidInit) {76    TEST_ASSERT_TRUE_MESSAGE(gDidInit != 0, "pcache1Init should have been called by a prior test");77    /* Do not call it again to avoid triggering assert in debug builds. */78  } else {79    /* Fallback: call once if prior test was skipped by harness. */80    configure_global_config(NULL, 3, 0);81    int rc = test_pcache1Init(NULL);82    gDidInit = 1;83    TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);84    restore_global_config();85  }86}87 88/* Document and intentionally skip double-initialization behavior, since it89   depends on assert() being enabled/disabled at build time. */90void test_pcache1Init_double_init_behavior_documented(void) {91#if defined(NDEBUG)92  TEST_IGNORE_MESSAGE("NDEBUG defined; assertions disabled. Skipping double-init behavior test to avoid altering global state.");93#else94  TEST_IGNORE_MESSAGE("Assertions enabled; double-initialization would assert. Skipping by design.");95#endif96}97 98int main(void) {99  UNITY_BEGIN();100  RUN_TEST(test_pcache1Init_success_and_no_config_mutation);101  RUN_TEST(test_pcache1Init_not_called_twice);102  RUN_TEST(test_pcache1Init_double_init_behavior_documented);103  return UNITY_END();104}