source: rtems/testsuites/psxtests/psxtime/test.c @ 9a4eca5

5
Last change on this file since 9a4eca5 was 1f67156, checked in by Sebastian Huber <sebastian.huber@…>, on 03/09/15 at 12:18:36

libcsupport: Delete superfluous _gettimeofday()

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