#include #include #include #include #include "ttail_check.h" #include "ttail.h" #include "ttail_init.h" #include "ttail_search.h" struct tm *now; struct check_relative_date_s { char * f; double d; }; void setup_date_relative() { time_t t; if(time(&t) < 0) { fprintf(stderr, "CHECK FAIL : Unable to retrieve time using time()\n"); exit(1); } if(!(now = localtime(&t))) { fprintf(stderr, "CHECK FAIL : Unable to retreive localtime()\n"); exit(1); } setup_ttail_empty(); } double tm_diff(struct tm *tm1, struct tm *tm2) { time_t t1,t2; t1 = mktime(tm1); t2 = mktime(tm2); return difftime(t1,t2); } START_TEST (wrong_formats) { int r,i; char *fmts[] = { "foobar", "%d", "#-1f", "#-1dour", "#-h", "-1h", "#-1h3m", "#--1hour", "ab1h", " 1h", "123h" }; for(i=0; idate_max):&(ttail->date_min); ttail_tm_init(tm); r = _ttail_set_date_relative(ttail, datas[i].f, c); ck_assert_int_eq(r, 0); d = tm_diff(now, tm); ck_assert_printf( d == datas[i].d, "Failed to set relative date for %s '%s' : \ expected diff was %0.0f but %0.0f was found", c?"date_max":"date_min", datas[i].f, datas[i].d, d); } } } END_TEST START_TEST (fmt_mon) { char *fmts[] = { "#-1M", "#-1Month"}; int r, i, c; struct tm* tm; for(i=0; idate_max):&(ttail->date_min); ttail_tm_init(tm); r = _ttail_set_date_relative(ttail, "#-1M", c); ck_assert_int_eq(r, 0); ck_assert_int_eq(tm->tm_sec, now->tm_sec); ck_assert_int_eq(tm->tm_min, now->tm_min); ck_assert_int_eq(tm->tm_hour, now->tm_hour); ck_assert_int_eq(tm->tm_mday, now->tm_mday); if(now->tm_mon != 0) { ck_assert_int_eq(tm->tm_mon, now->tm_mon - 1); ck_assert_int_eq(tm->tm_year, now->tm_year); } else { ck_assert_int_eq(tm->tm_mon, 11); ck_assert_int_eq(tm->tm_year, now->tm_year - 1); } } } } END_TEST START_TEST (fmt_year) { int r, i, c; struct tm *tm; struct check_relative_date_s datas[] = { {"#-1y", 1} , {"#-10year", 10}}; for(i=0; idate_max):&(ttail->date_min); ttail_tm_init(tm); r = _ttail_set_date_relative(ttail, datas[i].f, c); ck_assert_int_eq(r, 0); ck_assert_int_eq(tm->tm_sec, now->tm_sec); ck_assert_int_eq(tm->tm_min, now->tm_min); ck_assert_int_eq(tm->tm_hour, now->tm_hour); ck_assert_int_eq(tm->tm_mday, now->tm_mday); ck_assert_int_eq(tm->tm_mon, now->tm_mon); ck_assert_printf( now->tm_year - tm->tm_year == datas[i].d, "Failed for %s with format '%s' : found %d \ year of difference instead of %d\n", c?"date_max":"date_min", datas[i].f, now->tm_year - tm->tm_year, (int)datas[i].d); } } } END_TEST TTAIL_CHECK_START("ttail relative date checks", "ttail_set_date_relative() checks") TTAIL_SET_FIXTURE(setup_date_relative, teardown_ttail); TTAIL_ADD_TEST(wrong_formats); TTAIL_ADD_TEST(fmt_sec); TTAIL_ADD_TEST(fmt_mon); TTAIL_ADD_TEST(fmt_year); TTAIL_CHECK_END