source: rtems/testsuites/psxtests/psxtime/test.c @ 89b897f0

4.115
Last change on this file since 89b897f0 was 89b897f0, checked in by Joel Sherrill <joel.sherrill@…>, on 06/28/10 at 21:00:15

2010-06-28 Joel Sherrill <joel.sherrill@…>

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