LCOV - code coverage report
Current view: top level - lunchbox - test.h (source / functions) Hit Total Coverage
Test: Lunchbox Lines: 16 25 64.0 %
Date: 2018-10-03 05:33:11 Functions: 4 5 80.0 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2005-2014, Stefan Eilemann <eile@equalizergraphics.com>
       3             :  *
       4             :  * Redistribution and use in source and binary forms, with or without
       5             :  * modification, are permitted provided that the following conditions are met:
       6             :  *
       7             :  * - Redistributions of source code must retain the above copyright notice, this
       8             :  *   list of conditions and the following disclaimer.
       9             :  * - Redistributions in binary form must reproduce the above copyright notice,
      10             :  *   this list of conditions and the following disclaimer in the documentation
      11             :  *   and/or other materials provided with the distribution.
      12             :  * - Neither the name of Eyescale Software GmbH nor the names of its
      13             :  *   contributors may be used to endorse or promote products derived from this
      14             :  *   software without specific prior written permission.
      15             :  *
      16             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
      17             :  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      18             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      19             :  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
      20             :  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
      21             :  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
      22             :  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
      23             :  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
      24             :  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
      25             :  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
      26             :  * POSSIBILITY OF SUCH DAMAGE.
      27             :  */
      28             : 
      29             : #ifndef LBTEST_TEST_H
      30             : #define LBTEST_TEST_H
      31             : 
      32             : #include <lunchbox/log.h>
      33             : #include <lunchbox/sleep.h>
      34             : #include <lunchbox/thread.h>
      35             : 
      36             : #include <cstdlib>
      37             : #include <fstream>
      38             : #include <stdexcept>
      39             : 
      40             : #define OUTPUT lunchbox::Log::instance(__FILE__, __LINE__)
      41             : 
      42             : #define TEST(x)                                                            \
      43             :     {                                                                      \
      44             :         LBVERB << "Test " << #x << std::endl;                              \
      45             :         if (!(x))                                                          \
      46             :         {                                                                  \
      47             :             OUTPUT << #x << " failed (l." << __LINE__ << ')' << std::endl; \
      48             :             lunchbox::abort();                                             \
      49             :             ::exit(EXIT_FAILURE);                                          \
      50             :         }                                                                  \
      51             :     }
      52             : 
      53             : #define TESTINFO(x, info)                                              \
      54             :     {                                                                  \
      55             :         LBVERB << "Test " << #x << ": " << info << std::endl;          \
      56             :         if (!(x))                                                      \
      57             :         {                                                              \
      58             :             OUTPUT << #x << " failed (l." << __LINE__ << "): " << info \
      59             :                    << std::endl;                                       \
      60             :             lunchbox::abort();                                         \
      61             :             ::exit(EXIT_FAILURE);                                      \
      62             :         }                                                              \
      63             :     }
      64             : 
      65             : #define TESTRESULT(x, type)                                                  \
      66             :     {                                                                        \
      67             :         LBVERB << "Test " << #x << std::endl;                                \
      68             :         const type& testRes = (x);                                           \
      69             :         if (!testRes)                                                        \
      70             :         {                                                                    \
      71             :             OUTPUT << #x << " failed with " << testRes << " (l." << __LINE__ \
      72             :                    << ")" << std::endl;                                      \
      73             :             lunchbox::abort();                                               \
      74             :             ::exit(EXIT_FAILURE);                                            \
      75             :         }                                                                    \
      76             :     }
      77             : 
      78             : int testMain(int argc, char** argv);
      79             : 
      80             : namespace
      81             : {
      82          20 : class Watchdog : public lunchbox::Thread
      83             : {
      84             : public:
      85          20 :     explicit Watchdog(const std::string& name)
      86          20 :         : _name(name)
      87             :     {
      88          20 :     }
      89             : 
      90          17 :     virtual void run()
      91             :     {
      92          17 :         lunchbox::Thread::setName("Watchdog");
      93             : #ifdef TEST_RUNTIME
      94           3 :         lunchbox::sleep(TEST_RUNTIME * 1000);
      95             :         std::cerr << "Watchdog triggered - " << _name
      96           0 :                   << " did not terminate within " << TEST_RUNTIME << "s"
      97           0 :                   << std::endl;
      98             : #else
      99          14 :         lunchbox::sleep(60000);
     100             :         std::cerr << "Watchdog triggered - " << _name
     101           0 :                   << " did not terminate within 1 minute" << std::endl;
     102             : #endif
     103           0 :         lunchbox::abort(true /*dumpThreads*/);
     104           0 :     }
     105             : 
     106             : private:
     107             :     const std::string _name;
     108             : };
     109             : }
     110             : 
     111          20 : int main(int argc, char** argv)
     112             : {
     113             : #ifndef TEST_NO_WATCHDOG
     114          40 :     Watchdog watchdog(argv[0]);
     115          20 :     watchdog.start();
     116             : #endif
     117             : 
     118             :     try
     119             :     {
     120          20 :         const int result = testMain(argc, argv);
     121          20 :         if (result != EXIT_SUCCESS)
     122           0 :             return result;
     123             :     }
     124           0 :     catch (const std::runtime_error& e)
     125             :     {
     126           0 :         LBINFO << "Test exception: " << e.what() << std::endl;
     127           0 :         return EXIT_FAILURE;
     128             :     }
     129             : 
     130             : #ifndef TEST_NO_WATCHDOG
     131          20 :     watchdog.cancel();
     132          20 :     lunchbox::sleep(10); // give watchdog time to terminate
     133             : #endif
     134          20 :     return EXIT_SUCCESS;
     135             : }
     136             : 
     137             : #define main testMain
     138             : 
     139             : #endif // LBTEST_TEST_H

Generated by: LCOV version 1.11