Lunchbox  1.14.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
test.h
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." \
72  << __LINE__ << ")" << std::endl; \
73  lunchbox::abort(); \
74  ::exit( EXIT_FAILURE ); \
75  } \
76  }
77 
78 int testMain( int argc, char **argv );
79 
80 namespace
81 {
82 class Watchdog : public lunchbox::Thread
83 {
84 public:
85  explicit Watchdog( const std::string& name ) : _name( name ) {}
86 
87  virtual void run()
88  {
89  lunchbox::Thread::setName( "Watchdog" );
90 #ifdef TEST_RUNTIME
91  lunchbox::sleep( TEST_RUNTIME * 1000 );
92  std::cerr << "Watchdog triggered - " << _name
93  << " did not terminate within " << TEST_RUNTIME << "s"
94  << std::endl;
95 #else
96  lunchbox::sleep( 60000 );
97  std::cerr << "Watchdog triggered - " << _name
98  << " did not terminate within 1 minute" << std::endl;
99 #endif
100  lunchbox::abort( true /*dumpThreads*/ );
101  }
102 
103 private:
104  const std::string _name;
105 };
106 }
107 
108 int main( int argc, char **argv )
109 {
110 #ifndef TEST_NO_WATCHDOG
111  Watchdog watchdog( argv[0] );
112  watchdog.start();
113 #endif
114 
115  try
116  {
117  const int result = testMain( argc, argv );
118  if( result != EXIT_SUCCESS )
119  return result;
120  }
121  catch( const std::runtime_error& e )
122  {
123  LBINFO << "Test exception: " << e.what() << std::endl;
124  return EXIT_FAILURE;
125  }
126 
127 #ifndef TEST_NO_WATCHDOG
128  watchdog.cancel();
129  lunchbox::sleep( 10 ); // give watchdog time to terminate
130 #endif
131  return EXIT_SUCCESS;
132 }
133 
134 # define main testMain
135 
136 #endif // LBTEST_TEST_H
Utility class to execute code in a separate execution thread.
Definition: thread.h:41
void sleep(const uint32_t milliSeconds)
Sleep the current thread for a number of milliseconds.
#define LBINFO
Output an informational message to the per-thread Log.
Definition: log.h:192
This file contains logging classes.
virtual void run()=0
The entry function for the child thread.