#include "sqliteInt.h" #include "unity.h" #include #include /* Wrapper for the static function getWhitespace() provided by the build. */ extern int test_getWhitespace(const u8 *z); void setUp(void) { /* Setup code here, or leave empty */ } void tearDown(void) { /* Cleanup code here, or leave empty */ } /* Helper macro to cast string literals to u8* safely */ #define U8STR(z) ((const u8*)(z)) void test_getWhitespace_empty_string(void) { const char *z = ""; int n = test_getWhitespace(U8STR(z)); TEST_ASSERT_EQUAL_INT(0, n); } void test_getWhitespace_no_leading_whitespace(void) { const char *z = "SELECT"; int n = test_getWhitespace(U8STR(z)); TEST_ASSERT_EQUAL_INT(0, n); } void test_getWhitespace_simple_spaces_tabs_newlines(void) { const char *z = " \t\nabc"; /* Leading whitespace is 3 bytes: space, tab, newline */ int n = test_getWhitespace(U8STR(z)); TEST_ASSERT_EQUAL_INT(3, n); } void test_getWhitespace_c_style_comment_only_then_token(void) { const char *z = "/*hello*/world"; /* "/*hello*/" is 9 bytes long */ int expected = 9; int n = test_getWhitespace(U8STR(z)); TEST_ASSERT_EQUAL_INT(expected, n); } void test_getWhitespace_c_style_comment_then_whitespace_then_token(void) { const char *z = "/*x*/ \tY"; /* "/*x*/" = 5, ' ' = 1, '\t' = 1, total = 7 */ int expected = 7; int n = test_getWhitespace(U8STR(z)); TEST_ASSERT_EQUAL_INT(expected, n); } void test_getWhitespace_multiple_comments_and_whitespace(void) { const char *z = " /*a*/\t/*bcd*/ X"; /* ' ' = 1, "/*a*/" = 5, '\t' = 1, "/*bcd*/" = 7, ' ' = 2 => total 16 */ int expected = 16; int n = test_getWhitespace(U8STR(z)); TEST_ASSERT_EQUAL_INT(expected, n); } void test_getWhitespace_sql_line_comment_to_eof(void) { const char *z = "-- hello"; /* Entire string is a line comment to EOF. */ int expected = (int)strlen(z); int n = test_getWhitespace(U8STR(z)); TEST_ASSERT_EQUAL_INT(expected, n); } void test_getWhitespace_mixed_leading_ws_and_sql_line_comment_to_eof(void) { const char *z = " \t--abc"; /* Leading space+tab (2) then line comment "--abc" (5) => total 7; entire string */ int expected = (int)strlen(z); int n = test_getWhitespace(U8STR(z)); TEST_ASSERT_EQUAL_INT(expected, n); } void test_getWhitespace_stops_before_non_ws_or_comment_after_comment(void) { const char *z = "/*a*/X"; /* Should count only the comment (5), then stop before 'X' */ int expected = 5; int n = test_getWhitespace(U8STR(z)); TEST_ASSERT_EQUAL_INT(expected, n); } void test_getWhitespace_all_ws_and_comments_entire_string(void) { const char *z = " \t/*c*/--d"; /* space(1) + tab(1) + "/*c*/"(5) + "--d"(3) = 10; entire string */ int expected = (int)strlen(z); int n = test_getWhitespace(U8STR(z)); TEST_ASSERT_EQUAL_INT(expected, n); } int main(void) { UNITY_BEGIN(); RUN_TEST(test_getWhitespace_empty_string); RUN_TEST(test_getWhitespace_no_leading_whitespace); RUN_TEST(test_getWhitespace_simple_spaces_tabs_newlines); RUN_TEST(test_getWhitespace_c_style_comment_only_then_token); RUN_TEST(test_getWhitespace_c_style_comment_then_whitespace_then_token); RUN_TEST(test_getWhitespace_multiple_comments_and_whitespace); RUN_TEST(test_getWhitespace_sql_line_comment_to_eof); RUN_TEST(test_getWhitespace_mixed_leading_ws_and_sql_line_comment_to_eof); RUN_TEST(test_getWhitespace_stops_before_non_ws_or_comment_after_comment); RUN_TEST(test_getWhitespace_all_ws_and_comments_entire_string); return UNITY_END(); }