File size: 3,656 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
#include "sqliteInt.h"
#include "unity.h"
#include <string.h>
#include <stdlib.h>

/* Wrapper for the static function provided by the build system */
extern int test_quotedCompare(sqlite3_context *ctx, int t,
                              const u8 *zQuote, int nQuote,
                              const u8 *zCmp, int *pRes);

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

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

/* Helper to invoke the target function more conveniently */
static int call_quotedCompare(int t, const char *zFrag, int nFrag, const char *zCmp, int *pRes){
  return test_quotedCompare(NULL, t,
                            (const u8*)zFrag, nFrag,
                            (const u8*)zCmp, pRes);
}

void test_quotedCompare_tk_illegal_sets_res_and_returns_ok(void) {
  int res = -12345;
  /* Inputs should be ignored for TK_ILLEGAL path, but supply something trivial */
  const char *frag = "Anything";
  int rc = call_quotedCompare(TK_ILLEGAL, frag, (int)strlen(frag), "anything", &res);
  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);
  TEST_ASSERT_EQUAL_INT(1, res);
}

void test_quotedCompare_unquoted_case_insensitive_equal(void) {
  int res = -1;
  const char *frag = "AbC";
  const char *cmp  = "aBc";
  int rc = call_quotedCompare(TK_ID, frag, (int)strlen(frag), cmp, &res);
  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);
  TEST_ASSERT_EQUAL_INT(0, res); /* case-insensitive equal */
}

void test_quotedCompare_double_quoted_with_escaped_quotes_equal(void) {
  int res = -1;
  /* This literal is: "He""llo" (identifier with doubled internal quote) */
  const char frag[] = "\"He\"\"llo\"";
  const char *cmp   = "He\"llo";
  int rc = call_quotedCompare(TK_ID, frag, (int)strlen(frag), cmp, &res);
  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);
  TEST_ASSERT_EQUAL_INT(0, res);
}

void test_quotedCompare_bracket_quoted_identifier_equal(void) {
  int res = -1;
  const char frag[] = "[MyName]";
  const char *cmp   = "myname"; /* case-insensitive compare */
  int rc = call_quotedCompare(TK_ID, frag, (int)strlen(frag), cmp, &res);
  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);
  TEST_ASSERT_EQUAL_INT(0, res);
}

void test_quotedCompare_single_quoted_with_doubled_quote_equal(void) {
  int res = -1;
  /* This represents SQL-escaped single quotes: 'O''Reilly' -> O'Reilly */
  const char frag[] = "'O''Reilly'";
  const char *cmp   = "o'reilly";
  int rc = call_quotedCompare(TK_ID, frag, (int)strlen(frag), cmp, &res);
  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);
  TEST_ASSERT_EQUAL_INT(0, res);
}

void test_quotedCompare_not_equal_sets_nonzero_result(void) {
  int res = -1;
  const char *frag = "abc";
  const char *cmp  = "abd";
  int rc = call_quotedCompare(TK_ID, frag, (int)strlen(frag), cmp, &res);
  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);
  TEST_ASSERT_NOT_EQUAL(0, res);
}

void test_quotedCompare_empty_fragment_equal_to_empty_cmp(void) {
  int res = -1;
  const char *frag = ""; /* nFrag=0 means empty string after dequote */
  const char *cmp  = "";
  int rc = call_quotedCompare(TK_ID, frag, 0, cmp, &res);
  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);
  TEST_ASSERT_EQUAL_INT(0, res);
}

int main(void) {
  UNITY_BEGIN();
  RUN_TEST(test_quotedCompare_tk_illegal_sets_res_and_returns_ok);
  RUN_TEST(test_quotedCompare_unquoted_case_insensitive_equal);
  RUN_TEST(test_quotedCompare_double_quoted_with_escaped_quotes_equal);
  RUN_TEST(test_quotedCompare_bracket_quoted_identifier_equal);
  RUN_TEST(test_quotedCompare_single_quoted_with_doubled_quote_equal);
  RUN_TEST(test_quotedCompare_not_equal_sets_nonzero_result);
  RUN_TEST(test_quotedCompare_empty_fragment_equal_to_empty_cmp);
  return UNITY_END();
}