tst-clock.c 2.8 KB

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