make minix lwip make explicit use of 'int'
[minix3.git] / test / test69.c
blob7c1a6e631b8b40e88ad9cd952f065682f18b2c8e
1 /* Test 69. clock_getres(), clock_gettime(), clock_settime(), adjtime().
3 * Note, any type of ntpd or software that calls adjtime() or settimeofday()
4 * should be disabled while running this test. This test takes ~40s to run.
5 */
7 #include <time.h>
8 #include <sys/types.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
15 #define TRIALS 100
16 #define MAX_ERROR 4
17 #ifndef DEBUG
18 #define DEBUG 0
19 #endif
21 int subtest = 1;
23 #include "common.c"
25 int main(void);
26 void quit(void);
27 static void test_clock_getres();
28 static void test_clock_gettime();
29 static void test_clock_settime();
30 static void test_adjtime();
31 static void show_timespec(char *msg, struct timespec *ts);
33 static void test_clock_getres()
35 struct timespec res;
37 /* valid clock_id should succeed, invalid clock_id should fail */
38 if (clock_getres(CLOCK_REALTIME, &res) == -1) e(10);
39 if (res.tv_sec < 0 || res.tv_nsec < 0) e(11);
40 show_timespec("res(CLOCK_REALTIME)", &res);
42 if (clock_getres(CLOCK_MONOTONIC, &res) == -1) e(12);
43 if (res.tv_sec < 0 || res.tv_nsec < 0) e(13);
44 show_timespec("res(CLOCK_MONOTONIC)", &res);
46 if (clock_getres(-1, &res) == 0) e(14);
49 static void test_clock_gettime()
51 struct timespec ts, ts2;
53 /* valid clock_id should succeed, invalid clock_id should fail */
54 if (clock_gettime(CLOCK_REALTIME, &ts) == -1) e(21);
55 if (ts.tv_sec < 0 || ts.tv_nsec < 0) e(22);
56 show_timespec("time(CLOCK_REALTIME)", &ts);
57 sleep(2);
58 if (clock_gettime(CLOCK_REALTIME, &ts2) == -1) e(23);
59 if (ts2.tv_sec < 0 || ts2.tv_nsec < 0) e(24);
60 if (ts2.tv_sec <= ts.tv_sec) e(25);
62 if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) e(26);
63 if (ts.tv_sec < 0 || ts.tv_nsec < 0) e(27);
64 show_timespec("time(CLOCK_MONOTONIC)", &ts);
65 sleep(2);
66 if (clock_gettime(CLOCK_MONOTONIC, &ts2) == -1) e(28);
67 if (ts2.tv_sec < 0 || ts2.tv_nsec < 0) e(29);
68 if (ts2.tv_sec <= ts.tv_sec) e(30);
70 if (clock_gettime(-1, &ts) == 0) e(31);
73 static void test_clock_settime(void)
75 struct timespec ts;
76 struct timespec ts2;
78 /* shouldn't be able to set MONOTONIC */
79 if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) e(50);
80 if (clock_settime(CLOCK_MONOTONIC, &ts) == 0) e(51);
81 if (errno != EINVAL) e(52); /* reponse should be EINVAL */
83 /* set the time of REALTIME to that of MONOTONIC */
84 if (clock_settime(CLOCK_REALTIME, &ts) == -1) e(53);
86 ts.tv_sec += 600; /* time travel 10 minutes into the future */
87 if (clock_gettime(CLOCK_REALTIME, &ts2) == -1) e(54);
88 if (clock_settime(CLOCK_REALTIME, &ts) == -1) e(55);
89 if (clock_gettime(CLOCK_REALTIME, &ts) == -1) e(56);
91 /* get the value we set, if it's not about 10 minutes ahead, it failed */
92 if (ts.tv_sec - ts2.tv_sec < 500 ||
93 ts.tv_sec - ts2.tv_sec > 700) e(57);
95 /* back to current time - don't leave the system time 10 ahead */
96 if (clock_gettime(CLOCK_REALTIME, &ts) == -1) e(58);
97 ts.tv_sec -= 600;
98 if (clock_settime(CLOCK_REALTIME, &ts) == -1) e(59);
100 /* Test with an invalid clock */
101 if (clock_settime(-1, &ts) == 0) e(60);
104 static void test_adjtime(void)
106 struct timeval delta, olddelta;
107 struct timespec rt, mt;
109 /* set the realtime clock to the same value as the monotonic clock */
110 if (clock_gettime(CLOCK_MONOTONIC, &mt) == -1) e(65);
111 if (clock_settime(CLOCK_REALTIME, &mt) == -1) e(66);
113 delta.tv_sec = 7;
114 delta.tv_usec = 0;
116 if (adjtime(&delta, &olddelta) != 0) e(70); /* adjust +7 seconds */
117 sleep(15); /* should take 14 seconds to adjust the clock */
119 /* check that the 7 second adjustment puts us between 5 and 10 seconds
120 * ahead of the monotonic clock.
122 if (clock_gettime(CLOCK_MONOTONIC, &mt) == -1) e(71);
123 if (clock_gettime(CLOCK_REALTIME, &rt) == -1) e(72);
124 show_timespec("Monotonic", &mt);
125 show_timespec("Realtime (+7)", &rt);
126 if (rt.tv_sec - 5 < mt.tv_sec || rt.tv_sec - 10 > mt.tv_sec) e(73);
128 delta.tv_sec = -7;
129 if (adjtime(&delta, &olddelta) != 0) e(73); /* adjust -7 seconds */
130 sleep(15); /* should take 14 seconds to adjust the clock */
132 /* check that the 7 second adjustment puts us close to even with
133 * the monotonic clock.
135 if (clock_gettime(CLOCK_MONOTONIC, &mt) == -1) e(74);
136 if (clock_gettime(CLOCK_REALTIME, &rt) == -1) e(75);
137 show_timespec("Monotonic", &mt);
138 show_timespec("Realtime (-7)", &rt);
139 if (abs(rt.tv_sec - mt.tv_sec) > 5) e(76);
143 static void show_timespec(char *msg, struct timespec *ts)
145 #if DEBUG == 1
146 printf("[%s] tv_sec=%d tv_nsec=%ld\n", msg, ts->tv_sec, ts->tv_nsec);
147 #endif /* DEBUG == 1 */
150 int main()
152 start(69);
153 struct timespec starttime, endtime;
155 /* get test start time */
156 if (clock_gettime(CLOCK_MONOTONIC, &starttime) == -1) e(1);
158 test_clock_getres();
159 test_clock_gettime();
160 test_clock_settime();
161 test_adjtime();
163 /* get test end time */
164 if (clock_gettime(CLOCK_MONOTONIC, &endtime) == -1) e(2);
166 /* we shouldn't have gone backwards in time during this test */
167 if ((starttime.tv_sec > endtime.tv_sec) ||
168 (starttime.tv_sec == endtime.tv_sec &&
169 starttime.tv_nsec > endtime.tv_nsec)) e(3);
171 quit();
172 return(-1); /* impossible */