]>
git.scottworley.com Git - tl-append/blob - tl-append-test.c
1 #define _POSIX_C_SOURCE 2
7 static void die(const char *message
) {
8 fputs(message
, stderr
);
13 static void die_err(const char *message
) {
18 static void remove_logfile() {
19 if (remove("tl.log") != 0) {
20 if (errno
!= ENOENT
) {
21 die_err("Error removing log file");
26 static void write_to_tl_append(const char *content
) {
27 FILE *p
= popen("./tl-append", "w");
29 die_err("Couldn't run tl-append");
30 if (fputs(content
, p
) == EOF
)
31 die("Couldn't write to pipe");
32 int status
= pclose(p
);
34 die_err("Error closing pipe");
36 die("tl-append exited abnormally");
39 static void verify_log_contents(const char *contents
) {
40 size_t len
= strlen(contents
);
41 char *buf
= (char *)malloc(len
+ 2);
43 FILE *f
= fopen("tl.log", "r");
45 die_err("Error opening log file");
46 buf
[fread(buf
, 1, len
+ 1, f
)] = '\0';
48 die("Error reading log file");
49 if (strncmp(contents
, buf
, len
+ 1) != 0)
50 die("Wrong contents in log file");
52 die_err("Error closing log file");
56 static void write_and_read_line() {
58 write_to_tl_append("foo\n");
59 verify_log_contents("foo\n");
62 static void write_and_read_two_lines() {
64 write_to_tl_append("foo\n");
65 write_to_tl_append("bar\n");
66 verify_log_contents("foo\nbar\n");
70 write_and_read_line();
71 write_and_read_two_lines();