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

4.104.115
Last change on this file since ab2422c was ab2422c, checked in by Joel Sherrill <joel.sherrill@…>, on 09/14/09 at 17:59:29

2009-09-14 Joel Sherrill <joel.sherrill@…>

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