coreutils / tests /fmt /tests_for_check_punctuation.c
AryaWu's picture
Upload folder using huggingface_hub
78d2150 verified
#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();
}