Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_pager_assert_pager_state.c138 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4 5/* Wrapper for the static function under test (provided by pager.c) */6extern int test_assert_pager_state(Pager *p);7 8/* A closed sqlite3_file instance for use with isOpen(p->jfd) checks. 9   isOpen() will examine jfd->pMethods, which must be a valid pointer to avoid UB. */10static sqlite3_file gClosedJfd;11 12void setUp(void) {13  /* Ensure the "closed" file handle looks closed to isOpen() */14  memset(&gClosedJfd, 0, sizeof(gClosedJfd));15}16 17void tearDown(void) {18  /* Nothing to clean up */19}20 21/* Helper to initialize a Pager with safe defaults that satisfy most global invariants. */22static void initPagerCommon(Pager *p){23  memset(p, 0, sizeof(*p));24 25  /* Global invariants not dependent on state */26  p->memDb = 0;                      /* Avoid MEMDB branch */27  p->tempFile = 0;                   /* Default: not a temp file */28  p->changeCountDone = 0;            /* With tempFile==0, no constraint */29  p->jfd = &gClosedJfd;              /* Required for isOpen(p->jfd) assertion */30  p->journalMode = PAGER_JOURNALMODE_OFF; /* If useJournal==0, must be OFF */31  p->useJournal = 0;32  p->errCode = SQLITE_OK;33  p->pWal = 0;                       /* Not using WAL */34  p->eLock = SHARED_LOCK;            /* Valid lock for READER by default */35  /* db sizes default to 0; equalities satisfied where needed */36}37 38/* Test: PAGER_READER state with minimal valid configuration */39void test_assert_pager_state_reader_ok(void){40  Pager p;41  initPagerCommon(&p);42 43  p.eState = PAGER_READER;44  p.eLock = SHARED_LOCK;   /* >= SHARED_LOCK and != UNKNOWN */45  /* journal invariants already satisfied by initPagerCommon */46 47  int rc = test_assert_pager_state(&p);48  TEST_ASSERT_EQUAL_INT(1, rc);49}50 51/* Test: PAGER_WRITER_LOCKED state with correct lock and db size equalities */52void test_assert_pager_state_writer_locked_ok(void){53  Pager p;54  initPagerCommon(&p);55 56  p.eState = PAGER_WRITER_LOCKED;57  p.eLock = RESERVED_LOCK; /* Must be >= RESERVED_LOCK when not using WAL */58  p.dbSize = 10;59  p.dbOrigSize = 10;60  p.dbFileSize = 10;61  p.dbHintSize = 10;62  p.setSuper = 0;63 64  int rc = test_assert_pager_state(&p);65  TEST_ASSERT_EQUAL_INT(1, rc);66}67 68/* Test: PAGER_WRITER_CACHEMOD state with correct lock, journal invariants, and db size equalities */69void test_assert_pager_state_writer_cachemod_ok(void){70  Pager p;71  initPagerCommon(&p);72 73  p.eState = PAGER_WRITER_CACHEMOD;74  p.eLock = RESERVED_LOCK; /* Must be >= RESERVED_LOCK when not using WAL */75  /* isOpen(p->jfd) || journalMode==OFF || journalMode==WAL: we use OFF */76  p.journalMode = PAGER_JOURNALMODE_OFF;77  p.useJournal = 0;78  p.dbOrigSize = 5;79  p.dbFileSize = 5;80  p.dbHintSize = 5;81 82  int rc = test_assert_pager_state(&p);83  TEST_ASSERT_EQUAL_INT(1, rc);84}85 86/* Test: temp-file specific invariants (exclusive lock and changeCountDone set) */87void test_assert_pager_state_tempfile_rules_ok(void){88  Pager p;89  initPagerCommon(&p);90 91  p.eState = PAGER_READER;       /* Any state is fine for this invariant */92  p.tempFile = 1;                /* Trigger temp-file behavior */93  p.changeCountDone = 1;         /* Must be set for temp-file connections */94  p.eLock = EXCLUSIVE_LOCK;      /* Temp-file implies exclusive lock */95 96  int rc = test_assert_pager_state(&p);97  TEST_ASSERT_EQUAL_INT(1, rc);98}99 100/* Test: journal mode invariants:101   (a) useJournal==0 => journalMode must be OFF (and !isOpen(jfd)).102   (b) useJournal==1 is compatible with non-OFF journal modes (e.g., MEMORY). */103void test_assert_pager_state_journal_mode_invariants(void){104  /* Case (a): useJournal==0 and journalMode==OFF */105  {106    Pager p;107    initPagerCommon(&p);108    p.eState = PAGER_READER;109    p.useJournal = 0;110    p.journalMode = PAGER_JOURNALMODE_OFF;111    /* jfd appears closed due to gClosedJfd.pMethods==NULL */112 113    int rc = test_assert_pager_state(&p);114    TEST_ASSERT_EQUAL_INT(1, rc);115  }116 117  /* Case (b): useJournal==1 and journalMode==MEMORY (non-OFF) */118  {119    Pager p;120    initPagerCommon(&p);121    p.eState = PAGER_READER;122    p.useJournal = 1;123    p.journalMode = PAGER_JOURNALMODE_MEMORY;124 125    int rc = test_assert_pager_state(&p);126    TEST_ASSERT_EQUAL_INT(1, rc);127  }128}129 130int main(void){131  UNITY_BEGIN();132  RUN_TEST(test_assert_pager_state_reader_ok);133  RUN_TEST(test_assert_pager_state_writer_locked_ok);134  RUN_TEST(test_assert_pager_state_writer_cachemod_ok);135  RUN_TEST(test_assert_pager_state_tempfile_rules_ok);136  RUN_TEST(test_assert_pager_state_journal_mode_invariants);137  return UNITY_END();138}