Created
June 9, 2011 18:30
-
-
Save flsafe/1017397 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <sys/wait.h> | |
#include <ctype.h> | |
#define N_UNIT_TESTS 1 | |
#define MAX_YAML_LEN 1024 | |
#define INDENT 4 | |
#define IN 0 | |
#define OUT 1 | |
/* Pipe used to communicate with each forked process */ | |
int file_des[2]; | |
struct yaml_string{ | |
char yaml[MAX_YAML_LEN]; | |
int write; | |
}; | |
struct yaml_string * create_yaml(){ | |
struct yaml_string * y; | |
y = malloc(sizeof(struct yaml_string)); | |
y->write = 0; | |
return y; | |
} | |
void append_line(struct yaml_string * s, char * str, int indent){ | |
/* TODO: Add error checking. Make sure we don't write past | |
* the yaml buffer */ | |
int len = strlen(str); | |
int i; | |
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 print_now_before_crash(struct yaml_string * ys, | |
char * input, | |
char * expected, | |
char * points){ | |
/* TODO: Flush the buffer incase the | |
* unit test crashes */ | |
append_line(ys, "test_first_char: |", 0); | |
append_line(ys, "input: |", INDENT); | |
append_line(ys, "a abcd", INDENT * 2); | |
append_line(ys, "expected: |", INDENT); | |
append_line(ys, "bcd", INDENT * 2); | |
append_line(ys, "points: |", INDENT); | |
append_line(ys, "20", INDENT * 2); | |
write(file_des[OUT], ys->yaml, MAX_YAML_LEN); | |
} | |
void test_first_char(){ | |
/* TODO: Write the test case */ | |
struct yaml_string * ys = create_yaml(); | |
print_now_before_crash(ys, "a abcd", "bcd", "20"); | |
} | |
void collect_results(){ | |
/* TODO: Collect the yaml strings from each unit test */ | |
char buff[1024]; | |
read(file_des[IN], buff, 1024); | |
close(file_des[IN]); | |
printf("%s", buff); | |
} | |
void run_tests(){ | |
/* We'll wait for these processes to finish running thier unit test */ | |
pid_t child_pid; | |
int curr_fork, status; | |
/* Each of the test functions will be called in its own fork */ | |
void (*test_fn[N_UNIT_TESTS]) (void) = {test_first_char}; | |
for (curr_fork = 0 ; curr_fork < N_UNIT_TESTS ; curr_fork++){ | |
pipe(file_des); | |
switch (child_pid = fork()){ | |
case -1: | |
break; | |
case 0: | |
close(file_des[IN]); | |
test_fn[curr_fork](); | |
close(file_des[OUT]); | |
exit(0); | |
break; | |
default: | |
close(file_des[OUT]); | |
waitpid(child_pid, &status, 0); | |
collect_results(); | |
break; | |
} | |
} | |
} | |
int main(){ | |
run_tests(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment