AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <stdlib.h>4#include <string.h>5 6/*7 Unity test suite for sqlite3_sleep(int ms)8 9 Strategy:10 - Monkey-patch the default VFS's xSleep to a test double that records inputs11 and returns controlled microsecond values.12 - Verify that sqlite3_sleep:13 * Converts ms to microseconds (ms*1000) for xSleep,14 * Clamps negative ms to 0 microseconds,15 * Returns the number of milliseconds actually slept (returned_us/1000).16*/17 18static sqlite3_vfs *gOrigVfs = NULL;19static int (*gOrigSleep)(sqlite3_vfs*, int) = NULL;20 21/* Tracking variables for the test double */22static volatile int gLastSleepMicroseconds = -1;23static volatile int gSleepCallCount = 0;24/* If >=0, the test double returns this value; otherwise, echo the input. */25static volatile int gTestSleepReturn = -1;26 27/* Test double for xSleep */28static int test_xSleep(sqlite3_vfs *pVfs, int microseconds){29 (void)pVfs;30 gLastSleepMicroseconds = microseconds;31 gSleepCallCount++;32 if( gTestSleepReturn >= 0 ){33 return gTestSleepReturn;34 }35 /* Echo input by default */36 return microseconds;37}38 39void setUp(void) {40 /* Acquire default VFS and monkey-patch xSleep */41 gOrigVfs = sqlite3_vfs_find(0);42 TEST_ASSERT_MESSAGE(gOrigVfs != NULL, "No default VFS available; cannot test sqlite3_sleep.");43 gOrigSleep = gOrigVfs->xSleep;44 45 /* Install test double */46 gOrigVfs->xSleep = test_xSleep;47 48 /* Reset tracking */49 gLastSleepMicroseconds = -1;50 gSleepCallCount = 0;51 gTestSleepReturn = -1;52}53 54void tearDown(void) {55 /* Restore original xSleep if available */56 if( gOrigVfs ){57 gOrigVfs->xSleep = gOrigSleep;58 }59 gOrigVfs = NULL;60 gOrigSleep = NULL;61}62 63/* Test: ms == 0 should call xSleep with 0 microseconds and return 0 */64void test_sqlite3_sleep_ms_zero_calls_with_zero_and_returns_zero(void) {65 gTestSleepReturn = 0; /* Force return value */66 int rc = sqlite3_sleep(0);67 TEST_ASSERT_EQUAL_INT(1, gSleepCallCount);68 TEST_ASSERT_EQUAL_INT(0, gLastSleepMicroseconds);69 TEST_ASSERT_EQUAL_INT(0, rc);70}71 72/* Test: ms > 0 should pass ms*1000 microseconds and return exactly ms if xSleep echos input */73void test_sqlite3_sleep_ms_positive_echoes_input_returns_ms(void) {74 gTestSleepReturn = -1; /* Echo input */75 int ms = 25;76 int rc = sqlite3_sleep(ms);77 TEST_ASSERT_EQUAL_INT(1, gSleepCallCount);78 TEST_ASSERT_EQUAL_INT(ms*1000, gLastSleepMicroseconds);79 TEST_ASSERT_EQUAL_INT(ms, rc);80}81 82/* Test: ms > 0 with partial sleep from VFS (return fewer microseconds) */83void test_sqlite3_sleep_ms_positive_partial_sleep(void) {84 int ms = 10;85 int returned_us = 7000; /* Simulate sleeping only 7ms */86 gTestSleepReturn = returned_us;87 int rc = sqlite3_sleep(ms);88 TEST_ASSERT_EQUAL_INT(1, gSleepCallCount);89 TEST_ASSERT_EQUAL_INT(ms*1000, gLastSleepMicroseconds);90 TEST_ASSERT_EQUAL_INT(returned_us/1000, rc);91}92 93/* Test: ms < 0 should be clamped to 0 microseconds and return 0 */94void test_sqlite3_sleep_negative_ms_clamped_to_zero(void) {95 gTestSleepReturn = 0; /* Return 0 us slept */96 int rc = sqlite3_sleep(-5);97 TEST_ASSERT_EQUAL_INT(1, gSleepCallCount);98 TEST_ASSERT_EQUAL_INT(0, gLastSleepMicroseconds);99 TEST_ASSERT_EQUAL_INT(0, rc);100}101 102/* Test: conversion correctness for an arbitrary positive ms */103void test_sqlite3_sleep_conversion_correctness_123ms(void) {104 gTestSleepReturn = -1; /* Echo input */105 int ms = 123;106 int rc = sqlite3_sleep(ms);107 TEST_ASSERT_EQUAL_INT(1, gSleepCallCount);108 TEST_ASSERT_EQUAL_INT(123000, gLastSleepMicroseconds);109 TEST_ASSERT_EQUAL_INT(123, rc);110}111 112/* Test: multiple calls increment call count and track last microseconds */113void test_sqlite3_sleep_multiple_calls_tracking(void) {114 gTestSleepReturn = -1; /* Echo input */115 int rc1 = sqlite3_sleep(1);116 int last1 = gLastSleepMicroseconds;117 int count1 = gSleepCallCount;118 119 int rc2 = sqlite3_sleep(3);120 int last2 = gLastSleepMicroseconds;121 int count2 = gSleepCallCount;122 123 TEST_ASSERT_EQUAL_INT(1, rc1);124 TEST_ASSERT_EQUAL_INT(1000, last1);125 TEST_ASSERT_EQUAL_INT(1, count1);126 127 TEST_ASSERT_EQUAL_INT(3, rc2);128 TEST_ASSERT_EQUAL_INT(3000, last2);129 TEST_ASSERT_EQUAL_INT(2, count2);130}131 132int main(void) {133 UNITY_BEGIN();134 RUN_TEST(test_sqlite3_sleep_ms_zero_calls_with_zero_and_returns_zero);135 RUN_TEST(test_sqlite3_sleep_ms_positive_echoes_input_returns_ms);136 RUN_TEST(test_sqlite3_sleep_ms_positive_partial_sleep);137 RUN_TEST(test_sqlite3_sleep_negative_ms_clamped_to_zero);138 RUN_TEST(test_sqlite3_sleep_conversion_correctness_123ms);139 RUN_TEST(test_sqlite3_sleep_multiple_calls_tracking);140 return UNITY_END();141}