AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdlib.h>5#include <stdio.h>6 7/* Unity setup/teardown */8void setUp(void) {9 /* No setup required */10}11void tearDown(void) {12 /* No teardown required */13}14 15/* Helper to initialize a sqlite3 struct safely for these tests */16static void initDb(sqlite3 *db, int tempStoreVal){17 memset(db, 0, sizeof(*db));18 db->temp_store = (u8)tempStoreVal;19}20 21/* Map expected results for given temp_store under current build-time SQLITE_TEMP_STORE */22static int expected_for_temp_store_value(int tempStoreVal){23#if SQLITE_TEMP_STORE==124 return (tempStoreVal==2) ? 1 : 0;25#elif SQLITE_TEMP_STORE==226 return (tempStoreVal!=1) ? 1 : 0;27#elif SQLITE_TEMP_STORE==328 (void)tempStoreVal;29 return 1;30#else /* SQLITE_TEMP_STORE < 1 || SQLITE_TEMP_STORE > 3 */31 (void)tempStoreVal;32 return 0;33#endif34}35 36/* Test: basic mapping for temp_store values 0,1,2 */37void test_sqlite3TempInMemory_basic_values_0_1_2(void){38 sqlite3 db;39 int rc;40 41 /* temp_store = 0 */42 initDb(&db, 0);43 rc = sqlite3TempInMemory(&db);44 TEST_ASSERT_EQUAL_INT(expected_for_temp_store_value(0), rc);45 46 /* temp_store = 1 */47 initDb(&db, 1);48 rc = sqlite3TempInMemory(&db);49 TEST_ASSERT_EQUAL_INT(expected_for_temp_store_value(1), rc);50 51 /* temp_store = 2 */52 initDb(&db, 2);53 rc = sqlite3TempInMemory(&db);54 TEST_ASSERT_EQUAL_INT(expected_for_temp_store_value(2), rc);55}56 57/* Test: out-of-range temp_store values still follow the implemented logic */58void test_sqlite3TempInMemory_out_of_range_values(void){59 sqlite3 db;60 int rc;61 62 /* temp_store = 3 */63 initDb(&db, 3);64 rc = sqlite3TempInMemory(&db);65 TEST_ASSERT_EQUAL_INT(expected_for_temp_store_value(3), rc);66 67 /* temp_store = 42 */68 initDb(&db, 42);69 rc = sqlite3TempInMemory(&db);70 TEST_ASSERT_EQUAL_INT(expected_for_temp_store_value(42), rc);71}72 73/* Test: NULL pointer usage when db is unused (valid only for certain builds) */74void test_sqlite3TempInMemory_accepts_NULL_when_db_unused(void){75#if SQLITE_TEMP_STORE==376 int rc = sqlite3TempInMemory(NULL);77 TEST_ASSERT_EQUAL_INT(1, rc);78#elif SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>379 int rc = sqlite3TempInMemory(NULL);80 TEST_ASSERT_EQUAL_INT(0, rc);81#else82 TEST_IGNORE_MESSAGE("db parameter is used in this build; NULL would be dereferenced");83#endif84}85 86/* Test: Ensure the function does not modify the sqlite3 struct */87void test_sqlite3TempInMemory_does_not_modify_db(void){88 sqlite3 before, after;89 int vals[] = {0, 1, 2, 3, 42};90 for(size_t i=0; i<sizeof(vals)/sizeof(vals[0]); i++){91 initDb(&before, vals[i]);92 after = before; /* struct copy */93 (void)sqlite3TempInMemory(&before);94 /* The function should be read-only w.r.t. db */95 TEST_ASSERT_EQUAL_MEMORY(&after, &before, sizeof(sqlite3));96 }97}98 99int main(void){100 UNITY_BEGIN();101 RUN_TEST(test_sqlite3TempInMemory_basic_values_0_1_2);102 RUN_TEST(test_sqlite3TempInMemory_out_of_range_values);103 RUN_TEST(test_sqlite3TempInMemory_accepts_NULL_when_db_unused);104 RUN_TEST(test_sqlite3TempInMemory_does_not_modify_db);105 return UNITY_END();106}