MOAB: Mesh Oriented datABase  (version 5.5.0)
TestUtil.hpp File Reference
#include "moab/MOABConfig.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <csignal>
#include <csetjmp>
#include "moab/Types.hpp"
#include "Internals.hpp"
+ Include dependency graph for TestUtil.hpp:

Go to the source code of this file.

Macros

#define STRINGIFY_(X)   #X
 
#define STRINGIFY(X)   STRINGIFY_( X )
 
#define CHECK_ERR(A)   check_equal( MB_SUCCESS, ( A ), "MB_SUCCESS", #A, __LINE__, __FILE__ )
 
#define CHECK(A)   check_true( ( A ), #A, __LINE__, __FILE__ )
 
#define CHECK_EQUAL(EXP, ACT)   check_equal( ( EXP ), ( ACT ), #EXP, #ACT, __LINE__, __FILE__ )
 
#define CHECK_REAL_EQUAL(EXP, ACT, EPS)   check_equal( ( EXP ), ( ACT ), ( EPS ), #EXP, #ACT, __LINE__, __FILE__ )
 
#define CHECK_ARRAYS_EQUAL(EXP, EXP_LEN, ACT, ACT_LEN)    check_array_equal( ( EXP ), ( EXP_LEN ), ( ACT ), ( ACT_LEN ), #EXP, #ACT, __LINE__, __FILE__ )
 
#define CHECK_VECREAL_EQUAL(EXP, ACT, EPS)    check_equal_cartvect( ( EXP ), ( ACT ), ( EPS ), #EXP, #ACT, __LINE__, __FILE__ )
 
#define RUN_TEST(FUNC)   run_test( &( ( ( ( FUNC ) ) ) ), #FUNC )
 
#define EXCEPTION_MODE   1
 
#define FORK_MODE   2
 
#define LONGJMP_MODE   3
 
#define MODE   LONGJMP_MODE
 
#define FLAG_ERROR   siglongjmp( jmpenv, -1 )
 
#define IS_BUILDING_MB
 
#define EQUAL_TEST_IMPL(TEST, TYPE)
 

Typedefs

typedef void(* sigfunc_t) (int)
 
typedef void(* test_func) (void)
 

Functions

void sighandler (int sig)
 
int sethandler (int sig)
 
int init_signal_handlers ()
 
void flag_error ()
 
int run_test (test_func test, const char *func_name)
 
void check_equal (int A, int B, const char *sA, const char *sB, int line, const char *file)
 
void check_equal (unsigned A, unsigned B, const char *sA, const char *sB, int line, const char *file)
 
void check_equal (long A, long B, const char *sA, const char *sB, int line, const char *file)
 
void check_equal (unsigned long A, unsigned long B, const char *sA, const char *sB, int line, const char *file)
 
void check_equal (unsigned long long A, unsigned long long B, const char *sA, const char *sB, int line, const char *file)
 
void check_equal (long long A, long long B, const char *sA, const char *sB, int line, const char *file)
 
void check_equal (void *A, void *B, const char *sA, const char *sB, int line, const char *file)
 
void check_equal (const char *A, const char *B, const char *sA, const char *sB, int line, const char *file)
 
void check_equal (const std::string &A, const std::string &B, const char *sA, const char *sB, int line, const char *file)
 
void check_equal (float A, float B, float eps, const char *sA, const char *sB, int line, const char *file)
 
void check_equal (double A, double B, double eps, const char *sA, const char *sB, int line, const char *file)
 
const char * mb_error_str (moab::ErrorCode err)
 
void check_equal (moab::ErrorCode A, moab::ErrorCode B, const char *sA, const char *sB, int line, const char *file)
 
const char * mb_type_str (moab::EntityType type)
 
const char * mb_type_str (moab::EntityHandle a)
 
void check_true (bool cond, const char *str, int line, const char *file)
 

Variables

sigjmp_buf jmpenv
 
int junk_init_var = init_signal_handlers()
 

