source: rtems/testsuites/psxtests/psxtime/test.c @ c9460e1

4.104.115
Last change on this file since c9460e1 was c9460e1, checked in by Joel Sherrill <joel.sherrill@…>, on Aug 12, 2009 at 8:50:35 PM

2009-08-12 Joel Sherrill <joel.sherrill@…>

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