AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdlib.h>5#include <stdio.h>6 7/* Wrapper for the static function under test (provided by the build as per instructions) */8extern int test_getConstraintToken(const u8 *z, int *piToken);9 10void setUp(void) {11 /* No global setup required */12}13 14void tearDown(void) {15 /* No global teardown required */16}17 18/* Helper: return index of first occurrence of character c in s, or -1 if not found */19static int idx_of_char(const char *s, char c){20 const char *p = strchr(s, c);21 if( !p ) return -1;22 return (int)(p - s);23}24 25/* Helper: return index immediately after the first occurrence of substring sub in s, or -1 if not found */26static int idx_after_sub(const char *s, const char *sub){27 const char *p = strstr(s, sub);28 if( !p ) return -1;29 return (int)((p - s) + (int)strlen(sub));30}31 32/* 1) Simple identifier with no leading whitespace/comments */33void test_getConstraintToken_simple_identifier(void) {34 const char *z = "abc def";35 int t = 0;36 int n = test_getConstraintToken((const u8*)z, &t);37 TEST_ASSERT_EQUAL_INT(TK_ID, t);38 TEST_ASSERT_EQUAL_INT(3, n); /* "abc" length */39}40 41/* 2) Skips leading spaces and comments, then identifies "abc" */42void test_getConstraintToken_skips_space_and_comments_before_id(void) {43 const char *z = " /* comment */\n-- line comment\nabc def";44 int expected = idx_after_sub(z, "abc"); /* position immediately after "abc" */45 TEST_ASSERT_TRUE_MESSAGE(expected > 0, "Did not find 'abc' in test string");46 47 int t = 0;48 int n = test_getConstraintToken((const u8*)z, &t);49 50 TEST_ASSERT_EQUAL_INT(TK_ID, t);51 TEST_ASSERT_EQUAL_INT(expected, n);52}53 54/* 3) Minimal parenthesis token "()" followed by sentinel 'X' */55void test_getConstraintToken_minimal_parentheses(void) {56 const char *z = "()X";57 int expected = idx_of_char(z, 'X');58 TEST_ASSERT_TRUE_MESSAGE(expected >= 0, "Sentinel 'X' not found");59 60 int t = 0;61 int n = test_getConstraintToken((const u8*)z, &t);62 63 TEST_ASSERT_EQUAL_INT(TK_LP, t);64 TEST_ASSERT_EQUAL_INT(expected, n);65}66 67/* 4) Parenthesis token with inner tokens and leading spaces; should consume up to the matching ')' */68void test_getConstraintToken_parentheses_with_inner_tokens(void) {69 const char *z = " (two three)X";70 int expected = idx_of_char(z, 'X');71 TEST_ASSERT_TRUE_MESSAGE(expected >= 0, "Sentinel 'X' not found");72 73 int t = 0;74 int n = test_getConstraintToken((const u8*)z, &t);75 76 TEST_ASSERT_EQUAL_INT(TK_LP, t);77 TEST_ASSERT_EQUAL_INT(expected, n);78}79 80/* 5) Nested parentheses: (a(b)c)X -> should consume exactly up to the ')' before X */81void test_getConstraintToken_nested_parentheses(void) {82 const char *z = "(a(b)c)X";83 int expected = idx_of_char(z, 'X');84 TEST_ASSERT_TRUE_MESSAGE(expected >= 0, "Sentinel 'X' not found");85 86 int t = 0;87 int n = test_getConstraintToken((const u8*)z, &t);88 89 TEST_ASSERT_EQUAL_INT(TK_LP, t);90 TEST_ASSERT_EQUAL_INT(expected, n);91}92 93/* 6) Unclosed parenthesis at end-of-input: expect TK_ILLEGAL and only the '(' consumed */94void test_getConstraintToken_unclosed_parenthesis_hits_illegal(void) {95 const char *z = "(abc";96 int t = 0;97 int n = test_getConstraintToken((const u8*)z, &t);98 99 TEST_ASSERT_EQUAL_INT(TK_ILLEGAL, t);100 /* Expect at least the leading '(' to be consumed; tokenizer should return 0-length illegal at EOF */101 TEST_ASSERT_EQUAL_INT(1, n);102}103 104/* 7) Unterminated string literal inside parentheses should trigger TK_ILLEGAL */105void test_getConstraintToken_unterminated_string_inside_parentheses(void) {106 const char *z = "('unterminated";107 int t = 0;108 int n = test_getConstraintToken((const u8*)z, &t);109 110 TEST_ASSERT_EQUAL_INT(TK_ILLEGAL, t);111 /* Should have consumed the '(' plus at least part of the string token */112 TEST_ASSERT_TRUE(n > 1);113 TEST_ASSERT_TRUE(n <= (int)strlen(z));114}115 116/* 8) Non-parenthesis keyword token (SELECT) */117void test_getConstraintToken_keyword_token(void) {118 const char *z = "SELECT rest";119 int t = 0;120 int n = test_getConstraintToken((const u8*)z, &t);121 122 TEST_ASSERT_EQUAL_INT(TK_SELECT, t);123 TEST_ASSERT_EQUAL_INT(6, n); /* "SELECT" length */124}125 126int main(void) {127 UNITY_BEGIN();128 129 RUN_TEST(test_getConstraintToken_simple_identifier);130 RUN_TEST(test_getConstraintToken_skips_space_and_comments_before_id);131 RUN_TEST(test_getConstraintToken_minimal_parentheses);132 RUN_TEST(test_getConstraintToken_parentheses_with_inner_tokens);133 RUN_TEST(test_getConstraintToken_nested_parentheses);134 RUN_TEST(test_getConstraintToken_unclosed_parenthesis_hits_illegal);135 RUN_TEST(test_getConstraintToken_unterminated_string_inside_parentheses);136 RUN_TEST(test_getConstraintToken_keyword_token);137 138 return UNITY_END();139}