#include "sqliteInt.h" #include "unity.h" #include #include /* Wrapper for the static function provided by the build system */ extern int test_quotedCompare(sqlite3_context *ctx, int t, const u8 *zQuote, int nQuote, const u8 *zCmp, int *pRes); void setUp(void) { /* Setup code here, or leave empty */ } void tearDown(void) { /* Cleanup code here, or leave empty */ } /* Helper to invoke the target function more conveniently */ static int call_quotedCompare(int t, const char *zFrag, int nFrag, const char *zCmp, int *pRes){ return test_quotedCompare(NULL, t, (const u8*)zFrag, nFrag, (const u8*)zCmp, pRes); } void test_quotedCompare_tk_illegal_sets_res_and_returns_ok(void) { int res = -12345; /* Inputs should be ignored for TK_ILLEGAL path, but supply something trivial */ const char *frag = "Anything"; int rc = call_quotedCompare(TK_ILLEGAL, frag, (int)strlen(frag), "anything", &res); TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); TEST_ASSERT_EQUAL_INT(1, res); } void test_quotedCompare_unquoted_case_insensitive_equal(void) { int res = -1; const char *frag = "AbC"; const char *cmp = "aBc"; int rc = call_quotedCompare(TK_ID, frag, (int)strlen(frag), cmp, &res); TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); TEST_ASSERT_EQUAL_INT(0, res); /* case-insensitive equal */ } void test_quotedCompare_double_quoted_with_escaped_quotes_equal(void) { int res = -1; /* This literal is: "He""llo" (identifier with doubled internal quote) */ const char frag[] = "\"He\"\"llo\""; const char *cmp = "He\"llo"; int rc = call_quotedCompare(TK_ID, frag, (int)strlen(frag), cmp, &res); TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); TEST_ASSERT_EQUAL_INT(0, res); } void test_quotedCompare_bracket_quoted_identifier_equal(void) { int res = -1; const char frag[] = "[MyName]"; const char *cmp = "myname"; /* case-insensitive compare */ int rc = call_quotedCompare(TK_ID, frag, (int)strlen(frag), cmp, &res); TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); TEST_ASSERT_EQUAL_INT(0, res); } void test_quotedCompare_single_quoted_with_doubled_quote_equal(void) { int res = -1; /* This represents SQL-escaped single quotes: 'O''Reilly' -> O'Reilly */ const char frag[] = "'O''Reilly'"; const char *cmp = "o'reilly"; int rc = call_quotedCompare(TK_ID, frag, (int)strlen(frag), cmp, &res); TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); TEST_ASSERT_EQUAL_INT(0, res); } void test_quotedCompare_not_equal_sets_nonzero_result(void) { int res = -1; const char *frag = "abc"; const char *cmp = "abd"; int rc = call_quotedCompare(TK_ID, frag, (int)strlen(frag), cmp, &res); TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); TEST_ASSERT_NOT_EQUAL(0, res); } void test_quotedCompare_empty_fragment_equal_to_empty_cmp(void) { int res = -1; const char *frag = ""; /* nFrag=0 means empty string after dequote */ const char *cmp = ""; int rc = call_quotedCompare(TK_ID, frag, 0, cmp, &res); TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); TEST_ASSERT_EQUAL_INT(0, res); } int main(void) { UNITY_BEGIN(); RUN_TEST(test_quotedCompare_tk_illegal_sets_res_and_returns_ok); RUN_TEST(test_quotedCompare_unquoted_case_insensitive_equal); RUN_TEST(test_quotedCompare_double_quoted_with_escaped_quotes_equal); RUN_TEST(test_quotedCompare_bracket_quoted_identifier_equal); RUN_TEST(test_quotedCompare_single_quoted_with_doubled_quote_equal); RUN_TEST(test_quotedCompare_not_equal_sets_nonzero_result); RUN_TEST(test_quotedCompare_empty_fragment_equal_to_empty_cmp); return UNITY_END(); }