File size: 4,257 Bytes
78d2150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "../../unity/unity.h"
#include <string.h>
#include <stdbool.h>

/* Unity required hooks */
void setUp(void) {
  /* Setup code here, or leave empty */
}
void tearDown(void) {
  /* Cleanup code here, or leave empty */
}

/* Helper to create a WORD with given C string literal */
static WORD make_word_from_cstr(const char *s) {
  WORD w;
  memset(&w, 0, sizeof(w));
  w.text = s;
  w.length = (int)strlen(s);
  return w;
}

/* Simple word: no opening punctuation, no trailing punctuation */
static void test_check_punctuation_plain_word(void) {
  WORD w = make_word_from_cstr("hello");
  check_punctuation(&w);
  TEST_ASSERT_FALSE(w.paren);
  TEST_ASSERT_FALSE(w.punct);
  TEST_ASSERT_FALSE(w.period);
}

/* Starts with open quote and ends with a close quote; not a sentence end */
static void test_check_punctuation_start_quote_end_quote(void) {
  WORD w = make_word_from_cstr("\"hello\"");
  check_punctuation(&w);
  TEST_ASSERT_TRUE(w.paren);   /* starts with an opener '"' */
  TEST_ASSERT_TRUE(w.punct);   /* last char is '"' which is punctuation */
  TEST_ASSERT_FALSE(w.period); /* last non-close char is 'o' */
}

/* Ends with a period (simple sentence end) */
static void test_check_punctuation_end_period(void) {
  WORD w = make_word_from_cstr("end.");
  check_punctuation(&w);
  TEST_ASSERT_FALSE(w.paren);
  TEST_ASSERT_TRUE(w.punct);
  TEST_ASSERT_TRUE(w.period);
}

/* Sentence punctuation before a trailing closer: should still count as period */
static void test_check_punctuation_end_period_then_close_paren(void) {
  WORD w = make_word_from_cstr("done.)");
  check_punctuation(&w);
  TEST_ASSERT_FALSE(w.paren);
  TEST_ASSERT_TRUE(w.punct);  /* last char ')' is punctuation */
  TEST_ASSERT_TRUE(w.period); /* '.' before ')' */
}

/* Exclamation before closing quote: period detection must ignore closer */
static void test_check_punctuation_exclaim_then_close_quote(void) {
  WORD w = make_word_from_cstr("Wow!'");
  check_punctuation(&w);
  TEST_ASSERT_FALSE(w.paren);
  TEST_ASSERT_TRUE(w.punct);  /* last char '\'' is punctuation */
  TEST_ASSERT_TRUE(w.period); /* '!' before closer */
}

/* Trailing closer without sentence punctuation: not a period-ending word */
static void test_check_punctuation_trailing_closer_no_period(void) {
  WORD w = make_word_from_cstr("word)");
  check_punctuation(&w);
  TEST_ASSERT_FALSE(w.paren);
  TEST_ASSERT_TRUE(w.punct);  /* ')' is punctuation at end */
  TEST_ASSERT_FALSE(w.period);
}

/* Single-character opener */
static void test_check_punctuation_single_open_paren(void) {
  WORD w = make_word_from_cstr("(");
  check_punctuation(&w);
  TEST_ASSERT_TRUE(w.paren);
  TEST_ASSERT_TRUE(w.punct);
  TEST_ASSERT_FALSE(w.period);
}

/* Single-character period */
static void test_check_punctuation_single_period(void) {
  WORD w = make_word_from_cstr(".");
  check_punctuation(&w);
  TEST_ASSERT_FALSE(w.paren);
  TEST_ASSERT_TRUE(w.punct);
  TEST_ASSERT_TRUE(w.period);
}

/* Non-sentence-ending punctuation (comma) */
static void test_check_punctuation_nonperiod_punctuation_comma(void) {
  WORD w = make_word_from_cstr("a,");
  check_punctuation(&w);
  TEST_ASSERT_FALSE(w.paren);
  TEST_ASSERT_TRUE(w.punct);
  TEST_ASSERT_FALSE(w.period);
}

/* Starts with opener and has sentence punctuation before multiple closers */
static void test_check_punctuation_opener_and_punct_inside_closers(void) {
  WORD w = make_word_from_cstr("('Hi!')");
  check_punctuation(&w);
  TEST_ASSERT_TRUE(w.paren);   /* starts with '(' */
  TEST_ASSERT_TRUE(w.punct);   /* ends with ')' which is punctuation */
  TEST_ASSERT_TRUE(w.period);  /* '!' before the closers */
}

int main(void) {
  UNITY_BEGIN();
  RUN_TEST(test_check_punctuation_plain_word);
  RUN_TEST(test_check_punctuation_start_quote_end_quote);
  RUN_TEST(test_check_punctuation_end_period);
  RUN_TEST(test_check_punctuation_end_period_then_close_paren);
  RUN_TEST(test_check_punctuation_exclaim_then_close_quote);
  RUN_TEST(test_check_punctuation_trailing_closer_no_period);
  RUN_TEST(test_check_punctuation_single_open_paren);
  RUN_TEST(test_check_punctuation_single_period);
  RUN_TEST(test_check_punctuation_nonperiod_punctuation_comma);
  RUN_TEST(test_check_punctuation_opener_and_punct_inside_closers);
  return UNITY_END();
}