timer.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #ifdef G_OS_WIN32
  11. #include <windows.h>
  12. #define sleep(t) Sleep((t) * 1000)
  13. #endif
  14. #include "test.h"
  15. RESULT
  16. test_timer ()
  17. {
  18. GTimer *timer;
  19. gdouble elapsed1, elapsed2;
  20. gulong usec = 0;
  21. timer = g_timer_new ();
  22. sleep (1);
  23. elapsed1 = g_timer_elapsed (timer, NULL);
  24. if ((elapsed1 + 0.1) < 1.0)
  25. return FAILED ("Elapsed time should be around 1s and was %f", elapsed1);
  26. g_timer_stop (timer);
  27. elapsed1 = g_timer_elapsed (timer, NULL);
  28. elapsed2 = g_timer_elapsed (timer, &usec);
  29. if (fabs (elapsed1 - elapsed2) > 0.000001)
  30. return FAILED ("The elapsed times are not equal %f - %f.", elapsed1, elapsed2);
  31. elapsed2 *= 1000000;
  32. while (elapsed2 > 1000000)
  33. elapsed2 -= 1000000;
  34. if (fabs (usec - elapsed2) > 100.0)
  35. return FAILED ("usecs are wrong.");
  36. g_timer_destroy (timer);
  37. return OK;
  38. }
  39. static Test timer_tests [] = {
  40. {"g_timer", test_timer},
  41. {NULL, NULL}
  42. };
  43. DEFINE_TEST_GROUP_INIT(timer_tests_init, timer_tests)