]> git.ipfire.org Git - thirdparty/glibc.git/blob - rt/tst-clock.c
49c418d2db570bb151f54bfa07eb18e3ac763ddf
[thirdparty/glibc.git] / rt / tst-clock.c
1 /* Test program for POSIX clock_* functions.
2 Copyright (C) 2000-2013 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, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <time.h>
23
24
25 /* We want to see output immediately. */
26 #define STDOUT_UNBUFFERED
27
28 /* We expect to run at least 10 seconds. */
29 #define TIMEOUT 15
30
31 static int
32 clock_test (clockid_t cl)
33 {
34 struct timespec old_ts;
35 struct timespec ts;
36 struct timespec waitit;
37 int result = 0;
38 int i;
39
40 memset (&ts, '\0', sizeof ts);
41
42 waitit.tv_sec = 0;
43 waitit.tv_nsec = 500000000;
44
45 /* Get and print resolution of the clock. */
46 if (clock_getres (cl, &ts) == 0)
47 {
48 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
49 {
50 printf ("clock %d: nanosecond value of resolution wrong\n", cl);
51 result = 1;
52 }
53 else
54 printf ("clock %d: resolution = %ld.%09ld secs\n",
55 cl, ts.tv_sec, ts.tv_nsec);
56 }
57 else
58 {
59 printf ("clock %d: cannot get resolution\n", cl);
60 result = 1;
61 }
62
63 memset (&ts, '\0', sizeof ts);
64 memset (&old_ts, '\0', sizeof old_ts);
65
66 /* Next get the current time value a few times. */
67 for (i = 0; i < 10; ++i)
68 {
69 if (clock_gettime (cl, &ts) == 0)
70 {
71 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
72 {
73 printf ("clock %d: nanosecond value of time wrong (try %d)\n",
74 cl, i);
75 result = 1;
76 }
77 else
78 {
79 printf ("clock %d: time = %ld.%09ld secs\n",
80 cl, ts.tv_sec, ts.tv_nsec);
81
82 if (memcmp (&ts, &old_ts, sizeof ts) == 0)
83 {
84 printf ("clock %d: time hasn't changed (try %d)\n", cl, i);
85 result = 1;
86
87 old_ts = ts;
88 }
89 }
90 }
91 else
92 {
93 printf ("clock %d: cannot get time (try %d)\n", cl, i);
94 result = 1;
95 }
96
97 /* Wait a bit before the next iteration. */
98 nanosleep (&waitit, NULL);
99 }
100
101 return result;
102 }
103
104 static int
105 do_test (void)
106 {
107 clockid_t cl;
108 int result;
109
110 result = clock_test (CLOCK_REALTIME);
111
112 if (clock_getcpuclockid (0, &cl) == 0)
113 /* XXX It's not yet a bug when this fails. */
114 clock_test (cl);
115 else
116 printf("CPU clock unavailble, skipping test\n");
117
118 return result;
119 }
120 #define TEST_FUNCTION do_test ()
121
122
123 #include "../test-skeleton.c"