File size: 4,953 Bytes
7510827
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "sqliteInt.h"
#include "unity.h"
#include <string.h>
#include <stdlib.h>

/* Wrapper for the static function provided in the module */
extern int test_alterRtrimConstraint(sqlite3 *db, const char *pCons, int nCons);

void setUp(void) {
  /* Setup code here, or leave empty */
}

void tearDown(void) {
  /* Cleanup code here, or leave empty */
}

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;
}

/* 1. Trims trailing whitespace */
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);
}

/* 2. Drops trailing -- comment */
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);
}

/* 3. Keeps trailing C-style comment */
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);
}

/* 4. Keeps C-style comment, but drops subsequent -- comment */
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); /* include the '*/' */
  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();
}