X-Git-Url: http://git.scottworley.com/tl-append/blobdiff_plain/d753115a12c9368e24546c04f992740799da0257..bab98a3a230bcc2161254e5796ec97e1391b4907:/tl-append-test.c diff --git a/tl-append-test.c b/tl-append-test.c index ea42dc9..7e8455d 100644 --- a/tl-append-test.c +++ b/tl-append-test.c @@ -1,10 +1,15 @@ #define _POSIX_C_SOURCE 2 +#define _XOPEN_SOURCE +#include +#include #include #include #include #include #include +#include "common.h" + typedef struct expectation { time_t a, b; const char *message; @@ -22,19 +27,8 @@ static ex_t expectation(time_t a, time_t b, const char *message) { return exp; } -static void die(const char *message) { - fputs(message, stderr); - fputc('\n', stderr); - exit(1); -} - -static void die_err(const char *message) { - perror(message); - exit(1); -} - static void remove_logfile() { - if (remove("tl.log") != 0) { + if (remove(FILENAME) != 0) { if (errno != ENOENT) { die_err("Error removing log file"); } @@ -59,7 +53,7 @@ static ex_t write_to_tl_append(const char *content) { 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"); for (size_t i = 0; !is_end(exps[i]); i++) { @@ -77,6 +71,45 @@ static void verify_log_contents(ex_t exps[]) { die_err("Error closing log file"); } +static void test_encode_time() { + /* localtime_r to set tm's timezone */ + time_t now_time = time(NULL); + struct tm tm; + if (localtime_r(&now_time, &tm) == NULL) + die_err("Can't unpack current time?"); + + const char *strptime_result = strptime("2011-12-13 14:15:16", "%F %T", &tm); + if (strptime_result == NULL || *strptime_result != '\0') + die("Couldn't parse time?"); + time_t tt = mktime(&tm); + if (tt == (time_t)-1) + die_err("Can't pack time?"); + + const char *encoded = encode_time(tt); + /* Loose check to allow for daylight savings time changes between the current + * time and the target time. :( */ + assert(encoded[0] == '2'); + assert(encoded[1] == '0'); + assert(encoded[2] == '1'); + assert(encoded[3] == '1'); + assert(encoded[4] == ' '); + assert(encoded[5] == '1'); + assert(encoded[6] == '2'); + assert(encoded[7] == ' '); + assert(encoded[8] == '1'); + assert(encoded[9] == '3'); + assert(encoded[10] == ' '); + assert(encoded[11] == '1'); + assert(isdigit(encoded[12])); + assert(encoded[13] == ' '); + assert(isdigit(encoded[14])); + assert(isdigit(encoded[15])); + assert(encoded[16] == ' '); + assert(encoded[17] == '1'); + assert(encoded[18] == '6'); + assert(encoded[19] == '\0'); +} + static void write_and_read_line() { remove_logfile(); ex_t e = write_to_tl_append("foo\n"); @@ -91,6 +124,7 @@ static void write_and_read_two_lines() { } int main() { + test_encode_time(); write_and_read_line(); write_and_read_two_lines(); }