source: rtems/testsuites/psxtests/psxclock/init.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2012.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <time.h>
15#include <errno.h>
16
17#include "pmacros.h"
18#include "pritime.h"
19
20#include <rtems/score/todimpl.h>
21
22/* forward declarations to avoid warnings */
23rtems_task Init(rtems_task_argument argument);
24void check_enosys(int status);
25
26void check_enosys(int status)
27{
28  if ( (status == -1) && (errno == ENOSYS) )
29    return;
30  puts( "ERROR -- did not return ENOSYS as expected" );
31  rtems_test_exit(0);
32}
33
34rtems_task Init(
35  rtems_task_argument argument
36)
37{
38  struct timespec tv;
39  struct timespec tr;
40  int             sc;
41  time_t          seconds;
42  time_t          seconds1;
43  unsigned int    remaining;
44  struct tm       tm;
45  struct timespec delay_request;
46
47  puts( "\n\n*** POSIX CLOCK TEST ***" );
48
49  tm_build_time( &tm, TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 );
50
51  /* error cases in clock_gettime and clock_settime */
52
53  puts( "Init: clock_gettime - EINVAL (NULL timespec)" );
54  sc = clock_gettime( CLOCK_REALTIME, NULL );
55  rtems_test_assert( sc == -1 );
56  rtems_test_assert( errno == EINVAL );
57
58  puts( "Init: clock_gettime - EINVAL (invalid clockid)" );
59  sc = clock_gettime( (clockid_t)-1, &tv );
60  rtems_test_assert( sc == -1 );
61  rtems_test_assert( errno == EINVAL );
62
63  puts( "Init: clock_settime - EINVAL (invalid clockid)" );
64  sc = clock_settime( (clockid_t)-1, &tv );
65  rtems_test_assert( sc == -1 );
66  rtems_test_assert( errno == EINVAL );
67
68  /* way back near the dawn of time :D */
69  tv.tv_sec = 1;
70  tv.tv_nsec = 0;
71  printf( ctime( &tv.tv_sec ) );
72  puts( "Init: clock_settime - before 1988 EINVAL" );
73  sc = clock_settime( CLOCK_REALTIME, &tv );
74  rtems_test_assert( sc == -1 );
75  rtems_test_assert( errno == EINVAL );
76
77  /* exercise clock_getres */
78
79  puts( "Init: clock_getres - EINVAL (invalid clockid)" );
80  sc = clock_getres( (clockid_t) -1, &tv );
81  rtems_test_assert( sc == -1 );
82  rtems_test_assert( errno == EINVAL );
83
84  puts( "Init: clock_getres - EINVAL (NULL resolution)" );
85  sc = clock_getres( CLOCK_REALTIME, NULL );
86  rtems_test_assert( sc == -1 );
87  rtems_test_assert( errno == EINVAL );
88
89  puts( "Init: clock_getres - SUCCESSFUL" );
90  sc = clock_getres( CLOCK_REALTIME, &tv );
91  printf( "Init: resolution = sec (%" PRIdtime_t "), nsec (%ld)\n", tv.tv_sec, tv.tv_nsec );
92  rtems_test_assert( !sc );
93
94  /* set the time of day, and print our buffer in multiple ways */
95
96  tv.tv_sec = mktime( &tm );
97  rtems_test_assert( tv.tv_sec != -1 );
98
99  tv.tv_nsec = 0;
100
101  /* now set the time of day */
102
103  empty_line();
104
105  printf( asctime( &tm ) );
106  puts( "Init: clock_settime - SUCCESSFUL" );
107  sc = clock_settime( CLOCK_REALTIME, &tv );
108  rtems_test_assert( !sc );
109
110  printf( asctime( &tm ) );
111  printf( ctime( &tv.tv_sec ) );
112
113  /* use sleep to delay */
114
115  remaining = sleep( 3 );
116  rtems_test_assert( !remaining );
117
118  /* print new times to make sure it has changed and we can get the realtime */
119  sc = clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &tv );
120  rtems_test_assert( !sc );
121  printf("Time since boot: (%" PRIdtime_t ", %ld)\n", tv.tv_sec,tv.tv_nsec );
122
123  sc = clock_gettime( CLOCK_REALTIME, &tv );
124  rtems_test_assert( !sc );
125
126  printf( ctime( &tv.tv_sec ) );
127
128  seconds = time( NULL );
129  printf( ctime( &seconds ) );
130
131  /*  just to have the value copied out through the parameter */
132
133  seconds = time( &seconds1 );
134  rtems_test_assert( seconds == seconds1 );
135
136  /* check the time remaining */
137
138  printf( "Init: seconds remaining (%d)\n", (int)remaining );
139  rtems_test_assert( !remaining );
140
141  /* error cases in nanosleep */
142
143  empty_line();
144  puts( "Init: nanosleep - EINVAL (NULL time)" );
145  sc = nanosleep ( NULL, &tr );
146  rtems_test_assert( sc == -1 );
147  rtems_test_assert( errno == EINVAL );
148
149  tv.tv_sec = 0;
150  tv.tv_nsec = TOD_NANOSECONDS_PER_SECOND * 2;
151  puts( "Init: nanosleep - EINVAL (too many nanoseconds)" );
152  sc = nanosleep ( &tv, &tr );
153  rtems_test_assert( sc == -1 );
154  rtems_test_assert( errno == EINVAL );
155
156  /* this is an error */
157  tv.tv_sec = -1;
158  tv.tv_nsec = 0;
159  puts( "Init: nanosleep - negative seconds - EINVAL" );
160  sc = nanosleep ( &tv, &tr );
161  rtems_test_assert( sc == -1 );
162  rtems_test_assert( errno == EINVAL );
163
164  /* this is also an error */
165  tv.tv_sec = 0;
166  tv.tv_nsec = -1;
167  puts( "Init: nanosleep - negative nanoseconds - EINVAL" );
168  sc = nanosleep ( &tv, &tr );
169  rtems_test_assert( sc == -1 );
170  rtems_test_assert( errno == EINVAL );
171
172  /* this is actually a small delay */
173  tv.tv_sec = 0;
174  tv.tv_nsec = 1;
175  puts( "Init: nanosleep - delay so small results in one tick" );
176  sc = nanosleep ( &tv, &tr );
177  rtems_test_assert( !sc );
178  rtems_test_assert( !tr.tv_sec );
179  rtems_test_assert( !tr.tv_nsec );
180
181  /* use nanosleep to yield */
182
183  tv.tv_sec = 0;
184  tv.tv_nsec = 0;
185
186  puts( "Init: nanosleep - yield with remaining" );
187  sc = nanosleep ( &tv, &tr );
188  rtems_test_assert( !sc );
189  rtems_test_assert( !tr.tv_sec );
190  rtems_test_assert( !tr.tv_nsec );
191
192  puts( "Init: nanosleep - yield with NULL time remaining" );
193  sc = nanosleep ( &tv, NULL );
194  rtems_test_assert( !sc );
195  rtems_test_assert( !tr.tv_sec );
196  rtems_test_assert( !tr.tv_nsec );
197
198  /* use nanosleep to delay */
199
200  tv.tv_sec = 3;
201  tv.tv_nsec = 500000;
202
203  puts( "Init: nanosleep - 1.05 seconds" );
204  sc = nanosleep ( &tv, &tr );
205  rtems_test_assert( !sc );
206
207  /* print the current real time again */
208  sc = clock_gettime( CLOCK_REALTIME, &tv );
209  rtems_test_assert( !sc );
210  printf( ctime( &tv.tv_sec ) );
211
212  /* check the time remaining */
213
214  printf( "Init: sec (%" PRIdtime_t "), nsec (%ld) remaining\n", tr.tv_sec, tr.tv_nsec );
215  rtems_test_assert( !tr.tv_sec && !tr.tv_nsec );
216
217  puts( "Init: nanosleep - 1.35 seconds" );
218  delay_request.tv_sec = 1;
219  delay_request.tv_nsec = 35000000;
220  sc = nanosleep( &delay_request, NULL );
221  rtems_test_assert( !sc );
222
223  /* print the current real time again */
224  sc = clock_gettime( CLOCK_REALTIME, &tv );
225  rtems_test_assert( !sc );
226  printf( ctime( &tv.tv_sec ) );
227
228  empty_line();
229  puts( "clock_gettime - CLOCK_THREAD_CPUTIME_ID -- ENOSYS" );
230  #if defined(_POSIX_THREAD_CPUTIME)
231    {
232      struct timespec tp;
233      sc = clock_gettime( CLOCK_THREAD_CPUTIME_ID, &tp );
234      check_enosys( sc );
235    }
236  #endif
237
238  puts( "clock_settime - CLOCK_PROCESS_CPUTIME_ID -- ENOSYS" );
239  #if defined(_POSIX_CPUTIME)
240    {
241      struct timespec tp;
242      sc = clock_settime( CLOCK_PROCESS_CPUTIME_ID, &tp );
243      check_enosys( sc );
244    }
245  #endif
246
247  puts( "clock_settime - CLOCK_THREAD_CPUTIME_ID -- ENOSYS" );
248  #if defined(_POSIX_THREAD_CPUTIME)
249    {
250      struct timespec tp;
251      sc = clock_settime( CLOCK_THREAD_CPUTIME_ID, &tp );
252      check_enosys( sc );
253    }
254  #endif
255
256  puts( "*** END OF POSIX CLOCK TEST ***" );
257  rtems_test_exit(0);
258}
259
260
261/* configuration information */
262#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
263#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
264
265#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
266#define CONFIGURE_MAXIMUM_TASKS             1
267
268#define CONFIGURE_INIT
269#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.