summaryrefslogtreecommitdiffstats
path: root/testsuites/support/include/tmacros.h
blob: a67bb956e9cc684cb7b345a2fe73398dfe56a45e (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**
 * @file
 *
 * This include file contains macros which are useful in the RTEMS
 * test suites.
 */

/*
 *  COPYRIGHT (c) 1989-2014.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#ifndef __TMACROS_h
#define __TMACROS_h

#include <inttypes.h>
#include <bsp.h>    /* includes <rtems.h> */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rtems/error.h>
#include <rtems/test.h>
#include <rtems/score/threaddispatch.h>

#include <buffer_test_io.h>

#ifdef __cplusplus
extern "C" {
#endif

#define FOREVER 1                  /* infinite loop */

#ifdef CONFIGURE_INIT
#define TEST_EXTERN
#else
#define TEST_EXTERN extern
#endif

/*
 *  Check that that the dispatch disable level is proper for the
 *  mode/state of the test.  Normally it should be 0 when in task space.
 *
 *  This test is only valid when in a non-SMP system.  In an smp system
 *  another cpu may be accessing the core at any point when this core
 *  does not have it locked.
 */
#if defined SMPTEST
 #define check_dispatch_disable_level( _expect ) 
#else
 #define check_dispatch_disable_level( _expect ) \
  do { \
    if ( (_expect) != -1 \
           && (((!_Thread_Dispatch_is_enabled()) == false && (_expect) != 0) \
             || ((!_Thread_Dispatch_is_enabled()) && (_expect) == 0)) \
    ) { \
      printk( \
        "\n_Thread_Dispatch_disable_level is (%" PRId32 \
           ") not %d detected at %s:%d\n", \
         !_Thread_Dispatch_is_enabled(), (_expect), __FILE__, __LINE__ ); \
      FLUSH_OUTPUT(); \
      rtems_test_exit( 1 ); \
    } \
  } while ( 0 )
#endif

/*
 *  Check that that the allocator mutex is not locked. It should never
 *  be locked unless inside a service which is allocating a resource.
 *
 *  This test is only valid when in a non-SMP system.  In an SMP system
 *  another cpu may be allocating a resource while we are computing.
 */
#if defined SMPTEST
  #define check_if_allocator_mutex_is_unlocked() 
#else
  #include <rtems/score/apimutex.h>
  #define check_if_allocator_mutex_is_unlocked() \
    do { \
      if ( _RTEMS_Check_if_allocator_is_locked() ) { \
        printk( \
          "\nRTEMS Allocator Mutex is locked and should not be.\n" \
          "Detected at %s:%d\n", \
          __FILE__, \
          __LINE__ \
        ); \
        FLUSH_OUTPUT(); \
        rtems_test_exit( 1 ); \
      } \
    } while ( 0 )
#endif

/*
 *  These macros properly report errors within the Classic API
 */
#define directive_failed( _dirstat, _failmsg )  \
 fatal_directive_status( _dirstat, RTEMS_SUCCESSFUL, _failmsg )

#define directive_failed_with_level( _dirstat, _failmsg, _level )  \
 fatal_directive_status_with_level( \
      _dirstat, RTEMS_SUCCESSFUL, _failmsg, _level )

#define fatal_directive_status( _stat, _desired, _msg ) \
  fatal_directive_status_with_level( _stat, _desired, _msg, 0 )

#define fatal_directive_check_status_only( _stat, _desired, _msg ) \
  do { \
    if ( (_stat) != (_desired) ) { \
      printf( "\n%s FAILED -- expected (%s) got (%s)\n", \
              (_msg), rtems_status_text(_desired), rtems_status_text(_stat) ); \
      FLUSH_OUTPUT(); \
      rtems_test_exit( _stat ); \
    } \
  } while ( 0 )

#define fatal_directive_status_with_level( _stat, _desired, _msg, _level ) \
  do { \
    check_dispatch_disable_level( _level ); \
    check_if_allocator_mutex_is_unlocked(); \
    fatal_directive_check_status_only( _stat, _desired, _msg ); \
  } while ( 0 )

/*
 *  These macros properly report errors from the POSIX API
 */

#define posix_service_failed( _dirstat, _failmsg )  \
 fatal_posix_service_status( _dirstat, 0, _failmsg )

#define posix_service_failed_with_level( _dirstat, _failmsg, _level )  \
 fatal_posix_service_status_with_level( _dirstat, 0, _failmsg, _level )

#define fatal_posix_service_status_errno( _stat, _desired, _msg ) \
  if ( (_stat != -1) && (errno) != (_desired) ) { \
    long statx = _stat; \
    check_dispatch_disable_level( 0 ); \
    check_if_allocator_mutex_is_unlocked(); \
    printf( "\n%s FAILED -- expected (%d - %s) got (%ld %d - %s)\n", \
	    (_msg), _desired, strerror(_desired), \
            statx, errno, strerror(errno) ); \
    FLUSH_OUTPUT(); \
    rtems_test_exit( _stat ); \
  }

#define fatal_posix_service_status( _stat, _desired, _msg ) \
  fatal_posix_service_status_with_level( _stat, _desired, _msg, 0 )

#define fatal_posix_service_status_with_level( _stat, _desired, _msg, _level ) \
  do { \
    check_dispatch_disable_level( _level ); \
    check_if_allocator_mutex_is_unlocked(); \
    if ( (_stat) != (_desired) ) { \
      printf( "\n%s FAILED -- expected (%d - %s) got (%d - %s)\n", \
              (_msg), _desired, strerror(_desired), _stat, strerror(_stat) ); \
      printf( "\n FAILED -- errno (%d - %s)\n", \
              errno, strerror(errno) ); \
      FLUSH_OUTPUT(); \
      rtems_test_exit( _stat ); \
    } \
  } while ( 0 )

/*
 * This macro evaluates the semaphore id returned.
 */
#define fatal_posix_sem( _ptr, _msg ) \
  if ( (_ptr != SEM_FAILED) ) { \
    check_dispatch_disable_level( 0 ); \
    printf( "\n%s FAILED -- expected (-1) got (%p - %d/%s)\n", \
	    (_msg), _ptr, errno, strerror(errno) ); \
    FLUSH_OUTPUT(); \
    rtems_test_exit( -1 ); \
  }

/*
 * This macro evaluates the message queue id returned.
 */
#define fatal_posix_mqd( _ptr, _msg ) \
  if ( (_ptr != (mqd_t) -1) ) { \
    check_dispatch_disable_level( 0 ); \
    printf( "\n%s FAILED -- expected (-1) got (%" PRId32 " - %d/%s)\n", \
	    (_msg), _ptr, errno, strerror(errno) ); \
    FLUSH_OUTPUT(); \
    rtems_test_exit( -1 ); \
  }

/*
 *  Generic integer version of the error reporting
 */

#define int_service_failed( _dirstat, _failmsg )  \
 fatal_int_service_status( _dirstat, RTEMS_SUCCESSFUL, _failmsg )

#define int_service_failed_with_level( _dirstat, _failmsg, _level )  \
 fatal_int_service_status_with_level( \
      _dirstat, RTEMS_SUCCESSFUL, _failmsg, _level )

#define fatal_int_service_status( _stat, _desired, _msg ) \
  fatal_int_service_status_with_level( _stat, _desired, _msg, 0 )

#define fatal_int_service_status_with_level( _stat, _desired, _msg, _level ) \
  do { \
    check_dispatch_disable_level( _level ); \
    if ( (_stat) != (_desired) ) { \
      printf( "\n%s FAILED -- expected (%d) got (%d)\n", \
              (_msg), (_desired), (_stat) ); \
      FLUSH_OUTPUT(); \
      rtems_test_exit( _stat ); \
    } \
  } while ( 0 )


/*
 *  Print the time
 */

#define sprint_time(_str, _s1, _tb, _s2) \
  do { \
    sprintf( (_str), "%s%02d:%02d:%02d   %02d/%02d/%04d%s", \
       _s1, (_tb)->hour, (_tb)->minute, (_tb)->second, \
       (_tb)->month, (_tb)->day, (_tb)->year, _s2 ); \
  } while ( 0 )

#define print_time(_s1, _tb, _s2) \
  do { \
    printf( "%s%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 "   %02" PRIu32 "/%02" PRIu32 "/%04" PRIu32 "%s", \
       _s1, (_tb)->hour, (_tb)->minute, (_tb)->second, \
       (_tb)->month, (_tb)->day, (_tb)->year, _s2 ); \
  } while ( 0 )

#define put_dot( _c ) \
  do { \
    putchar( _c ); \
    FLUSH_OUTPUT(); \
  } while ( 0 )

#define new_line  puts( "" )

#define puts_nocr printf

#ifdef RTEMS_TEST_NO_PAUSE
#define rtems_test_pause() \
    do { \
      printf( "<pause>\n" ); \
      FLUSH_OUTPUT(); \
  } while ( 0 )

#define rtems_test_pause_and_screen_number( _screen ) \
  do { \
    printf( "<pause - screen %d>\n", (_screen) ); \
    FLUSH_OUTPUT(); \
  } while ( 0 )
#else
#define rtems_test_pause() \
  do { \
    char buffer[ 80 ]; \
    printf( "<pause>" ); \
    FLUSH_OUTPUT(); \
    gets( buffer ); \
    puts( "" ); \
  } while ( 0 )

#define rtems_test_pause_and_screen_number( _screen ) \
  do { \
    char buffer[ 80 ]; \
    printf( "<pause - screen %d>", (_screen) ); \
    FLUSH_OUTPUT(); \
    gets( buffer ); \
    puts( "" ); \
  } while ( 0 )
#endif

#define put_name( name, crlf ) \
{ int c0, c1, c2, c3; \
  c0 = (name >> 24) & 0xff; \
  c1 = (name >> 16) & 0xff; \
  c2 = (name >> 8) & 0xff; \
  c3 = name & 0xff; \
  putchar( (isprint(c0)) ? c0 : '*' ); \
  if ( c1 ) putchar( (isprint(c1)) ? c1 : '*' ); \
  if ( c2 ) putchar( (isprint(c2)) ? c2 : '*' ); \
  if ( c3 ) putchar( (isprint(c3)) ? c3 : '*' ); \
  if ( crlf ) \
    putchar( '\n' ); \
}

#ifndef build_time
#define build_time( TB, MON, DAY, YR, HR, MIN, SEC, TK ) \
  { (TB)->year   = YR;  \
    (TB)->month  = MON; \
    (TB)->day    = DAY; \
    (TB)->hour   = HR;  \
    (TB)->minute = MIN; \
    (TB)->second = SEC; \
    (TB)->ticks  = TK; }
#endif

#define task_number( tid ) \
  ( rtems_object_id_get_index( tid ) - \
      rtems_configuration_get_rtems_api_configuration()-> \
        number_of_initialization_tasks )

#define rtems_test_assert(__exp) \
  do { \
    if (!(__exp)) { \
      printf( "%s: %d %s\n", __FILE__, __LINE__, #__exp ); \
      rtems_test_exit(0); \
    } \
  } while (0)

/*
 * Various inttypes.h-stype macros to assist printing
 * certain system types on different targets.
 */

#if defined(RTEMS_USE_16_BIT_OBJECT)
#define PRIxrtems_id PRIx16
#else
#define PRIxrtems_id PRIx32
#endif

/* c.f. cpukit/score/include/rtems/score/priority.h */
#define PRIdPriority_Control PRId32
#define PRIxPriority_Control PRIx32
/* rtems_task_priority is a typedef to Priority_Control */
#define PRIdrtems_task_priority PRIdPriority_Control
#define PRIxrtems_task_priority PRIxPriority_Control

/* c.f. cpukit/score/include/rtems/score/watchdog.h */
#define PRIdWatchdog_Interval PRIu32
/* rtems_interval is a typedef to Watchdog_Interval */
#define PRIdrtems_interval    PRIdWatchdog_Interval

/* c.f. cpukit/score/include/rtems/score/thread.h */
#define PRIdThread_Entry_numeric_type PRIuPTR
/* rtems_task_argument is a typedef to Thread_Entry_numeric_type */
#define PRIdrtems_task_argument PRIdThread_Entry_numeric_type

/* rtems_event_set is a typedef to unit32_t */
#define PRIxrtems_event_set PRIx32

/* HACK: newlib defines pthread_t as a typedef to __uint32_t */
/* HACK: There is no portable way to print pthread_t's */
#define PRIxpthread_t PRIx32

/* rtems_signal_set is a typedef to uint32_t */
#define PRIxrtems_signal_set PRIx32

/* newlib's ino_t is a typedef to "unsigned long" */
#define PRIxino_t "lx"

/**
 * This assists in clearly disabling warnings on GCC in certain very
 * specific cases.
 *
 * + -Wnon-null - If a method is declared as never having a NULL first
 *   parameter. We need to explicitly disable this compiler warning to make
 *   the code warning free.
 */
#ifdef __GNUC__
  #define COMPILER_DIAGNOSTIC_SETTINGS_PUSH _Pragma("GCC diagnostic push")
  #define COMPILER_DIAGNOSTIC_SETTINGS_POP _Pragma("GCC diagnostic pop")
  #define COMPILER_DIAGNOSTIC_SETTINGS_DISABLE_NONNULL \
    _Pragma("GCC diagnostic ignored \"-Wnonnull\"")
#else
  #define COMPILER_DIAGNOSTIC_SETTINGS_PUSH
  #define COMPILER_DIAGNOSTIC_SETTINGS_POP
  #define COMPILER_DIAGNOSTIC_SETTINGS_DISABLE_NONNULL
#endif

#ifdef __cplusplus
}
#endif

#endif