diff --git a/tests/gswe-timestamp-test.c b/tests/gswe-timestamp-test.c index 2d79605..cf70786 100644 --- a/tests/gswe-timestamp-test.c +++ b/tests/gswe-timestamp-test.c @@ -4,13 +4,20 @@ #include #include +#include "test-asserts.h" + static void test_timestamp_jd(void) { GsweTimestamp *timestamp; - gdouble jd; + gdouble jd, + seconds; - timestamp = gswe_timestamp_new_from_gregorian_full(1983, 3, 7, 11, 54, 45, 0, 1.0); + timestamp = gswe_timestamp_new_from_gregorian_full( + 1983, 3, 7, + 11, 54, 45, 0, + 1.0 + ); g_assert(timestamp); jd = gswe_timestamp_get_julian_day_et(timestamp, NULL); @@ -19,16 +26,44 @@ test_timestamp_jd(void) timestamp = gswe_timestamp_new_from_julian_day(jd); g_assert(timestamp); - g_assert(gswe_timestamp_get_gregorian_timezone(timestamp) == 1.0); - g_assert(gswe_timestamp_get_gregorian_year(timestamp, NULL) == 1983); - g_assert(gswe_timestamp_get_gregorian_month(timestamp, NULL) == 3); - g_assert(gswe_timestamp_get_gregorian_day(timestamp, NULL) == 7); - g_assert(gswe_timestamp_get_gregorian_hour(timestamp, NULL) == 11); - g_assert(gswe_timestamp_get_gregorian_minute(timestamp, NULL) == 54); - /* The next two lines seem to provide an inaccurate result due to floating - * point inaccuracies (may give 44.999999 instead of 45.0) */ -// g_assert(gswe_timestamp_get_gregorian_second(timestamp, NULL) == 45); -// g_assert(gswe_timestamp_get_gregorian_microsecond(timestamp, NULL) == 0); + gswe_timestamp_set_gregorian_timezone(timestamp, 1.0, NULL); + g_assert_cmpfloat( + gswe_timestamp_get_gregorian_timezone(timestamp), + ==, + 1.0 + ); + g_assert_cmpint( + gswe_timestamp_get_gregorian_year(timestamp, NULL), + ==, + 1983 + ); + g_assert_cmpuint( + gswe_timestamp_get_gregorian_month(timestamp, NULL), + ==, + 3 + ); + g_assert_cmpuint(gswe_timestamp_get_gregorian_day(timestamp, NULL), ==, 7); + g_assert_cmpuint( + gswe_timestamp_get_gregorian_hour(timestamp, NULL), + ==, + 11 + ); + g_assert_cmpuint( + gswe_timestamp_get_gregorian_minute(timestamp, NULL), + ==, + 54 + ); + + seconds = + gswe_timestamp_get_gregorian_second(timestamp, NULL) + + gswe_timestamp_get_gregorian_microsecond(timestamp, NULL) / 1000.0; + + /* Due to floating point errors, we allow 5 microseconds fuzzyness here */ + gswe_assert_fuzzy_equals( + seconds, + 45.0, + 0.005 + ); } int diff --git a/tests/test-asserts.h b/tests/test-asserts.h new file mode 100644 index 0000000..756d107 --- /dev/null +++ b/tests/test-asserts.h @@ -0,0 +1,27 @@ +#ifndef __SWE_GLIB_TEST_ASSERTS_H__ +#define __SWE_GLIB_TEST_ASSERTS_H__ + +#include + +/* Check equality with fuzzyness. Thanks for ebassi@GNOME and graphene */ +#define gswe_assert_fuzzy_equals(n1,n2,epsilon) \ + G_STMT_START { \ + typeof ((n1)) __n1 = (n1); \ + typeof ((n2)) __n2 = (n2); \ + typeof ((epsilon)) __epsilon = (epsilon); \ + if (__n1 > __n2) { \ + if ((__n1 - __n2) <= __epsilon) ; else { \ + g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ + #n1 " == " #n2 " (+/- " #epsilon ")", \ + __n1, "==", __n2, 'f'); \ + } \ + } else { \ + if ((__n2 - __n1) <= __epsilon) ; else { \ + g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ + #n1 " == " #n2 " (+/- " #epsilon ")", \ + __n1, "==", __n2, 'f'); \ + } \ + } \ + } G_STMT_END + +#endif /* __SWE_GLIB_TEST_ASSERTS_H__ */