tst-clock.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Test program for POSIX clock_* functions.
  2. Copyright (C) 2000 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <time.h>
  19. /* We want to see output immediately. */
  20. #define STDOUT_UNBUFFERED
  21. /* We expect to run at least 10 seconds. */
  22. #define TIMEOUT 15
  23. static int
  24. clock_test (clockid_t cl)
  25. {
  26. struct timespec old_ts;
  27. struct timespec ts;
  28. struct timespec waitit;
  29. int result = 0;
  30. int i;
  31. memset (&ts, '\0', sizeof ts);
  32. waitit.tv_sec = 0;
  33. waitit.tv_nsec = 500000000;
  34. /* Get and print resolution of the clock. */
  35. if (clock_getres (cl, &ts) == 0)
  36. {
  37. if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
  38. {
  39. printf ("clock %d: nanosecond value of resolution wrong\n", cl);
  40. result = 1;
  41. }
  42. else
  43. printf ("clock %d: resolution = %ld.%09ld secs\n",
  44. cl, ts.tv_sec, ts.tv_nsec);
  45. }
  46. else
  47. {
  48. printf ("clock %d: cannot get resolution\n", cl);
  49. result = 1;
  50. }
  51. memset (&ts, '\0', sizeof ts);
  52. memset (&old_ts, '\0', sizeof old_ts);
  53. /* Next get the current time value a few times. */
  54. for (i = 0; i < 10; ++i)
  55. {
  56. if (clock_gettime (cl, &ts) == 0)
  57. {
  58. if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
  59. {
  60. printf ("clock %d: nanosecond value of time wrong (try %d)\n",
  61. cl, i);
  62. result = 1;
  63. }
  64. else
  65. {
  66. printf ("clock %d: time = %ld.%09ld secs\n",
  67. cl, ts.tv_sec, ts.tv_nsec);
  68. if (memcmp (&ts, &old_ts, sizeof ts) == 0)
  69. {
  70. printf ("clock %d: time hasn't changed (try %d)\n", cl, i);
  71. result = 1;
  72. old_ts = ts;
  73. }
  74. }
  75. }
  76. else
  77. {
  78. printf ("clock %d: cannot get time (try %d)\n", cl, i);
  79. result = 1;
  80. }
  81. /* Wait a bit before the next iteration. */
  82. nanosleep (&waitit, NULL);
  83. }
  84. return result;
  85. }
  86. static int
  87. do_test (void)
  88. {
  89. clockid_t cl;
  90. int result;
  91. result = clock_test (CLOCK_REALTIME);
  92. if (clock_getcpuclockid (0, &cl) == 0)
  93. /* XXX It's not yet a bug when this fails. */
  94. clock_test (cl);
  95. else
  96. printf("CPU clock unavailble, skipping test\n");
  97. return result;
  98. }
  99. #define TEST_FUNCTION do_test ()
  100. #include "../test-skeleton.c"