#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <time.h>
-static void die(const char *message) {
- fputs(message, stderr);
- fputc('\n', stderr);
- exit(1);
-}
+#include "common.h"
+
+typedef struct expectation {
+ time_t a, b;
+ const char *message;
+} ex_t;
-static void die_err(const char *message) {
- perror(message);
- exit(1);
+const ex_t END = {((time_t)-1), ((time_t)-1), NULL};
+static int is_end(ex_t exp) {
+ return exp.a == END.a && exp.b == END.b && exp.message == END.message;
+}
+static ex_t expectation(time_t a, time_t b, const char *message) {
+ ex_t exp;
+ exp.a = a;
+ exp.b = b;
+ exp.message = message;
+ return exp;
}
static void remove_logfile() {
- if (remove("tl.log") != 0) {
+ if (remove(FILENAME) != 0) {
if (errno != ENOENT) {
die_err("Error removing log file");
}
}
}
-static void write_to_tl_append(const char *content) {
+static ex_t write_to_tl_append(const char *content) {
FILE *p = popen("./tl-append", "w");
if (p == NULL)
die_err("Couldn't run tl-append");
+ time_t start = time(NULL);
if (fputs(content, p) == EOF)
die("Couldn't write to pipe");
int status = pclose(p);
+ time_t end = time(NULL);
if (status < 0)
die_err("Error closing pipe");
if (status != 0)
die("tl-append exited abnormally");
+ return expectation(start, end, content);
}
-static void verify_log_contents(const char *contents) {
- size_t len = strlen(contents);
- char *buf = (char *)malloc(len + 2);
+static void verify_log_contents(ex_t exps[]) {
- FILE *f = fopen("tl.log", "r");
+ FILE *f = fopen(FILENAME, "r");
if (f == NULL)
die_err("Error opening log file");
- buf[fread(buf, 1, len + 1, f)] = '\0';
- if (ferror(f))
- die("Error reading log file");
- if (strncmp(contents, buf, len + 1) != 0)
- die("Wrong contents in log file");
+ for (size_t i = 0; !is_end(exps[i]); i++) {
+ size_t len = strlen(exps[i].message);
+ char *buf = (char *)malloc(len + 2);
+ if (fgets(buf, len + 1, f) == NULL)
+ die("Error reading log file");
+ if (ferror(f))
+ die("Error reading log file");
+ if (strncmp(exps[i].message, buf, len + 1) != 0)
+ die("Wrong contents in log file");
+ free(buf);
+ }
if (fclose(f) != 0)
die_err("Error closing log file");
- free(buf);
}
static void write_and_read_line() {
remove_logfile();
- write_to_tl_append("foo\n");
- verify_log_contents("foo\n");
+ ex_t e = write_to_tl_append("foo\n");
+ verify_log_contents((ex_t[]){e, END});
}
static void write_and_read_two_lines() {
remove_logfile();
- write_to_tl_append("foo\n");
- write_to_tl_append("bar\n");
- verify_log_contents("foo\nbar\n");
+ ex_t e1 = write_to_tl_append("foo\n");
+ ex_t e2 = write_to_tl_append("bar\n");
+ verify_log_contents((ex_t[]){e1, e2, END});
}
int main() {