summaryrefslogtreecommitdiff
path: root/test_occurrences.c
blob: b11818e031b95b194e3836fd8a86034c5f154256 (plain)
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
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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();
}