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

/* 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();
}