|
|
#include "sqliteInt.h" |
|
|
#include "unity.h" |
|
|
#include <string.h> |
|
|
#include <stdlib.h> |
|
|
|
|
|
|
|
|
extern int test_alterRtrimConstraint(sqlite3 *db, const char *pCons, int nCons); |
|
|
|
|
|
void setUp(void) { |
|
|
|
|
|
} |
|
|
|
|
|
void tearDown(void) { |
|
|
|
|
|
} |
|
|
|
|
|
static int call_alterRtrimConstraint_with_db(const char *s, int nCons){ |
|
|
sqlite3 *db = 0; |
|
|
int rc = sqlite3_open(":memory:", &db); |
|
|
TEST_ASSERT_EQUAL_INT(0, rc); |
|
|
int n = test_alterRtrimConstraint(db, s, nCons); |
|
|
sqlite3_close(db); |
|
|
return n; |
|
|
} |
|
|
|
|
|
|
|
|
void test_alterRtrimConstraint_trims_trailing_whitespace(void){ |
|
|
const char *s = "NOT NULL \t\r\n"; |
|
|
int n = call_alterRtrimConstraint_with_db(s, (int)strlen(s)); |
|
|
TEST_ASSERT_EQUAL_INT((int)strlen("NOT NULL"), n); |
|
|
} |
|
|
|
|
|
|
|
|
void test_alterRtrimConstraint_drops_trailing_line_comment(void){ |
|
|
const char *s = "CHECK(x) -- trailing comment"; |
|
|
int n = call_alterRtrimConstraint_with_db(s, (int)strlen(s)); |
|
|
TEST_ASSERT_EQUAL_INT((int)strlen("CHECK(x)"), n); |
|
|
} |
|
|
|
|
|
|
|
|
void test_alterRtrimConstraint_keeps_trailing_c_style_comment(void){ |
|
|
const char *s = "CHECK(x) /* trailing c-style comment */"; |
|
|
int n = call_alterRtrimConstraint_with_db(s, (int)strlen(s)); |
|
|
TEST_ASSERT_EQUAL_INT((int)strlen(s), n); |
|
|
} |
|
|
|
|
|
|
|
|
void test_alterRtrimConstraint_keeps_c_comment_then_drops_line_comment(void){ |
|
|
const char *s = "CHECK(x) /* c */ -- drop"; |
|
|
const char *endc = strstr(s, "*/"); |
|
|
TEST_ASSERT_NOT_NULL(endc); |
|
|
int expected = (int)((endc - s) + 2); ' */ |
|
|
int n = call_alterRtrimConstraint_with_db(s, (int)strlen(s)); |
|
|
TEST_ASSERT_EQUAL_INT(expected, n); |
|
|
} |
|
|
|
|
|
/* 5. All whitespace -> returns 0 */ |
|
|
void test_alterRtrimConstraint_all_whitespace_returns_zero(void){ |
|
|
const char *s = " \t \n"; |
|
|
int n = call_alterRtrimConstraint_with_db(s, (int)strlen(s)); |
|
|
TEST_ASSERT_EQUAL_INT(0, n); |
|
|
} |
|
|
|
|
|
/* 6. Only -- comment -> returns 0 */ |
|
|
void test_alterRtrimConstraint_only_line_comment_returns_zero(void){ |
|
|
const char *s = "-- comment only\n"; |
|
|
int n = call_alterRtrimConstraint_with_db(s, (int)strlen(s)); |
|
|
TEST_ASSERT_EQUAL_INT(0, n); |
|
|
} |
|
|
|
|
|
/* 7. Only C-style comment -> preserved (returns full length) */ |
|
|
void test_alterRtrimConstraint_only_c_style_comment_full_length(void){ |
|
|
const char *s = "/* only c comment */"; |
|
|
int n = call_alterRtrimConstraint_with_db(s, (int)strlen(s)); |
|
|
TEST_ASSERT_EQUAL_INT((int)strlen(s), n); |
|
|
} |
|
|
|
|
|
/* 8. Respects nCons cutoff (nCons less than full string) */ |
|
|
void test_alterRtrimConstraint_respects_nCons_cutoff(void){ |
|
|
const char *s = "CHECK(x) -- comment should be removed but more text after not part of nCons"; |
|
|
const char *cut = strstr(s, " after"); |
|
|
TEST_ASSERT_NOT_NULL(cut); |
|
|
int nCons = (int)(cut - s); /* only consider prefix up to (but not including) " after" */ |
|
|
int n = call_alterRtrimConstraint_with_db(s, nCons); |
|
|
TEST_ASSERT_EQUAL_INT((int)strlen("CHECK(x)"), n); |
|
|
} |
|
|
|
|
|
/* 9. nCons = 0 -> returns 0 */ |
|
|
void test_alterRtrimConstraint_zero_length_returns_zero(void){ |
|
|
const char *s = "CHECK(x)"; |
|
|
int n = call_alterRtrimConstraint_with_db(s, 0); |
|
|
TEST_ASSERT_EQUAL_INT(0, n); |
|
|
} |
|
|
|
|
|
/* 10. Dashes inside string literal are not treated as comment */ |
|
|
void test_alterRtrimConstraint_handles_dashes_in_string_literal(void){ |
|
|
const char *s = "CHECK(x='--not comment') -- drop"; |
|
|
const char *endparen = strchr(s, ')'); |
|
|
TEST_ASSERT_NOT_NULL(endparen); |
|
|
int expected = (int)((endparen - s) + 1); /* include ')' */ |
|
|
int n = call_alterRtrimConstraint_with_db(s, (int)strlen(s)); |
|
|
TEST_ASSERT_EQUAL_INT(expected, n); |
|
|
} |
|
|
|
|
|
/* 11. Leading whitespace is preserved (function only trims trailing) */ |
|
|
void test_alterRtrimConstraint_preserves_leading_whitespace(void){ |
|
|
const char *s = " CHECK(x) -- comment"; |
|
|
/* Expected is leading spaces + "CHECK(x)" */ |
|
|
int expected = (int)(strlen(" ") + strlen("CHECK(x)")); |
|
|
int n = call_alterRtrimConstraint_with_db(s, (int)strlen(s)); |
|
|
TEST_ASSERT_EQUAL_INT(expected, n); |
|
|
} |
|
|
|
|
|
int main(void){ |
|
|
UNITY_BEGIN(); |
|
|
RUN_TEST(test_alterRtrimConstraint_trims_trailing_whitespace); |
|
|
RUN_TEST(test_alterRtrimConstraint_drops_trailing_line_comment); |
|
|
RUN_TEST(test_alterRtrimConstraint_keeps_trailing_c_style_comment); |
|
|
RUN_TEST(test_alterRtrimConstraint_keeps_c_comment_then_drops_line_comment); |
|
|
RUN_TEST(test_alterRtrimConstraint_all_whitespace_returns_zero); |
|
|
RUN_TEST(test_alterRtrimConstraint_only_line_comment_returns_zero); |
|
|
RUN_TEST(test_alterRtrimConstraint_only_c_style_comment_full_length); |
|
|
RUN_TEST(test_alterRtrimConstraint_respects_nCons_cutoff); |
|
|
RUN_TEST(test_alterRtrimConstraint_zero_length_returns_zero); |
|
|
RUN_TEST(test_alterRtrimConstraint_handles_dashes_in_string_literal); |
|
|
RUN_TEST(test_alterRtrimConstraint_preserves_leading_whitespace); |
|
|
return UNITY_END(); |
|
|
} |