#include #include #include #include #include void occurrences(char * phrase); static char buffer[256]; static int redirect_stdout(void) { fflush(stdout); freopen("/dev/null", "a", stdout); setvbuf(stdout, buffer, _IOFBF, sizeof(buffer)); return 0; } static int reset_stdout(void) { freopen("/dev/tty", "a", stdout); return 0; } void test_empty_string(void) { redirect_stdout(); occurrences(""); reset_stdout(); CU_ASSERT_STRING_EQUAL(buffer, "The number of characters is 0, the number of digits is 0, and the number of vowels is 0\n"); } void test_only_vowels(void) { redirect_stdout(); occurrences("aeiou"); reset_stdout(); CU_ASSERT_STRING_EQUAL(buffer, "The number of characters is 5, the number of digits is 0, and the number of vowels is 5\n"); } void test_only_digits(void) { redirect_stdout(); occurrences("12345"); reset_stdout(); CU_ASSERT_STRING_EQUAL(buffer, "The number of characters is 5, the number of digits is 5, and the number of vowels is 0\n"); } void test_mixed_input(void) { redirect_stdout(); occurrences("Hello123World"); reset_stdout(); CU_ASSERT_STRING_EQUAL(buffer, "The number of characters is 13, the number of digits is 3, and the number of vowels is 3\n"); } void test_spaces(void) { redirect_stdout(); occurrences("Hello 123World"); reset_stdout(); CU_ASSERT_STRING_EQUAL(buffer, "The number of characters is 14, the number of digits is 3, and the number of vowels is 3\n"); } void test_special_characters(void) { redirect_stdout(); occurrences("Hello&[{}(=*)+]!#/@special"); reset_stdout(); CU_ASSERT_STRING_EQUAL(buffer, "The number of characters is 26, the number of digits is 0, and the number of vowels is 5\n"); } void test_only_spaces_and_digits(void) { redirect_stdout(); occurrences(" 1 2 3 4 5 "); reset_stdout(); CU_ASSERT_STRING_EQUAL(buffer, "The number of characters is 11, the number of digits is 5, and the number of vowels is 0\n"); } int main() { CU_initialize_registry(); CU_pSuite suite = CU_add_suite("OccurrencesTestSuite", NULL, NULL); CU_add_test(suite, "test of empty string", test_empty_string); CU_add_test(suite, "test of only vowels", test_only_vowels); CU_add_test(suite, "test of only digits", test_only_digits); CU_add_test(suite, "test of mixed input", test_mixed_input); CU_add_test(suite, "test of spaces", test_spaces); CU_add_test(suite, "test of special characters", test_special_characters); CU_add_test(suite, "test of only spaces and digits", test_only_spaces_and_digits); CU_basic_run_tests(); CU_cleanup_registry(); return CU_get_error(); }