ocarina/tests/core/date.c

105 lines
2.6 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_DATA("", "date", 0);
date_today(NULL);
date_set(NULL, 0, 0, 0);
data_file_open(&f, OPEN_WRITE);
date_set(&date, 1988, 6, 17);
g_assert_cmpuint(date.d_year, ==, 1988);
g_assert_cmpuint(date.d_month, ==, 6);
g_assert_cmpuint(date.d_day, ==, 17);
g_assert_cmpuint(date.d_stamp, ==, 285607876);
date_write(&f, &date);
data_file_writef(&f, "\n");
date_today(&date);
g_assert_cmpuint(date.d_year, ==, today->tm_year + 1900);
g_assert_cmpuint(date.d_month, ==, today->tm_mon + 1);
g_assert_cmpuint(date.d_day, ==, today->tm_mday);
date_write(&f, &date);
data_file_writef(&f, "\n");
date_set(&date, 1992, 10, 05);
g_assert_cmpuint(date.d_stamp, ==, 84543432);
date_write_stamp(&f, &date);
file_close(&f);
file_open(&f, OPEN_READ);
date_read(&f, &date);
g_assert_cmpuint(date.d_year, ==, 1988);
g_assert_cmpuint(date.d_month, ==, 6);
g_assert_cmpuint(date.d_day, ==, 17);
g_assert_cmpuint(date.d_stamp, ==, 285607876);
g_assert_cmpstr_free(date_string(&date), ==, "06/17/88");
date_read(&f, &date);
g_assert_cmpuint(date.d_year, ==, today->tm_year + 1900);
g_assert_cmpuint(date.d_month, ==, today->tm_mon + 1);
g_assert_cmpuint(date.d_day, ==, today->tm_mday);
date_read_stamp(&f, &date);
g_assert_cmpuint(date.d_year, ==, 1992);
g_assert_cmpuint(date.d_month, ==, 10);
g_assert_cmpuint(date.d_day, ==, 5);
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. */
g_assert_cmpint(date_compare(&a, &b), ==, 5);
g_assert_cmpint(date_compare(&b, &a), ==, -5);
/* Check month comparison. */
b.d_year = a.d_year;
g_assert_cmpint(date_compare(&a, &b), ==, -6);
g_assert_cmpint(date_compare(&b, &a), ==, 6);
/* Check day comparison. */
b.d_month = a.d_month;
g_assert_cmpint(date_compare(&a, &a), ==, 0);
g_assert_cmpint(date_compare(&a, &b), ==, 7);
g_assert_cmpint(date_compare(&b, &a), ==, -7);
}
int main(int argc, char **argv)
{
setlocale(LC_TIME, "C");
g_test_init(&argc, &argv, NULL);
g_test_add_func("/Core/Date", test_date);
g_test_add_func("/Core/Date/Comparison", test_compare);
return g_test_run();
}