AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3 4#include <string.h>5#include <stdio.h>6 7static sqlite3 *gDb = NULL;8 9/* Helper: open a new SQLite connection (used by each test) */10static void openDb(void){11 int rc = sqlite3_open(":memory:", &gDb);12 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);13 TEST_ASSERT_NOT_NULL(gDb);14}15 16/* Helper: close SQLite connection */17static void closeDb(void){18 if( gDb ){19 int rc = sqlite3_close(gDb);20 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);21 gDb = NULL;22 }23}24 25/* Helper: open a Btree handle. Caller must hold db mutex. */26static Btree* openBtreeFile(const char *zName){27 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);28 TEST_ASSERT_NOT_NULL(pVfs);29 30 Btree *pBtree = NULL;31 int vfsFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB;32 int rc = sqlite3BtreeOpen(pVfs, zName, gDb, &pBtree, 0, vfsFlags);33 TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "sqlite3BtreeOpen failed");34 TEST_ASSERT_NOT_NULL(pBtree);35 return pBtree;36}37 38/* Helper: open an in-memory Btree handle. Caller must hold db mutex. */39static Btree* openBtreeMemory(void){40 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);41 TEST_ASSERT_NOT_NULL(pVfs);42 43 Btree *pBtree = NULL;44 int vfsFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;45 int rc = sqlite3BtreeOpen(pVfs, ":memory:", gDb, &pBtree, 0, vfsFlags);46 TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "sqlite3BtreeOpen(:memory:) failed");47 TEST_ASSERT_NOT_NULL(pBtree);48 return pBtree;49}50 51void setUp(void) {52 openDb();53}54 55void tearDown(void) {56 closeDb();57}58 59/* Test 1: Basic open/close of a file-backed Btree. */60void test_sqlite3BtreeClose_basic_file_db(void){61 sqlite3_mutex_enter(gDb->mutex);62 Btree *pBtree = openBtreeFile("test_btree_close1.db");63 64 int rc = sqlite3BtreeClose(pBtree);65 sqlite3_mutex_leave(gDb->mutex);66 67 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);68}69 70/* Test 2: Open a Btree, begin write transaction, create table, insert, close cursor, then close Btree */71void test_sqlite3BtreeClose_with_write_txn_and_cursor(void){72 sqlite3_mutex_enter(gDb->mutex);73 Btree *pBtree = openBtreeFile("test_btree_close2.db");74 75 /* Begin write transaction */76 int rc = sqlite3BtreeBeginTrans(pBtree, 1, 0);77 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);78 79 /* Create a new table (intkey table) */80 Pgno root = 0;81 rc = sqlite3BtreeCreateTable(pBtree, &root, BTREE_INTKEY | BTREE_LEAFDATA);82 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);83 TEST_ASSERT_TRUE(root >= 2);84 85 /* Open a write cursor on the new table */86 BtCursor cur;87 memset(&cur, 0, sizeof(cur));88 rc = sqlite3BtreeCursor(pBtree, root, BTREE_WRCSR, NULL, &cur);89 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);90 91 /* Insert a single row: rowid=1, data="abc" */92 const char payload[] = "abc";93 BtreePayload x;94 memset(&x, 0, sizeof(x));95 x.pKey = NULL; /* intkey table */96 x.nKey = 1; /* rowid */97 x.pData = payload;98 x.nData = (int)sizeof(payload)-1; /* exclude null terminator */99 x.nZero = 0;100 101 rc = sqlite3BtreeInsert(&cur, &x, 0, 0);102 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);103 104 /* Close the cursor before closing the Btree */105 rc = sqlite3BtreeCloseCursor(&cur);106 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);107 108 /* Close the Btree */109 rc = sqlite3BtreeClose(pBtree);110 sqlite3_mutex_leave(gDb->mutex);111 112 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);113}114 115/* Test 3: Open/close of an in-memory Btree. */116void test_sqlite3BtreeClose_memory_btree(void){117 sqlite3_mutex_enter(gDb->mutex);118 Btree *pBtree = openBtreeMemory();119 120 int rc = sqlite3BtreeClose(pBtree);121 sqlite3_mutex_leave(gDb->mutex);122 123 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);124}125 126int main(void) {127 UNITY_BEGIN();128 129 RUN_TEST(test_sqlite3BtreeClose_basic_file_db);130 RUN_TEST(test_sqlite3BtreeClose_with_write_txn_and_cursor);131 RUN_TEST(test_sqlite3BtreeClose_memory_btree);132 133 return UNITY_END();134}