File size: 4,581 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
#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();
} |