1 //
2 //  assert_msg_exp_test.cpp - tests BOOST_ASSERT_MSG expansion
3 //
4 //  Copyright (c) 2014 Peter Dimov
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt
9 //
10 
11 #include <boost/config.hpp>
12 #include <boost/current_function.hpp>
13 #include <boost/detail/lightweight_test.hpp>
14 #include <string>
15 
16 // Each backslash in __FILE__ when passed through BOOST_STRINGIZE is doubled
quote(std::string const & s)17 static std::string quote( std::string const & s )
18 {
19     std::string r;
20     r.reserve( s.size() );
21 
22     for( char const * p = s.c_str(); *p; ++p )
23     {
24         r += *p;
25         if( *p == '\\' ) r += *p;
26     }
27 
28     return r;
29 }
30 
31 // default case, !NDEBUG
32 // BOOST_ASSERT_MSG(x,"m") -> assert((x)&&("m"))
33 
34 #undef NDEBUG
35 #include <boost/assert.hpp>
36 #undef assert
37 
test_default()38 void test_default()
39 {
40     std::string v1 = BOOST_STRINGIZE(BOOST_ASSERT_MSG(x1, "m1"));
41     BOOST_TEST_EQ( v1, "assert((x1)&&(\"m1\"))" );
42 }
43 
44 // default case, NDEBUG
45 // BOOST_ASSERT_MSG(x,"m") -> assert((x)&&("m"))
46 
47 #define NDEBUG
48 #include <boost/assert.hpp>
49 #undef assert
50 
test_default_ndebug()51 void test_default_ndebug()
52 {
53     std::string v2 = BOOST_STRINGIZE(BOOST_ASSERT_MSG(x2, "m2"));
54     BOOST_TEST_EQ( v2, "assert((x2)&&(\"m2\"))" );
55 }
56 
57 // BOOST_DISABLE_ASSERTS, !NDEBUG
58 // BOOST_ASSERT_MSG(x,"m") -> ((void)0)
59 
60 #define BOOST_DISABLE_ASSERTS
61 
62 #undef NDEBUG
63 #include <boost/assert.hpp>
64 
test_disabled()65 void test_disabled()
66 {
67     std::string v3 = BOOST_STRINGIZE(BOOST_ASSERT_MSG(x3, "m3"));
68     BOOST_TEST_EQ( v3, "((void)0)" );
69 }
70 
71 // BOOST_DISABLE_ASSERTS, NDEBUG
72 // BOOST_ASSERT_MSG(x,"m") -> ((void)0)
73 
74 #define NDEBUG
75 #include <boost/assert.hpp>
76 
test_disabled_ndebug()77 void test_disabled_ndebug()
78 {
79     std::string v4 = BOOST_STRINGIZE(BOOST_ASSERT_MSG(x4, "m4"));
80     BOOST_TEST_EQ( v4, "((void)0)" );
81 }
82 
83 #undef BOOST_DISABLE_ASSERTS
84 
85 // BOOST_ENABLE_ASSERT_HANDLER, !NDEBUG
86 // BOOST_ASSERT_MSG(expr, msg) -> (BOOST_LIKELY(!!(expr))? ((void)0): ::boost::assertion_failed_msg(#expr, msg, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
87 
88 #undef BOOST_LIKELY
89 #undef BOOST_CURRENT_FUNCTION
90 
91 #define BOOST_ENABLE_ASSERT_HANDLER
92 
93 #undef NDEBUG
94 #include <boost/assert.hpp>
95 
test_handler()96 void test_handler()
97 {
98     std::string v5 = BOOST_STRINGIZE(BOOST_ASSERT_MSG(x5, "m5")); std::string w5 = "(BOOST_LIKELY(!!(x5))? ((void)0): ::boost::assertion_failed_msg(\"x5\", \"m5\", BOOST_CURRENT_FUNCTION, \"" + quote( __FILE__ ) + "\", " BOOST_STRINGIZE(__LINE__) "))";
99 
100     char const * BOOST_CURRENT_FUNCTION = "void test_handler()";
101     BOOST_TEST_EQ( v5, w5 );
102 }
103 
104 // BOOST_ENABLE_ASSERT_HANDLER, NDEBUG
105 // BOOST_ASSERT_MSG(expr, msg) -> (BOOST_LIKELY(!!(expr))? ((void)0): ::boost::assertion_failed_msg(#expr, msg, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
106 
107 #define NDEBUG
108 #include <boost/assert.hpp>
109 
test_handler_ndebug()110 void test_handler_ndebug()
111 {
112     std::string v6 = BOOST_STRINGIZE(BOOST_ASSERT_MSG(x6, "m6")); std::string w6 = "(BOOST_LIKELY(!!(x6))? ((void)0): ::boost::assertion_failed_msg(\"x6\", \"m6\", BOOST_CURRENT_FUNCTION, \"" + quote( __FILE__ ) + "\", " BOOST_STRINGIZE(__LINE__) "))";
113 
114     char const * BOOST_CURRENT_FUNCTION = "void test_handler_ndebug()";
115     BOOST_TEST_EQ( v6, w6 );
116 }
117 
118 #undef BOOST_ENABLE_ASSERT_HANDLER
119 
120 // BOOST_ENABLE_ASSERT_DEBUG_HANDLER, !NDEBUG
121 // same as BOOST_ENABLE_ASSERT_HANDLER
122 
123 #define BOOST_ENABLE_ASSERT_DEBUG_HANDLER
124 
125 #undef NDEBUG
126 #include <boost/assert.hpp>
127 
test_debug_handler()128 void test_debug_handler()
129 {
130     std::string v7 = BOOST_STRINGIZE(BOOST_ASSERT_MSG(x7, "m7")); std::string w7 = "(BOOST_LIKELY(!!(x7))? ((void)0): ::boost::assertion_failed_msg(\"x7\", \"m7\", BOOST_CURRENT_FUNCTION, \"" + quote( __FILE__ ) + "\", " BOOST_STRINGIZE(__LINE__) "))";
131 
132     char const * BOOST_CURRENT_FUNCTION = "void test_debug_handler()";
133     BOOST_TEST_EQ( v7, w7 );
134 }
135 
136 // BOOST_ENABLE_ASSERT_DEBUG_HANDLER, NDEBUG
137 // BOOST_ASSERT_MSG(x,"m") -> ((void)0)
138 
139 #define NDEBUG
140 #include <boost/assert.hpp>
141 
test_debug_handler_ndebug()142 void test_debug_handler_ndebug()
143 {
144     std::string v8 = BOOST_STRINGIZE(BOOST_ASSERT_MSG(x8, "m8"));
145 
146     char const * BOOST_CURRENT_FUNCTION = "void test_debug_handler_ndebug()";
147     BOOST_TEST_EQ( v8, "((void)0)" );
148 }
149 
150 #undef BOOST_ENABLE_ASSERT_DEBUG_HANDLER
151 
main()152 int main()
153 {
154     test_default();
155     test_default_ndebug();
156     test_disabled();
157     test_disabled_ndebug();
158     test_handler();
159     test_handler_ndebug();
160     test_debug_handler();
161     test_debug_handler_ndebug();
162 
163     return boost::report_errors();
164 }
165