source: rtems/testsuites/psxtests/psxtime/test.c @ 947015f

4.115
Last change on this file since 947015f was 947015f, checked in by Joel Sherrill <joel.sherrill@…>, on 07/15/10 at 13:59:25

2010-07-15 Bharath Suri <bharath.s.jois@…>

PR 1617/testing

  • psxfile01/test.c, psxfile01/psxfile01.scn: Added new cases to exercise_link_r and _unlink_r
  • psxstat/test.c, psxstat/psxstat.scn: Added new cases to exercise _lstat_r and _stat_r.
  • psxtime/test.c, psxtime/psxtime.scn: Added new cases to exercise _gettimeofday.
  • Property mode set to 100644
File size: 5.5 KB
Line 
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 <sys/stat.h>
17#include <fcntl.h>
18#include <stdio.h>
19#include <unistd.h>
20#include <errno.h>
21#include <string.h>
22#include <rtems.h>
23#include <rtems/libio.h>
24#include <sys/time.h>
25
26#if !HAVE_DECL_ADJTIME
27extern int adjtime(const struct timeval *delta, struct timeval *olddelta);
28#endif
29
30extern int _gettimeofday(struct timeval *__p, void *__tz);
31
32void test_adjtime(void);
33void check_a_tod(
34  rtems_time_of_day *the_tod
35);
36
37/*
38 *  List of dates and times to test.
39 */
40#define NUMBER_OF_DATES   8
41rtems_time_of_day Dates[ NUMBER_OF_DATES ] = {
42  /* YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, TICKS */
43  {  1988,   1,    1,   12,    45,     00,     0 },
44  {  1988,  12,   31,    9,    00,     00,     0 },
45  {  1999,  12,   31,   23,    55,     59,     0 },
46  {  1999,  06,   30,   00,    01,     30,     0 },
47  {  2000,   1,    1,    0,    15,     59,     0 },
48  {  2005,   2,    2,    5,    10,     59,     0 },
49  {  2010,   3,    3,   10,     5,     59,     0 },
50  {  2020,   4,    4,   15,     0,     59,     0 },
51};
52
53/*
54 *  Check out a single date and time
55 */
56
57void check_a_tod(
58  rtems_time_of_day *the_tod
59)
60{
61  rtems_status_code status;
62  rtems_time_of_day new_tod;
63  time_t            a_time_t;
64  struct timeval    tv;
65  struct tm        *a_tm;
66  int               result;
67  int               i = 0;
68
69  print_time( "rtems_clock_set          ", the_tod, "\n" );
70  status = rtems_clock_set( the_tod );
71  rtems_test_assert( !status );
72
73  do {
74    status = rtems_clock_get_tod( &new_tod );
75    rtems_test_assert( !status );
76    print_time( "rtems_clock_get_tod          ", &new_tod, "\n" );
77
78    /* now do the posix time gets */
79    result = gettimeofday( &tv, 0 );
80    rtems_test_assert( result == 0 );
81    a_time_t = tv.tv_sec;   /* ctime() takes a time_t */
82    printf( "gettimeofday: %s", ctime( &a_time_t) );
83
84    a_time_t = time( 0 );
85    printf( "time:         %s", ctime( &a_time_t ) );
86
87    a_tm = localtime( &a_time_t );
88    printf( "localtime:    %s", asctime( a_tm ) );
89
90    a_tm = gmtime( &a_time_t );
91    printf( "gmtime:       %s\n",  asctime( a_tm ) );
92
93    status = rtems_task_wake_after( 5 * rtems_clock_get_ticks_per_second() );
94
95    i++;
96
97  } while( i < 6 );
98}
99
100void test_adjtime(void)
101{
102  int                sc;
103  rtems_status_code  status;
104  struct timeval     delta;
105  struct timeval     olddelta;
106  rtems_time_of_day *the_tod;
107  rtems_time_of_day  tod;
108  rtems_interval     ticks;
109
110  the_tod = &Dates[0];
111
112  print_time( "rtems_clock_set          ", the_tod, "\n" );
113  status = rtems_clock_set( the_tod );
114  rtems_test_assert( !status );
115
116  delta.tv_sec = 0;
117  delta.tv_usec = 0;
118  olddelta.tv_sec = 0;
119  olddelta.tv_usec = 0;
120
121  puts( "adjtime - NULL delta - EINVAL" );
122  sc = adjtime( NULL, &olddelta );
123  rtems_test_assert( sc == -1 );
124  rtems_test_assert( errno == EINVAL );
125
126  puts( "adjtime - delta out of range - EINVAL" );
127  delta.tv_usec = 1000000000; /* 100 seconds worth */
128  sc = adjtime( &delta, &olddelta );
129  rtems_test_assert( sc == -1 );
130  rtems_test_assert( errno == EINVAL );
131
132  puts( "adjtime - delta too small - do nothing" );
133  delta.tv_sec = 0;
134  delta.tv_usec = 1;
135  sc = adjtime( &delta, &olddelta );
136  rtems_test_assert( sc == 0 );
137
138  puts( "adjtime - delta too small - do nothing, olddelta=NULL" );
139  sc = adjtime( &delta, NULL );
140  rtems_test_assert( sc == 0 );
141
142  puts( "adjtime - delta of one second forward, olddelta=NULL" );
143  delta.tv_sec = 1;
144  delta.tv_usec = 0;
145  sc = adjtime( &delta, NULL );
146  rtems_test_assert( sc == 0 );
147
148  puts( "adjtime - delta of one second forward" );
149  delta.tv_sec = 1;
150  delta.tv_usec = 0;
151  sc = adjtime( &delta, &olddelta );
152  rtems_test_assert( sc == 0 );
153
154  puts( "adjtime - delta of almost two seconds forward" );
155  delta.tv_sec = 1;
156  delta.tv_usec = 1000000 - 1;
157  sc = adjtime( &delta, &olddelta );
158  rtems_test_assert( sc == 0 );
159
160  /*
161   * spin until over 1/2 of the way to the
162   */
163  ticks = rtems_clock_get_ticks_per_second();
164  rtems_test_assert( ticks );
165  ticks /= 2;
166  do {
167    status = rtems_clock_get_tod( &tod );
168    rtems_test_assert( !status );
169  } while ( tod.ticks <= ticks );
170
171  puts( "adjtime - delta of almost one second forward which bumps second" );
172  delta.tv_sec = 0;
173  delta.tv_usec = 1000000 - 1;
174  sc = adjtime( &delta, &olddelta );
175  rtems_test_assert( sc == 0 );
176
177  status = rtems_clock_get_tod( &tod );
178  rtems_test_assert( !status );
179  print_time( "rtems_clock_get_tod          ", &tod, "\n" );
180}
181
182/*
183 *  main entry point to the test
184 */
185
186#if defined(__rtems__)
187int test_main(void);
188
189int test_main(void)
190#else
191int main(
192  int    argc,
193  char **argv
194)
195#endif
196{
197  int i;
198  int sc;
199
200  puts( "\n\n*** POSIX TIME OF DAY TEST ***" );
201
202  puts( "gettimeofday( NULL, NULL ) - EFAULT" );
203  sc = gettimeofday( NULL, NULL );
204  rtems_test_assert( sc == -1 );
205  rtems_test_assert( errno == EFAULT );
206
207  puts( "_gettimeofday( NULL, NULL ) - EFAULT" );
208  sc = _gettimeofday( NULL, NULL );
209  rtems_test_assert( sc == -1 );
210  rtems_test_assert( errno == EFAULT );
211
212  test_adjtime();
213
214  /*
215   * Now test a number of dates
216   */
217
218  i = 0;
219  while ( i < NUMBER_OF_DATES ) {
220    check_a_tod( &Dates[i] );
221    i++;
222  }
223
224  puts( "\n\n*** END OF TIME OF DAY TEST 01 ***" );
225  rtems_test_exit(0);
226}
Note: See TracBrowser for help on using the repository browser.