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