coreutils / tests /basename /tests_for_remove_suffix.c
AryaWu's picture
Upload folder using huggingface_hub
78d2150 verified
#include "../../unity/unity.h"
#include <string.h>
#include <stdlib.h>
/* Unity required hooks */
void setUp(void) {
/* No setup needed */
}
void tearDown(void) {
/* No teardown needed */
}
/* Tests for static void remove_suffix (char *name, char const *suffix) */
/* Basic removal when suffix matches exactly at end */
void test_remove_suffix_basic_removal(void) {
char name[32];
strcpy(name, "foo.txt");
remove_suffix(name, ".txt");
TEST_ASSERT_EQUAL_STRING("foo", name);
}
/* No removal when suffix does not match at end */
void test_remove_suffix_no_match(void) {
char name[32];
strcpy(name, "foo.txt");
remove_suffix(name, ".log");
TEST_ASSERT_EQUAL_STRING("foo.txt", name);
}
/* No removal when name equals suffix entirely */
void test_remove_suffix_exact_match_no_removal(void) {
char name[16];
strcpy(name, "abc");
remove_suffix(name, "abc");
TEST_ASSERT_EQUAL_STRING("abc", name);
}
/* No removal when suffix is longer than name */
void test_remove_suffix_suffix_longer_than_name(void) {
char name[16];
strcpy(name, "abc");
remove_suffix(name, "zabcd");
TEST_ASSERT_EQUAL_STRING("abc", name);
}
/* Empty suffix should not change the name */
void test_remove_suffix_empty_suffix_no_change(void) {
char name[16];
strcpy(name, "abc");
remove_suffix(name, "");
TEST_ASSERT_EQUAL_STRING("abc", name);
}
/* Empty name should remain empty regardless of suffix */
void test_remove_suffix_empty_name(void) {
char name[4];
strcpy(name, "");
remove_suffix(name, "abc");
TEST_ASSERT_EQUAL_STRING("", name);
}
/* Case sensitivity: must not remove when case differs */
void test_remove_suffix_case_sensitivity(void) {
char name[32];
strcpy(name, "File.TXT");
remove_suffix(name, ".txt");
TEST_ASSERT_EQUAL_STRING("File.TXT", name);
}
/* Remove only the trailing occurrence of a shorter suffix */
void test_remove_suffix_remove_only_trailing_occurrence(void) {
char name[32];
strcpy(name, "foobarbar");
remove_suffix(name, "bar");
TEST_ASSERT_EQUAL_STRING("foobar", name);
}
/* Remove a longer multi-character suffix that matches at end */
void test_remove_suffix_longer_suffix_removal(void) {
char name[32];
strcpy(name, "foobarbar");
remove_suffix(name, "barbar");
TEST_ASSERT_EQUAL_STRING("foo", name);
}
/* Partial match from end that fails earlier must not modify */
void test_remove_suffix_partial_match_then_mismatch_should_not_modify(void) {
char name[32];
strcpy(name, "abcdXY");
remove_suffix(name, "cXY"); /* Ends with dXY not cXY */
TEST_ASSERT_EQUAL_STRING("abcdXY", name);
}
/* Suffix matches all but the first character: leaves a single-char name */
void test_remove_suffix_leave_single_char(void) {
char name[32];
strcpy(name, "abcdef");
remove_suffix(name, "bcdef");
TEST_ASSERT_EQUAL_STRING("a", name);
}
/* Suffix longer than name but ending matches the whole name: no removal */
void test_remove_suffix_suffix_longer_but_tail_matches_name(void) {
char name[8];
strcpy(name, "ab");
remove_suffix(name, "cab");
TEST_ASSERT_EQUAL_STRING("ab", name);
}
/* UTF-8 content: treated as bytes, suffix removal still works */
void test_remove_suffix_utf8_bytes(void) {
char name[64];
strcpy(name, "grün.txt"); /* 'ü' is multibyte in UTF-8 */
remove_suffix(name, ".txt");
TEST_ASSERT_EQUAL_STRING("grün", name);
/* Also ensure non-ASCII mismatch behaves as expected */
strcpy(name, "naïve.TXT");
remove_suffix(name, ".txt"); /* case mismatch */
TEST_ASSERT_EQUAL_STRING("naïve.TXT", name);
}
/* Exact-length match should not remove (protect from emptying) */
void test_remove_suffix_exact_length_protection(void) {
char name[16];
strcpy(name, "aa");
remove_suffix(name, "aa");
TEST_ASSERT_EQUAL_STRING("aa", name);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_remove_suffix_basic_removal);
RUN_TEST(test_remove_suffix_no_match);
RUN_TEST(test_remove_suffix_exact_match_no_removal);
RUN_TEST(test_remove_suffix_suffix_longer_than_name);
RUN_TEST(test_remove_suffix_empty_suffix_no_change);
RUN_TEST(test_remove_suffix_empty_name);
RUN_TEST(test_remove_suffix_case_sensitivity);
RUN_TEST(test_remove_suffix_remove_only_trailing_occurrence);
RUN_TEST(test_remove_suffix_longer_suffix_removal);
RUN_TEST(test_remove_suffix_partial_match_then_mismatch_should_not_modify);
RUN_TEST(test_remove_suffix_leave_single_char);
RUN_TEST(test_remove_suffix_suffix_longer_but_tail_matches_name);
RUN_TEST(test_remove_suffix_utf8_bytes);
RUN_TEST(test_remove_suffix_exact_length_protection);
return UNITY_END();
}