[PATCH 3/6] tests: Avoid rounding errors with double precision variables



The double precision variable 'elapsed' (computed from an guint64 by
division by 1e6) must be "rounded up" in order to pass the test for
equality reliably.

Signed-off-by: Ingo Brückl <ib wupperonline de>
---
 glib/tests/timer.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/glib/tests/timer.c b/glib/tests/timer.c
index 18a762f..f165301 100644
--- a/glib/tests/timer.c
+++ b/glib/tests/timer.c
@@ -36,7 +36,7 @@ test_timer_basic (void)
   elapsed = g_timer_elapsed (timer, &micros);

   g_assert_cmpfloat (elapsed, <, 1.0);
-  g_assert_cmpuint (micros, ==, ((guint64)(elapsed * 1e6)) % 1000000);
+  g_assert_cmpuint (micros, ==, ((guint64)((elapsed + 0.0000005) * 1e6)) % 1000000);

   g_timer_destroy (timer);
 }
@@ -56,7 +56,7 @@ test_timer_stop (void)
   g_usleep (100);
   elapsed2 = g_timer_elapsed (timer, NULL);

-  g_assert_cmpfloat (elapsed, ==, elapsed2);
+  g_assert_cmpfloat ((float)elapsed, ==, (float)elapsed2);

   g_timer_destroy (timer);
 }
--
2.8.2



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]