AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3 4#include <stdlib.h>5#include <string.h>6 7/* Globals for test fixtures */8static sqlite3 *gDb = NULL;9static Btree *gBt = NULL;10 11void setUp(void) {12 int rc = sqlite3_open(":memory:", &gDb);13 TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "sqlite3_open failed");14 15 /* Main database handle (index 0) should be initialized */16 TEST_ASSERT_NOT_NULL(gDb);17 TEST_ASSERT_NOT_NULL(gDb->aDb);18 TEST_ASSERT_NOT_NULL(gDb->aDb[0].pBt);19 gBt = gDb->aDb[0].pBt;20 21 /* Sanity: connection mutex exists */22 TEST_ASSERT_NOT_NULL(gDb->mutex);23}24 25void tearDown(void) {26 if (gDb) {27 sqlite3_close(gDb);28 gDb = NULL;29 gBt = NULL;30 }31}32 33/* Helper to call sqlite3BtreeSetPagerFlags safely with db mutex held */34static int call_setpagerflags(unsigned flags){35 int rc;36 sqlite3_mutex_enter(gDb->mutex);37 rc = sqlite3BtreeSetPagerFlags(gBt, flags);38 sqlite3_mutex_leave(gDb->mutex);39 return rc;40}41 42/* After changing pager flags, ensure basic SQL still works */43static void assert_connection_usable(void){44 char *errmsg = NULL;45 int rc = sqlite3_exec(gDb, "SELECT 1", NULL, NULL, &errmsg);46 if( rc!=SQLITE_OK ){47 if( errmsg ){48 /* Attach detail for easier debugging */49 TEST_FAIL_MESSAGE(errmsg);50 }else{51 TEST_FAIL_MESSAGE("sqlite3_exec SELECT 1 failed");52 }53 }54 sqlite3_free(errmsg);55}56 57void test_sqlite3BtreeSetPagerFlags_returns_ok_zero(void){58 int rc = call_setpagerflags(0u);59 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);60 assert_connection_usable();61}62 63void test_sqlite3BtreeSetPagerFlags_various_flag_patterns(void){64 /* These patterns exercise a variety of bit combinations, some of which65 map to pager synchronous and full-fsync options internally. The function66 just forwards the bits to the pager, so any unsigned value should be OK. */67 unsigned patterns[] = {68 0u,69 1u,70 2u,71 3u,72 0x04u,73 0x08u,74 0x10u,75 0x0Fu,76 0x1Fu,77 0xFFu78 };79 size_t i;80 for(i=0;i<sizeof(patterns)/sizeof(patterns[0]);i++){81 int rc = call_setpagerflags(patterns[i]);82 TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "sqlite3BtreeSetPagerFlags failed");83 }84 assert_connection_usable();85}86 87void test_sqlite3BtreeSetPagerFlags_multiple_calls_and_basic_use(void){88 /* Call with a sequence of different values, then do a tiny write to ensure89 the connection remains usable after altering pager flags repeatedly. */90 int rc;91 rc = call_setpagerflags(0x03u); TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);92 rc = call_setpagerflags(0x00u); TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);93 rc = call_setpagerflags(0x1Au); TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);94 95 /* Perform a small DB action to ensure functionality after flag changes. */96 char *errmsg = NULL;97 rc = sqlite3_exec(gDb, "CREATE TABLE t(x)", NULL, NULL, &errmsg);98 if( rc!=SQLITE_OK ){99 if( errmsg ){100 TEST_FAIL_MESSAGE(errmsg);101 }else{102 TEST_FAIL_MESSAGE("CREATE TABLE failed");103 }104 }105 sqlite3_free(errmsg);106}107 108/* Main */109int main(void) {110 UNITY_BEGIN();111 112 RUN_TEST(test_sqlite3BtreeSetPagerFlags_returns_ok_zero);113 RUN_TEST(test_sqlite3BtreeSetPagerFlags_various_flag_patterns);114 RUN_TEST(test_sqlite3BtreeSetPagerFlags_multiple_calls_and_basic_use);115 116 return UNITY_END();117}