#include "sqliteInt.h" #include "unity.h" #include #include #include /* Wrapper for the static function under test. ** As per instructions, a global wrapper named test_getConstraint() ** is provided in the module for static functions. */ int test_getConstraint(const u8 *z); void setUp(void) { /* Setup code here, if needed */ } void tearDown(void) { /* Cleanup code here, if needed */ } /* Helper to run a single check */ static void check_len(const unsigned char *z, int expected){ int got = test_getConstraint((const u8*)z); TEST_ASSERT_EQUAL_INT_MESSAGE(expected, got, (const char*)z); } /* 1) Ends at DEFAULT: "NULL DEFAULT 5" -> length of "NULL" == 4 */ void test_getConstraint_notnull_default(void){ const unsigned char s[] = "NULL DEFAULT 5"; check_len(s, 4); } /* 2) Whitespace and comment before UNIQUE: "BINARY /*c* / UNIQUE" -> 6 */ void test_getConstraint_whitespace_and_comment_before_unique(void){ const unsigned char s[] = "BINARY /*comment*/ UNIQUE"; check_len(s, 6); } /* 3) Parentheses group followed by PRIMARY: "(a + b) PRIMARY KEY" -> 7 */ void test_getConstraint_parentheses_then_primary(void){ const unsigned char s[] = "(a + b) PRIMARY KEY"; check_len(s, 7); } /* 4) Termination by COMMA: "NULL , CONSTRAINT ..." -> 4 */ void test_getConstraint_terminated_by_comma(void){ const unsigned char s[] = "NULL , CONSTRAINT c1 UNIQUE"; check_len(s, 4); } /* 5) Termination by right parenthesis: "NULL )" -> 4 */ void test_getConstraint_terminated_by_rparen(void){ const unsigned char s[] = "NULL )"; check_len(s, 4); } /* 6) Termination by AS: "xyz AS (1+2)" -> 3 */ void test_getConstraint_terminated_by_as(void){ const unsigned char s[] = "xyz AS (1+2)"; check_len(s, 3); } /* 7) Nested parentheses consumed as one token: "( (a) + func(b, c) ) DEFAULT 0" -> length of "( (a) + func(b, c) )" == 20 */ void test_getConstraint_nested_parentheses_then_default(void){ const unsigned char s[] = "( (a) + func(b, c) ) DEFAULT 0"; /* Counted as: "("=1," "=1,"("=1,"a"=1,")"=1," "=1,"+"=1," "=1,"func"=4,"("=1,"b"=1,","=1," "=1,"c"=1,")"=1," "=1,")"=1 => total 20 */ check_len(s, 20); } /* 8) Termination by FOREIGN: "ref FOREIGN KEY" -> 3 */ void test_getConstraint_terminated_by_foreign(void){ const unsigned char s[] = "ref FOREIGN KEY"; check_len(s, 3); } /* 9) Termination by CONSTRAINT: "name CONSTRAINT c1 ..." -> 4 */ void test_getConstraint_terminated_by_constraint(void){ const unsigned char s[] = "name CONSTRAINT c1 CHECK(x>0)"; check_len(s, 4); } /* 10) Termination by GENERATED: "abc GENERATED ALWAYS ..." -> 3 */ void test_getConstraint_terminated_by_generated(void){ const unsigned char s[] = "abc GENERATED ALWAYS AS (x+y)"; check_len(s, 3); } int main(void){ UNITY_BEGIN(); RUN_TEST(test_getConstraint_notnull_default); RUN_TEST(test_getConstraint_whitespace_and_comment_before_unique); RUN_TEST(test_getConstraint_parentheses_then_primary); RUN_TEST(test_getConstraint_terminated_by_comma); RUN_TEST(test_getConstraint_terminated_by_rparen); RUN_TEST(test_getConstraint_terminated_by_as); RUN_TEST(test_getConstraint_nested_parentheses_then_default); RUN_TEST(test_getConstraint_terminated_by_foreign); RUN_TEST(test_getConstraint_terminated_by_constraint); RUN_TEST(test_getConstraint_terminated_by_generated); return UNITY_END(); }