lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 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 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 19 | 02111-1307 USA. */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <time.h> |
| 24 | |
| 25 | |
| 26 | /* We want to see output immediately. */ |
| 27 | #define STDOUT_UNBUFFERED |
| 28 | |
| 29 | /* We expect to run at least 10 seconds. */ |
| 30 | #define TIMEOUT 15 |
| 31 | |
| 32 | static int |
| 33 | clock_test (clockid_t cl) |
| 34 | { |
| 35 | struct timespec old_ts; |
| 36 | struct timespec ts; |
| 37 | struct timespec waitit; |
| 38 | int result = 0; |
| 39 | int i; |
| 40 | |
| 41 | memset (&ts, '\0', sizeof ts); |
| 42 | |
| 43 | waitit.tv_sec = 0; |
| 44 | waitit.tv_nsec = 500000000; |
| 45 | |
| 46 | /* Get and print resolution of the clock. */ |
| 47 | if (clock_getres (cl, &ts) == 0) |
| 48 | { |
| 49 | if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000) |
| 50 | { |
| 51 | printf ("clock %d: nanosecond value of resolution wrong\n", cl); |
| 52 | result = 1; |
| 53 | } |
| 54 | else |
| 55 | printf ("clock %d: resolution = %ld.%09ld secs\n", |
| 56 | cl, ts.tv_sec, ts.tv_nsec); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | printf ("clock %d: cannot get resolution\n", cl); |
| 61 | result = 1; |
| 62 | } |
| 63 | |
| 64 | memset (&ts, '\0', sizeof ts); |
| 65 | memset (&old_ts, '\0', sizeof old_ts); |
| 66 | |
| 67 | /* Next get the current time value a few times. */ |
| 68 | for (i = 0; i < 10; ++i) |
| 69 | { |
| 70 | if (clock_gettime (cl, &ts) == 0) |
| 71 | { |
| 72 | if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000) |
| 73 | { |
| 74 | printf ("clock %d: nanosecond value of time wrong (try %d)\n", |
| 75 | cl, i); |
| 76 | result = 1; |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | printf ("clock %d: time = %ld.%09ld secs\n", |
| 81 | cl, ts.tv_sec, ts.tv_nsec); |
| 82 | |
| 83 | if (memcmp (&ts, &old_ts, sizeof ts) == 0) |
| 84 | { |
| 85 | printf ("clock %d: time hasn't changed (try %d)\n", cl, i); |
| 86 | result = 1; |
| 87 | |
| 88 | old_ts = ts; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | printf ("clock %d: cannot get time (try %d)\n", cl, i); |
| 95 | result = 1; |
| 96 | } |
| 97 | |
| 98 | /* Wait a bit before the next iteration. */ |
| 99 | nanosleep (&waitit, NULL); |
| 100 | } |
| 101 | |
| 102 | return result; |
| 103 | } |
| 104 | |
| 105 | static int |
| 106 | do_test (void) |
| 107 | { |
| 108 | clockid_t cl; |
| 109 | int result; |
| 110 | |
| 111 | result = clock_test (CLOCK_REALTIME); |
| 112 | |
| 113 | if (clock_getcpuclockid (0, &cl) == 0) |
| 114 | /* XXX It's not yet a bug when this fails. */ |
| 115 | clock_test (cl); |
| 116 | else |
| 117 | printf("CPU clock unavailble, skipping test\n"); |
| 118 | |
| 119 | return result; |
| 120 | } |
| 121 | #define TEST_FUNCTION do_test () |
| 122 | |
| 123 | |
| 124 | #include "../test-skeleton.c" |