]> git.lyx.org Git - lyx.git/blob - boost/boost/detail/lightweight_test.hpp
Don't allow newline characters in document settings.
[lyx.git] / boost / boost / detail / lightweight_test.hpp
1 #ifndef BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED
2 #define BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED
3
4 // MS compatible compilers support #pragma once
5
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
7 # pragma once
8 #endif
9
10 //
11 //  boost/detail/lightweight_test.hpp - lightweight test library
12 //
13 //  Copyright (c) 2002, 2009 Peter Dimov
14 //  Copyright (2) Beman Dawes 2010, 2011
15 //
16 //  Distributed under the Boost Software License, Version 1.0.
17 //  See accompanying file LICENSE_1_0.txt or copy at
18 //  http://www.boost.org/LICENSE_1_0.txt
19 //
20 //  BOOST_TEST(expression)
21 //  BOOST_ERROR(message)
22 //  BOOST_TEST_EQ(expr1, expr2)
23 //
24 //  int boost::report_errors()
25 //
26
27 #include <iostream>
28 #include <boost/current_function.hpp>
29 #include <boost/assert.hpp>
30
31 //  IDE's like Visual Studio perform better if output goes to std::cout or
32 //  some other stream, so allow user to configure output stream:
33 #ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM
34 # define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr
35 #endif
36
37 namespace boost
38 {
39
40 namespace detail
41 {
42
43 struct report_errors_reminder
44 {
45   bool called_report_errors_function;
46   report_errors_reminder() : called_report_errors_function(false) {}
47  ~report_errors_reminder()
48   {
49     BOOST_ASSERT(called_report_errors_function);  // verify report_errors() was called  
50   }
51 };
52
53 inline report_errors_reminder& report_errors_remind()
54 {
55   static report_errors_reminder r;
56   return r;
57 }
58
59 inline int & test_errors()
60 {
61     static int x = 0;
62     report_errors_remind();
63     return x;
64 }
65
66 inline void test_failed_impl(char const * expr, char const * file, int line, char const * function)
67 {
68     BOOST_LIGHTWEIGHT_TEST_OSTREAM
69       << file << "(" << line << "): test '" << expr << "' failed in function '"
70       << function << "'" << std::endl;
71     ++test_errors();
72 }
73
74 inline void error_impl(char const * msg, char const * file, int line, char const * function)
75 {
76     BOOST_LIGHTWEIGHT_TEST_OSTREAM
77       << file << "(" << line << "): " << msg << " in function '"
78       << function << "'" << std::endl;
79     ++test_errors();
80 }
81
82 template<class T, class U> inline void test_eq_impl( char const * expr1, char const * expr2,
83   char const * file, int line, char const * function, T const & t, U const & u )
84 {
85     if( t == u )
86     {
87     }
88     else
89     {
90         BOOST_LIGHTWEIGHT_TEST_OSTREAM
91             << file << "(" << line << "): test '" << expr1 << " == " << expr2
92             << "' failed in function '" << function << "': "
93             << "'" << t << "' != '" << u << "'" << std::endl;
94         ++test_errors();
95     }
96 }
97
98 template<class T, class U> inline void test_ne_impl( char const * expr1, char const * expr2,
99   char const * file, int line, char const * function, T const & t, U const & u )
100 {
101     if( t != u )
102     {
103     }
104     else
105     {
106         BOOST_LIGHTWEIGHT_TEST_OSTREAM
107             << file << "(" << line << "): test '" << expr1 << " != " << expr2
108             << "' failed in function '" << function << "': "
109             << "'" << t << "' == '" << u << "'" << std::endl;
110         ++test_errors();
111     }
112 }
113
114 } // namespace detail
115
116 inline int report_errors()
117 {
118     detail::report_errors_remind().called_report_errors_function = true;
119
120     int errors = detail::test_errors();
121
122     if( errors == 0 )
123     {
124         BOOST_LIGHTWEIGHT_TEST_OSTREAM
125           << "No errors detected." << std::endl;
126         return 0;
127     }
128     else
129     {
130         BOOST_LIGHTWEIGHT_TEST_OSTREAM
131           << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
132         return 1;
133     }
134 }
135
136 } // namespace boost
137
138 #define BOOST_TEST(expr) ((expr)? (void)0: ::boost::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
139 #define BOOST_ERROR(msg) ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
140 #define BOOST_TEST_EQ(expr1,expr2) ( ::boost::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
141 #define BOOST_TEST_NE(expr1,expr2) ( ::boost::detail::test_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
142
143 #endif // #ifndef BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED