summaryrefslogtreecommitdiffstats
path: root/testsuites/psxtests/psxtimer02/psxtimer.c
blob: 26d1d5a67cd3c3abed6acafa1241851f1d85cd99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file POSIX Timer Test #2
 *
 *  This is a simple real-time applications which contains 3 periodic tasks.
 *
 *  Task A is an independent task.
 *
 *  Task B and C share a data.
 *
 *  Tasks are implemented as POSIX threads.
 *
 *  The share data is protected with a POSIX mutex.
 *
 *  Other POSIX facilities such as timers, condition, .. are also used.
 */

/*
 *  COPYRIGHT (c) 1989-2009.
 *  On-Line Applications Research Corporation (OAR).
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#define CONFIGURE_INIT
#include "system.h"
#include "tmacros.h"
#include <signal.h>   /* signal facilities */
#include <unistd.h>   /* sleep facilities */
#include <time.h>     /* time facilities */
#include <stdio.h>    /* console facilities */

const char rtems_test_name[] = "PSXTIMER 2";

void *POSIX_Init (
  void *argument
)

{
  struct timespec   now;
  struct sigevent   event;
  int               status;
  timer_t           timer;
  timer_t           timer1;
  struct itimerspec itimer;

  /*
   *  If these are not filled in correctly, we don't pass its error checking.
   */
  event.sigev_notify = SIGEV_SIGNAL;
  event.sigev_signo = SIGUSR1;

  TEST_BEGIN();

  puts( "timer_create - bad clock id - EINVAL" );
  status = timer_create( (clockid_t) -1, &event, &timer );
  fatal_posix_service_status_errno( status, EINVAL, "bad clock id" );

  puts( "timer_create - bad timer id pointer - EINVAL" );
  status = timer_create( CLOCK_REALTIME, &event, NULL );
  fatal_posix_service_status_errno( status, EINVAL, "bad timer id" );

  puts( "timer_create - OK" );
  status = timer_create( CLOCK_REALTIME, NULL, &timer );
  posix_service_failed( status, "timer_create OK" );

  puts( "timer_create - too many - EAGAIN" );
  status = timer_create( CLOCK_REALTIME, NULL, &timer1 );
  fatal_posix_service_status_errno( status, EAGAIN, "too many" );

  puts( "timer_delete - bad id - EINVAL" );
  status = timer_delete( timer1 + 1 );
  fatal_posix_service_status_errno( status, EINVAL, "bad id" );

  puts( "timer_getoverrun - bad id - EINVAL" );
  status = timer_getoverrun( timer1 + 1 );
  fatal_posix_service_status_errno( status, EINVAL, "bad id" );

  puts( "timer_gettime - bad itimer - EINVAL" );
  status = timer_gettime( timer1, NULL );
  fatal_posix_service_status_errno( status, EINVAL, "bad id" );

  puts( "timer_gettime - bad id - EINVAL" );
  status = timer_gettime( timer1 + 1, &itimer );
  fatal_posix_service_status_errno( status, EINVAL, "bad id" );

  puts( "timer_settime - bad itimer pointer - EINVAL" );
  status = timer_settime( timer, TIMER_ABSTIME, &itimer, NULL );
  fatal_posix_service_status_errno( status, EINVAL, "bad itimer pointer" );

  itimer.it_value.tv_nsec = 2000000000;
  puts( "timer_settime - bad itimer value - too many nanosecond - EINVAL" );
  status = timer_settime( timer, TIMER_ABSTIME, &itimer, NULL );
  fatal_posix_service_status_errno( status, EINVAL, "bad itimer value #1" );

  itimer.it_value.tv_nsec = -1;
  puts( "timer_settime - bad itimer value - negative nanosecond - EINVAL" );
  status = timer_settime( timer, TIMER_ABSTIME, &itimer, NULL );
  fatal_posix_service_status_errno( status, EINVAL, "bad itimer value #2" );

  clock_gettime( CLOCK_REALTIME, &now );
  itimer.it_value = now;
  itimer.it_value.tv_sec = itimer.it_value.tv_sec - 1;
  puts( "timer_settime - bad itimer value - previous time - EINVAL" );
  status = timer_settime( timer, TIMER_ABSTIME, &itimer, NULL );
  fatal_posix_service_status_errno( status, EINVAL, "bad itimer value #3" );

  clock_gettime( CLOCK_REALTIME, &now );
  itimer.it_value = now;
  itimer.it_value.tv_sec = itimer.it_value.tv_sec + 1;
  puts( "timer_settime - bad id - EINVAL" );
  status = timer_settime( timer1, TIMER_ABSTIME, &itimer, NULL );
  fatal_posix_service_status_errno( status, EINVAL, "bad id" );

  itimer.it_value.tv_nsec = 0;
  puts( "timer_settime - bad clock value - EINVAL" );
  status = timer_settime( timer, 0x80, &itimer, NULL );
  fatal_posix_service_status_errno( status, EINVAL, "bad clock value" );

  puts( "timer_delete - OK" );
  status = timer_delete( timer );
  posix_service_failed( status, "timer_delete OK" );

  puts( "timer_delete - bad id - EINVAL" );
  status = timer_delete( timer );
  fatal_posix_service_status_errno( status, EINVAL, "bad id" );

  puts( "timer_create (monotonic) - bad timer id pointer - EINVAL" );
  status = timer_create( CLOCK_MONOTONIC, &event, NULL );
  fatal_posix_service_status_errno( status, EINVAL, "bad timer id" );

  puts( "timer_create (monotonic) - OK" );
  status = timer_create( CLOCK_MONOTONIC, NULL, &timer );
  posix_service_failed( status, "timer_create OK" );

  puts( "timer_create (monotonic) - too many - EAGAIN" );
  status = timer_create( CLOCK_MONOTONIC, NULL, &timer1 );
  fatal_posix_service_status_errno( status, EAGAIN, "too many" );

  clock_gettime( CLOCK_MONOTONIC, &now );
  itimer.it_value = now;
  itimer.it_value.tv_sec = itimer.it_value.tv_sec - 1;
  puts( "timer_settime (monotonic) - bad itimer value - previous time - EINVAL" );
  status = timer_settime( timer, TIMER_ABSTIME, &itimer, NULL );
  fatal_posix_service_status_errno( status, EINVAL, "bad itimer value #3" );

  clock_gettime( CLOCK_MONOTONIC, &now );
  itimer.it_value = now;
  itimer.it_value.tv_sec = itimer.it_value.tv_sec + 1;
  puts( "timer_settime (monotonic) - bad id - EINVAL" );
  status = timer_settime( timer1, TIMER_ABSTIME, &itimer, NULL );
  fatal_posix_service_status_errno( status, EINVAL, "bad id" );

  TEST_END();
  rtems_test_exit (0);
}