Realistic 3D camera system
3D camera system components
unit_test.hpp
Go to the documentation of this file.
1 //
2 // unit_test.hpp
3 // ~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef UNIT_TEST_HPP
12 #define UNIT_TEST_HPP
13 
14 #include "asio/detail/config.hpp"
15 #include <iostream>
17 
18 #if defined(__sun)
19 # include <stdlib.h> // Needed for lrand48.
20 #endif // defined(__sun)
21 
22 #if defined(__BORLANDC__)
23 
24 // Prevent use of intrinsic for strcmp.
25 # include <cstring>
26 # undef strcmp
27 
28 // Suppress error about condition always being true.
29 # pragma option -w-ccc
30 
31 #endif // defined(__BORLANDC__)
32 
33 #if defined(ASIO_MSVC)
34 # pragma warning (disable:4127)
35 # pragma warning (push)
36 # pragma warning (disable:4244)
37 # pragma warning (disable:4702)
38 #endif // defined(ASIO_MSVC)
39 
40 #if !defined(ASIO_TEST_IOSTREAM)
41 # define ASIO_TEST_IOSTREAM std::cerr
42 #endif // !defined(ASIO_TEST_IOSTREAM)
43 
44 namespace asio {
45 namespace detail {
46 
47 inline const char*& test_name()
48 {
49  static const char* name = 0;
50  return name;
51 }
52 
54 {
55  static atomic_count errors(0);
56  return errors;
57 }
58 
59 inline void begin_test_suite(const char* name)
60 {
63  ASIO_TEST_IOSTREAM << name << " test suite begins" << std::endl;
64 }
65 
66 inline int end_test_suite(const char* name)
67 {
68  ASIO_TEST_IOSTREAM << name << " test suite ends" << std::endl;
69  ASIO_TEST_IOSTREAM << "\n*** ";
70  long errors = asio::detail::test_errors();
71  if (errors == 0)
72  ASIO_TEST_IOSTREAM << "No errors detected.";
73  else if (errors == 1)
74  ASIO_TEST_IOSTREAM << "1 error detected.";
75  else
76  ASIO_TEST_IOSTREAM << errors << " errors detected." << std::endl;
77  ASIO_TEST_IOSTREAM << std::endl;
78  return errors == 0 ? 0 : 1;
79 }
80 
81 template <void (*Test)()>
82 inline void run_test(const char* name)
83 {
84  test_name() = name;
85  long errors_before = asio::detail::test_errors();
86  Test();
87  if (test_errors() == errors_before)
88  ASIO_TEST_IOSTREAM << name << " passed" << std::endl;
89  else
90  ASIO_TEST_IOSTREAM << name << " failed" << std::endl;
91 }
92 
93 template <void (*)()>
94 inline void compile_test(const char* name)
95 {
96  ASIO_TEST_IOSTREAM << name << " passed" << std::endl;
97 }
98 
99 #if defined(ASIO_NO_EXCEPTIONS)
100 
101 template <typename T>
102 void throw_exception(const T& t)
103 {
104  ASIO_TEST_IOSTREAM << "Exception: " << t.what() << std::endl;
105  std::abort();
106 }
107 
108 #endif // defined(ASIO_NO_EXCEPTIONS)
109 
110 } // namespace detail
111 } // namespace asio
112 
113 #define ASIO_CHECK(expr) \
114  do { if (!(expr)) { \
115  ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
116  << asio::detail::test_name() << ": " \
117  << "check '" << #expr << "' failed" << std::endl; \
118  ++asio::detail::test_errors(); \
119  } } while (0)
120 
121 #define ASIO_CHECK_MESSAGE(expr, msg) \
122  do { if (!(expr)) { \
123  ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
124  << asio::detail::test_name() << ": " \
125  << msg << std::endl; \
126  ++asio::detail::test_errors(); \
127  } } while (0)
128 
129 #define ASIO_WARN_MESSAGE(expr, msg) \
130  do { if (!(expr)) { \
131  ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
132  << asio::detail::test_name() << ": " \
133  << msg << std::endl; \
134  } } while (0)
135 
136 #define ASIO_ERROR(msg) \
137  do { \
138  ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
139  << asio::detail::test_name() << ": " \
140  << msg << std::endl; \
141  ++asio::detail::test_errors(); \
142  } while (0)
143 
144 #define ASIO_TEST_SUITE(name, tests) \
145  int main() \
146  { \
147  asio::detail::begin_test_suite(name); \
148  tests \
149  return asio::detail::end_test_suite(name); \
150  }
151 
152 #define ASIO_TEST_CASE(test) \
153  asio::detail::run_test<&test>(#test);
154 
155 #define ASIO_COMPILE_TEST_CASE(test) \
156  asio::detail::compile_test<&test>(#test);
157 
158 inline void null_test()
159 {
160 }
161 
162 #if defined(__GNUC__) && defined(_AIX)
163 
164 // AIX needs this symbol defined in asio, even if it doesn't do anything.
165 int test_main(int, char**)
166 {
167 }
168 
169 #endif // defined(__GNUC__) && defined(_AIX)
170 
171 #if defined(ASIO_MSVC)
172 # pragma warning (pop)
173 #endif // defined(ASIO_MSVC)
174 
175 #endif // UNIT_TEST_HPP
atomic_count & test_errors()
Definition: unit_test.hpp:53
int end_test_suite(const char *name)
Definition: unit_test.hpp:66
void null_test()
Definition: unit_test.hpp:158
#define ASIO_TEST_IOSTREAM
Definition: unit_test.hpp:41
void begin_test_suite(const char *name)
Definition: unit_test.hpp:59
void run_test(const char *name)
Definition: unit_test.hpp:82
const char *& test_name()
Definition: unit_test.hpp:47
void compile_test(const char *name)
Definition: unit_test.hpp:94
void throw_exception(const Exception &e)