2010-03-26 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / eglib / test / timer.c
blob7b41f8004c3956c6bb01317839fefe90cf3fac8c
1 #include <config.h>
2 #include <glib.h>
3 #include <string.h>
4 #include <math.h>
5 #ifdef HAVE_UNISTD_H
6 #include <unistd.h>
7 #endif
8 #include <stdlib.h>
9 #include <stdio.h>
11 #ifdef G_OS_WIN32
12 #include <windows.h>
13 #define sleep(t) Sleep((t) * 1000)
14 #endif
16 #include "test.h"
18 RESULT
19 test_timer ()
21 GTimer *timer;
22 gdouble elapsed1, elapsed2;
23 gulong usec = 0;
25 timer = g_timer_new ();
26 sleep (1);
27 elapsed1 = g_timer_elapsed (timer, NULL);
28 if ((elapsed1 + 0.1) < 1.0)
29 return FAILED ("Elapsed time should be around 1s and was %f", elapsed1);
31 g_timer_stop (timer);
32 elapsed1 = g_timer_elapsed (timer, NULL);
33 elapsed2 = g_timer_elapsed (timer, &usec);
34 if (fabs (elapsed1 - elapsed2) > 0.000001)
35 return FAILED ("The elapsed times are not equal %f - %f.", elapsed1, elapsed2);
37 elapsed2 *= 1000000;
38 while (elapsed2 > 1000000)
39 elapsed2 -= 1000000;
41 if (fabs (usec - elapsed2) > 100.0)
42 return FAILED ("usecs are wrong.");
44 g_timer_destroy (timer);
45 return OK;
48 static Test timer_tests [] = {
49 {"g_timer", test_timer},
50 {NULL, NULL}
53 DEFINE_TEST_GROUP_INIT(timer_tests_init, timer_tests)