|
|
#include "sqliteInt.h" |
|
|
#include "unity.h" |
|
|
#include <string.h> |
|
|
#include <stdlib.h> |
|
|
#include <stdio.h> |
|
|
|
|
|
|
|
|
extern int test_getConstraintToken(const u8 *z, int *piToken); |
|
|
|
|
|
void setUp(void) { |
|
|
|
|
|
} |
|
|
|
|
|
void tearDown(void) { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
static int idx_of_char(const char *s, char c){ |
|
|
const char *p = strchr(s, c); |
|
|
if( !p ) return -1; |
|
|
return (int)(p - s); |
|
|
} |
|
|
|
|
|
|
|
|
static int idx_after_sub(const char *s, const char *sub){ |
|
|
const char *p = strstr(s, sub); |
|
|
if( !p ) return -1; |
|
|
return (int)((p - s) + (int)strlen(sub)); |
|
|
} |
|
|
|
|
|
|
|
|
void test_getConstraintToken_simple_identifier(void) { |
|
|
const char *z = "abc def"; |
|
|
int t = 0; |
|
|
int n = test_getConstraintToken((const u8*)z, &t); |
|
|
TEST_ASSERT_EQUAL_INT(TK_ID, t); |
|
|
TEST_ASSERT_EQUAL_INT(3, n); |
|
|
} |
|
|
|
|
|
|
|
|
void test_getConstraintToken_skips_space_and_comments_before_id(void) { |
|
|
const char *z = " /* comment */\n-- line comment\nabc def"; |
|
|
int expected = idx_after_sub(z, "abc"); |
|
|
TEST_ASSERT_TRUE_MESSAGE(expected > 0, "Did not find 'abc' in test string"); |
|
|
|
|
|
int t = 0; |
|
|
int n = test_getConstraintToken((const u8*)z, &t); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(TK_ID, t); |
|
|
TEST_ASSERT_EQUAL_INT(expected, n); |
|
|
} |
|
|
|
|
|
|
|
|
void test_getConstraintToken_minimal_parentheses(void) { |
|
|
const char *z = "()X"; |
|
|
int expected = idx_of_char(z, 'X'); |
|
|
TEST_ASSERT_TRUE_MESSAGE(expected >= 0, "Sentinel 'X' not found"); |
|
|
|
|
|
int t = 0; |
|
|
int n = test_getConstraintToken((const u8*)z, &t); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(TK_LP, t); |
|
|
TEST_ASSERT_EQUAL_INT(expected, n); |
|
|
} |
|
|
|
|
|
|
|
|
void test_getConstraintToken_parentheses_with_inner_tokens(void) { |
|
|
const char *z = " (two three)X"; |
|
|
int expected = idx_of_char(z, 'X'); |
|
|
TEST_ASSERT_TRUE_MESSAGE(expected >= 0, "Sentinel 'X' not found"); |
|
|
|
|
|
int t = 0; |
|
|
int n = test_getConstraintToken((const u8*)z, &t); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(TK_LP, t); |
|
|
TEST_ASSERT_EQUAL_INT(expected, n); |
|
|
} |
|
|
|
|
|
|
|
|
void test_getConstraintToken_nested_parentheses(void) { |
|
|
const char *z = "(a(b)c)X"; |
|
|
int expected = idx_of_char(z, 'X'); |
|
|
TEST_ASSERT_TRUE_MESSAGE(expected >= 0, "Sentinel 'X' not found"); |
|
|
|
|
|
int t = 0; |
|
|
int n = test_getConstraintToken((const u8*)z, &t); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(TK_LP, t); |
|
|
TEST_ASSERT_EQUAL_INT(expected, n); |
|
|
} |
|
|
|
|
|
|
|
|
void test_getConstraintToken_unclosed_parenthesis_hits_illegal(void) { |
|
|
const char *z = "(abc"; |
|
|
int t = 0; |
|
|
int n = test_getConstraintToken((const u8*)z, &t); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(TK_ILLEGAL, t); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(1, n); |
|
|
} |
|
|
|
|
|
|
|
|
void test_getConstraintToken_unterminated_string_inside_parentheses(void) { |
|
|
const char *z = "('unterminated"; |
|
|
int t = 0; |
|
|
int n = test_getConstraintToken((const u8*)z, &t); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(TK_ILLEGAL, t); |
|
|
|
|
|
TEST_ASSERT_TRUE(n > 1); |
|
|
TEST_ASSERT_TRUE(n <= (int)strlen(z)); |
|
|
} |
|
|
|
|
|
|
|
|
void test_getConstraintToken_keyword_token(void) { |
|
|
const char *z = "SELECT rest"; |
|
|
int t = 0; |
|
|
int n = test_getConstraintToken((const u8*)z, &t); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(TK_SELECT, t); |
|
|
TEST_ASSERT_EQUAL_INT(6, n); |
|
|
} |
|
|
|
|
|
int main(void) { |
|
|
UNITY_BEGIN(); |
|
|
|
|
|
RUN_TEST(test_getConstraintToken_simple_identifier); |
|
|
RUN_TEST(test_getConstraintToken_skips_space_and_comments_before_id); |
|
|
RUN_TEST(test_getConstraintToken_minimal_parentheses); |
|
|
RUN_TEST(test_getConstraintToken_parentheses_with_inner_tokens); |
|
|
RUN_TEST(test_getConstraintToken_nested_parentheses); |
|
|
RUN_TEST(test_getConstraintToken_unclosed_parenthesis_hits_illegal); |
|
|
RUN_TEST(test_getConstraintToken_unterminated_string_inside_parentheses); |
|
|
RUN_TEST(test_getConstraintToken_keyword_token); |
|
|
|
|
|
return UNITY_END(); |
|
|
} |