AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4 5/* Minimal test VFS file object embedding sqlite3_file as the first member */6typedef struct TestFile {7 sqlite3_file base; /* Must be first */8 int called;9 int lastOp;10 void *lastArg;11 void *seenAp0;12 void *seenAp1;13} TestFile;14 15/* xFileControl implementation to capture calls from sqlite3PagerSetBusyHandler */16static int test_xFileControl(sqlite3_file *pFile, int op, void *pArg){17 TestFile *tf = (TestFile*)pFile;18 tf->called++;19 tf->lastOp = op;20 tf->lastArg = pArg;21 22 if( op==SQLITE_FCNTL_BUSYHANDLER && pArg ){23 void **ap = (void**)pArg;24 tf->seenAp0 = ap[0];25 tf->seenAp1 = ap[1];26 }else{27 tf->seenAp0 = tf->seenAp1 = 0;28 }29 return SQLITE_OK;30}31 32/* Provide only the methods needed; others default to NULL via designated init */33static const sqlite3_io_methods testIoMethods = {34 .iVersion = 1,35 .xFileControl = test_xFileControl36};37 38/* Sample busy-handler functions */39static int busyHandler1(void *p){ (void)p; return 1; }40static int busyHandler0(void *p){ (void)p; return 0; }41 42/* Unity setup/teardown */43void setUp(void) {44 /* Setup code here, or leave empty */45}46void tearDown(void) {47 /* Cleanup code here, or leave empty */48}49 50/* Helper to initialize a TestFile and Pager that uses it */51static void initPagerWithTestFile(Pager *pPager, TestFile *pTF){52 memset(pPager, 0, sizeof(*pPager));53 memset(pTF, 0, sizeof(*pTF));54 pTF->base.pMethods = &testIoMethods;55 pPager->fd = &pTF->base;56}57 58/* Test 1: Basic non-NULL handler and argument */59void test_sqlite3PagerSetBusyHandler_basic(void){60 Pager pager;61 TestFile tf;62 int arg1 = 12345;63 64 initPagerWithTestFile(&pager, &tf);65 66 sqlite3PagerSetBusyHandler(&pager, busyHandler1, &arg1);67 68 /* Pager fields updated */69 TEST_ASSERT_EQUAL_PTR((void*)busyHandler1, (void*)pager.xBusyHandler);70 TEST_ASSERT_EQUAL_PTR(&arg1, pager.pBusyHandlerArg);71 72 /* xFileControl called with expected opcode and arg */73 TEST_ASSERT_EQUAL_INT(1, tf.called);74 TEST_ASSERT_EQUAL_INT(SQLITE_FCNTL_BUSYHANDLER, tf.lastOp);75 TEST_ASSERT_EQUAL_PTR((void*)&pager.xBusyHandler, tf.lastArg);76 77 /* The array contents reflect the handler and arg */78 TEST_ASSERT_EQUAL_PTR((void*)busyHandler1, tf.seenAp0);79 TEST_ASSERT_EQUAL_PTR(&arg1, tf.seenAp1);80}81 82/* Test 2: Subsequent call updates handler and argument, invokes xFileControl again */83void test_sqlite3PagerSetBusyHandler_change_handler(void){84 Pager pager;85 TestFile tf;86 int arg1 = 111, arg2 = 222;87 88 initPagerWithTestFile(&pager, &tf);89 90 sqlite3PagerSetBusyHandler(&pager, busyHandler1, &arg1);91 sqlite3PagerSetBusyHandler(&pager, busyHandler0, &arg2);92 93 /* Pager fields updated to second set */94 TEST_ASSERT_EQUAL_PTR((void*)busyHandler0, (void*)pager.xBusyHandler);95 TEST_ASSERT_EQUAL_PTR(&arg2, pager.pBusyHandlerArg);96 97 /* xFileControl called twice; last call has new values */98 TEST_ASSERT_EQUAL_INT(2, tf.called);99 TEST_ASSERT_EQUAL_INT(SQLITE_FCNTL_BUSYHANDLER, tf.lastOp);100 TEST_ASSERT_EQUAL_PTR((void*)&pager.xBusyHandler, tf.lastArg);101 TEST_ASSERT_EQUAL_PTR((void*)busyHandler0, tf.seenAp0);102 TEST_ASSERT_EQUAL_PTR(&arg2, tf.seenAp1);103}104 105/* Test 3: NULL handler with non-NULL argument */106void test_sqlite3PagerSetBusyHandler_null_handler(void){107 Pager pager;108 TestFile tf;109 int arg = 999;110 111 initPagerWithTestFile(&pager, &tf);112 113 sqlite3PagerSetBusyHandler(&pager, NULL, &arg);114 115 TEST_ASSERT_EQUAL_PTR(NULL, (void*)pager.xBusyHandler);116 TEST_ASSERT_EQUAL_PTR(&arg, pager.pBusyHandlerArg);117 118 TEST_ASSERT_EQUAL_INT(1, tf.called);119 TEST_ASSERT_EQUAL_INT(SQLITE_FCNTL_BUSYHANDLER, tf.lastOp);120 TEST_ASSERT_EQUAL_PTR((void*)&pager.xBusyHandler, tf.lastArg);121 TEST_ASSERT_EQUAL_PTR(NULL, tf.seenAp0);122 TEST_ASSERT_EQUAL_PTR(&arg, tf.seenAp1);123}124 125/* Test 4: Non-NULL handler with NULL argument */126void test_sqlite3PagerSetBusyHandler_null_arg(void){127 Pager pager;128 TestFile tf;129 130 initPagerWithTestFile(&pager, &tf);131 132 sqlite3PagerSetBusyHandler(&pager, busyHandler1, NULL);133 134 TEST_ASSERT_EQUAL_PTR((void*)busyHandler1, (void*)pager.xBusyHandler);135 TEST_ASSERT_EQUAL_PTR(NULL, pager.pBusyHandlerArg);136 137 TEST_ASSERT_EQUAL_INT(1, tf.called);138 TEST_ASSERT_EQUAL_INT(SQLITE_FCNTL_BUSYHANDLER, tf.lastOp);139 TEST_ASSERT_EQUAL_PTR((void*)&pager.xBusyHandler, tf.lastArg);140 TEST_ASSERT_EQUAL_PTR((void*)busyHandler1, tf.seenAp0);141 TEST_ASSERT_EQUAL_PTR(NULL, tf.seenAp1);142}143 144int main(void){145 UNITY_BEGIN();146 RUN_TEST(test_sqlite3PagerSetBusyHandler_basic);147 RUN_TEST(test_sqlite3PagerSetBusyHandler_change_handler);148 RUN_TEST(test_sqlite3PagerSetBusyHandler_null_handler);149 RUN_TEST(test_sqlite3PagerSetBusyHandler_null_arg);150 return UNITY_END();151}