7b41f8004c3956c6bb01317839fefe90cf3fac8c
[mono.git] / eglib / test / timer.c
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>
10
11 #ifdef G_OS_WIN32
12 #include <windows.h>
13 #define sleep(t)                 Sleep((t) * 1000)
14 #endif
15
16 #include "test.h"
17
18 RESULT
19 test_timer ()
20 {
21         GTimer *timer;
22         gdouble elapsed1, elapsed2;
23         gulong usec = 0;
24
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);
30
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);
36
37         elapsed2 *= 1000000;
38         while (elapsed2 > 1000000)
39                 elapsed2 -= 1000000;
40
41         if (fabs (usec - elapsed2) > 100.0)
42                 return FAILED ("usecs are wrong.");
43
44         g_timer_destroy (timer);
45         return OK;
46 }
47
48 static Test timer_tests [] = {
49         {"g_timer", test_timer},
50         {NULL, NULL}
51 };
52
53 DEFINE_TEST_GROUP_INIT(timer_tests_init, timer_tests)
54
55