X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=t%2Funit%2Fcore%2Ftime_test.c;h=978fc02d6f99eb47636391e975520489f3fbfe9b;hb=90174606fdfac1dba1330b48cb16bb2ca1efb0a4;hp=19b2cc081e4027665ef9310b06d6df1d148a2409;hpb=f353d8fe20a02ea9b4700371e94f82da33d7beab;p=sysdb.git diff --git a/t/unit/core/time_test.c b/t/unit/core/time_test.c index 19b2cc0..978fc02 100644 --- a/t/unit/core/time_test.c +++ b/t/unit/core/time_test.c @@ -25,8 +25,12 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#if HAVE_CONFIG_H +# include "config.h" +#endif + #include "core/time.h" -#include "libsysdb_test.h" +#include "testutils.h" #include @@ -40,6 +44,51 @@ #define US 1000L #define NS 1L +struct { + sdb_time_t t; + const char *tz; + const char *expected; +} strftime_data[] = { + { 0, "UTC", "1970-01-01 00:00:00 +0000" }, + { 1428066243000000000L, "Europe/Berlin", "2015-04-03 15:04:03 +0200" }, + { 1420113661000000000L, "Europe/Berlin", "2015-01-01 13:01:01 +0100" }, + { 1428066243000000000L, "US/Pacific", "2015-04-03 06:04:03 -0700" }, + { 1420113661000000000L, "US/Pacific", "2015-01-01 04:01:01 -0800" }, + { 1146747723000000123L, "UTC", "2006-05-04 13:02:03.000000123 +0000" }, + { 1146747723123456789L, "UTC", "2006-05-04 13:02:03.123456789 +0000" }, +}; + +START_TEST(test_strftime) +{ + char buf[1024], tz[64]; + size_t check; + + /* strftime does not provide the number of bytes that would have been + * written. Only check that it does not segfault. */ + sdb_strftime(NULL, 0, strftime_data[_i].t); + + snprintf(tz, sizeof(tz), "TZ=%s", strftime_data[_i].tz); + putenv(tz); + tzset(); + + check = sdb_strftime(buf, sizeof(buf), strftime_data[_i].t); + fail_unless(check > 0, + "%s; sdb_strftime(, , %"PRIsdbTIME") = %zu; " + "expected: >0", tz, strftime_data[_i].t, check); + fail_unless(!strcmp(buf, strftime_data[_i].expected), + "%s; sdb_strftime(, , %"PRIsdbTIME") did not " + "format time correctly; got: '%s'; expected: '%s'", + tz, strftime_data[_i].t, buf, strftime_data[_i].expected); + fail_unless(check == strlen(strftime_data[_i].expected), + "%s; sdb_strftime(, , %"PRIsdbTIME") = %zu; " + "expected: %zu", tz, strftime_data[_i].t, check, + strlen(strftime_data[_i].expected)); + + putenv("TZ=UTC"); + tzset(); +} +END_TEST + struct { sdb_time_t interval; const char *expected; @@ -123,19 +172,15 @@ START_TEST(test_strpunit) } END_TEST -Suite * -core_time_suite(void) +TEST_MAIN("core::time") { - Suite *s = suite_create("core::time"); - TCase *tc; - - tc = tcase_create("core"); + TCase *tc = tcase_create("core"); + TC_ADD_LOOP_TEST(tc, strftime); TC_ADD_LOOP_TEST(tc, strfinterval); TC_ADD_LOOP_TEST(tc, strpunit); - suite_add_tcase(s, tc); - - return s; -} /* core_time_suite */ + ADD_TCASE(tc); +} +TEST_MAIN_END /* vim: set tw=78 sw=4 ts=4 noexpandtab : */