Show
Ignore:
Timestamp:
09/09/10 22:40:57 (21 months ago)
Author:
westram
Message:
  • unit-test improvements (merged [6757] [6758] [6759] [6760] [6761] [6762] [6764] [6791] [6795] [6798] [6799] [6800] [6801] [6802] [6809] [6810])
    • moved file compare to test_unit.h
    • fake report for skipped tests
    • added templates for char compare
    • perform minihexdump
    • flush output
    • added
      • TEST_ASSERT_FILES_EQUAL__BROKEN
      • TEST_ASSERT_ZERO_OR_SHOW_ERRNO
    • create a fake.patch if there are no changes
    • all code affecting assertions in UNIT_TESTS-mode went to test_global.h
      • overwrites arb_assert()
    • undefine UNIT_TESTS for GDE submakefiles (foreign code there)
    • arb_test::FlushedOutput::errorf raises assertion itself
    • test against I/O errors
    • moved test code
    • refactored message printing
    • unit-tester switches to run directory itself
      • eliminates path hack for valgrind
    • control leak-check by variable
    • added global test-environment (does nothing yet)
Location:
trunk/UNIT_TESTER
Files:
1 modified
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/UNIT_TESTER

    • Property svn:ignore
      •  

        old new  
        55*.gcno 
        66logs 
         7test_environment 
  • trunk/UNIT_TESTER/test_global.h

    r6791 r6816  
    2020#include <cstdio> 
    2121#endif 
     22#ifndef _CPP_CERRNO 
     23#include <cerrno> 
     24#endif 
     25#ifndef _CPP_CSTRING 
     26#include <cstring> 
     27#endif 
    2228 
     29#if (UNIT_TESTS == 1) 
     30 
     31# if defined(DEVEL_RELEASE) 
     32#  error Unit testing not allowed in release 
     33# endif 
     34 
     35# ifdef __cplusplus 
     36 
     37#  define SET_ASSERTION_FAILED_FLAG() arb_test::test_data().assertion_failed = true   
     38#  define PRINT_ASSERTION_FAILED_MSG(cond) arb_test::GlobalTestData::assertfailmsg(__FILE__, __LINE__, #cond)   
     39 
     40# else 
     41#  define SET_ASSERTION_FAILED_FLAG() // impossible in C (assertions in C code will be handled like normal SEGV) 
     42#  define PRINT_ASSERTION_FAILED_MSG(cond)                      \ 
     43    do {                                                        \ 
     44        fflush(stdout);                                         \ 
     45        fflush(stderr);                                         \ 
     46        fprintf(stderr, "%s:%i: Assertion '%s' failed [C]\n",   \ 
     47                __FILE__, __LINE__, #cond);                     \ 
     48        fflush(stderr);                                         \ 
     49    } while(0) 
     50# endif 
     51 
     52# define TRIGGER_ASSERTION()                            \ 
     53    do {                                                \ 
     54        SET_ASSERTION_FAILED_FLAG();                    \ 
     55        ARB_SIGSEGV(0);                                 \ 
     56    } while(0) 
    2357 
    2458namespace arb_test { 
     59    class FlushedOutputNoLF { 
     60        inline void flushall() { fflush(stdout); fflush(stderr); } 
     61    public: 
     62        FlushedOutputNoLF() { flushall(); } 
     63        ~FlushedOutputNoLF() { flushall(); } 
     64    }; 
     65    struct FlushedOutput : public FlushedOutputNoLF { 
     66        ~FlushedOutput() { fputc('\n', stderr); } 
     67    }; 
     68 
    2569    class GlobalTestData { 
    2670        GlobalTestData() 
     
    5195        static GlobalTestData& get_instance() { return *instance(false); } 
    5296        static void erase_instance() { instance(true); } 
     97         
     98        static void assertfailmsg(const char *filename, int lineno, const char *condition) { 
     99            FlushedOutput yes; 
     100            fprintf(stderr, "%s:%i: Assertion '%s' failed", filename, lineno, condition); 
     101        } 
    53102    }; 
    54103 
    55104    inline GlobalTestData& test_data() { return GlobalTestData::get_instance(); } 
     105}; 
    56106 
    57      
    58     class FlushedOutput { 
    59         inline void flushall() { fflush(stdout); fflush(stderr); } 
    60     public: 
    61         FlushedOutput() { flushall(); } 
    62         ~FlushedOutput() { flushall(); } 
    63          
    64 #define VPRINTFORMAT(format) do { va_list parg; va_start(parg, format); vfprintf(stderr, format, parg); va_end(parg); } while(0) 
    65      
    66         static void printf(const char *format, ...) __attribute__((format(printf, 1, 2))) { 
    67             FlushedOutput yes; 
    68             VPRINTFORMAT(format); 
    69         } 
    70         static void messagef(const char *filename, int lineno, const char *format, ...) __attribute__((format(printf, 3, 4))) { 
    71             FlushedOutput yes; 
    72             fprintf(stderr, "%s:%i: ", filename, lineno); 
    73             fputc('\n', stderr); 
    74             VPRINTFORMAT(format); 
    75         } 
    76         static void warningf(const char *filename, int lineno, const char *format, ...) __attribute__((format(printf, 3, 4))) { 
    77             GlobalTestData& global = test_data(); 
    78             if (global.show_warnings) { 
    79                 FlushedOutput yes; 
    80                 fprintf(stderr, "%s:%i: Warning: ", filename, lineno); 
    81                 VPRINTFORMAT(format); 
    82                 fputc('\n', stderr); 
    83                 global.warnings++; 
    84             } 
    85         } 
    86         static void errorf(const char *filename, int lineno, const char *format, ...) __attribute__((format(printf, 3, 4))) { 
    87             FlushedOutput yes; 
    88             fprintf(stderr, "%s:%i: Error: ", filename, lineno); 
    89             VPRINTFORMAT(format); 
    90             fputc('\n', stderr); 
    91         } 
    92 #undef VPRINTFORMAT 
    93     }; 
     107// -------------------------------------------------------------------------------- 
    94108 
    95 }; 
     109// special assert for unit tests (additionally to SEGV it sets a global flag) 
     110# define test_assert(cond)                      \ 
     111    do {                                        \ 
     112        if (!(cond)) {                          \ 
     113            PRINT_ASSERTION_FAILED_MSG(cond);   \ 
     114            TRIGGER_ASSERTION();                \ 
     115        }                                       \ 
     116    } while(0) 
     117 
     118 
     119// redefine arb_assert with test_assert when compiling for unit tests 
     120# if defined(ASSERTION_USED) 
     121#  undef arb_assert 
     122#  define arb_assert(cond) test_assert(cond) 
     123# endif 
     124 
     125#else // UNIT_TESTS != 1 
     126#error test_global.h may only be included if UNIT_TESTS is 1 
     127#endif 
    96128 
    97129#else