123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- #include <check.h>
- #include <stdio.h>
- #include <libgen.h>
- #include <time.h>
-
- #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; i<sizeof(fmts)/sizeof(char*);i++)
- {
- r = _ttail_set_date_relative(ttail, fmts[i], 0);
- ck_assert_printf(r == -1, "Returns %d with '%s' on date 0",
- r, fmts[i]);
- r = _ttail_set_date_relative(ttail, fmts[i], 1);
- ck_assert_printf(r == -1, "Returns %d with '%s' on date 1",
- r, fmts[i]);
- }
- }
- END_TEST
-
- /* Test for sec, min, hour, day formats. Comparing with tm_diff */
- START_TEST (fmt_sec)
- {
- struct check_relative_date_s datas[] = {
- {"#-10s", 10}, {"#-142sec", 142}, {"#-4096s", 4096},
- {"#-1m", 60}, {"#-10min", 600},
- {"#-1h", 3600}, {"#-10h", 36000},
- {"#-1d", 3600*24}, {"#-2day", 3600*24*2},
- };
-
- int r, i, c;
- double d;
- struct tm* tm;
- for(i=0; i<sizeof(datas) / (sizeof(char*)+sizeof(double)); i++)
- {
- for(c=0;c<2;c++)
- {
- tm = c?&(ttail->date_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; i<sizeof(fmts) / sizeof(char*); i++)
- {
- for(c=0; c<2; c++)
- {
- tm = c?&(ttail->date_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; i<sizeof(datas) / (sizeof(char*)+sizeof(double)); i++)
- {
- for(c=0;c<2;c++)
- {
- tm = c?&(ttail->date_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
|