AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <limits.h>5 6/* Global fake sector size used by our xSectorSize stub */7static int gFakeSectorSize = 0;8 9/* sqlite3_io_methods we will initialize in setUp */10static struct sqlite3_io_methods gIoMethods;11 12/* Fake xSectorSize implementation returning the controllable global value */13static int fake_xSectorSize(sqlite3_file *pFile){14 (void)pFile;15 return gFakeSectorSize;16}17 18void setUp(void) {19 memset(&gIoMethods, 0, sizeof(gIoMethods));20 gIoMethods.iVersion = 3; /* A valid version for testing */21 gIoMethods.xSectorSize = fake_xSectorSize;22 gFakeSectorSize = 0;23}24 25void tearDown(void) {26 /* Nothing to clean */27}28 29/* Helper to make a sqlite3_file that uses our fake io methods */30static void makeFakeFile(sqlite3_file *pOut){31 memset(pOut, 0, sizeof(*pOut));32 pOut->pMethods = &gIoMethods;33}34 35/* Tests */36 37void test_sqlite3SectorSize_returns_512_when_os_reports_0(void){38 sqlite3_file f;39 makeFakeFile(&f);40 gFakeSectorSize = 0;41 int rc = sqlite3SectorSize(&f);42 TEST_ASSERT_EQUAL_INT(512, rc);43}44 45void test_sqlite3SectorSize_returns_512_when_os_reports_31(void){46 sqlite3_file f;47 makeFakeFile(&f);48 gFakeSectorSize = 31;49 int rc = sqlite3SectorSize(&f);50 TEST_ASSERT_EQUAL_INT(512, rc);51}52 53void test_sqlite3SectorSize_returns_512_when_os_reports_negative(void){54 sqlite3_file f;55 makeFakeFile(&f);56 gFakeSectorSize = -1;57 int rc = sqlite3SectorSize(&f);58 TEST_ASSERT_EQUAL_INT(512, rc);59}60 61void test_sqlite3SectorSize_returns_32_when_os_reports_32(void){62 sqlite3_file f;63 makeFakeFile(&f);64 gFakeSectorSize = 32;65 int rc = sqlite3SectorSize(&f);66 TEST_ASSERT_EQUAL_INT(32, rc);67}68 69void test_sqlite3SectorSize_passthrough_for_common_values(void){70 sqlite3_file f;71 makeFakeFile(&f);72 73 gFakeSectorSize = 512;74 int rc = sqlite3SectorSize(&f);75 TEST_ASSERT_EQUAL_INT(512, rc);76 77 gFakeSectorSize = 4096;78 rc = sqlite3SectorSize(&f);79 TEST_ASSERT_EQUAL_INT(4096, rc);80}81 82void test_sqlite3SectorSize_returns_exact_MAX_when_os_reports_MAX(void){83 sqlite3_file f;84 makeFakeFile(&f);85 gFakeSectorSize = MAX_SECTOR_SIZE;86 int rc = sqlite3SectorSize(&f);87 TEST_ASSERT_EQUAL_INT(MAX_SECTOR_SIZE, rc);88}89 90void test_sqlite3SectorSize_clamps_to_MAX_when_os_reports_MAX_plus_1(void){91 sqlite3_file f;92 makeFakeFile(&f);93 gFakeSectorSize = MAX_SECTOR_SIZE + 1;94 int rc = sqlite3SectorSize(&f);95 TEST_ASSERT_EQUAL_INT(MAX_SECTOR_SIZE, rc);96}97 98void test_sqlite3SectorSize_clamps_to_MAX_when_os_reports_INT_MAX(void){99 sqlite3_file f;100 makeFakeFile(&f);101 gFakeSectorSize = INT_MAX;102 int rc = sqlite3SectorSize(&f);103 TEST_ASSERT_EQUAL_INT(MAX_SECTOR_SIZE, rc);104}105 106void test_sqlite3SectorSize_returns_512_when_no_io_methods_present(void){107 sqlite3_file f;108 memset(&f, 0, sizeof(f)); /* pMethods = NULL */109 int rc = sqlite3SectorSize(&f);110 TEST_ASSERT_EQUAL_INT(512, rc);111}112 113int main(void) {114 UNITY_BEGIN();115 RUN_TEST(test_sqlite3SectorSize_returns_512_when_os_reports_0);116 RUN_TEST(test_sqlite3SectorSize_returns_512_when_os_reports_31);117 RUN_TEST(test_sqlite3SectorSize_returns_512_when_os_reports_negative);118 RUN_TEST(test_sqlite3SectorSize_returns_32_when_os_reports_32);119 RUN_TEST(test_sqlite3SectorSize_passthrough_for_common_values);120 RUN_TEST(test_sqlite3SectorSize_returns_exact_MAX_when_os_reports_MAX);121 RUN_TEST(test_sqlite3SectorSize_clamps_to_MAX_when_os_reports_MAX_plus_1);122 RUN_TEST(test_sqlite3SectorSize_clamps_to_MAX_when_os_reports_INT_MAX);123 RUN_TEST(test_sqlite3SectorSize_returns_512_when_no_io_methods_present);124 return UNITY_END();125}