NeoMutt  2020-06-26-89-g172cd3
Teaching an old dog new tricks
DOXYGEN
acutest.h File Reference
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
+ Include dependency graph for acutest.h:

Go to the source code of this file.

Data Structures

struct  test_
 
struct  test_detail_
 
struct  TEST_CMDLINE_OPTION_
 

Macros

#define TEST_LIST   const struct test_ test_list_[]
 
#define TEST_CHECK_(cond, ...)   test_check_((cond), __FILE__, __LINE__, __VA_ARGS__)
 
#define TEST_CHECK(cond)   test_check_((cond), __FILE__, __LINE__, "%s", #cond)
 
#define TEST_ASSERT_(cond, ...)
 
#define TEST_ASSERT(cond)
 
#define TEST_CASE_(...)   test_case_(__VA_ARGS__)
 
#define TEST_CASE(name)   test_case_("%s", name)
 
#define TEST_CASE_MAXSIZE   64
 
#define TEST_MSG(...)   test_message_(__VA_ARGS__)
 
#define TEST_MSG_MAXSIZE   1024
 
#define TEST_DUMP(title, addr, size)   test_dump_(title, addr, size)
 
#define TEST_DUMP_MAXSIZE   1024
 
#define TEST_ATTRIBUTE_(attr)
 
#define TEST_COLOR_DEFAULT_   0
 
#define TEST_COLOR_GREEN_   1
 
#define TEST_COLOR_RED_   2
 
#define TEST_COLOR_DEFAULT_INTENSIVE_   3
 
#define TEST_COLOR_GREEN_INTENSIVE_   4
 
#define TEST_COLOR_RED_INTENSIVE_   5
 
#define TEST_CMDLINE_OPTFLAG_OPTIONALARG_   0x0001
 
#define TEST_CMDLINE_OPTFLAG_REQUIREDARG_   0x0002
 
#define TEST_CMDLINE_OPTID_NONE_   0
 
#define TEST_CMDLINE_OPTID_UNKNOWN_   (-0x7fffffff + 0)
 
#define TEST_CMDLINE_OPTID_MISSINGARG_   (-0x7fffffff + 1)
 
#define TEST_CMDLINE_OPTID_BOGUSARG_   (-0x7fffffff + 2)
 
#define TEST_CMDLINE_AUXBUF_SIZE_   32
 

Typedefs

typedef int test_timer_type_
 
typedef struct TEST_CMDLINE_OPTION_ TEST_CMDLINE_OPTION_
 

Enumerations

enum  { TEST_FLAG_RUN_ = 1 << 0, TEST_FLAG_SUCCESS_ = 1 << 1, TEST_FLAG_FAILURE_ = 1 << 2 }
 

Functions

int test_check_ (int cond, const char *file, int line, const char *fmt,...)
 
void test_case_ (const char *fmt,...)
 
void test_message_ (const char *fmt,...)
 
void test_dump_ (const char *title, const void *addr, size_t size)
 
void test_abort_ (void) TEST_ATTRIBUTE_(noreturn)
 
void test_timer_init_ (void)
 
static void test_timer_get_time_ (int *ts)
 
static double test_timer_diff_ (int start, int end)
 
static void test_timer_print_diff_ (void)
 
