Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_btree_sqlite3BtreeCommitPhaseTwo.c153 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdio.h>5 6static sqlite3 *gDb = NULL;7 8static Btree *getMainBtree(sqlite3 *db){9  /* Main database is index 0 */10  return db->aDb[0].pBt;11}12 13void setUp(void) {14  int rc = sqlite3_open(":memory:", &gDb);15  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);16  TEST_ASSERT_NOT_NULL(gDb);17}18 19void tearDown(void) {20  if( gDb ){21    sqlite3_close(gDb);22    gDb = NULL;23  }24}25 26/* Test: No-op when there is no active transaction */27void test_sqlite3BtreeCommitPhaseTwo_no_transaction(void){28  Btree *p;29  int rc;30  TEST_ASSERT_NOT_NULL(gDb);31 32  sqlite3_mutex_enter(gDb->mutex);33  p = getMainBtree(gDb);34  TEST_ASSERT_NOT_NULL(p);35 36  /* Ensure no transaction is active */37  TEST_ASSERT_EQUAL_INT(TRANS_NONE, p->inTrans);38 39  rc = sqlite3BtreeCommitPhaseTwo(p, 0);40  sqlite3_mutex_leave(gDb->mutex);41 42  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);43}44 45/* Test: End a read-only transaction */46void test_sqlite3BtreeCommitPhaseTwo_read_transaction(void){47  Btree *p;48  int rc;49 50  sqlite3_mutex_enter(gDb->mutex);51  p = getMainBtree(gDb);52  TEST_ASSERT_NOT_NULL(p);53 54  /* Begin a read transaction */55  rc = sqlite3BtreeBeginTrans(p, 0, 0);56  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);57  TEST_ASSERT_EQUAL_INT(TRANS_READ, p->inTrans);58  TEST_ASSERT_EQUAL_INT(TRANS_READ, p->pBt->inTransaction);59 60  /* Commit phase two should end the read transaction */61  rc = sqlite3BtreeCommitPhaseTwo(p, 0);62  sqlite3_mutex_leave(gDb->mutex);63 64  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);65 66  sqlite3_mutex_enter(gDb->mutex);67  TEST_ASSERT_EQUAL_INT(TRANS_NONE, p->inTrans);68  TEST_ASSERT_EQUAL_INT(TRANS_NONE, p->pBt->inTransaction);69  sqlite3_mutex_leave(gDb->mutex);70}71 72/* Test: Write transaction normal commit (phase one then phase two) */73void test_sqlite3BtreeCommitPhaseTwo_write_transaction_normal(void){74  Btree *p;75  int rc;76  Pgno pgno = 0;77 78  sqlite3_mutex_enter(gDb->mutex);79  p = getMainBtree(gDb);80  TEST_ASSERT_NOT_NULL(p);81 82  /* Begin a write transaction */83  rc = sqlite3BtreeBeginTrans(p, 1, 0);84  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);85  TEST_ASSERT_EQUAL_INT(TRANS_WRITE, p->inTrans);86  TEST_ASSERT_EQUAL_INT(TRANS_WRITE, p->pBt->inTransaction);87 88  /* Create a table to ensure there is something to commit */89  rc = sqlite3BtreeCreateTable(p, &pgno, BTREE_INTKEY | PTF_LEAFDATA);90  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);91  TEST_ASSERT_TRUE(pgno >= 2);92 93  /* Commit phase one then phase two */94  rc = sqlite3BtreeCommitPhaseOne(p, NULL);95  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);96 97  rc = sqlite3BtreeCommitPhaseTwo(p, 0);98  sqlite3_mutex_leave(gDb->mutex);99 100  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);101 102  /* Verify transaction state reset */103  sqlite3_mutex_enter(gDb->mutex);104  TEST_ASSERT_EQUAL_INT(TRANS_NONE, p->inTrans);105  TEST_ASSERT_EQUAL_INT(TRANS_NONE, p->pBt->inTransaction);106  sqlite3_mutex_leave(gDb->mutex);107}108 109/* Test: Commit phase two with bCleanup==1 on normal success */110void test_sqlite3BtreeCommitPhaseTwo_write_transaction_bCleanup_success(void){111  Btree *p;112  int rc;113  Pgno pgno = 0;114 115  sqlite3_mutex_enter(gDb->mutex);116  p = getMainBtree(gDb);117  TEST_ASSERT_NOT_NULL(p);118 119  /* Begin a write transaction */120  rc = sqlite3BtreeBeginTrans(p, 1, 0);121  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);122  TEST_ASSERT_EQUAL_INT(TRANS_WRITE, p->inTrans);123  TEST_ASSERT_EQUAL_INT(TRANS_WRITE, p->pBt->inTransaction);124 125  /* Create a table to ensure there is something to commit */126  rc = sqlite3BtreeCreateTable(p, &pgno, BTREE_INTKEY | PTF_LEAFDATA);127  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);128  TEST_ASSERT_TRUE(pgno >= 2);129 130  /* Commit phase one then phase two with bCleanup==1 */131  rc = sqlite3BtreeCommitPhaseOne(p, NULL);132  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);133 134  rc = sqlite3BtreeCommitPhaseTwo(p, 1);135  sqlite3_mutex_leave(gDb->mutex);136 137  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);138 139  /* Verify transaction state reset */140  sqlite3_mutex_enter(gDb->mutex);141  TEST_ASSERT_EQUAL_INT(TRANS_NONE, p->inTrans);142  TEST_ASSERT_EQUAL_INT(TRANS_NONE, p->pBt->inTransaction);143  sqlite3_mutex_leave(gDb->mutex);144}145 146int main(void) {147  UNITY_BEGIN();148  RUN_TEST(test_sqlite3BtreeCommitPhaseTwo_no_transaction);149  RUN_TEST(test_sqlite3BtreeCommitPhaseTwo_read_transaction);150  RUN_TEST(test_sqlite3BtreeCommitPhaseTwo_write_transaction_normal);151  RUN_TEST(test_sqlite3BtreeCommitPhaseTwo_write_transaction_bCleanup_success);152  return UNITY_END();153}