tst-clock.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <time.h>
  20. /* We want to see output immediately. */
  21. #define STDOUT_UNBUFFERED
  22. /* We expect to run at least 10 seconds. */
  23. #define TIMEOUT 15
  24. static int
  25. clock_test (clockid_t cl)
  26. {
  27. struct timespec old_ts;
  28. struct timespec ts;
  29. struct timespec waitit;
  30. int result = 0;
  31. int i;
  32. memset (&ts, '\0', sizeof ts);
  33. waitit.tv_sec = 0;
  34. waitit.tv_nsec = 500000000;
  35. /* Get and print resolution of the clock. */
  36. if (clock_getres (cl, &ts) == 0)
  37. {
  38. if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
  39. {
  40. printf ("clock %d: nanosecond value of resolution wrong\n", cl);
  41. result = 1;
  42. }
  43. else
  44. printf ("clock %d: resolution = %ld.%09ld secs\n",
  45. cl, ts.tv_sec, ts.tv_nsec);
  46. }
  47. else
  48. {
  49. printf ("clock %d: cannot get resolution\n", cl);
  50. result = 1;
  51. }
  52. memset (&ts, '\0', sizeof ts);
  53. memset (&old_ts, '\0', sizeof old_ts);
  54. /* Next get the current time value a few times. */
  55. for (i = 0; i < 10; ++i)
  56. {
  57. if (clock_gettime (cl, &ts) == 0)
  58. {
  59. if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
  60. {
  61. printf ("clock %d: nanosecond value of time wrong (try %d)\n",
  62. cl, i);
  63. result = 1;
  64. }
  65. else
  66. {
  67. printf ("clock %d: time = %ld.%09ld secs\n",
  68. cl, ts.tv_sec, ts.tv_nsec);
  69. if (memcmp (&ts, &old_ts, sizeof ts) == 0)
  70. {
  71. printf ("clock %d: time hasn't changed (try %d)\n", cl, i);
  72. result = 1;
  73. old_ts = ts;
  74. }
  75. }
  76. }
  77. else
  78. {
  79. printf ("clock %d: cannot get time (try %d)\n", cl, i);
  80. result = 1;
  81. }
  82. /* Wait a bit before the next iteration. */
  83. nanosleep (&waitit, NULL);
  84. }
  85. return result;
  86. }
  87. static int
  88. do_test (void)
  89. {
  90. clockid_t cl;
  91. int result;
  92. result = clock_test (CLOCK_REALTIME);
  93. if (clock_getcpuclockid (0, &cl) == 0)
  94. /* XXX It's not yet a bug when this fails. */
  95. clock_test (cl);
  96. else
  97. printf("CPU clock unavailble, skipping test\n");
  98. return result;
  99. }
  100. #define TEST_FUNCTION do_test ()
  101. #include "../test-skeleton.c"