AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <stdlib.h>4#include <string.h>5 6static sqlite3 *gDb = NULL;7 8static FuncDef* mustFindFunc(const char *zName, int nArg){9 FuncDef *pDef = sqlite3FindFunction(gDb, zName, nArg, SQLITE_UTF8, 0);10 TEST_ASSERT_MESSAGE(pDef != NULL, "Expected function not found");11 TEST_ASSERT_EQUAL_INT_MESSAGE(nArg, pDef->nArg, "Function arity mismatch");12 return pDef;13}14 15void setUp(void) {16 int rc;17 /* Ensure the ALTER functions are registered before a connection is opened */18 sqlite3AlterFunctions();19 rc = sqlite3_open(":memory:", &gDb);20 TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "sqlite3_open failed");21}22 23void tearDown(void) {24 if( gDb ){25 sqlite3_close(gDb);26 gDb = NULL;27 }28}29 30void test_sqlite3AlterFunctions_registers_all_expected_functions(void){31 struct {32 const char *zName;33 int nArg;34 } aFuncs[] = {35 { "sqlite_rename_column", 9 },36 { "sqlite_rename_table", 7 },37 { "sqlite_rename_test", 7 },38 { "sqlite_drop_column", 3 },39 { "sqlite_rename_quotefix", 2 },40 { "sqlite_drop_constraint", 2 },41 { "sqlite_fail", 2 },42 { "sqlite_add_constraint", 3 },43 { "sqlite_find_constraint", 2 },44 };45 for(size_t i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){46 FuncDef *pDef = mustFindFunc(aFuncs[i].zName, aFuncs[i].nArg);47 /* Internal functions should be flagged as such and should have an xSFunc */48 TEST_ASSERT_MESSAGE((pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0,49 "Function not flagged as internal");50 TEST_ASSERT_MESSAGE(pDef->xSFunc != NULL, "xSFunc should be non-NULL for internal scalar");51 }52}53 54void test_sqlite3AlterFunctions_case_insensitive_lookup(void){55 FuncDef *p1 = sqlite3FindFunction(gDb, "SQLITE_RENAME_TABLE", 7, SQLITE_UTF8, 0);56 TEST_ASSERT_NOT_NULL(p1);57 TEST_ASSERT_EQUAL_INT(7, p1->nArg);58 TEST_ASSERT_TRUE((p1->funcFlags & SQLITE_FUNC_INTERNAL)!=0);59}60 61void test_sqlite3AlterFunctions_wrong_arity_returns_null(void){62 /* These internal functions have fixed arity; wrong arity should not resolve */63 FuncDef *pBad1 = sqlite3FindFunction(gDb, "sqlite_rename_table", 6, SQLITE_UTF8, 0);64 FuncDef *pBad2 = sqlite3FindFunction(gDb, "sqlite_drop_column", 2, SQLITE_UTF8, 0);65 TEST_ASSERT_NULL(pBad1);66 TEST_ASSERT_NULL(pBad2);67}68 69void test_sqlite3AlterFunctions_idempotent_registration(void){70 /* Call the registrar again; lookups must still succeed */71 sqlite3AlterFunctions();72 73 FuncDef *p1 = sqlite3FindFunction(gDb, "sqlite_add_constraint", 3, SQLITE_UTF8, 0);74 TEST_ASSERT_NOT_NULL(p1);75 TEST_ASSERT_TRUE((p1->funcFlags & SQLITE_FUNC_INTERNAL)!=0);76 77 FuncDef *p2 = sqlite3FindFunction(gDb, "sqlite_find_constraint", 2, SQLITE_UTF8, 0);78 TEST_ASSERT_NOT_NULL(p2);79 TEST_ASSERT_TRUE((p2->funcFlags & SQLITE_FUNC_INTERNAL)!=0);80}81 82void test_sqlite3AlterFunctions_unknown_function_not_found(void){83 FuncDef *p = sqlite3FindFunction(gDb, "sqlite_nonexistent", 1, SQLITE_UTF8, 0);84 TEST_ASSERT_NULL(p);85}86 87int main(void) {88 UNITY_BEGIN();89 RUN_TEST(test_sqlite3AlterFunctions_registers_all_expected_functions);90 RUN_TEST(test_sqlite3AlterFunctions_case_insensitive_lookup);91 RUN_TEST(test_sqlite3AlterFunctions_wrong_arity_returns_null);92 RUN_TEST(test_sqlite3AlterFunctions_idempotent_registration);93 RUN_TEST(test_sqlite3AlterFunctions_unknown_function_not_found);94 return UNITY_END();95}