1 | /* |
---|
2 | * This test exercises the time of day services via the Classic |
---|
3 | * and POSIX APIs to make sure they are consistent. |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-2009. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #include <tmacros.h> |
---|
16 | #include <assert.h> |
---|
17 | #include <sys/stat.h> |
---|
18 | #include <fcntl.h> |
---|
19 | #include <stdio.h> |
---|
20 | #include <unistd.h> |
---|
21 | #include <errno.h> |
---|
22 | #include <string.h> |
---|
23 | #include <rtems.h> |
---|
24 | #include <rtems/libio.h> |
---|
25 | |
---|
26 | void test_adjtime(void); |
---|
27 | void check_a_tod( |
---|
28 | rtems_time_of_day *the_tod |
---|
29 | ); |
---|
30 | |
---|
31 | /* |
---|
32 | * List of dates and times to test. |
---|
33 | */ |
---|
34 | #define NUMBER_OF_DATES 8 |
---|
35 | rtems_time_of_day Dates[ NUMBER_OF_DATES ] = { |
---|
36 | /* YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, TICKS */ |
---|
37 | { 1988, 1, 1, 12, 45, 00, 0 }, |
---|
38 | { 1988, 12, 31, 9, 00, 00, 0 }, |
---|
39 | { 1999, 12, 31, 23, 55, 59, 0 }, |
---|
40 | { 1999, 06, 30, 00, 01, 30, 0 }, |
---|
41 | { 2000, 1, 1, 0, 15, 59, 0 }, |
---|
42 | { 2005, 2, 2, 5, 10, 59, 0 }, |
---|
43 | { 2010, 3, 3, 10, 5, 59, 0 }, |
---|
44 | { 2020, 4, 4, 15, 0, 59, 0 }, |
---|
45 | }; |
---|
46 | |
---|
47 | /* |
---|
48 | * Check out a single date and time |
---|
49 | */ |
---|
50 | |
---|
51 | void check_a_tod( |
---|
52 | rtems_time_of_day *the_tod |
---|
53 | ) |
---|
54 | { |
---|
55 | rtems_status_code status; |
---|
56 | rtems_time_of_day new_tod; |
---|
57 | time_t a_time_t; |
---|
58 | struct timeval tv; |
---|
59 | struct tm *a_tm; |
---|
60 | int result; |
---|
61 | int i = 0; |
---|
62 | |
---|
63 | print_time( "rtems_clock_set ", the_tod, "\n" ); |
---|
64 | status = rtems_clock_set( the_tod ); |
---|
65 | assert( !status ); |
---|
66 | |
---|
67 | do { |
---|
68 | status = rtems_clock_get_tod( &new_tod ); |
---|
69 | assert( !status ); |
---|
70 | print_time( "rtems_clock_get_tod ", &new_tod, "\n" ); |
---|
71 | |
---|
72 | /* now do the posix time gets */ |
---|
73 | result = gettimeofday( &tv, 0 ); |
---|
74 | assert( result == 0 ); |
---|
75 | a_time_t = tv.tv_sec; /* ctime() takes a time_t */ |
---|
76 | printf( "gettimeofday: %s", ctime( &a_time_t) ); |
---|
77 | |
---|
78 | a_time_t = time( 0 ); |
---|
79 | printf( "time: %s", ctime( &a_time_t ) ); |
---|
80 | |
---|
81 | a_tm = localtime( &a_time_t ); |
---|
82 | printf( "localtime: %s", asctime( a_tm ) ); |
---|
83 | |
---|
84 | a_tm = gmtime( &a_time_t ); |
---|
85 | printf( "gmtime: %s\n", asctime( a_tm ) ); |
---|
86 | |
---|
87 | status = rtems_task_wake_after( 5 * rtems_clock_get_ticks_per_second() ); |
---|
88 | |
---|
89 | i++; |
---|
90 | |
---|
91 | } while( i < 6 ); |
---|
92 | } |
---|
93 | |
---|
94 | void test_adjtime(void) |
---|
95 | { |
---|
96 | int sc; |
---|
97 | rtems_status_code status; |
---|
98 | struct timeval delta; |
---|
99 | struct timeval olddelta; |
---|
100 | rtems_time_of_day *the_tod; |
---|
101 | rtems_time_of_day tod; |
---|
102 | rtems_interval ticks; |
---|
103 | |
---|
104 | the_tod = &Dates[0]; |
---|
105 | |
---|
106 | print_time( "rtems_clock_set ", the_tod, "\n" ); |
---|
107 | status = rtems_clock_set( the_tod ); |
---|
108 | assert( !status ); |
---|
109 | |
---|
110 | delta.tv_sec = 0; |
---|
111 | delta.tv_usec = 0; |
---|
112 | olddelta.tv_sec = 0; |
---|
113 | olddelta.tv_usec = 0; |
---|
114 | |
---|
115 | puts( "adjtime - NULL delta - EINVAL" ); |
---|
116 | sc = adjtime( NULL, &olddelta ); |
---|
117 | assert( sc == -1 ); |
---|
118 | assert( errno == EINVAL ); |
---|
119 | |
---|
120 | puts( "adjtime - delta out of range - EINVAL" ); |
---|
121 | delta.tv_usec = 1000000000; /* 100 seconds worth */ |
---|
122 | sc = adjtime( &delta, &olddelta ); |
---|
123 | assert( sc == -1 ); |
---|
124 | assert( errno == EINVAL ); |
---|
125 | |
---|
126 | puts( "adjtime - delta too small - do nothing" ); |
---|
127 | delta.tv_sec = 0; |
---|
128 | delta.tv_usec = 1; |
---|
129 | sc = adjtime( &delta, &olddelta ); |
---|
130 | assert( sc == 0 ); |
---|
131 | |
---|
132 | puts( "adjtime - delta too small - do nothing, olddelta=NULL" ); |
---|
133 | sc = adjtime( &delta, NULL ); |
---|
134 | assert( sc == 0 ); |
---|
135 | |
---|
136 | puts( "adjtime - delta of one second forward" ); |
---|
137 | delta.tv_sec = 1; |
---|
138 | delta.tv_usec = 0; |
---|
139 | sc = adjtime( &delta, &olddelta ); |
---|
140 | assert( sc == 0 ); |
---|
141 | |
---|
142 | puts( "adjtime - delta of almost two seconds forward" ); |
---|
143 | delta.tv_sec = 1; |
---|
144 | delta.tv_usec = 1000000 - 1; |
---|
145 | sc = adjtime( &delta, &olddelta ); |
---|
146 | assert( sc == 0 ); |
---|
147 | |
---|
148 | /* |
---|
149 | * spin until over 1/2 of the way to the |
---|
150 | */ |
---|
151 | ticks = rtems_clock_get_ticks_per_second(); |
---|
152 | assert( ticks ); |
---|
153 | ticks /= 2; |
---|
154 | do { |
---|
155 | status = rtems_clock_get_tod( &tod ); |
---|
156 | assert( !status ); |
---|
157 | } while ( tod.ticks <= ticks ); |
---|
158 | |
---|
159 | puts( "adjtime - delta of almost one second forward which bumps second" ); |
---|
160 | delta.tv_sec = 0; |
---|
161 | delta.tv_usec = 1000000 - 1; |
---|
162 | sc = adjtime( &delta, &olddelta ); |
---|
163 | assert( sc == 0 ); |
---|
164 | |
---|
165 | status = rtems_clock_get_tod( &tod ); |
---|
166 | assert( !status ); |
---|
167 | print_time( "rtems_clock_get_tod ", &tod, "\n" ); |
---|
168 | } |
---|
169 | |
---|
170 | /* |
---|
171 | * main entry point to the test |
---|
172 | */ |
---|
173 | |
---|
174 | #if defined(__rtems__) |
---|
175 | int test_main(void); |
---|
176 | |
---|
177 | int test_main(void) |
---|
178 | #else |
---|
179 | int main( |
---|
180 | int argc, |
---|
181 | char **argv |
---|
182 | ) |
---|
183 | #endif |
---|
184 | { |
---|
185 | int i; |
---|
186 | |
---|
187 | puts( "\n\n*** POSIX TIME OF DAY TEST ***" ); |
---|
188 | |
---|
189 | test_adjtime(); |
---|
190 | |
---|
191 | /* |
---|
192 | * Now test a number of dates |
---|
193 | */ |
---|
194 | |
---|
195 | i = 0; |
---|
196 | while ( i < NUMBER_OF_DATES ) { |
---|
197 | check_a_tod( &Dates[i] ); |
---|
198 | i++; |
---|
199 | } |
---|
200 | |
---|
201 | puts( "\n\n*** END OF TIME OF DAY TEST 01 ***" ); |
---|
202 | rtems_test_exit(0); |
---|
203 | } |
---|