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

/* Wrapper for the static function getWhitespace() provided by the build. */
extern int test_getWhitespace(const u8 *z);

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

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

/* Helper macro to cast string literals to u8* safely */
#define U8STR(z) ((const u8*)(z))

void test_getWhitespace_empty_string(void) {
  const char *z = "";
  int n = test_getWhitespace(U8STR(z));
  TEST_ASSERT_EQUAL_INT(0, n);
}

void test_getWhitespace_no_leading_whitespace(void) {
  const char *z = "SELECT";
  int n = test_getWhitespace(U8STR(z));
  TEST_ASSERT_EQUAL_INT(0, n);
}

void test_getWhitespace_simple_spaces_tabs_newlines(void) {
  const char *z = " \t\nabc";
  /* Leading whitespace is 3 bytes: space, tab, newline */
  int n = test_getWhitespace(U8STR(z));
  TEST_ASSERT_EQUAL_INT(3, n);
}

void test_getWhitespace_c_style_comment_only_then_token(void) {
  const char *z = "/*hello*/world";
  /* "/*hello*/" is 9 bytes long */
  int expected = 9;
  int n = test_getWhitespace(U8STR(z));
  TEST_ASSERT_EQUAL_INT(expected, n);
}

void test_getWhitespace_c_style_comment_then_whitespace_then_token(void) {
  const char *z = "/*x*/ \tY";
  /* "/*x*/" = 5, ' ' = 1, '\t' = 1, total = 7 */
  int expected = 7;
  int n = test_getWhitespace(U8STR(z));
  TEST_ASSERT_EQUAL_INT(expected, n);
}

void test_getWhitespace_multiple_comments_and_whitespace(void) {
  const char *z = " /*a*/\t/*bcd*/  X";
  /* ' ' = 1, "/*a*/" = 5, '\t' = 1, "/*bcd*/" = 7, '  ' = 2 => total 16 */
  int expected = 16;
  int n = test_getWhitespace(U8STR(z));
  TEST_ASSERT_EQUAL_INT(expected, n);
}

void test_getWhitespace_sql_line_comment_to_eof(void) {
  const char *z = "-- hello";
  /* Entire string is a line comment to EOF. */
  int expected = (int)strlen(z);
  int n = test_getWhitespace(U8STR(z));
  TEST_ASSERT_EQUAL_INT(expected, n);
}

void test_getWhitespace_mixed_leading_ws_and_sql_line_comment_to_eof(void) {
  const char *z = " \t--abc";
  /* Leading space+tab (2) then line comment "--abc" (5) => total 7; entire string */
  int expected = (int)strlen(z);
  int n = test_getWhitespace(U8STR(z));
  TEST_ASSERT_EQUAL_INT(expected, n);
}

void test_getWhitespace_stops_before_non_ws_or_comment_after_comment(void) {
  const char *z = "/*a*/X";
  /* Should count only the comment (5), then stop before 'X' */
  int expected = 5;
  int n = test_getWhitespace(U8STR(z));
  TEST_ASSERT_EQUAL_INT(expected, n);
}

void test_getWhitespace_all_ws_and_comments_entire_string(void) {
  const char *z = " \t/*c*/--d";
  /* space(1) + tab(1) + "/*c*/"(5) + "--d"(3) = 10; entire string */
  int expected = (int)strlen(z);
  int n = test_getWhitespace(U8STR(z));
  TEST_ASSERT_EQUAL_INT(expected, n);
}

int main(void) {
  UNITY_BEGIN();

  RUN_TEST(test_getWhitespace_empty_string);
  RUN_TEST(test_getWhitespace_no_leading_whitespace);
  RUN_TEST(test_getWhitespace_simple_spaces_tabs_newlines);
  RUN_TEST(test_getWhitespace_c_style_comment_only_then_token);
  RUN_TEST(test_getWhitespace_c_style_comment_then_whitespace_then_token);
  RUN_TEST(test_getWhitespace_multiple_comments_and_whitespace);
  RUN_TEST(test_getWhitespace_sql_line_comment_to_eof);
  RUN_TEST(test_getWhitespace_mixed_leading_ws_and_sql_line_comment_to_eof);
  RUN_TEST(test_getWhitespace_stops_before_non_ws_or_comment_after_comment);
  RUN_TEST(test_getWhitespace_all_ws_and_comments_entire_string);

  return UNITY_END();
}