LCOV - code coverage report
Current view: top level - CMake/common/cpp - test.h (source / functions) Hit Total Coverage
Test: lcov2.info Lines: 14 19 73.7 %
Date: 2014-10-01 Functions: 4 5 80.0 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2005-2014, Stefan Eilemann <eile@equalizergraphics.com>
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or modify it under
       5             :  * the terms of the GNU Lesser General Public License version 2.1 as published
       6             :  * by the Free Software Foundation.
       7             :  *
       8             :  * This library is distributed in the hope that it will be useful, but WITHOUT
       9             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      10             :  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
      11             :  * details.
      12             :  *
      13             :  * You should have received a copy of the GNU Lesser General Public License
      14             :  * along with this library; if not, write to the Free Software Foundation, Inc.,
      15             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      16             :  */
      17             : 
      18             : #ifndef LBTEST_TEST_H
      19             : #define LBTEST_TEST_H
      20             : 
      21             : #include <lunchbox/log.h>
      22             : #include <lunchbox/sleep.h>
      23             : #include <lunchbox/thread.h>
      24             : 
      25             : #include <cstdlib>
      26             : #include <fstream>
      27             : #include <stdexcept>
      28             : 
      29             : #define OUTPUT lunchbox::Log::instance( __FILE__, __LINE__ )
      30             : 
      31             : #define TEST( x )                                                       \
      32             :     {                                                                   \
      33             :         LBVERB << "Test " << #x << std::endl;                           \
      34             :         if( !(x) )                                                      \
      35             :         {                                                               \
      36             :             OUTPUT << #x << " failed (l." << __LINE__ << ')' << std::endl; \
      37             :             lunchbox::abort();                                          \
      38             :             ::exit( EXIT_FAILURE );                                     \
      39             :         }                                                               \
      40             :     }
      41             : 
      42             : #define TESTINFO( x, info )                                           \
      43             :     {                                                                 \
      44             :         LBVERB << "Test " << #x << ": " << info << std::endl;         \
      45             :         if( !(x) )                                                    \
      46             :         {                                                             \
      47             :             OUTPUT << #x << " failed (l." << __LINE__ << "): " << info  \
      48             :                    << std::endl;                                        \
      49             :             lunchbox::abort();                                          \
      50             :             ::exit( EXIT_FAILURE );                                     \
      51             :         }                                                               \
      52             :     }
      53             : 
      54             : #define TESTRESULT( x, type )                                           \
      55             :     {                                                                   \
      56             :         LBVERB << "Test " << #x << std::endl;                           \
      57             :         const type& testRes = (x);                                      \
      58             :         if( !testRes )                                                  \
      59             :         {                                                               \
      60             :             OUTPUT << #x << " failed with " << testRes << " (l."        \
      61             :                    << __LINE__ << ")" << std::endl;                     \
      62             :             lunchbox::abort();                                          \
      63             :             ::exit( EXIT_FAILURE );                                     \
      64             :         }                                                               \
      65             :     }
      66             : 
      67             : int testMain( int argc, char **argv );
      68             : 
      69             : namespace
      70             : {
      71          30 : class Watchdog : public lunchbox::Thread
      72             : {
      73             : public:
      74          30 :     explicit Watchdog( const std::string& name ) : _name( name ) {}
      75             : 
      76          29 :     virtual void run()
      77             :         {
      78          29 :             lunchbox::Thread::setName( "Watchdog" );
      79             : #ifdef TEST_RUNTIME
      80           7 :             lunchbox::sleep( TEST_RUNTIME * 1000 );
      81           0 :             TESTINFO( false,
      82             :                       "Watchdog triggered - " << _name <<
      83           0 :                       " did not terminate within " << TEST_RUNTIME << "s" );
      84             : #else
      85          22 :             lunchbox::sleep( 60000 );
      86           0 :             TESTINFO( false,
      87             :                       "Watchdog triggered - " << _name <<
      88           0 :                       " did not terminate within 1 minute" );
      89             : #endif
      90             :         }
      91             : 
      92             : private:
      93             :     const std::string _name;
      94             : };
      95             : }
      96             : 
      97          30 : int main( int argc, char **argv )
      98             : {
      99             : #ifndef TEST_NO_WATCHDOG
     100          30 :     Watchdog watchdog( argv[0] );
     101          30 :     watchdog.start();
     102             : #endif
     103             : 
     104             :     try
     105             :     {
     106          30 :         const int result = testMain( argc, argv );
     107          30 :         if( result != EXIT_SUCCESS )
     108           0 :             return result;
     109             :     }
     110             :     catch( const std::runtime_error& e )
     111             :     {
     112             :         LBINFO << e.what() << std::endl;
     113             :         return EXIT_FAILURE;
     114             :     }
     115             : 
     116             : #ifndef TEST_NO_WATCHDOG
     117          30 :     watchdog.cancel();
     118          30 :     lunchbox::sleep( 10 ); // give watchdog time to terminate
     119             : #endif
     120          30 :     return EXIT_SUCCESS;
     121             : }
     122             : 
     123             : #  define main testMain
     124             : 
     125             : #endif // LBTEST_TEST_H

Generated by: LCOV version 1.10