ocarina/tests/core/date.c

93 lines
1.9 KiB
C

/*
* Copyright 2015 (c) Anna Schumaker.
*/
#define _GNU_SOURCE
#include <core/date.h>
#include <tests/test.h>
#include <locale.h>
#include <time.h>
void test_date()
{
time_t rawtime = time(NULL);
struct tm *today = localtime(&rawtime);
struct date date = {
.d_year = 0,
.d_month = 0,
.d_day = 0,
};
struct file f = FILE_INIT("date", 0, 0);
gchar *str;
date_today(NULL);
date_set(NULL, 0, 0, 0);
setlocale(LC_TIME, "C");
file_open(&f, OPEN_WRITE);
date_set(&date, 1988, 6, 17);
test_equal(date.d_year, 1988);
test_equal(date.d_month, 6);
test_equal(date.d_day, 17);
date_write(&f, &date);
file_writef(&f, "\n");
date_today(&date);
test_equal(date.d_year, today->tm_year + 1900);
test_equal(date.d_month, today->tm_mon + 1);
test_equal(date.d_day, today->tm_mday);
date_write(&f, &date);
file_close(&f);
file_open(&f, OPEN_READ);
date_read(&f, &date);
test_equal(date.d_year, 1988);
test_equal(date.d_month, 6);
test_equal(date.d_day, 17);
str = date_string(&date);
test_equal(str, "06/17/88");
g_free(str);
date_read(&f, &date);
test_equal(date.d_year, today->tm_year + 1900);
test_equal(date.d_month, today->tm_mon + 1);
test_equal(date.d_day, today->tm_mday);
file_close(&f);
}
void test_compare()
{
struct date a = {
.d_year = 2015,
.d_month = 6,
.d_day = 17,
};
struct date b = {
.d_year = 2010,
.d_month = 12,
.d_day = 10,
};
/* Check year comparison. */
test_equal(date_compare(&a, &b), 5);
test_equal(date_compare(&b, &a), -5);
/* Check month comparison. */
b.d_year = a.d_year;
test_equal(date_compare(&a, &b), -6);
test_equal(date_compare(&b, &a), 6);
/* Check day comparison. */
b.d_month = a.d_month;
test_equal(date_compare(&a, &a), 0);
test_equal(date_compare(&a, &b), 7);
test_equal(date_compare(&b, &a), -7);
}
DECLARE_UNIT_TESTS(
UNIT_TEST("Date", test_date),
UNIT_TEST("Date Comparison", test_compare),
);