lightweight_test.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP
  2. #define BOOST_CORE_LIGHTWEIGHT_TEST_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. //
  8. // boost/core/lightweight_test.hpp - lightweight test library
  9. //
  10. // Copyright (c) 2002, 2009, 2014 Peter Dimov
  11. // Copyright (2) Beman Dawes 2010, 2011
  12. // Copyright (3) Ion Gaztanaga 2013
  13. //
  14. // Copyright 2018 Glen Joseph Fernandes
  15. // (glenjofe@gmail.com)
  16. //
  17. // Distributed under the Boost Software License, Version 1.0.
  18. // See accompanying file LICENSE_1_0.txt or copy at
  19. // http://www.boost.org/LICENSE_1_0.txt
  20. //
  21. #include <boost/current_function.hpp>
  22. #include <boost/config.hpp>
  23. #include <iostream>
  24. #include <iterator>
  25. #include <cstdlib>
  26. #include <cstring>
  27. #include <cstddef>
  28. // IDE's like Visual Studio perform better if output goes to std::cout or
  29. // some other stream, so allow user to configure output stream:
  30. #ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM
  31. # define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr
  32. #endif
  33. namespace boost
  34. {
  35. namespace detail
  36. {
  37. class test_result {
  38. public:
  39. test_result()
  40. : report_(false)
  41. , errors_(0) {
  42. #if defined(_MSC_VER) && (_MSC_VER > 1310)
  43. ::_set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
  44. #endif
  45. }
  46. ~test_result() {
  47. if (!report_) {
  48. BOOST_LIGHTWEIGHT_TEST_OSTREAM << "main() should return report_errors()" << std::endl;
  49. std::abort();
  50. }
  51. }
  52. int& errors() {
  53. return errors_;
  54. }
  55. void done() {
  56. report_ = true;
  57. }
  58. private:
  59. bool report_;
  60. int errors_;
  61. };
  62. inline test_result& test_results()
  63. {
  64. static test_result instance;
  65. return instance;
  66. }
  67. inline int& test_errors()
  68. {
  69. return test_results().errors();
  70. }
  71. inline void test_failed_impl(char const * expr, char const * file, int line, char const * function)
  72. {
  73. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  74. << file << "(" << line << "): test '" << expr << "' failed in function '"
  75. << function << "'" << std::endl;
  76. ++test_results().errors();
  77. }
  78. inline void error_impl(char const * msg, char const * file, int line, char const * function)
  79. {
  80. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  81. << file << "(" << line << "): " << msg << " in function '"
  82. << function << "'" << std::endl;
  83. ++test_results().errors();
  84. }
  85. inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function)
  86. {
  87. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  88. << file << "(" << line << "): Exception '" << excep << "' not thrown in function '"
  89. << function << "'" << std::endl;
  90. ++test_results().errors();
  91. }
  92. // In the comparisons below, it is possible that T and U are signed and unsigned integer types, which generates warnings in some compilers.
  93. // A cleaner fix would require common_type trait or some meta-programming, which would introduce a dependency on Boost.TypeTraits. To avoid
  94. // the dependency we just disable the warnings.
  95. #if defined(__clang__) && defined(__has_warning)
  96. # if __has_warning("-Wsign-compare")
  97. # pragma clang diagnostic push
  98. # pragma clang diagnostic ignored "-Wsign-compare"
  99. # endif
  100. #elif defined(_MSC_VER)
  101. # pragma warning(push)
  102. # pragma warning(disable: 4389)
  103. #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
  104. # pragma GCC diagnostic push
  105. # pragma GCC diagnostic ignored "-Wsign-compare"
  106. #endif
  107. // specialize test output for char pointers to avoid printing as cstring
  108. template <class T> inline const T& test_output_impl(const T& v) { return v; }
  109. inline const void* test_output_impl(const char* v) { return v; }
  110. inline const void* test_output_impl(const unsigned char* v) { return v; }
  111. inline const void* test_output_impl(const signed char* v) { return v; }
  112. inline const void* test_output_impl(char* v) { return v; }
  113. inline const void* test_output_impl(unsigned char* v) { return v; }
  114. inline const void* test_output_impl(signed char* v) { return v; }
  115. template<class T> inline const void* test_output_impl(T volatile* v) { return const_cast<T*>(v); }
  116. #if !defined( BOOST_NO_CXX11_NULLPTR )
  117. inline const void* test_output_impl(std::nullptr_t) { return nullptr; }
  118. #endif
  119. struct lw_test_eq {
  120. template <typename T, typename U>
  121. bool operator()(const T& t, const U& u) const { return t == u; }
  122. static const char* op() { return "=="; }
  123. };
  124. struct lw_test_ne {
  125. template <typename T, typename U>
  126. bool operator()(const T& t, const U& u) const { return t != u; }
  127. static const char* op() { return "!="; }
  128. };
  129. struct lw_test_lt {
  130. template <typename T, typename U>
  131. bool operator()(const T& t, const U& u) const { return t < u; }
  132. static const char* op() { return "<"; }
  133. };
  134. struct lw_test_le {
  135. template <typename T, typename U>
  136. bool operator()(const T& t, const U& u) const { return t <= u; }
  137. static const char* op() { return "<="; }
  138. };
  139. struct lw_test_gt {
  140. template <typename T, typename U>
  141. bool operator()(const T& t, const U& u) const { return t > u; }
  142. static const char* op() { return ">"; }
  143. };
  144. struct lw_test_ge {
  145. template <typename T, typename U>
  146. bool operator()(const T& t, const U& u) const { return t >= u; }
  147. static const char* op() { return ">="; }
  148. };
  149. template<class BinaryPredicate, class T, class U>
  150. inline void test_with_impl(BinaryPredicate pred, char const * expr1, char const * expr2,
  151. char const * file, int line, char const * function,
  152. T const & t, U const & u)
  153. {
  154. if( pred(t, u) )
  155. {
  156. test_results();
  157. }
  158. else
  159. {
  160. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  161. << file << "(" << line << "): test '" << expr1 << " " << pred.op() << " " << expr2
  162. << "' ('" << test_output_impl(t) << "' " << pred.op() << " '" << test_output_impl(u)
  163. << "') failed in function '" << function << "'" << std::endl;
  164. ++test_results().errors();
  165. }
  166. }
  167. inline void test_cstr_eq_impl( char const * expr1, char const * expr2,
  168. char const * file, int line, char const * function, char const * const t, char const * const u )
  169. {
  170. if( std::strcmp(t, u) == 0 )
  171. {
  172. test_results();
  173. }
  174. else
  175. {
  176. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  177. << file << "(" << line << "): test '" << expr1 << " == " << expr2 << "' ('" << t
  178. << "' == '" << u << "') failed in function '" << function << "'" << std::endl;
  179. ++test_results().errors();
  180. }
  181. }
  182. inline void test_cstr_ne_impl( char const * expr1, char const * expr2,
  183. char const * file, int line, char const * function, char const * const t, char const * const u )
  184. {
  185. if( std::strcmp(t, u) != 0 )
  186. {
  187. test_results();
  188. }
  189. else
  190. {
  191. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  192. << file << "(" << line << "): test '" << expr1 << " != " << expr2 << "' ('" << t
  193. << "' != '" << u << "') failed in function '" << function << "'" << std::endl;
  194. ++test_results().errors();
  195. }
  196. }
  197. template<class FormattedOutputFunction, class InputIterator1, class InputIterator2>
  198. void test_all_eq_impl(FormattedOutputFunction& output,
  199. char const * file, int line, char const * function,
  200. InputIterator1 first_begin, InputIterator1 first_end,
  201. InputIterator2 second_begin, InputIterator2 second_end)
  202. {
  203. InputIterator1 first_it = first_begin;
  204. InputIterator2 second_it = second_begin;
  205. typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
  206. typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
  207. std::size_t error_count = 0;
  208. const std::size_t max_count = 8;
  209. do
  210. {
  211. while ((first_it != first_end) && (second_it != second_end) && (*first_it == *second_it))
  212. {
  213. ++first_it;
  214. ++second_it;
  215. ++first_index;
  216. ++second_index;
  217. }
  218. if ((first_it == first_end) || (second_it == second_end))
  219. {
  220. break; // do-while
  221. }
  222. if (error_count == 0)
  223. {
  224. output << file << "(" << line << "): Container contents differ in function '" << function << "':";
  225. }
  226. else if (error_count >= max_count)
  227. {
  228. output << " ...";
  229. break;
  230. }
  231. output << " [" << first_index << "] '" << test_output_impl(*first_it) << "' != '" << test_output_impl(*second_it) << "'";
  232. ++first_it;
  233. ++second_it;
  234. ++first_index;
  235. ++second_index;
  236. ++error_count;
  237. } while (first_it != first_end);
  238. first_index += std::distance(first_it, first_end);
  239. second_index += std::distance(second_it, second_end);
  240. if (first_index != second_index)
  241. {
  242. if (error_count == 0)
  243. {
  244. output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
  245. }
  246. else
  247. {
  248. output << " [*] size(" << first_index << ") != size(" << second_index << ")";
  249. }
  250. ++error_count;
  251. }
  252. if (error_count == 0)
  253. {
  254. test_results();
  255. }
  256. else
  257. {
  258. output << std::endl;
  259. ++test_results().errors();
  260. }
  261. }
  262. template<class FormattedOutputFunction, class InputIterator1, class InputIterator2, typename BinaryPredicate>
  263. void test_all_with_impl(FormattedOutputFunction& output,
  264. char const * file, int line, char const * function,
  265. InputIterator1 first_begin, InputIterator1 first_end,
  266. InputIterator2 second_begin, InputIterator2 second_end,
  267. BinaryPredicate predicate)
  268. {
  269. InputIterator1 first_it = first_begin;
  270. InputIterator2 second_it = second_begin;
  271. typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
  272. typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
  273. std::size_t error_count = 0;
  274. const std::size_t max_count = 8;
  275. do
  276. {
  277. while ((first_it != first_end) && (second_it != second_end) && predicate(*first_it, *second_it))
  278. {
  279. ++first_it;
  280. ++second_it;
  281. ++first_index;
  282. ++second_index;
  283. }
  284. if ((first_it == first_end) || (second_it == second_end))
  285. {
  286. break; // do-while
  287. }
  288. if (error_count == 0)
  289. {
  290. output << file << "(" << line << "): Container contents differ in function '" << function << "':";
  291. }
  292. else if (error_count >= max_count)
  293. {
  294. output << " ...";
  295. break;
  296. }
  297. output << " [" << first_index << "]";
  298. ++first_it;
  299. ++second_it;
  300. ++first_index;
  301. ++second_index;
  302. ++error_count;
  303. } while (first_it != first_end);
  304. first_index += std::distance(first_it, first_end);
  305. second_index += std::distance(second_it, second_end);
  306. if (first_index != second_index)
  307. {
  308. if (error_count == 0)
  309. {
  310. output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
  311. }
  312. else
  313. {
  314. output << " [*] size(" << first_index << ") != size(" << second_index << ")";
  315. }
  316. ++error_count;
  317. }
  318. if (error_count == 0)
  319. {
  320. test_results();
  321. }
  322. else
  323. {
  324. output << std::endl;
  325. ++test_results().errors();
  326. }
  327. }
  328. #if defined(__clang__) && defined(__has_warning)
  329. # if __has_warning("-Wsign-compare")
  330. # pragma clang diagnostic pop
  331. # endif
  332. #elif defined(_MSC_VER)
  333. # pragma warning(pop)
  334. #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
  335. # pragma GCC diagnostic pop
  336. #endif
  337. } // namespace detail
  338. inline int report_errors()
  339. {
  340. boost::detail::test_result& result = boost::detail::test_results();
  341. result.done();
  342. int errors = result.errors();
  343. if( errors == 0 )
  344. {
  345. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  346. << "No errors detected." << std::endl;
  347. }
  348. else
  349. {
  350. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  351. << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
  352. }
  353. // `return report_errors();` from main only supports 8 bit exit codes
  354. return errors < 256? errors: 255;
  355. }
  356. } // namespace boost
  357. #define BOOST_TEST(expr) ((expr)? (void)::boost::detail::test_results(): ::boost::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
  358. #define BOOST_TEST_NOT(expr) BOOST_TEST(!(expr))
  359. #define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
  360. #define BOOST_TEST_EQ(expr1,expr2) ( ::boost::detail::test_with_impl(::boost::detail::lw_test_eq(), #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
  361. #define BOOST_TEST_NE(expr1,expr2) ( ::boost::detail::test_with_impl(::boost::detail::lw_test_ne(), #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
  362. #define BOOST_TEST_LT(expr1,expr2) ( ::boost::detail::test_with_impl(::boost::detail::lw_test_lt(), #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
  363. #define BOOST_TEST_LE(expr1,expr2) ( ::boost::detail::test_with_impl(::boost::detail::lw_test_le(), #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
  364. #define BOOST_TEST_GT(expr1,expr2) ( ::boost::detail::test_with_impl(::boost::detail::lw_test_gt(), #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
  365. #define BOOST_TEST_GE(expr1,expr2) ( ::boost::detail::test_with_impl(::boost::detail::lw_test_ge(), #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
  366. #define BOOST_TEST_CSTR_EQ(expr1,expr2) ( ::boost::detail::test_cstr_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
  367. #define BOOST_TEST_CSTR_NE(expr1,expr2) ( ::boost::detail::test_cstr_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
  368. #define BOOST_TEST_ALL_EQ(begin1, end1, begin2, end2) ( ::boost::detail::test_all_eq_impl(BOOST_LIGHTWEIGHT_TEST_OSTREAM, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, begin1, end1, begin2, end2) )
  369. #define BOOST_TEST_ALL_WITH(begin1, end1, begin2, end2, predicate) ( ::boost::detail::test_all_with_impl(BOOST_LIGHTWEIGHT_TEST_OSTREAM, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, begin1, end1, begin2, end2, predicate) )
  370. #ifndef BOOST_NO_EXCEPTIONS
  371. #define BOOST_TEST_THROWS( EXPR, EXCEP ) \
  372. try { \
  373. EXPR; \
  374. ::boost::detail::throw_failed_impl \
  375. (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
  376. } \
  377. catch(EXCEP const&) { \
  378. ::boost::detail::test_results(); \
  379. } \
  380. catch(...) { \
  381. ::boost::detail::throw_failed_impl \
  382. (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
  383. } \
  384. //
  385. #else
  386. #define BOOST_TEST_THROWS( EXPR, EXCEP )
  387. #endif
  388. #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP