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