static int TEST_ATTRIBUTE_ (format(printf, 2, 3)) test_print_in_color_(int color
 
 va_start (args, fmt)
 
 vsnprintf (buffer, sizeof(buffer), fmt, args)
 
 va_end (args)
 
 if (!test_colorize_)
 
static void test_begin_test_line_ (const struct test_ *test)
 
static void test_finish_test_line_ (int result)
 
static void test_line_indent_ (int level)
 
int TEST_ATTRIBUTE_ (format(printf, 4, 5)) test_check_(int cond
 
 if (cond)
 
 if (test_verbose_level_ >=verbose_level)
 
void TEST_ATTRIBUTE_ (format(printf, 1, 2)) test_case_(const char *fmt
 
void if (test_verbose_level_< 2) return
 
 if (test_case_name_[0])
 
 if (fmt==NULL) return
 
 vsnprintf (test_case_name_, sizeof(test_case_name_) - 1, fmt, args)
 
 if (test_current_unit_==NULL||!test_cond_failed_) return
 
 vsnprintf (buffer, TEST_MSG_MAXSIZE, fmt, args)
 
 while (1)
 
 if (line_beg[0] !='\0')
 
static void test_list_names_ (void)
 
static void test_remember_ (int i)
 
static void test_set_success_ (int i, int success)
 
static void test_set_duration_ (int i, double duration)
 
static int test_name_contains_word_ (const char *name, const char *pattern)
 
static int test_lookup_ (const char *pattern)
 
static void test_init_ (const char *test_name)
 
static void test_fini_ (const char *test_name)
 
static int test_do_run_ (const struct test_ *test, int index)
 
static void test_run_ (const struct test_ *test, int index, int master_index)
 
static int test_cmdline_handle_short_opt_group_ (const TEST_CMDLINE_OPTION_ *options, const char *arggroup, int(*callback)(int, const char *))
 
static int test_cmdline_read_ (const TEST_CMDLINE_OPTION_ *options, int argc, char **argv, int(*callback)(int, const char *))
 
static void test_help_ (void)
 
static int test_cmdline_callback_ (int id, const char *arg)
 
int main (int argc, char **argv)
 

Variables

const struct test_ test_list_ []
 
static char * test_argv0_ = NULL
 
static size_t test_list_size_ = 0
 
static struct test_detail_test_details_ = NULL
 
static size_t test_count_ = 0
 
static int test_no_exec_ = -1
 
static int test_no_summary_ = 0
 
static int test_tap_ = 0
 
static int test_skip_mode_ = 0
 
static int test_worker_ = 0
 
static int test_worker_index_ = 0
 
static int test_cond_failed_ = 0
 
static int test_was_aborted_ = 0
 
static FILE * test_xml_output_ = NULL
 
static int test_stat_failed_units_ = 0
 
static int test_stat_run_units_ = 0
 
static const struct test_test_current_unit_ = NULL
 
static int test_current_index_ = 0
 
static char test_case_name_ [TEST_CASE_MAXSIZE] = ""
 
static int test_current_already_logged_ = 0
 
static int test_case_current_already_logged_ = 0
 
static int test_verbose_level_ = 2
 
static int test_current_failures_ = 0
 
static int test_colorize_ = 0
 
static int test_timer_ = 0
 
static int test_abort_has_jmp_buf_ = 0
 
static jmp_buf test_abort_jmp_buf_
 
static test_timer_type_ test_timer_start_
 
static test_timer_type_ test_timer_end_
 
static int const char * fmt
 
static int const char char buffer [256]
 
int n = printf("%s", buffer)
 
int const char * file
 
int const char int line
 
int const char int const char int result_color
 
int verbose_level = 2
 
 else
 
 result_str = "failed"
 
return !test_cond_failed_
 
void char * line_beg
 
char * line_end
 
va_list args
 
static const TEST_CMDLINE_OPTION_ test_cmdline_options_ []
 

Macro Definition Documentation

◆ TEST_LIST

#define TEST_LIST   const struct test_ test_list_[]

Definition at line 60 of file acutest.h.

◆ TEST_CHECK_

#define TEST_CHECK_ (   cond,
  ... 
)    test_check_((cond), __FILE__, __LINE__, __VA_ARGS__)

Definition at line 82 of file acutest.h.

◆ TEST_CHECK

#define TEST_CHECK (   cond)    test_check_((cond), __FILE__, __LINE__, "%s", #cond)

Definition at line 83 of file acutest.h.

◆ TEST_ASSERT_

#define TEST_ASSERT_ (   cond,
  ... 
)
Value:
do { \
if(!test_check_((cond), __FILE__, __LINE__, __VA_ARGS__)) \
test_abort_(); \
} while(0)
int test_check_(int cond, const char *file, int line, const char *fmt,...)

Definition at line 101 of file acutest.h.

◆ TEST_ASSERT

#define TEST_ASSERT (   cond)
Value:
do { \
if(!test_check_((cond), __FILE__, __LINE__, "%s", #cond)) \
test_abort_(); \
} while(0)
int test_check_(int cond, const char *file, int line, const char *fmt,...)

Definition at line 106 of file acutest.h.

◆ TEST_CASE_

#define TEST_CASE_ (   ...)    test_case_(__VA_ARGS__)

Definition at line 181 of file acutest.h.

◆ TEST_CASE

#define TEST_CASE (   name)    test_case_("%s", name)

Definition at line 182 of file acutest.h.

◆ TEST_CASE_MAXSIZE

#define TEST_CASE_MAXSIZE   64

Definition at line 189 of file acutest.h.

◆ TEST_MSG

#define TEST_MSG (   ...)    test_message_(__VA_ARGS__)

Definition at line 214 of file acutest.h.

◆ TEST_MSG_MAXSIZE

#define TEST_MSG_MAXSIZE   1024

Definition at line 221 of file acutest.h.

◆ TEST_DUMP

#define TEST_DUMP (   title,
  addr,
  size 
)    test_dump_(title, addr, size)

Definition at line 236 of file acutest.h.

◆ TEST_DUMP_MAXSIZE

#define TEST_DUMP_MAXSIZE   1024

Definition at line 242 of file acutest.h.

◆ TEST_ATTRIBUTE_

#define TEST_ATTRIBUTE_ (   attr)

Definition at line 301 of file acutest.h.

◆ TEST_COLOR_DEFAULT_

#define TEST_COLOR_DEFAULT_   0

Definition at line 480 of file acutest.h.

◆ TEST_COLOR_GREEN_

#define TEST_COLOR_GREEN_   1

Definition at line 481 of file acutest.h.

◆ TEST_COLOR_RED_

#define TEST_COLOR_RED_   2

Definition at line 482 of file acutest.h.

◆ TEST_COLOR_DEFAULT_INTENSIVE_

#define TEST_COLOR_DEFAULT_INTENSIVE_   3

Definition at line 483 of file acutest.h.

◆ TEST_COLOR_GREEN_INTENSIVE_

#define TEST_COLOR_GREEN_INTENSIVE_   4

Definition at line 484 of file acutest.h.

◆ TEST_COLOR_RED_INTENSIVE_

#define TEST_COLOR_RED_INTENSIVE_   5

Definition at line 485 of file acutest.h.

◆ TEST_CMDLINE_OPTFLAG_OPTIONALARG_

#define TEST_CMDLINE_OPTFLAG_OPTIONALARG_   0x0001

Definition at line 1181 of file acutest.h.

◆ TEST_CMDLINE_OPTFLAG_REQUIREDARG_

#define TEST_CMDLINE_OPTFLAG_REQUIREDARG_   0x0002

Definition at line 1182 of file acutest.h.

◆ TEST_CMDLINE_OPTID_NONE_

#define TEST_CMDLINE_OPTID_NONE_   0

Definition at line 1184 of file acutest.h.

◆ TEST_CMDLINE_OPTID_UNKNOWN_

#define TEST_CMDLINE_OPTID_UNKNOWN_   (-0x7fffffff + 0)

Definition at line 1185 of file acutest.h.

◆ TEST_CMDLINE_OPTID_MISSINGARG_

#define TEST_CMDLINE_OPTID_MISSINGARG_   (-0x7fffffff + 1)

Definition at line 1186 of file acutest.h.

◆ TEST_CMDLINE_OPTID_BOGUSARG_

#define TEST_CMDLINE_OPTID_BOGUSARG_   (-0x7fffffff + 2)

Definition at line 1187 of file acutest.h.

◆ TEST_CMDLINE_AUXBUF_SIZE_

#define TEST_CMDLINE_AUXBUF_SIZE_   32

Definition at line 1230 of file acutest.h.

Typedef Documentation

◆ test_timer_type_

typedef int test_timer_type_

Definition at line 453 of file acutest.h.

◆ TEST_CMDLINE_OPTION_

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
TEST_FLAG_RUN_ 
TEST_FLAG_SUCCESS_ 
TEST_FLAG_FAILURE_ 

Definition at line 330 of file acutest.h.

330  {
331  TEST_FLAG_RUN_ = 1 << 0,
332  TEST_FLAG_SUCCESS_ = 1 << 1,
333  TEST_FLAG_FAILURE_ = 1 << 2,
334 };

Function Documentation

◆ test_check_()

int test_check_ ( int  cond,
const char *  file,
int  line,
const char *  fmt,
  ... 
)
+ Here is the caller graph for this function:

◆ test_case_()

void test_case_ ( const char *  fmt,
  ... 
)
+ Here is the caller graph for this function:

◆ test_message_()

void test_message_ ( const char *  fmt,
  ... 
)
+ Here is the caller graph for this function:

◆ test_dump_()

void test_dump_ ( const char *  title,
const void *  addr,
size_t  size 
)

Definition at line 746 of file acutest.h.

747 {
748  static const size_t BYTES_PER_LINE = 16;
749  size_t line_beg;
750  size_t truncate = 0;
751 
752  if(test_verbose_level_ < 2)
753  return;
754 
755  /* We allow extra message only when something is already wrong in the
756  * current test. */
757  if(test_current_unit_ == NULL || !test_cond_failed_)
758  return;
759 
760  if(size > TEST_DUMP_MAXSIZE) {
761  truncate = size - TEST_DUMP_MAXSIZE;
762  size = TEST_DUMP_MAXSIZE;
763  }
764 
766  printf((title[strlen(title)-1] == ':') ? "%s\n" : "%s:\n", title);
767 
768  for(line_beg = 0; line_beg < size; line_beg += BYTES_PER_LINE) {
769  size_t line_end = line_beg + BYTES_PER_LINE;
770  size_t off;
771 
773  printf("%08lx: ", (unsigned long)line_beg);
774  for(off = line_beg; off < line_end; off++) {
775  if(off < size)
776  printf(" %02x", ((const unsigned char*)addr)[off]);
777  else
778  printf(" ");
779  }
780 
781  printf(" ");
782  for(off = line_beg; off < line_end; off++) {
783  unsigned char byte = ((const unsigned char*)addr)[off];
784  if(off < size)
785  printf("%c", (iscntrl(byte) ? '.' : byte));
786  else
787  break;
788  }
789 
790  printf("\n");
791  }
792 
793  if(truncate > 0) {
795  printf(" ... (and more %u bytes)\n", (unsigned) truncate);
796  }
797 }
static int test_cond_failed_
Definition: acutest.h:357
char * line_end
Definition: acutest.h:714
#define TEST_DUMP_MAXSIZE
Definition: acutest.h:242
static int test_verbose_level_
Definition: acutest.h:369
static char test_case_name_[TEST_CASE_MAXSIZE]
Definition: acutest.h:366
static void test_line_indent_(int level)
Definition: acutest.h:599
void char * line_beg
Definition: acutest.h:711
static const struct test_ * test_current_unit_
Definition: acutest.h:364
+ Here is the call graph for this function:

◆ test_abort_()

void test_abort_ ( void  )

Definition at line 800 of file acutest.h.

801 {
803  longjmp(test_abort_jmp_buf_, 1);
804  else
805  abort();
806 }
static jmp_buf test_abort_jmp_buf_
Definition: acutest.h:375
static int test_abort_has_jmp_buf_
Definition: acutest.h:374

◆ test_timer_init_()

void test_timer_init_ ( void  )

Definition at line 458 of file acutest.h.

459  {}
+ Here is the caller graph for this function:

◆ test_timer_get_time_()

static void test_timer_get_time_ ( int *  ts)
static

Definition at line 462 of file acutest.h.

463  {
464  (void) ts;
465  }
+ Here is the caller graph for this function:

◆ test_timer_diff_()

static double test_timer_diff_ ( int  start,
int  end 
)
static

Definition at line 468 of file acutest.h.

469  {
470  (void) start;
471  (void) end;
472  return 0.0;
473  }
+ Here is the caller graph for this function:

◆ test_timer_print_diff_()

static void test_timer_print_diff_ ( void  )
static

Definition at line 476 of file acutest.h.

477  {}
+ Here is the caller graph for this function:

◆ TEST_ATTRIBUTE_() [1/3]

static int TEST_ATTRIBUTE_ ( format(printf, 2, 3)  )
static

◆ va_start()

va_start ( args  ,
fmt   
)
+ Here is the caller graph for this function:

◆ vsnprintf() [1/3]

vsnprintf ( buffer  ,
sizeof(buffer ,
fmt  ,
args   
)
+ Here is the caller graph for this function:

◆ va_end()

va_end ( args  )
+ Here is the caller graph for this function:

◆ if() [1/8]

if ( test_colorize_)

Definition at line 499 of file acutest.h.

499  {
500  return printf("%s", buffer);
501  }
static int const char char buffer[256]
Definition: acutest.h:489
+ Here is the caller graph for this function:

◆ test_begin_test_line_()

static void test_begin_test_line_ ( const struct test_ test)
static

Definition at line 549 of file acutest.h.

550 {
551  if(!test_tap_) {
552  if(test_verbose_level_ >= 3) {
553  test_print_in_color_(TEST_COLOR_DEFAULT_INTENSIVE_, "Test %s:\n", test->name);
555  } else if(test_verbose_level_ >= 1) {
556  int n;
557  char spaces[48];
558 
559  n = test_print_in_color_(TEST_COLOR_DEFAULT_INTENSIVE_, "Test %s... ", test->name);
560  memset(spaces, ' ', sizeof(spaces));
561  if(n < (int) sizeof(spaces))
562  printf("%.*s", (int) sizeof(spaces) - n, spaces);
563  } else {
565  }
566  }
567 }
static int test_tap_
Definition: acutest.h:353
static int test_verbose_level_
Definition: acutest.h:369
int n
Definition: acutest.h:492
static int test_current_already_logged_
Definition: acutest.h:367
const char * name
Definition: acutest.h:321
#define TEST_COLOR_DEFAULT_INTENSIVE_
Definition: acutest.h:483
+ Here is the caller graph for this function:

◆ test_finish_test_line_()

static void test_finish_test_line_ ( int  result)
static

Definition at line 570 of file acutest.h.

571 {
572  if(test_tap_) {
573  const char* str = (result == 0) ? "ok" : "not ok";
574 
575  printf("%s %d - %s\n", str, test_current_index_ + 1, test_current_unit_->name);
576 
577  if(result == 0 && test_timer_) {
578  printf("# Duration: ");
580  printf("\n");
581  }
582  } else {
583  int color = (result == 0) ? TEST_COLOR_GREEN_INTENSIVE_ : TEST_COLOR_RED_INTENSIVE_;
584  const char* str = (result == 0) ? "OK" : "FAILED";
585  printf("[ ");
586  test_print_in_color_(color, "%s", str);
587  printf(" ]");
588 
589  if(result == 0 && test_timer_) {
590  printf(" ");
592  }
593 
594  printf("\n");
595  }
596 }
static void test_timer_print_diff_(void)
Definition: acutest.h:476
static int test_tap_
Definition: acutest.h:353
#define TEST_COLOR_GREEN_INTENSIVE_
Definition: acutest.h:484
#define TEST_COLOR_RED_INTENSIVE_
Definition: acutest.h:485
static int test_timer_
Definition: acutest.h:372
const char * name
Definition: acutest.h:321
static const struct test_ * test_current_unit_
Definition: acutest.h:364
static int test_current_index_
Definition: acutest.h:365
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ test_line_indent_()

static void test_line_indent_ ( int  level)
static

Definition at line 599 of file acutest.h.

600 {
601  static const char spaces[] = " ";
602  int n = level * 2;
603 
604  if(test_tap_ && n > 0) {
605  n--;
606  printf("#");
607  }
608 
609  while(n > 16) {
610  printf("%s", spaces);
611  n -= 16;
612  }
613  printf("%.*s", n, spaces);
614 }
static int test_tap_
Definition: acutest.h:353
int n
Definition: acutest.h:492
+ Here is the caller graph for this function:

◆ TEST_ATTRIBUTE_() [2/3]

int TEST_ATTRIBUTE_ ( format(printf, 4, 5)  )

◆ if() [2/8]

if ( cond  )

Definition at line 623 of file acutest.h.

623  {
624  result_str = "ok";
626  verbose_level = 3;
627  } else {
int verbose_level
Definition: acutest.h:621
result_str
Definition: acutest.h:631
int const char int const char int result_color
Definition: acutest.h:618
#define TEST_COLOR_GREEN_
Definition: acutest.h:481

◆ if() [3/8]

if ( test_verbose_level_ >=  verbose_level)
Initial value:
{
return
static int test_verbose_level_
Definition: acutest.h:369

Definition at line 638 of file acutest.h.

638  {
639  va_list args;
640 
643  test_print_in_color_(TEST_COLOR_DEFAULT_INTENSIVE_, "Case %s:\n", test_case_name_);
646  }
647 
649  if(file != NULL) {
650 #ifdef ACUTEST_WIN_
651  const char* lastsep1 = strrchr(file, '\\');
652  const char* lastsep2 = strrchr(file, '/');
653  if(lastsep1 == NULL)
654  lastsep1 = file-1;
655  if(lastsep2 == NULL)
656  lastsep2 = file-1;
657  file = (lastsep1 > lastsep2 ? lastsep1 : lastsep2) + 1;
658 #else
659  const char* lastsep = strrchr(file, '/');
660  if(lastsep != NULL)
661  file = lastsep+1;
662 #endif
663  printf("%s:%d: Check ", file, line);
664  }
665 
666  va_start(args, fmt);
667  vprintf(fmt, args);
668  va_end(args);
669 
670  printf("... ");
671  test_print_in_color_(result_color, "%s", result_str);
672  printf("\n");
674  }
static int const char * fmt
Definition: acutest.h:488
va_start(args, fmt)
static char test_case_name_[TEST_CASE_MAXSIZE]
Definition: acutest.h:366
result_str
Definition: acutest.h:631
static void test_line_indent_(int level)
Definition: acutest.h:599
static int test_current_already_logged_
Definition: acutest.h:367
int const char int const char int result_color
Definition: acutest.h:618
static int test_case_current_already_logged_
Definition: acutest.h:368
int const char * file
Definition: acutest.h:617
va_list args
Definition: acutest.h:715
#define TEST_COLOR_DEFAULT_INTENSIVE_
Definition: acutest.h:483
int const char int line
Definition: acutest.h:617
va_end(args)
+ Here is the call graph for this function:

◆ TEST_ATTRIBUTE_() [3/3]

static void TEST_ATTRIBUTE_ ( format(printf, 1, 2)  ) const

◆ if() [4/8]

if ( )

◆ if() [5/8]

if ( test_case_name_  [0])

Definition at line 688 of file acutest.h.

688  {
690  test_case_name_[0] = '\0';
691  }
static char test_case_name_[TEST_CASE_MAXSIZE]
Definition: acutest.h:366
static int test_case_current_already_logged_
Definition: acutest.h:368
+ Here is the call graph for this function:

◆ if() [6/8]

if ( fmt  = =NULL)

◆ vsnprintf() [2/3]

vsnprintf ( test_case_name_  ,
sizeof(test_case_name_) -  1,
fmt  ,
args   
)

◆ if() [7/8]

◆ vsnprintf() [3/3]

vsnprintf ( buffer  ,
TEST_MSG_MAXSIZE  ,
fmt  ,
args   
)

◆ while()

while ( )

Definition at line 731 of file acutest.h.

731  {
732  line_end = strchr(line_beg, '\n');
733  if(line_end == NULL)
734  break;
736  printf("%.*s\n", (int)(line_end - line_beg), line_beg);
737  line_beg = line_end + 1;
738  }
char * line_end
Definition: acutest.h:714
static char test_case_name_[TEST_CASE_MAXSIZE]
Definition: acutest.h:366
static void test_line_indent_(int level)
Definition: acutest.h:599
void char * line_beg
Definition: acutest.h:711
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ if() [8/8]

if ( line_beg [0] = '\0')

Definition at line 739 of file acutest.h.

739  {
741  printf("%s\n", line_beg);
742  }
static char test_case_name_[TEST_CASE_MAXSIZE]
Definition: acutest.h:366
static void test_line_indent_(int level)
Definition: acutest.h:599
void char * line_beg
Definition: acutest.h:711
+ Here is the call graph for this function:

◆ test_list_names_()

static void test_list_names_ ( void  )
static

Definition at line 809 of file acutest.h.

810 {
811  const struct test_* test;
812 
813  printf("Unit tests:\n");
814  for(test = &test_list_[0]; test->func != NULL; test++)
815  printf(" %s\n", test->name);
816 }
const struct test_ test_list_[]
Definition: acutest.h:320
void(* func)(void)
Definition: acutest.h:322
struct TestData test[]
const char * name
Definition: acutest.h:321
+ Here is the caller graph for this function:

◆ test_remember_()

static void test_remember_ ( int  i)
static

Definition at line 819 of file acutest.h.

820 {
821  if(test_details_[i].flags & TEST_FLAG_RUN_)
822  return;
823 
825  test_count_++;
826 }
static size_t test_count_
Definition: acutest.h:350
static struct test_detail_ * test_details_
Definition: acutest.h:349
unsigned char flags
Definition: acutest.h:326
+ Here is the caller graph for this function:

◆ test_set_success_()

static void test_set_success_ ( int  i,
int  success 
)
static

Definition at line 829 of file acutest.h.

830 {
832 }
static struct test_detail_ * test_details_
Definition: acutest.h:349
unsigned char flags
Definition: acutest.h:326
+ Here is the caller graph for this function:

◆ test_set_duration_()

static void test_set_duration_ ( int  i,
double  duration 
)
static

Definition at line 835 of file acutest.h.

836 {
837  test_details_[i].duration = duration;
838 }
double duration
Definition: acutest.h:327
static struct test_detail_ * test_details_
Definition: acutest.h:349
+ Here is the caller graph for this function:

◆ test_name_contains_word_()

static int test_name_contains_word_ ( const char *  name,
const char *  pattern 
)
static

Definition at line 841 of file acutest.h.

842 {
843  static const char word_delim[] = " \t-_/.,:;";
844  const char* substr;
845  size_t pattern_len;
846 
847  pattern_len = strlen(pattern);
848 
849  substr = strstr(name, pattern);
850  while(substr != NULL) {
851  int starts_on_word_boundary = (substr == name || strchr(word_delim, substr[-1]) != NULL);
852  int ends_on_word_boundary = (substr[pattern_len] == '\0' || strchr(word_delim, substr[pattern_len]) != NULL);
853 
854  if(starts_on_word_boundary && ends_on_word_boundary)
855  return 1;
856 
857  substr = strstr(substr+1, pattern);
858  }
859 
860  return 0;
861 }
const char * name
Definition: acutest.h:321
+ Here is the caller graph for this function:

◆ test_lookup_()

static int test_lookup_ ( const char *  pattern)
static

Definition at line 864 of file acutest.h.

865 {
866  int i;
867  int n = 0;
868 
869  /* Try exact match. */
870  for(i = 0; i < (int) test_list_size_; i++) {
871  if(strcmp(test_list_[i].name, pattern) == 0) {
872  test_remember_(i);
873  n++;
874  break;
875  }
876  }
877  if(n > 0)
878  return n;
879 
880  /* Try word match. */
881  for(i = 0; i < (int) test_list_size_; i++) {
882  if(test_name_contains_word_(test_list_[i].name, pattern)) {
883  test_remember_(i);
884  n++;
885  }
886  }
887  if(n > 0)
888  return n;
889 
890  /* Try relaxed match. */
891  for(i = 0; i < (int) test_list_size_; i++) {
892  if(strstr(test_list_[i].name, pattern) != NULL) {
893  test_remember_(i);
894  n++;
895  }
896  }
897 
898  return n;
899 }
static void test_remember_(int i)
Definition: acutest.h:819
const struct test_ test_list_[]
static int test_name_contains_word_(const char *name, const char *pattern)
Definition: acutest.h:841
static size_t test_list_size_
Definition: acutest.h:348
int n
Definition: acutest.h:492
const char * name
Definition: acutest.h:321
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ test_init_()

static void test_init_ ( const char *  test_name)
static

Definition at line 930 of file acutest.h.

931 {
932 #ifdef TEST_INIT
933  TEST_INIT
934  ; /* Allow for a single unterminated function call */
935 #endif
936 }
#define TEST_INIT
Definition: main.c:26
+ Here is the caller graph for this function:

◆ test_fini_()

static void test_fini_ ( const char *  test_name)
static

Definition at line 940 of file acutest.h.

941 {
942 #ifdef TEST_FINI
943  TEST_FINI
944  ; /* Allow for a single unterminated function call */
945 #endif
946 }
+ Here is the caller graph for this function:

◆ test_do_run_()

static int test_do_run_ ( const struct test_ test,
int  index 
)
static

Definition at line 950 of file acutest.h.

951 {
952  test_was_aborted_ = 0;
954  test_current_index_ = index;
957  test_cond_failed_ = 0;
958 
959 #ifdef __cplusplus
960  try {
961 #endif
962  test_init_(test->name);
963  test_begin_test_line_(test);
964 
965  /* This is good to do in case the test unit crashes. */
966  fflush(stdout);
967  fflush(stderr);
968 
969  if(!test_worker_) {
971  if(setjmp(test_abort_jmp_buf_) != 0) {
972  test_was_aborted_ = 1;
973  goto aborted;
974  }
975  }
976 
978  test->func();
979 aborted:
982 
983  if(test_verbose_level_ >= 3) {
985  if(test_current_failures_ == 0) {
986  test_print_in_color_(TEST_COLOR_GREEN_INTENSIVE_, "SUCCESS: ");
987  printf("All conditions have passed.\n");
988 
989  if(test_timer_) {
991  printf("Duration: ");
993  printf("\n");
994  }
995  } else {
996  test_print_in_color_(TEST_COLOR_RED_INTENSIVE_, "FAILED: ");
997  if(!test_was_aborted_) {
998  printf("%d condition%s %s failed.\n",
1000  (test_current_failures_ == 1) ? "" : "s",
1001  (test_current_failures_ == 1) ? "has" : "have");
1002  } else {
1003  printf("Aborted.\n");
1004  }
1005  }
1006  printf("\n");
1007  } else if(test_verbose_level_ >= 1 && test_current_failures_ == 0) {
1009  }
1010 
1011  test_fini_(test->name);
1012  test_case_(NULL);
1013  test_current_unit_ = NULL;
1014  return (test_current_failures_ == 0) ? 0 : -1;
1015 
1016 #ifdef __cplusplus
1017  } catch(std::exception& e) {
1018  const char* what = e.what();
1019  test_check_(0, NULL, 0, "Threw std::exception");
1020  if(what != NULL)
1021  test_message_("std::exception::what(): %s", what);
1022 
1023  if(test_verbose_level_ >= 3) {
1024  test_line_indent_(1);
1025  test_print_in_color_(TEST_COLOR_RED_INTENSIVE_, "FAILED: ");
1026  printf("C++ exception.\n\n");
1027  }
1028 
1029  return -1;
1030  } catch(...) {
1031  test_check_(0, NULL, 0, "Threw an exception");
1032 
1033  if(test_verbose_level_ >= 3) {
1034  test_line_indent_(1);
1035  test_print_in_color_(TEST_COLOR_RED_INTENSIVE_, "FAILED: ");
1036  printf("C++ exception.\n\n");
1037  }
1038 
1039  return -1;
1040  }
1041 #endif
1042 }
static void test_timer_print_diff_(void)
Definition: acutest.h:476
static void test_init_(const char *test_name)
Definition: acutest.h:930
static int test_cond_failed_
Definition: acutest.h:357
static void test_fini_(const char *test_name)
Definition: acutest.h:940
void test_case_(const char *fmt,...)
void test_message_(const char *fmt,...)
static test_timer_type_ test_timer_start_
Definition: acutest.h:454
static void test_timer_get_time_(int *ts)
Definition: acutest.h:462
static int test_verbose_level_
Definition: acutest.h:369
static jmp_buf test_abort_jmp_buf_
Definition: acutest.h:375
#define TEST_COLOR_GREEN_INTENSIVE_
Definition: acutest.h:484
#define TEST_COLOR_RED_INTENSIVE_
Definition: acutest.h:485
static test_timer_type_ test_timer_end_
Definition: acutest.h:455
static int test_abort_has_jmp_buf_
Definition: acutest.h:374
static int test_timer_
Definition: acutest.h:372
void(* func)(void)
Definition: acutest.h:322
static int test_worker_
Definition: acutest.h:355
static int test_current_failures_
Definition: acutest.h:370
static void test_line_indent_(int level)
Definition: acutest.h:599
int test_check_(int cond, const char *file, int line, const char *fmt,...)
struct TestData test[]
static void test_finish_test_line_(int result)
Definition: acutest.h:570
static int test_current_already_logged_
Definition: acutest.h:367
const char * name
Definition: acutest.h:321
static int test_was_aborted_
Definition: acutest.h:358
static const struct test_ * test_current_unit_
Definition: acutest.h:364
static void test_begin_test_line_(const struct test_ *test)
Definition: acutest.h:549
static int test_current_index_
Definition: acutest.h:365
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ test_run_()

static void test_run_ ( const struct test_ test,
int  index,
int  master_index 
)
static

Definition at line 1048 of file acutest.h.

1049 {
1050  int failed = 1;
1051  test_timer_type_ start, end;
1052 
1055  test_timer_get_time_(&start);
1056 
1057  if(!test_no_exec_) {
1058 
1059 #if defined(ACUTEST_UNIX_)
1060 
1061  pid_t pid;
1062  int exit_code;
1063 
1064  /* Make sure the child starts with empty I/O buffers. */
1065  fflush(stdout);
1066  fflush(stderr);
1067 
1068  pid = fork();
1069  if(pid == (pid_t)-1) {
1070  test_error_("Cannot fork. %s [%d]", strerror(errno), errno);
1071  failed = 1;
1072  } else if(pid == 0) {
1073  /* Child: Do the test. */
1074  test_worker_ = 1;
1075  failed = (test_do_run_(test, index) != 0);
1076  exit(failed ? 1 : 0);
1077  } else {
1078  /* Parent: Wait until child terminates and analyze its exit code. */
1079  waitpid(pid, &exit_code, 0);
1080  if(WIFEXITED(exit_code)) {
1081  switch(WEXITSTATUS(exit_code)) {
1082  case 0: failed = 0; break; /* test has passed. */
1083  case 1: /* noop */ break; /* "normal" failure. */
1084  default: test_error_("Unexpected exit code [%d]", WEXITSTATUS(exit_code));
1085  }
1086  } else if(WIFSIGNALED(exit_code)) {
1087  char tmp[32];
1088  const char* signame;
1089  switch(WTERMSIG(exit_code)) {
1090  case SIGINT: signame = "SIGINT"; break;
1091  case SIGHUP: signame = "SIGHUP"; break;
1092  case SIGQUIT: signame = "SIGQUIT"; break;
1093  case SIGABRT: signame = "SIGABRT"; break;
1094  case SIGKILL: signame = "SIGKILL"; break;
1095  case SIGSEGV: signame = "SIGSEGV"; break;
1096  case SIGILL: signame = "SIGILL"; break;
1097  case SIGTERM: signame = "SIGTERM"; break;
1098  default: sprintf(tmp, "signal %d", WTERMSIG(exit_code)); signame = tmp; break;
1099  }
1100  test_error_("Test interrupted by %s.", signame);
1101  } else {
1102  test_error_("Test ended in an unexpected way [%d].", exit_code);
1103  }
1104  }
1105 
1106 #elif defined(ACUTEST_WIN_)
1107 
1108  char buffer[512] = {0};
1109  STARTUPINFOA startupInfo;
1110  PROCESS_INFORMATION processInfo;
1111  DWORD exitCode;
1112 
1113  /* Windows has no fork(). So we propagate all info into the child
1114  * through a command line arguments. */
1115  _snprintf(buffer, sizeof(buffer)-1,
1116  "%s --worker=%d %s --no-exec --no-summary %s --verbose=%d --color=%s -- \"%s\"",
1117  test_argv0_, index, test_timer_ ? "--time" : "",
1118  test_tap_ ? "--tap" : "", test_verbose_level_,
1119  test_colorize_ ? "always" : "never",
1120  test->name);
1121  memset(&startupInfo, 0, sizeof(startupInfo));
1122  startupInfo.cb = sizeof(STARTUPINFO);
1123  if(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInfo)) {
1124  WaitForSingleObject(processInfo.hProcess, INFINITE);
1125  GetExitCodeProcess(processInfo.hProcess, &exitCode);
1126  CloseHandle(processInfo.hThread);
1127  CloseHandle(processInfo.hProcess);
1128  failed = (exitCode != 0);
1129  if(exitCode > 1) {
1130  switch(exitCode) {
1131  case 3: test_error_("Aborted."); break;
1132  case 0xC0000005: test_error_("Access violation."); break;
1133  default: test_error_("Test ended in an unexpected way [%lu].", exitCode); break;
1134  }
1135  }
1136  } else {
1137  test_error_("Cannot create unit test subprocess [%ld].", GetLastError());
1138  failed = 1;
1139  }
1140 
1141 #else
1142 
1143  /* A platform where we don't know how to run child process. */
1144  failed = (test_do_run_(test, index) != 0);
1145 
1146 #endif
1147 
1148  } else {
1149  /* Child processes suppressed through --no-exec. */
1150  failed = (test_do_run_(test, index) != 0);
1151  }
1152  test_timer_get_time_(&end);
1153 
1154  test_current_unit_ = NULL;
1155 
1157  if(failed)
1159 
1160  test_set_success_(master_index, !failed);
1161  test_set_duration_(master_index, test_timer_diff_(start, end));
1162 }
static void test_set_success_(int i, int success)
Definition: acutest.h:829
static int test_do_run_(const struct test_ *test, int index)
Definition: acutest.h:950
static int test_stat_failed_units_
Definition: acutest.h:361
static void test_timer_get_time_(int *ts)
Definition: acutest.h:462
static int test_tap_
Definition: acutest.h:353
static int test_verbose_level_
Definition: acutest.h:369
static int const char char buffer[256]
Definition: acutest.h:489
static char * test_argv0_
Definition: acutest.h:347
static int test_timer_
Definition: acutest.h:372
static int test_no_exec_
Definition: acutest.h:351
static int test_worker_
Definition: acutest.h:355
static int test_stat_run_units_
Definition: acutest.h:362
struct TestData test[]
static int test_current_already_logged_
Definition: acutest.h:367
static int test_colorize_
Definition: acutest.h:371
const char * name
Definition: acutest.h:321
static const struct test_ * test_current_unit_
Definition: acutest.h:364
static void test_set_duration_(int i, double duration)
Definition: acutest.h:835
int test_timer_type_
Definition: acutest.h:453
static double test_timer_diff_(int start, int end)
Definition: acutest.h:468
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ test_cmdline_handle_short_opt_group_()

static int test_cmdline_handle_short_opt_group_ ( const TEST_CMDLINE_OPTION_ options,
const char *  arggroup,
int(*)(int, const char *)  callback 
)
static

Definition at line 1197 of file acutest.h.

1200 {
1201  const TEST_CMDLINE_OPTION_* opt;
1202  int i;
1203  int ret = 0;
1204 
1205  for(i = 0; arggroup[i] != '\0'; i++) {
1206  for(opt = options; opt->id != 0; opt++) {
1207  if(arggroup[i] == opt->shortname)
1208  break;
1209  }
1210 
1211  if(opt->id != 0 && !(opt->flags & TEST_CMDLINE_OPTFLAG_REQUIREDARG_)) {
1212  ret = callback(opt->id, NULL);
1213  } else {
1214  /* Unknown option. */
1215  char badoptname[3];
1216  badoptname[0] = '-';
1217  badoptname[1] = arggroup[i];
1218  badoptname[2] = '\0';
1219  ret = callback((opt->id != 0 ? TEST_CMDLINE_OPTID_MISSINGARG_ : TEST_CMDLINE_OPTID_UNKNOWN_),
1220  badoptname);
1221  }
1222 
1223  if(ret != 0)
1224  break;
1225  }
1226 
1227  return ret;
1228 }
#define TEST_CMDLINE_OPTFLAG_REQUIREDARG_
Definition: acutest.h:1182
#define TEST_CMDLINE_OPTID_MISSINGARG_
Definition: acutest.h:1186
#define TEST_CMDLINE_OPTID_UNKNOWN_
Definition: acutest.h:1185
+ Here is the caller graph for this function:

◆ test_cmdline_read_()

static int test_cmdline_read_ ( const TEST_CMDLINE_OPTION_ options,
int  argc,
char **  argv,
int(*)(int, const char *)  callback 
)
static

Definition at line 1233 of file acutest.h.

1235 {
1236 
1237  const TEST_CMDLINE_OPTION_* opt;
1238  char auxbuf[TEST_CMDLINE_AUXBUF_SIZE_+1];
1239  int after_doubledash = 0;
1240  int i = 1;
1241  int ret = 0;
1242 
1243  auxbuf[TEST_CMDLINE_AUXBUF_SIZE_] = '\0';
1244 
1245  while(i < argc) {
1246  if(after_doubledash || strcmp(argv[i], "-") == 0) {
1247  /* Non-option argument. */
1248  ret = callback(TEST_CMDLINE_OPTID_NONE_, argv[i]);
1249  } else if(strcmp(argv[i], "--") == 0) {
1250  /* End of options. All the remaining members are non-option arguments. */
1251  after_doubledash = 1;
1252  } else if(argv[i][0] != '-') {
1253  /* Non-option argument. */
1254  ret = callback(TEST_CMDLINE_OPTID_NONE_, argv[i]);
1255  } else {
1256  for(opt = options; opt->id != 0; opt++) {
1257  if(opt->longname != NULL && strncmp(argv[i], "--", 2) == 0) {
1258  size_t len = strlen(opt->longname);
1259  if(strncmp(argv[i]+2, opt->longname, len) == 0) {
1260  /* Regular long option. */
1261  if(argv[i][2+len] == '\0') {
1262  /* with no argument provided. */
1264  ret = callback(opt->id, NULL);
1265  else
1266  ret = callback(TEST_CMDLINE_OPTID_MISSINGARG_, argv[i]);
1267  break;
1268  } else if(argv[i][2+len] == '=') {
1269  /* with an argument provided. */
1271  ret = callback(opt->id, argv[i]+2+len+1);
1272  } else {
1273  sprintf(auxbuf, "--%s", opt->longname);
1274  ret = callback(TEST_CMDLINE_OPTID_BOGUSARG_, auxbuf);
1275  }
1276  break;
1277  } else {
1278  continue;
1279  }
1280  }
1281  } else if(opt->shortname != '\0' && argv[i][0] == '-') {
1282  if(argv[i][1] == opt->shortname) {
1283  /* Regular short option. */
1285  if(argv[i][2] != '\0')
1286  ret = callback(opt->id, argv[i]+2);
1287  else if(i+1 < argc)
1288  ret = callback(opt->id, argv[++i]);
1289  else
1290  ret = callback(TEST_CMDLINE_OPTID_MISSINGARG_, argv[i]);
1291  break;
1292  } else {
1293  ret = callback(opt->id, NULL);
1294 
1295  /* There might be more (argument-less) short options
1296  * grouped together. */
1297  if(ret == 0 && argv[i][2] != '\0')
1298  ret = test_cmdline_handle_short_opt_group_(options, argv[i]+2, callback);
1299  break;
1300  }
1301  }
1302  }
1303  }
1304 
1305  if(opt->id == 0) { /* still not handled? */
1306  if(argv[i][0] != '-') {
1307  /* Non-option argument. */
1308  ret = callback(TEST_CMDLINE_OPTID_NONE_, argv[i]);
1309  } else {
1310  /* Unknown option. */
1311  char* badoptname = argv[i];
1312 
1313  if(strncmp(badoptname, "--", 2) == 0) {
1314  /* Strip any argument from the long option. */
1315  char* assignment = strchr(badoptname, '=');
1316  if(assignment != NULL) {
1317  size_t len = assignment - badoptname;
1318  if(len > TEST_CMDLINE_AUXBUF_SIZE_)
1320  strncpy(auxbuf, badoptname, len);
1321  auxbuf[len] = '\0';
1322  badoptname = auxbuf;
1323  }
1324  }
1325 
1326  ret = callback(TEST_CMDLINE_OPTID_UNKNOWN_, badoptname);
1327  }
1328  }
1329  }
1330 
1331  if(ret != 0)
1332  return ret;
1333  i++;
1334  }
1335 
1336  return ret;
1337 }
#define TEST_CMDLINE_OPTFLAG_REQUIREDARG_
Definition: acutest.h:1182
static int test_cmdline_handle_short_opt_group_(const TEST_CMDLINE_OPTION_ *options, const char *arggroup, int(*callback)(int, const char *))
Definition: acutest.h:1197
#define TEST_CMDLINE_OPTID_MISSINGARG_
Definition: acutest.h:1186
#define TEST_CMDLINE_OPTID_BOGUSARG_
Definition: acutest.h:1187
#define TEST_CMDLINE_OPTFLAG_OPTIONALARG_
Definition: acutest.h:1181
#define TEST_CMDLINE_OPTID_NONE_
Definition: acutest.h:1184
#define TEST_CMDLINE_OPTID_UNKNOWN_
Definition: acutest.h:1185
const char * longname
Definition: acutest.h:1191
#define TEST_CMDLINE_AUXBUF_SIZE_
Definition: acutest.h:1230
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ test_help_()

static void test_help_ ( void  )
static

Definition at line 1340 of file acutest.h.

1341 {
1342  printf("Usage: %s [options] [test...]\n", test_argv0_);
1343  printf("\n");
1344  printf("Run the specified unit tests; or if the option '--skip' is used, run all\n");
1345  printf("tests in the suite but those listed. By default, if no tests are specified\n");
1346  printf("on the command line, all unit tests in the suite are run.\n");
1347  printf("\n");
1348  printf("Options:\n");
1349  printf(" -s, --skip Execute all unit tests but the listed ones\n");
1350  printf(" --exec[=WHEN] If supported, execute unit tests as child processes\n");
1351  printf(" (WHEN is one of 'auto', 'always', 'never')\n");
1352  printf(" -E, --no-exec Same as --exec=never\n");
1353 #if defined ACUTEST_WIN_
1354  printf(" -t, --time Measure test duration\n");
1355 #elif defined ACUTEST_HAS_POSIX_TIMER_
1356  printf(" -t, --time Measure test duration (real time)\n");
1357  printf(" --time=TIMER Measure test duration, using given timer\n");
1358  printf(" (TIMER is one of 'real', 'cpu')\n");
1359 #endif
1360  printf(" --no-summary Suppress printing of test results summary\n");
1361  printf(" --tap Produce TAP-compliant output\n");
1362  printf(" (See https://testanything.org/)\n");
1363  printf(" -x, --xml-output=FILE Enable XUnit output to the given file\n");
1364  printf(" -l, --list List unit tests in the suite and exit\n");
1365  printf(" -v, --verbose Make output more verbose\n");
1366  printf(" --verbose=LEVEL Set verbose level to LEVEL:\n");
1367  printf(" 0 ... Be silent\n");
1368  printf(" 1 ... Output one line per test (and summary)\n");
1369  printf(" 2 ... As 1 and failed conditions (this is default)\n");
1370  printf(" 3 ... As 1 and all conditions (and extended summary)\n");
1371  printf(" -q, --quiet Same as --verbose=0\n");
1372  printf(" --color[=WHEN] Enable colorized output\n");
1373  printf(" (WHEN is one of 'auto', 'always', 'never')\n");
1374  printf(" --no-color Same as --color=never\n");
1375  printf(" -h, --help Display this help and exit\n");
1376 
1377  if(test_list_size_ < 16) {
1378  printf("\n");
1379  test_list_names_();
1380  }
1381 }
static void test_list_names_(void)
Definition: acutest.h:809
static size_t test_list_size_
Definition: acutest.h:348
static char * test_argv0_
Definition: acutest.h:347
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ test_cmdline_callback_()

static int test_cmdline_callback_ ( int  id,
const char *  arg 
)
static

Definition at line 1408 of file acutest.h.

1409 {
1410  switch(id) {
1411  case 's':
1412  test_skip_mode_ = 1;
1413  break;
1414 
1415  case 'e':
1416  if(arg == NULL || strcmp(arg, "always") == 0) {
1417  test_no_exec_ = 0;
1418  } else if(strcmp(arg, "never") == 0) {
1419  test_no_exec_ = 1;
1420  } else if(strcmp(arg, "auto") == 0) {
1421  /*noop*/
1422  } else {
1423  fprintf(stderr, "%s: Unrecognized argument '%s' for option --exec.\n", test_argv0_, arg);
1424  fprintf(stderr, "Try '%s --help' for more information.\n", test_argv0_);
1425  exit(2);
1426  }
1427  break;
1428 
1429  case 'E':
1430  test_no_exec_ = 1;
1431  break;
1432 
1433  case 't':
1434 #if defined ACUTEST_WIN_ || defined ACUTEST_HAS_POSIX_TIMER_
1435  if(arg == NULL || strcmp(arg, "real") == 0) {
1436  test_timer_ = 1;
1437  #ifndef ACUTEST_WIN_
1438  } else if(strcmp(arg, "cpu") == 0) {
1439  test_timer_ = 2;
1440  #endif
1441  } else {
1442  fprintf(stderr, "%s: Unrecognized argument '%s' for option --time.\n", test_argv0_, arg);
1443  fprintf(stderr, "Try '%s --help' for more information.\n", test_argv0_);
1444  exit(2);
1445  }
1446 #endif
1447  break;
1448 
1449  case 'S':
1450  test_no_summary_ = 1;
1451  break;
1452 
1453  case 'T':
1454  test_tap_ = 1;
1455  break;
1456 
1457  case 'l':
1458  test_list_names_();
1459  exit(0);
1460 
1461  case 'v':
1462  test_verbose_level_ = (arg != NULL ? atoi(arg) : test_verbose_level_+1);
1463  break;
1464 
1465  case 'q':
1466  test_verbose_level_ = 0;
1467  break;
1468 
1469  case 'c':
1470  if(arg == NULL || strcmp(arg, "always") == 0) {
1471  test_colorize_ = 1;
1472  } else if(strcmp(arg, "never") == 0) {
1473  test_colorize_ = 0;
1474  } else if(strcmp(arg, "auto") == 0) {
1475  /*noop*/
1476  } else {
1477  fprintf(stderr, "%s: Unrecognized argument '%s' for option --color.\n", test_argv0_, arg);
1478  fprintf(stderr, "Try '%s --help' for more information.\n", test_argv0_);
1479  exit(2);
1480  }
1481  break;
1482 
1483  case 'C':
1484  test_colorize_ = 0;
1485  break;
1486 
1487  case 'h':
1488  test_help_();
1489  exit(0);
1490 
1491  case 'w':
1492  test_worker_ = 1;
1493  test_worker_index_ = atoi(arg);
1494  break;
1495  case 'x':
1496  test_xml_output_ = fopen(arg, "w");
1497  if (!test_xml_output_) {
1498  fprintf(stderr, "Unable to open '%s': %s\n", arg, strerror(errno));
1499  exit(2);
1500  }
1501  break;
1502 
1503  case 0:
1504  if(test_lookup_(arg) == 0) {
1505  fprintf(stderr, "%s: Unrecognized unit test '%s'\n", test_argv0_, arg);
1506  fprintf(stderr, "Try '%s --list' for list of unit tests.\n", test_argv0_);
1507  exit(2);
1508  }
1509  break;
1510 
1512  fprintf(stderr, "Unrecognized command line option '%s'.\n", arg);
1513  fprintf(stderr, "Try '%s --help' for more information.\n", test_argv0_);
1514  exit(2);
1515 
1517  fprintf(stderr, "The command line option '%s' requires an argument.\n", arg);
1518  fprintf(stderr, "Try '%s --help' for more information.\n", test_argv0_);
1519  exit(2);
1520 
1522  fprintf(stderr, "The command line option '%s' does not expect an argument.\n", arg);
1523  fprintf(stderr, "Try '%s --help' for more information.\n", test_argv0_);
1524  exit(2);
1525  }
1526 
1527  return 0;
1528 }
static int test_no_summary_
Definition: acutest.h:352
static int test_skip_mode_
Definition: acutest.h:354
static void test_list_names_(void)
Definition: acutest.h:809
static FILE * test_xml_output_
Definition: acutest.h:359
static void test_help_(void)
Definition: acutest.h:1340
#define TEST_CMDLINE_OPTID_MISSINGARG_
Definition: acutest.h:1186
static int test_tap_
Definition: acutest.h:353
static int test_verbose_level_
Definition: acutest.h:369
#define TEST_CMDLINE_OPTID_BOGUSARG_
Definition: acutest.h:1187
static char * test_argv0_
Definition: acutest.h:347
#define TEST_CMDLINE_OPTID_UNKNOWN_
Definition: acutest.h:1185
static int test_timer_
Definition: acutest.h:372
static int test_no_exec_
Definition: acutest.h:351
static int test_worker_
Definition: acutest.h:355
static int test_colorize_
Definition: acutest.h:371
static int test_worker_index_
Definition: acutest.h:356
static int test_lookup_(const char *pattern)
Definition: acutest.h:864
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 1582 of file acutest.h.

1583 {
1584  int i;
1585  test_argv0_ = argv[0];
1586 
1587 #if defined ACUTEST_UNIX_
1588  test_colorize_ = isatty(STDOUT_FILENO);
1589 #elif defined ACUTEST_WIN_
1590  #if defined _BORLANDC_
1591  test_colorize_ = isatty(_fileno(stdout));
1592  #else
1593  test_colorize_ = _isatty(_fileno(stdout));
1594  #endif
1595 #else
1596  test_colorize_ = 0;
1597 #endif
1598 
1599  /* Count all test units */
1600  test_list_size_ = 0;
1601  for(i = 0; test_list_[i].func != NULL; i++)
1602  test_list_size_++;
1603 
1604  test_details_ = (struct test_detail_*)calloc(test_list_size_, sizeof(struct test_detail_));
1605  if(test_details_ == NULL) {
1606  fprintf(stderr, "Out of memory.\n");
1607  exit(2);
1608  }
1609 
1610  /* Parse options */
1612 
1613  /* Initialize the proper timer. */
1614  test_timer_init_();
1615 
1616 #if defined(ACUTEST_WIN_)
1617  SetUnhandledExceptionFilter(test_seh_exception_filter_);
1618 #ifdef _MSC_VER
1619  _set_abort_behavior(0, _WRITE_ABORT_MSG);
1620 #endif
1621 #endif
1622 
1623  /* By default, we want to run all tests. */
1624  if(test_count_ == 0) {
1625  for(i = 0; test_list_[i].func != NULL; i++)
1626  test_remember_(i);
1627  }
1628 
1629  /* Guess whether we want to run unit tests as child processes. */
1630  if(test_no_exec_ < 0) {
1631  test_no_exec_ = 0;
1632 
1633  if(test_count_ <= 1) {
1634  test_no_exec_ = 1;
1635  } else {
1636 #ifdef ACUTEST_WIN_
1637  if(IsDebuggerPresent())
1638  test_no_exec_ = 1;
1639 #endif
1640 #ifdef ACUTEST_LINUX_
1641  if(test_is_tracer_present_())
1642  test_no_exec_ = 1;
1643 #endif
1644 #ifdef RUNNING_ON_VALGRIND
1645  /* RUNNING_ON_VALGRIND is provided by valgrind.h */
1646  if(RUNNING_ON_VALGRIND)
1647  test_no_exec_ = 1;
1648 #endif
1649  }
1650  }
1651 
1652  if(test_tap_) {
1653  /* TAP requires we know test result ("ok", "not ok") before we output
1654  * anything about the test, and this gets problematic for larger verbose
1655  * levels. */
1656  if(test_verbose_level_ > 2)
1657  test_verbose_level_ = 2;
1658 
1659  /* TAP harness should provide some summary. */
1660  test_no_summary_ = 1;
1661 
1662  if(!test_worker_)
1663  printf("1..%d\n", (int) test_count_);
1664  }
1665 
1666  int index = test_worker_index_;
1667  for(i = 0; test_list_[i].func != NULL; i++) {
1668  int run = (test_details_[i].flags & TEST_FLAG_RUN_);
1669  if (test_skip_mode_) /* Run all tests except those listed. */
1670  run = !run;
1671  if(run)
1672  test_run_(&test_list_[i], index++, i);
1673  }
1674 
1675  /* Write a summary */
1676  if(!test_no_summary_ && test_verbose_level_ >= 1) {
1677  if(test_verbose_level_ >= 3) {
1678  test_print_in_color_(TEST_COLOR_DEFAULT_INTENSIVE_, "Summary:\n");
1679 
1680  printf(" Count of all unit tests: %4d\n", (int) test_list_size_);
1681  printf(" Count of run unit tests: %4d\n", test_stat_run_units_);
1682  printf(" Count of failed unit tests: %4d\n", test_stat_failed_units_);
1683  printf(" Count of skipped unit tests: %4d\n", (int) test_list_size_ - test_stat_run_units_);
1684  }
1685 
1686  if(test_stat_failed_units_ == 0) {
1687  test_print_in_color_(TEST_COLOR_GREEN_INTENSIVE_, "SUCCESS:");
1688  printf(" All unit tests have passed.\n");
1689  } else {
1690  test_print_in_color_(TEST_COLOR_RED_INTENSIVE_, "FAILED:");
1691  printf(" %d of %d unit tests %s failed.\n",
1693  (test_stat_failed_units_ == 1) ? "has" : "have");
1694  }
1695 
1696  if(test_verbose_level_ >= 3)
1697  printf("\n");
1698  }
1699 
1700  if (test_xml_output_) {
1701 #if defined ACUTEST_UNIX_
1702  char *suite_name = basename(argv[0]);
1703 #elif defined ACUTEST_WIN_
1704  char suite_name[_MAX_FNAME];
1705  _splitpath(argv[0], NULL, NULL, suite_name, NULL);
1706 #else
1707  const char *suite_name = argv[0];
1708 #endif
1709  fprintf(test_xml_output_, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1710  fprintf(test_xml_output_, "<testsuite name=\"%s\" tests=\"%d\" errors=\"%d\" failures=\"%d\" skip=\"%d\">\n",
1712  (int)test_list_size_ - test_stat_run_units_);
1713  for(i = 0; test_list_[i].func != NULL; i++) {
1714  struct test_detail_ *details = &test_details_[i];
1715  fprintf(test_xml_output_, " <testcase name=\"%s\" time=\"%.2f\">\n", test_list_[i].name, details->duration);
1716  if (details->flags & TEST_FLAG_FAILURE_)
1717  fprintf(test_xml_output_, " <failure />\n");
1718  if (!(details->flags & TEST_FLAG_FAILURE_) && !(details->flags & TEST_FLAG_SUCCESS_))
1719  fprintf(test_xml_output_, " <skipped />\n");
1720  fprintf(test_xml_output_, " </testcase>\n");
1721  }
1722  fprintf(test_xml_output_, "</testsuite>\n");
1723  fclose(test_xml_output_);
1724  }
1725 
1726  free((void*) test_details_);
1727 
1728  return (test_stat_failed_units_ == 0) ? 0 : 1;
1729 }
static void test_remember_(int i)
Definition: acutest.h:819
static int test_no_summary_
Definition: acutest.h:352
static int test_cmdline_callback_(int id, const char *arg)
Definition: acutest.h:1408
const struct test_ test_list_[]
static int test_skip_mode_
Definition: acutest.h:354
static void test_run_(const struct test_ *test, int index, int master_index)
Definition: acutest.h:1048
static int test_stat_failed_units_
Definition: acutest.h:361
static FILE * test_xml_output_
Definition: acutest.h:359
static int test_tap_
Definition: acutest.h:353
static int test_verbose_level_
Definition: acutest.h:369
double duration
Definition: acutest.h:327
#define TEST_COLOR_GREEN_INTENSIVE_
Definition: acutest.h:484
static const TEST_CMDLINE_OPTION_ test_cmdline_options_[]
Definition: acutest.h:1383
#define TEST_COLOR_RED_INTENSIVE_
Definition: acutest.h:485
static size_t test_list_size_
Definition: acutest.h:348
static char * test_argv0_
Definition: acutest.h:347
static size_t test_count_
Definition: acutest.h:350
void test_timer_init_(void)
Definition: acutest.h:458
static int test_no_exec_
Definition: acutest.h:351
void(* func)(void)
Definition: acutest.h:322
static int test_worker_
Definition: acutest.h:355
static int test_stat_run_units_
Definition: acutest.h:362
static int test_colorize_
Definition: acutest.h:371
static int test_worker_index_
Definition: acutest.h:356
#define TEST_COLOR_DEFAULT_INTENSIVE_
Definition: acutest.h:483
static struct test_detail_ * test_details_
Definition: acutest.h:349
static int test_cmdline_read_(const TEST_CMDLINE_OPTION_ *options, int argc, char **argv, int(*callback)(int, const char *))
Definition: acutest.h:1233
unsigned char flags
Definition: acutest.h:326
+ Here is the call graph for this function:

Variable Documentation

◆ test_list_

const struct test_ test_list_[]

◆ test_argv0_

char* test_argv0_ = NULL
static

Definition at line 347 of file acutest.h.

◆ test_list_size_

size_t test_list_size_ = 0
static

Definition at line 348 of file acutest.h.

◆ test_details_

struct test_detail_* test_details_ = NULL
static

Definition at line 349 of file acutest.h.

◆ test_count_

size_t test_count_ = 0
static

Definition at line 350 of file acutest.h.

◆ test_no_exec_

int test_no_exec_ = -1
static

Definition at line 351 of file acutest.h.

◆ test_no_summary_

int test_no_summary_ = 0
static

Definition at line 352 of file acutest.h.

◆ test_tap_

int test_tap_ = 0
static

Definition at line 353 of file acutest.h.

◆ test_skip_mode_

int test_skip_mode_ = 0
static

Definition at line 354 of file acutest.h.

◆ test_worker_

int test_worker_ = 0
static

Definition at line 355 of file acutest.h.

◆ test_worker_index_

int test_worker_index_ = 0
static

Definition at line 356 of file acutest.h.

◆ test_cond_failed_

test_cond_failed_ = 0
static

Definition at line 357 of file acutest.h.

◆ test_was_aborted_

int test_was_aborted_ = 0
static

Definition at line 358 of file acutest.h.

◆ test_xml_output_

FILE* test_xml_output_ = NULL
static

Definition at line 359 of file acutest.h.

◆ test_stat_failed_units_

int test_stat_failed_units_ = 0
static

Definition at line 361 of file acutest.h.

◆ test_stat_run_units_

int test_stat_run_units_ = 0
static

Definition at line 362 of file acutest.h.

◆ test_current_unit_

const struct test_* test_current_unit_ = NULL
static

Definition at line 364 of file acutest.h.

◆ test_current_index_

int test_current_index_ = 0
static

Definition at line 365 of file acutest.h.

◆ test_case_name_

test_case_name_[sizeof(test_case_name_) - 1] = ""
static

Definition at line 366 of file acutest.h.

◆ test_current_already_logged_

test_current_already_logged_ = 0
static

Definition at line 367 of file acutest.h.

◆ test_case_current_already_logged_

int test_case_current_already_logged_ = 0
static

Definition at line 368 of file acutest.h.

◆ test_verbose_level_

int test_verbose_level_ = 2
static

Definition at line 369 of file acutest.h.

◆ test_current_failures_

test_current_failures_ = 0
static

Definition at line 370 of file acutest.h.

◆ test_colorize_

int test_colorize_ = 0
static

Definition at line 371 of file acutest.h.

◆ test_timer_

int test_timer_ = 0
static

Definition at line 372 of file acutest.h.

◆ test_abort_has_jmp_buf_

int test_abort_has_jmp_buf_ = 0
static

Definition at line 374 of file acutest.h.

◆ test_abort_jmp_buf_

jmp_buf test_abort_jmp_buf_
static

Definition at line 375 of file acutest.h.

◆ test_timer_start_

test_timer_type_ test_timer_start_
static

Definition at line 454 of file acutest.h.

◆ test_timer_end_

test_timer_type_ test_timer_end_
static

Definition at line 455 of file acutest.h.

◆ fmt

int const char int const char * fmt

Definition at line 488 of file acutest.h.

◆ buffer

buffer[TEST_MSG_MAXSIZE-1]
Initial value:
{
va_list args
va_list args
Definition: acutest.h:715

Definition at line 489 of file acutest.h.

◆ n

return n = printf("%s", buffer)

Definition at line 492 of file acutest.h.

◆ file

int const char* file

Definition at line 617 of file acutest.h.

◆ line

int const char int line

Definition at line 617 of file acutest.h.

◆ result_color

result_color
Initial value:
{
const char *result_str
result_str
Definition: acutest.h:631

Definition at line 618 of file acutest.h.

◆ verbose_level

verbose_level = 2

Definition at line 621 of file acutest.h.

◆ else

else
Initial value:
{
static void test_finish_test_line_(int result)
Definition: acutest.h:570
static int test_current_already_logged_
Definition: acutest.h:367
static const struct test_ * test_current_unit_
Definition: acutest.h:364

Definition at line 627 of file acutest.h.

◆ result_str

result_str = "failed"

Definition at line 631 of file acutest.h.

◆ !test_cond_failed_

Definition at line 677 of file acutest.h.

◆ line_beg

line_beg
Initial value:
{
#define TEST_MSG_MAXSIZE
Definition: acutest.h:221
static int const char char buffer[256]
Definition: acutest.h:489

Definition at line 711 of file acutest.h.

◆ line_end

char* line_end

Definition at line 714 of file acutest.h.

◆ args

va_list args

Definition at line 715 of file acutest.h.

◆ test_cmdline_options_

const TEST_CMDLINE_OPTION_ test_cmdline_options_[]
static
Initial value:
= {
{ 's', "skip", 's', 0 },
{ 0, "exec", 'e', TEST_CMDLINE_OPTFLAG_OPTIONALARG_ },
{ 'E', "no-exec", 'E', 0 },
{ 0, "no-summary", 'S', 0 },
{ 0, "tap", 'T', 0 },
{ 'l', "list", 'l', 0 },
{ 'v', "verbose", 'v', TEST_CMDLINE_OPTFLAG_OPTIONALARG_ },
{ 'q', "quiet", 'q', 0 },
{ 0, "color", 'c', TEST_CMDLINE_OPTFLAG_OPTIONALARG_ },
{ 0, "no-color", 'C', 0 },
{ 'h', "help", 'h', 0 },
{ 0, "worker", 'w', TEST_CMDLINE_OPTFLAG_REQUIREDARG_ },
{ 'x', "xml-output", 'x', TEST_CMDLINE_OPTFLAG_REQUIREDARG_ },
{ 0, NULL, 0, 0 }
}
#define TEST_CMDLINE_OPTFLAG_REQUIREDARG_
Definition: acutest.h:1182
#define TEST_CMDLINE_OPTFLAG_OPTIONALARG_
Definition: acutest.h:1181

Definition at line 1383 of file acutest.h.