AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4 5/* Saved copy of the mutex methods to restore after each test */6static sqlite3_mutex_methods gSavedMutexMethods;7 8/* Stub and counters for xMutexEnd */9static int gStubEndCalled = 0;10static int gStubEndRc = SQLITE_OK;11static int stub_xMutexEnd(void){12 gStubEndCalled++;13 return gStubEndRc;14}15 16void setUp(void) {17 /* Save original mutex methods to restore after each test */18 memcpy(&gSavedMutexMethods, &sqlite3GlobalConfig.mutex, sizeof(sqlite3_mutex_methods));19 gStubEndCalled = 0;20 gStubEndRc = SQLITE_OK;21}22 23void tearDown(void) {24 /* Restore original mutex methods */25 memcpy(&sqlite3GlobalConfig.mutex, &gSavedMutexMethods, sizeof(sqlite3_mutex_methods));26}27 28/* Test: When xMutexEnd is NULL, sqlite3MutexEnd returns SQLITE_OK */29void test_sqlite3MutexEnd_returns_ok_when_xMutexEnd_is_null(void) {30 sqlite3GlobalConfig.mutex.xMutexEnd = 0;31 int rc = sqlite3MutexEnd();32 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);33 TEST_ASSERT_EQUAL_INT(0, gStubEndCalled);34}35 36/* Test: sqlite3MutexEnd calls xMutexEnd and returns its value */37void test_sqlite3MutexEnd_calls_xMutexEnd_and_propagates_return_code(void) {38 sqlite3GlobalConfig.mutex.xMutexEnd = stub_xMutexEnd;39 40 gStubEndRc = SQLITE_ERROR;41 int rc = sqlite3MutexEnd();42 TEST_ASSERT_EQUAL_INT(SQLITE_ERROR, rc);43 TEST_ASSERT_EQUAL_INT(1, gStubEndCalled);44}45 46/* Test: Multiple calls to sqlite3MutexEnd invoke xMutexEnd each time */47void test_sqlite3MutexEnd_multiple_calls_invoke_xMutexEnd_each_time(void) {48 sqlite3GlobalConfig.mutex.xMutexEnd = stub_xMutexEnd;49 50 gStubEndRc = SQLITE_OK;51 int rc1 = sqlite3MutexEnd();52 int rc2 = sqlite3MutexEnd();53 int rc3 = sqlite3MutexEnd();54 55 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc1);56 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc2);57 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc3);58 TEST_ASSERT_EQUAL_INT(3, gStubEndCalled);59}60 61/* Test: sqlite3MutexEnd does not modify the xMutexEnd function pointer */62void test_sqlite3MutexEnd_does_not_modify_xMutexEnd_pointer(void) {63 sqlite3GlobalConfig.mutex.xMutexEnd = stub_xMutexEnd;64 int (*before)(void) = sqlite3GlobalConfig.mutex.xMutexEnd;65 66 (void)sqlite3MutexEnd();67 68 int (*after)(void) = sqlite3GlobalConfig.mutex.xMutexEnd;69 TEST_ASSERT_EQUAL_PTR(before, after);70}71 72/* Optional: Ensure non-OK, non-standard code is propagated unchanged */73void test_sqlite3MutexEnd_propagates_arbitrary_error_code(void) {74 sqlite3GlobalConfig.mutex.xMutexEnd = stub_xMutexEnd;75 76 /* Use a defined but different error code */77 gStubEndRc = SQLITE_MISUSE;78 int rc = sqlite3MutexEnd();79 TEST_ASSERT_EQUAL_INT(SQLITE_MISUSE, rc);80 TEST_ASSERT_EQUAL_INT(1, gStubEndCalled);81}82 83int main(void) {84 UNITY_BEGIN();85 RUN_TEST(test_sqlite3MutexEnd_returns_ok_when_xMutexEnd_is_null);86 RUN_TEST(test_sqlite3MutexEnd_calls_xMutexEnd_and_propagates_return_code);87 RUN_TEST(test_sqlite3MutexEnd_multiple_calls_invoke_xMutexEnd_each_time);88 RUN_TEST(test_sqlite3MutexEnd_does_not_modify_xMutexEnd_pointer);89 RUN_TEST(test_sqlite3MutexEnd_propagates_arbitrary_error_code);90 return UNITY_END();91}