Macro Definition Documentation

◆ CHECK

#define CHECK (   A)    check_true( ( A ), #A, __LINE__, __FILE__ )

Ensure that A is true

Definition at line 37 of file TestUtil.hpp.

◆ CHECK_ARRAYS_EQUAL

#define CHECK_ARRAYS_EQUAL (   EXP,
  EXP_LEN,
  ACT,
  ACT_LEN 
)     check_array_equal( ( EXP ), ( EXP_LEN ), ( ACT ), ( ACT_LEN ), #EXP, #ACT, __LINE__, __FILE__ )

Check that two arrays contain the same values in the same order

Definition at line 43 of file TestUtil.hpp.

◆ CHECK_EQUAL

#define CHECK_EQUAL (   EXP,
  ACT 
)    check_equal( ( EXP ), ( ACT ), #EXP, #ACT, __LINE__, __FILE__ )

Check that two values are equal

Definition at line 39 of file TestUtil.hpp.

◆ CHECK_ERR

#define CHECK_ERR (   A)    check_equal( MB_SUCCESS, ( A ), "MB_SUCCESS", #A, __LINE__, __FILE__ )

Check that A is MB_SUCCESS

Definition at line 35 of file TestUtil.hpp.

◆ CHECK_REAL_EQUAL

#define CHECK_REAL_EQUAL (   EXP,
  ACT,
  EPS 
)    check_equal( ( EXP ), ( ACT ), ( EPS ), #EXP, #ACT, __LINE__, __FILE__ )

Check that two real (float or double) values are equal within EPS

Definition at line 41 of file TestUtil.hpp.

◆ CHECK_VECREAL_EQUAL

#define CHECK_VECREAL_EQUAL (   EXP,
  ACT,
  EPS 
)     check_equal_cartvect( ( EXP ), ( ACT ), ( EPS ), #EXP, #ACT, __LINE__, __FILE__ )

Check that two CartVect objects contain same values

Definition at line 46 of file TestUtil.hpp.

◆ EQUAL_TEST_IMPL

#define EQUAL_TEST_IMPL (   TEST,
  TYPE 
)
Value:
if( !( TEST ) ) \
{ \
printf( "Equality Test Failed: %s == %s\n", sA, sB ); \
printf( " at line %d of '%s'\n", line, file ); \
printf( " Expected value: %" #TYPE "\n", A ); \
printf( " Actual value: %" #TYPE "\n", B ); \
printf( "\n" ); \
flag_error(); \
}

Definition at line 391 of file TestUtil.hpp.

◆ EXCEPTION_MODE

#define EXCEPTION_MODE   1

Definition at line 56 of file TestUtil.hpp.

◆ FLAG_ERROR

#define FLAG_ERROR   siglongjmp( jmpenv, -1 )

Definition at line 124 of file TestUtil.hpp.

◆ FORK_MODE

#define FORK_MODE   2

Definition at line 61 of file TestUtil.hpp.

◆ IS_BUILDING_MB

#define IS_BUILDING_MB

Definition at line 265 of file TestUtil.hpp.

◆ LONGJMP_MODE

#define LONGJMP_MODE   3

Definition at line 70 of file TestUtil.hpp.

◆ MODE

#define MODE   LONGJMP_MODE

Definition at line 77 of file TestUtil.hpp.

◆ RUN_TEST

#define RUN_TEST (   FUNC)    run_test( &( ( ( ( FUNC ) ) ) ), #FUNC )

Run a test Argument should be a function with the signature: void func(void) Evaluates to zero if test is successful, one otherwise.

Definition at line 52 of file TestUtil.hpp.

◆ STRINGIFY

#define STRINGIFY (   X)    STRINGIFY_( X )

Definition at line 16 of file TestUtil.hpp.

◆ STRINGIFY_

#define STRINGIFY_ (   X)    #X

Definition at line 15 of file TestUtil.hpp.

Typedef Documentation

◆ sigfunc_t

typedef void( * sigfunc_t) (int)

Definition at line 149 of file TestUtil.hpp.

◆ test_func

typedef void( * test_func) (void)

Definition at line 273 of file TestUtil.hpp.

Function Documentation

◆ check_equal() [1/12]

void check_equal ( const char *  A,
const char *  B,
const char *  sA,
const char *  sB,
int  line,
const char *  file 
)

Definition at line 442 of file TestUtil.hpp.

443 {
444  EQUAL_TEST_IMPL( !strcmp( ( A ), ( B ) ), s )
445 }

References EQUAL_TEST_IMPL.

◆ check_equal() [2/12]

void check_equal ( const std::string &  A,
const std::string &  B,
const char *  sA,
const char *  sB,
int  line,
const char *  file 
)

Definition at line 447 of file TestUtil.hpp.

453 {
454  check_equal( A.c_str(), B.c_str(), sA, sB, line, file );
455 }

References check_equal().

◆ check_equal() [3/12]

void check_equal ( double  A,
double  B,
double  eps,
const char *  sA,
const char *  sB,
int  line,
const char *  file 
)

Definition at line 462 of file TestUtil.hpp.

463 {
464  EQUAL_TEST_IMPL( fabs( A - B ) <= eps, f )
465 }

References eps, and EQUAL_TEST_IMPL.

◆ check_equal() [4/12]

void check_equal ( float  A,
float  B,
float  eps,
const char *  sA,
const char *  sB,
int  line,
const char *  file 
)

Definition at line 457 of file TestUtil.hpp.

458 {
459  EQUAL_TEST_IMPL( fabsf( A - B ) <= eps, f )
460 }

References eps, and EQUAL_TEST_IMPL.

◆ check_equal() [5/12]

void check_equal ( int  A,
int  B,
const char *  sA,
const char *  sB,
int  line,
const char *  file 
)

Definition at line 402 of file TestUtil.hpp.

403 {
404  EQUAL_TEST_IMPL( A == B, d )
405 }

References EQUAL_TEST_IMPL.

Referenced by check_equal().

◆ check_equal() [6/12]

void check_equal ( long  A,
long  B,
const char *  sA,
const char *  sB,
int  line,
const char *  file 
)

Definition at line 412 of file TestUtil.hpp.

413 {
414  EQUAL_TEST_IMPL( A == B, ld )
415 }

References EQUAL_TEST_IMPL.

◆ check_equal() [7/12]

void check_equal ( long long  A,
long long  B,
const char *  sA,
const char *  sB,
int  line,
const char *  file 
)

Definition at line 432 of file TestUtil.hpp.

433 {
434  EQUAL_TEST_IMPL( A == B, lld )
435 }

References EQUAL_TEST_IMPL.

◆ check_equal() [8/12]

void check_equal ( moab::ErrorCode  A,
moab::ErrorCode  B,
const char *  sA,
const char *  sB,
int  line,
const char *  file 
)

Definition at line 512 of file TestUtil.hpp.

513 {
514  if( A == B ) return;
515 
516  printf( "ErrorCode Test Failed: %s == %s\n", sA, sB );
517  printf( " at line %d of '%s'\n", line, file );
518  printf( " Expected value: %s (%d)\n", mb_error_str( A ), (int)A );
519  printf( " Actual value: %s (%d)\n", mb_error_str( B ), (int)B );
520  printf( "\n" );
521  flag_error();
522 }

References flag_error(), and mb_error_str().

◆ check_equal() [9/12]

void check_equal ( unsigned  A,
unsigned  B,
const char *  sA,
const char *  sB,
int  line,
const char *  file 
)

Definition at line 407 of file TestUtil.hpp.

408 {
409  EQUAL_TEST_IMPL( A == B, u )
410 }

References EQUAL_TEST_IMPL.

◆ check_equal() [10/12]

void check_equal ( unsigned long  A,
unsigned long  B,
const char *  sA,
const char *  sB,
int  line,
const char *  file 
)

Definition at line 417 of file TestUtil.hpp.

418 {
419  EQUAL_TEST_IMPL( A == B, lu )
420 }

References EQUAL_TEST_IMPL.

◆ check_equal() [11/12]

void check_equal ( unsigned long long  A,
unsigned long long  B,
const char *  sA,
const char *  sB,
int  line,
const char *  file 
)

Definition at line 422 of file TestUtil.hpp.

428 {
429  EQUAL_TEST_IMPL( A == B, llu )
430 }

References EQUAL_TEST_IMPL.

◆ check_equal() [12/12]

void check_equal ( void *  A,
void *  B,
const char *  sA,
const char *  sB,
int  line,
const char *  file 
)

Definition at line 437 of file TestUtil.hpp.

438 {
439  EQUAL_TEST_IMPL( A == B, p )
440 }

References EQUAL_TEST_IMPL.

◆ check_true()

void check_true ( bool  cond,
const char *  str,
int  line,
const char *  file 
)

Definition at line 585 of file TestUtil.hpp.

586 {
587  if( !cond )
588  {
589  printf( "Test Failed: %s\n", str );
590  printf( " at line %d of '%s'\n", line, file );
591  printf( "\n" );
592  flag_error();
593  }
594 }

References flag_error().

◆ flag_error()

void flag_error ( )
inline

Definition at line 253 of file TestUtil.hpp.

254 {
255  FLAG_ERROR;
256 }

References FLAG_ERROR.

Referenced by check_equal(), check_equal_eigvect(), and check_true().

◆ init_signal_handlers()

int init_signal_handlers ( )

Definition at line 166 of file TestUtil.hpp.

167 {
168  int result = 0;
169  /* Don't trap these. It is unlikely that a test would ever generate such
170  a signal on its own and trapping them interfers with a user's ability
171  to stop a test. SIGHUP implies that the controlling terminal was closed.
172  If the user does ctrl-C or ctrl-\ (SIGINT and SIGQUIT, respectively) and
173  we trap these then just the current test stops. If we leave the default
174  behavior for them then the whole test suite stops. The latter is likely
175  the desired behavior. SIGTERM is the default signal sent by the 'kill'
176  command.
177  #ifdef SIGHUP
178  result += sethandler( SIGHUP );
179  #endif
180  #ifdef SIGINT
181  result += sethandler( SIGINT );
182  #endif
183  #ifdef SIGQUIT
184  result += sethandler( SIGQUIT );
185  #endif
186  #ifdef SIGTERM
187  result += sethandler( SIGTERM );
188  #endif
189  */
190 
191 #ifdef SIGILL
192  result += sethandler( SIGILL );
193 #endif
194 #ifdef SIGTRAP
195  result += sethandler( SIGTRAP );
196 #endif
197 #ifdef SIGABRT
198  result += sethandler( SIGABRT );
199 #endif
200 #ifdef SIGBUS
201  result += sethandler( SIGBUS );
202 #endif
203 #ifdef SIGFPE
204  result += sethandler( SIGFPE );
205 #endif
206 #ifdef SIGSEGV
207  result += sethandler( SIGSEGV );
208 #endif
209 
210  /* Catching these causes problems with mpich2 1.3.1p1 and a
211  test should never receive such a signal anyway.
212  #ifdef SIGUSR1
213  result += sethandler( SIGUSR1 );
214  #endif
215  #ifdef SIGUSR2
216  result += sethandler( SIGUSR2 );
217  #endif
218  */
219 
220  /* Don't trap SIGCHLD. The only way a test should receive
221  such a signal is if it actually forked a child process.
222  That is unlikely, but if it does happen the test probably
223  wants to handle the signal itself.
224  #ifdef SIGCHLD
225  result += sethandler( SIGCHLD );
226  #endif
227  */
228 
229 #ifdef SIGPIPE
230  result += sethandler( SIGPIPE );
231 #endif
232 #ifdef SIGIO
233  result += sethandler( SIGIO );
234 #endif
235 #ifdef SIGSYS
236  result += sethandler( SIGSYS );
237 #endif
238  return result;
239 }

References sethandler().

◆ mb_error_str()

const char* mb_error_str ( moab::ErrorCode  err)

Definition at line 467 of file TestUtil.hpp.

468 {
469  switch( err )
470  {
471  case moab::MB_SUCCESS:
472  return "Success";
474  return "Index Out of Range";
476  return "Type Out of Range";
478  return "Memory Alloc. Failed";
480  return "Entity Not Found";
482  return "Multiple Entities Found";
484  return "Tag Not Found";
486  return "File Not Found";
488  return "File Write Error";
490  return "Not Implemented";
492  return "Already Allocated";
494  return "Variable Data Length";
496  return "Invalid Size";
498  return "Unsupported Operation";
500  return "Unhandled Option";
502  return "Structured Mesh";
503  case moab::MB_FAILURE:
504  return "Failure";
505  default:
506  return "(unknown)";
507  }
508 }

References MB_ALREADY_ALLOCATED, MB_ENTITY_NOT_FOUND, MB_FILE_DOES_NOT_EXIST, MB_FILE_WRITE_ERROR, MB_INDEX_OUT_OF_RANGE, MB_INVALID_SIZE, MB_MEMORY_ALLOCATION_FAILED, MB_MULTIPLE_ENTITIES_FOUND, MB_NOT_IMPLEMENTED, MB_STRUCTURED_MESH, MB_SUCCESS, MB_TAG_NOT_FOUND, MB_TYPE_OUT_OF_RANGE, MB_UNHANDLED_OPTION, MB_UNSUPPORTED_OPERATION, and MB_VARIABLE_DATA_LENGTH.

Referenced by check_equal().

◆ mb_type_str() [1/2]

const char* mb_type_str ( moab::EntityHandle  a)

Definition at line 559 of file TestUtil.hpp.

560 {
561  return mb_type_str( moab::TYPE_FROM_HANDLE( a ) );
562 }

References mb_type_str(), and moab::TYPE_FROM_HANDLE().

◆ mb_type_str() [2/2]

const char* mb_type_str ( moab::EntityType  type)

Definition at line 524 of file TestUtil.hpp.

525 {
526  switch( type )
527  {
528  case moab::MBVERTEX:
529  return "Vertex";
530  case moab::MBEDGE:
531  return "Edge";
532  case moab::MBTRI:
533  return "Triangle";
534  case moab::MBQUAD:
535  return "Quadrilateral";
536  case moab::MBPOLYGON:
537  return "Polygon";
538  case moab::MBTET:
539  return "Tetrahedron";
540  case moab::MBPYRAMID:
541  return "Pyramid";
542  case moab::MBPRISM:
543  return "Prism (wedge)";
544  case moab::MBKNIFE:
545  return "Knife";
546  case moab::MBHEX:
547  return "Hexahedron";
548  case moab::MBPOLYHEDRON:
549  return "Polyhedron";
550  case moab::MBENTITYSET:
551  return "Entity (Mesh) Set";
552  case moab::MBMAXTYPE:
553  return "(max type)";
554  default:
555  return "(unknown)";
556  }
557 }

References MBEDGE, MBENTITYSET, MBHEX, MBKNIFE, MBMAXTYPE, MBPOLYGON, MBPOLYHEDRON, MBPRISM, MBPYRAMID, MBQUAD, MBTET, MBTRI, and MBVERTEX.

Referenced by mb_type_str().

◆ run_test()

int run_test ( test_func  test,
const char *  func_name 
)

Definition at line 274 of file TestUtil.hpp.

279 {
280  // check if we are running parallel MPI tests
281  int rank = 0;
282 #ifdef MOAB_HAVE_MPI
283  int isInit;
284  MPI_Initialized( &isInit );
285  if( isInit )
286  {
287  MPI_Comm_rank( MPI_COMM_WORLD, &rank );
288  }
289 #endif
290 
291  if( rank == 0 ) printf( "Running %s ...\n", func_name );
292 
293 #if MODE == EXCEPTION_MODE
294  /* On Windows, run all tests in same process.
295  Flag errors by throwing an exception.
296  */
297  try
298  {
299  ( *test )();
300  return 0;
301  }
302  catch( ErrorExcept )
303  {
304  printf( "[%d] %s: FAILED\n", rank, func_name );
305  return 1;
306  }
307  catch( ... )
308  {
309  printf( "[%d] %s: UNCAUGHT EXCEPTION\n", rank, func_name );
310  return 1;
311  }
312 
313 #elif MODE == FORK_MODE
314  /* For non-Windows OSs, fork() and run test in child process. */
315  pid_t pid = fork();
316  int status;
317 
318  /* Fork failed? */
319  if( pid == -1 )
320  {
321  perror( "fork()" );
322  abort(); /* abort all tests (can't fork child processes) */
323  }
324 
325  /* If child process*/
326  if( pid == 0 )
327  {
328  ( *test )(); /* call test function */
329  exit( 0 ); /* if function returned, then it succeeded */
330  }
331 
332  /* If here, then parent process */
333 
334  /* Wait until child process exits */
335  waitpid( pid, &status, 0 );
336 
337  /* Check child exit status */
338  if( WIFSIGNALED( status ) )
339  {
340  if( WTERMSIG( status ) ) printf( " %s: TERMINATED (signal %d)\n", func_name, (int)WTERMSIG( status ) );
341  if( WCOREDUMP( status ) ) printf( " %s: CORE DUMP\n", func_name );
342  return 1;
343  }
344  else if( WEXITSTATUS( status ) )
345  {
346  printf( " %s: FAILED\n", func_name );
347  return 1;
348  }
349  else
350  {
351  return 0;
352  }
353 
354 #elif MODE == LONGJMP_MODE
355  // Save stack state at this location.
356  int rval = sigsetjmp( jmpenv, 1 );
357  // If rval is zero, then we haven't run the test yet.
358  // If rval is non-zero then
359  // a) we ran the test
360  // b) the test failed
361  // c) we did a longjmp back to the location where we called setsigjmp.
362 
363  // run test
364  if( !rval )
365  {
366  ( *test )();
367  return 0;
368  }
369  // some check failed
370  else if( rval == -1 )
371  {
372  printf( " %s: FAILED\n", func_name );
373  return 1;
374  }
375  // a signal was raised (e.g. segfault)
376  else
377  {
378  printf( " %s: TERMINATED (signal %d)\n", func_name, rval );
379  return 1;
380  }
381 #else
382 #error "MODE not set"
383 #endif // MODE
384 }

References jmpenv, MPI_COMM_WORLD, and rank.

◆ sethandler()

int sethandler ( int  sig)

Definition at line 153 of file TestUtil.hpp.

154 {
155  sigfunc_t h = signal( sig, &sighandler );
156  if( h == SIG_ERR ) return 1;
157  // If user-defined signal handler (or signal is ignored),
158  // than unregister our handler.
159  else if( h != SIG_DFL )
160  signal( sig, h );
161  return 0;
162 }

References sighandler().

Referenced by init_signal_handlers().

◆ sighandler()

void sighandler ( int  sig)

Definition at line 142 of file TestUtil.hpp.

143 {
144  signal( sig, sighandler );
145  siglongjmp( jmpenv, sig );
146  // should never return from longjmp
147  exit( 1 );
148 }

References jmpenv.

Referenced by sethandler().

Variable Documentation

◆ jmpenv

sigjmp_buf jmpenv

Definition at line 136 of file TestUtil.hpp.

Referenced by run_test(), and sighandler().

◆ junk_init_var

int junk_init_var = init_signal_handlers()

Definition at line 243 of file TestUtil.hpp.