-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_char_unit_test.c
290 lines (216 loc) · 5.71 KB
/
remove_char_unit_test.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
/* Specifies the number of unit tests */
#define N_UNIT_TESTS 5
/* The maxium length of the YAML output string */
#define MAX_YAML_LEN 1024
/* The indentation in spaces in the yaml string */
#define INDENT 4
/* File descriptors for stdin and stdout */
#define IN 0
#define OUT 1
/*start_prototype*/
void remove_char(char c, char *str){
/* Your C program goes here */
}
/*end_prototype*/
/* Pipe used to communicate with each forked process */
int pipe_des[2];
/* Used to ditch when something goes wrong */
void quitif(int err){
if (-1 == err){
perror("error");
exit(1);
}
}
/* Used to build a yaml string */
struct yaml_string{
char yaml[MAX_YAML_LEN];
int write;
};
struct yaml_string * create_yaml(){
struct yaml_string * y;
y = calloc(sizeof(struct yaml_string), sizeof(char));
y->write = 0;
return y;
}
void append_line(struct yaml_string * s, char * str, int indent){
int i;
int len = strlen(str);
if (s->write + len > MAX_YAML_LEN)
return;
indent = indent < 0 ? 0 : indent;
for (i = 0 ; i < indent ; i++)
s->yaml[s->write++] = ' ';
for (i = 0 ; i < len ; i++)
s->yaml[s->write++] = str[i];
s->yaml[s->write++] = '\n';
}
void flush_yaml(struct yaml_string * ys){
int i, bytes_written;
bytes_written = write(pipe_des[OUT], ys->yaml, ys->write);
quitif(bytes_written);
for (i = ys->write - 1 ; i ; i--)
ys->yaml[i] = '\0';
ys->write = 0;
}
void print_test_info(struct yaml_string * ys,
char * name,
char * input,
char * expected,
char * points){
char test_name[256];
sprintf(test_name, "%s:", name);
append_line(ys, test_name, 0);
append_line(ys, "input: |", INDENT);
append_line(ys, input, INDENT * 2);
append_line(ys, "expected: |", INDENT);
append_line(ys, expected, INDENT * 2);
append_line(ys, "points: |", INDENT);
append_line(ys, points, INDENT * 2);
flush_yaml(ys);
}
void print_test_output(struct yaml_string * ys, char * str){
append_line(ys, "output: |", INDENT);
append_line(ys, str, INDENT * 2);
flush_yaml(ys);
}
void test_first_char(){
char str[128] = "abcd";
struct yaml_string * ys = create_yaml();
print_test_info(ys, "test_first_char", "a abcd", "bcd", "20");
remove_char('a', str);
str[sizeof(str) - 1] = '\0';
print_test_output(ys, str);
}
void test_last_char(){
char str[128] = "abcde";
struct yaml_string * ys = create_yaml();
print_test_info(ys, "test_last_char", "e abcde", "abcd", "20");
remove_char('e', str);
str[ sizeof(str) - 1] = '\0';
print_test_output(ys, str);
}
void test_3(){
char str[128] = "abcde";
struct yaml_string * ys = create_yaml();
print_test_info(ys, "test_3", "e abcde", "abcd", "20");
remove_char('e', str);
str[ sizeof(str) - 1] = '\0';
print_test_output(ys, str);
}
void test_4(){
char str[128] = "abcde";
struct yaml_string * ys = create_yaml();
print_test_info(ys, "test_4", "e abcde", "abcd", "20");
remove_char('e', str);
str[ sizeof(str) - 1] = '\0';
print_test_output(ys, str);
}
void test_5(){
char str[128] = "abcde";
struct yaml_string * ys = create_yaml();
print_test_info(ys, "test_5", "e abcde", "abcd", "20");
remove_char('e', str);
str[ sizeof(str) - 1] = '\0';
print_test_output(ys, str);
}
void print_results(){
int bytes_read, err;
char buff[MAX_YAML_LEN] = "";
bytes_read = read(pipe_des[IN], buff, MAX_YAML_LEN);
quitif(bytes_read);
err = close(pipe_des[IN]);
quitif(err);
err = write(OUT, buff, bytes_read);
quitif(err);
}
void err_handler(int sig){
int i;
char arith_err[128] = "";
char mem_err[128] = "";
char sys_err[128] = "";
char indent[128] = "";
for (i = 0 ; i < INDENT ; i++)
indent[i] = ' ';
sprintf(arith_err, "%s%s", indent, "error: arithmetic\n");
sprintf(mem_err, "%s%s", indent, "error: memory\n");
sprintf(sys_err, "%s%s", indent, "error: memory\n");
switch (sig){
case SIGFPE:
write(pipe_des[OUT], arith_err, strlen(arith_err));
break;
case SIGBUS:
write(pipe_des[OUT], mem_err, strlen(mem_err));
break;
case SIGSEGV:
write(pipe_des[OUT], mem_err, strlen(mem_err));
break;
case SIGSYS:
write(pipe_des[OUT], sys_err, strlen(sys_err));
break;
case SIGILL:
write(pipe_des[OUT], mem_err, strlen(mem_err));
break;
}
raise(sig);
}
void handle_signals(){
struct sigaction act;
sigemptyset(&act.sa_mask);
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESETHAND;
act.sa_handler = err_handler;
sigaction(SIGFPE, &act, NULL);
sigaction(SIGBUS, &act, NULL);
sigaction(SIGSEGV, &act, NULL);
sigaction(SIGSYS, &act, NULL);
sigaction(SIGILL, &act, NULL);
sigaction(SIGSYS, &act, NULL);
}
void run_tests(){
int err;
pid_t stat;
/* Each of the test functions will be called in its own fork */
void (** unit_test) (void);
void (* test_fns[N_UNIT_TESTS + 1]) (void) =
{test_first_char,
test_last_char,
test_3,
test_4,
test_5, NULL};
for (unit_test = test_fns ; *unit_test ; unit_test++){
err = pipe(pipe_des);
quitif(err);
switch (fork()){
case -1:
quitif(-1);
break;
/* Execute the unit test in the child */
case 0:
close(pipe_des[IN]);
handle_signals();
(*unit_test)();
err = close(pipe_des[OUT]);
quitif(err);
exit(0);
break;
/* Wait for the child to finish the unit test */
default:
close(pipe_des[OUT]);
err = wait(&stat);
quitif(err);
print_results();
break;
}
}
}
int main(){
run_tests();
return 0;
}