-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_functions.c
More file actions
176 lines (131 loc) · 3.61 KB
/
test_functions.c
File metadata and controls
176 lines (131 loc) · 3.61 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <sys/types.h>
#include <netinet/in.h>
#include <dirent.h>
#include <stddef.h>
#include <sys/utsname.h>
#include "CuTest.h"
char LS = '1';
char FILES_NUMBER = '2';
char OS_INFO = '3';
char message[1024] = "1Non exisnant folder";
char message2[1024] = "2/home/travis";
char message3[1024] = "3";
char buf[1024];
/*------------------------------------------------------------------------
-------------------------------MOCK FUNCTIONS---------------------------*/
const char * getLS(char * path) {
int length = 0;
DIR * d;
struct dirent * dir;
char * res = (char *)malloc(1024);
d = opendir(path);
if (d) {
while ((dir = readdir(d)) != NULL) {
if (!strcmp(dir->d_name, "."))
continue;
if (!strcmp(dir->d_name, ".."))
continue;
if (!dir->d_type == DT_REG || !dir->d_type == DT_DIR)
continue;
strcat(res, dir->d_name);
strcat(res, "\n");
}
}
else {
strcat(res, "There is no file or directory:\n");
strcat(res, path);
}
closedir(d);
return res;
}
const char * getFilesNumber(char * path) {
int count = 0;
char append[5];
DIR * d;
struct dirent * dir;
char * res = (char *)malloc(256);
d = opendir(path);
if (d) {
while ((dir = readdir(d)) != NULL) {
if (dir->d_type == DT_REG) {
count++;
printf("%s\n", dir->d_name);
}
}
strcat(res, "Files in directory:\n");
strcat(res, path);
strcat(res, "\n");
sprintf(append, "%d", count);
strcat(res, append);
}
else {
strcat(res, "There is no file or directory:\n");
strcat(res, path);
}
closedir(d);
return res;
}
const char * getOSInfo() {
char * res = (char *)malloc(256);
struct utsname sysinfo;
uname(&sysinfo);
strcat(res, "System name:\n");
strcat(res, sysinfo.sysname);
return res;
}
char * manager(char * buf) {
char task = '0';
task = buf[0];
buf++;
if (task == LS)
return getLS(buf);
if (task == FILES_NUMBER)
return getFilesNumber(buf);
if (task == OS_INFO)
return getOSInfo();
}
/*----------------------------------------------------------------------
-----------------------------------------------------------------------*/
/*------------------------------------------------------------------------
-------------------------------TESTS------------------------------------*/
void should_throw_error_msg (CuTest* testContext) {
memset(buf, 0, sizeof buf);
char *returned_str = manager(message);
CuAssertStrEquals(testContext, "There is no file or directory:\nNon exisnant folder", returned_str);
}
void should_get_files_number (CuTest* testContext) {
memset(buf, 0, sizeof buf);
char *returned_str = manager(message2);
CuAssertStrEquals(testContext, "Files in directory:\n/home/travis\n18", returned_str);
}
void should_get_system_info (CuTest* testContext) {
memset(buf, 0, sizeof buf);
char *returned_str = manager(message3);
CuAssertStrEquals(testContext, "System name:\nLinux", returned_str);
}
CuSuite* test_suite() {
CuSuite* suite = CuSuiteNew();
SUITE_ADD_TEST(suite, should_throw_error_msg);
SUITE_ADD_TEST(suite, should_get_files_number);
SUITE_ADD_TEST(suite, should_get_system_info);
return suite;
}
int all_tests()
{
CuString *output = CuStringNew();
CuSuite* suite = CuSuiteNew();
CuSuiteAddSuite(suite, test_suite());
CuSuiteRun(suite);
CuSuiteSummary(suite, output);
CuSuiteDetails(suite, output);
printf("%s\n", output->buffer);
CuStringDelete(output);
CuSuiteDelete(suite);
return suite->failCount;
}
/*----------------------------------------------------------------------
------------------------------------------------------------------------*/
int main(void)
{
return all_tests();
}