source: rtems/testsuites/psxtests/psxclock/init.c @ 2441f74

4.104.115
Last change on this file since 2441f74 was 17c7533, checked in by Ralf Corsepius <ralf.corsepius@…>, on 10/27/09 at 03:57:28

Use %ld to print *.tv_nsec.

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