Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_btree_sqlite3BtreeGetAutoVacuum.c88 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdlib.h>5 6static sqlite3 *g_db = NULL;7static Btree *g_btree = NULL;8 9static void open_db_and_fetch_btree(void){10  int rc = sqlite3_open(":memory:", &g_db);11  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);12  TEST_ASSERT_NOT_NULL(g_db);13  /* main database handle is aDb[0] */14  TEST_ASSERT_NOT_NULL(g_db->aDb);15  TEST_ASSERT_NOT_NULL(g_db->aDb[0].pBt);16  g_btree = g_db->aDb[0].pBt;17  TEST_ASSERT_NOT_NULL(g_btree);18}19 20static void close_db(void){21  if( g_db ){22    sqlite3_close(g_db);23    g_db = NULL;24    g_btree = NULL;25  }26}27 28void setUp(void) {29  /* Setup before each test */30  open_db_and_fetch_btree();31}32 33void tearDown(void) {34  /* Cleanup after each test */35  close_db();36}37 38#ifndef SQLITE_OMIT_AUTOVACUUM39/* Helper to set the internal autoVacuum flags safely */40static void set_av_flags(u8 autoVac, u8 incrVac){41  /* Enter the btree mutex to modify internals safely */42  sqlite3BtreeEnter(g_btree);43  g_btree->pBt->autoVacuum = autoVac ? 1 : 0;44  g_btree->pBt->incrVacuum = incrVac ? 1 : 0;45  sqlite3BtreeLeave(g_btree);46}47#endif48 49void test_sqlite3BtreeGetAutoVacuum_none(void) {50#ifdef SQLITE_OMIT_AUTOVACUUM51  /* In OMIT build, always none regardless of fields */52  int mode = sqlite3BtreeGetAutoVacuum(g_btree);53  TEST_ASSERT_EQUAL_INT(BTREE_AUTOVACUUM_NONE, mode);54#else55  set_av_flags(0, 0);56  int mode = sqlite3BtreeGetAutoVacuum(g_btree);57  TEST_ASSERT_EQUAL_INT(BTREE_AUTOVACUUM_NONE, mode);58 59  /* Even if incrVacuum is set, autoVacuum==0 should force NONE */60  set_av_flags(0, 1);61  mode = sqlite3BtreeGetAutoVacuum(g_btree);62  TEST_ASSERT_EQUAL_INT(BTREE_AUTOVACUUM_NONE, mode);63#endif64}65 66#ifndef SQLITE_OMIT_AUTOVACUUM67void test_sqlite3BtreeGetAutoVacuum_full(void) {68  set_av_flags(1, 0);69  int mode = sqlite3BtreeGetAutoVacuum(g_btree);70  TEST_ASSERT_EQUAL_INT(BTREE_AUTOVACUUM_FULL, mode);71}72 73void test_sqlite3BtreeGetAutoVacuum_incr(void) {74  set_av_flags(1, 1);75  int mode = sqlite3BtreeGetAutoVacuum(g_btree);76  TEST_ASSERT_EQUAL_INT(BTREE_AUTOVACUUM_INCR, mode);77}78#endif79 80int main(void) {81  UNITY_BEGIN();82  RUN_TEST(test_sqlite3BtreeGetAutoVacuum_none);83#ifndef SQLITE_OMIT_AUTOVACUUM84  RUN_TEST(test_sqlite3BtreeGetAutoVacuum_full);85  RUN_TEST(test_sqlite3BtreeGetAutoVacuum_incr);86#endif87  return UNITY_END();88}