Skip to content

Commit 6ef31ba

Browse files
committed
Stop using assert() in tests so we can test with NDEBUG defined.
[SVN r33026]
1 parent c15216b commit 6ef31ba

19 files changed

+125
-90
lines changed

test/back_reference.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#include <boost/ref.hpp>
1111
#include <boost/utility.hpp>
1212
#include <memory>
13-
#include <cassert>
13+
#define BOOST_ENABLE_ASSERT_HANDLER
14+
#include <boost/assert.hpp>
1415
#include <boost/python/copy_const_reference.hpp>
1516
#include <boost/python/return_value_policy.hpp>
1617
#include <boost/mpl/bool.hpp>
@@ -24,10 +25,10 @@ struct X
2425
{
2526
explicit X(int x) : x(x), magic(7654321) { ++counter; }
2627
X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; }
27-
virtual ~X() { assert(magic == 7654321); magic = 6666666; x = 9999; --counter; }
28+
virtual ~X() { BOOST_ASSERT(magic == 7654321); magic = 6666666; x = 9999; --counter; }
2829

29-
void set(int x) { assert(magic == 7654321); this->x = x; }
30-
int value() const { assert(magic == 7654321); return x; }
30+
void set(int x) { BOOST_ASSERT(magic == 7654321); this->x = x; }
31+
int value() const { BOOST_ASSERT(magic == 7654321); return x; }
3132
static int count() { return counter; }
3233
private:
3334
void operator=(X const&);

test/callbacks.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Distributed under the Boost Software License, Version 1.0. (See
33
// accompanying file LICENSE_1_0.txt or copy at
44
// http://www.boost.org/LICENSE_1_0.txt)
5+
#include <boost/assert.hpp>
6+
57
#include <boost/python/module.hpp>
68
#include <boost/python/def.hpp>
79
#include <boost/python/class.hpp>
@@ -29,10 +31,10 @@ struct X
2931
{
3032
explicit X(int x) : x(x), magic(7654321) { ++counter; }
3133
X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; }
32-
~X() { assert(magic == 7654321); magic = 6666666; x = 9999; --counter; }
34+
~X() { BOOST_ASSERT(magic == 7654321); magic = 6666666; x = 9999; --counter; }
3335

34-
void set(int x) { assert(magic == 7654321); this->x = x; }
35-
int value() const { assert(magic == 7654321); return x; }
36+
void set(int x) { BOOST_ASSERT(magic == 7654321); this->x = x; }
37+
int value() const { BOOST_ASSERT(magic == 7654321); return x; }
3638
static int count() { return counter; }
3739
private:
3840
void operator=(X const&);

test/destroy_test.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// Software License, Version 1.0. (See accompanying
33
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
44
#include <boost/python/detail/destroy.hpp>
5-
#include <cassert>
5+
6+
#include <boost/detail/lightweight_test.hpp>
67

78
int count;
89
int marks[] = {
@@ -26,8 +27,8 @@ struct foo
2627
void assert_destructions(int n)
2728
{
2829
for (int i = 0; i < n; ++i)
29-
assert(marks[i] == i);
30-
assert(marks[n] == -1);
30+
BOOST_TEST(marks[i] == i);
31+
BOOST_TEST(marks[n] == -1);
3132
}
3233

3334
int main()
@@ -50,5 +51,5 @@ int main()
5051
boost::python::detail::destroy_referent<y&>(f3);
5152
assert_destructions(7);
5253

53-
return 0;
54+
return boost::report_errors();
5455
}

test/dict.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Software License, Version 1.0. (See accompanying
33
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
44
#include <boost/python/module.hpp>
5+
#include <boost/assert.hpp>
6+
57
#include <boost/python/def.hpp>
68
#include <boost/python/class.hpp>
79
#include <boost/python/dict.hpp>
@@ -69,7 +71,7 @@ void test_templates(object print)
6971
print(tmp.get(2,"default"));
7072
print(tmp.setdefault(3,"default"));
7173

72-
assert(!tmp.has_key(key));
74+
BOOST_ASSERT(!tmp.has_key(key));
7375
//print(tmp[3]);
7476
}
7577

test/exec.cpp

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,10 @@
44
// http://www.boost.org/LICENSE_1_0.txt)
55

66
#include <boost/python.hpp>
7-
#define BOOST_ENABLE_ASSERT_HANDLER 1
8-
#include <boost/assert.hpp>
9-
#include <iostream>
10-
11-
namespace boost
12-
{
137

14-
void assertion_failed(char const * expr, char const * function,
15-
char const * file, long line)
16-
{
17-
std::cerr << "assertion failed : " << expr << " in " << function
18-
<< " at " << file << ':' << line << std::endl;
19-
abort();
20-
}
8+
#include <boost/detail/lightweight_test.hpp>
9+
#include <iostream>
2110

22-
} // namespace boost
2311

2412
namespace python = boost::python;
2513

@@ -85,15 +73,15 @@ void exec_test()
8573

8674
// Creating and using instances of the C++ class is as easy as always.
8775
CppDerived cpp;
88-
BOOST_ASSERT(cpp.hello() == "Hello from C++!");
76+
BOOST_TEST(cpp.hello() == "Hello from C++!");
8977

9078
// But now creating and using instances of the Python class is almost
9179
// as easy!
9280
python::object py_base = PythonDerived();
9381
Base& py = python::extract<Base&>(py_base) BOOST_EXTRACT_WORKAROUND;
9482

9583
// Make sure the right 'hello' method is called.
96-
BOOST_ASSERT(py.hello() == "Hello from Python!");
84+
BOOST_TEST(py.hello() == "Hello from Python!");
9785
}
9886

9987
void exec_file_test(std::string const &script)
@@ -103,7 +91,7 @@ void exec_file_test(std::string const &script)
10391
python::object result = python::exec_file(script.c_str(), global, global);
10492

10593
// Extract an object the script stored in the global dictionary.
106-
BOOST_ASSERT(python::extract<int>(global["number"]) == 42);
94+
BOOST_TEST(python::extract<int>(global["number"]) == 42);
10795
}
10896

10997
void exec_test_error()
@@ -115,9 +103,8 @@ void exec_test_error()
115103

116104
int main(int argc, char **argv)
117105
{
118-
assert(argc == 2);
106+
BOOST_TEST(argc == 2);
119107
std::string script = argv[1];
120-
bool success = true;
121108
// Initialize the interpreter
122109
Py_Initialize();
123110

@@ -126,15 +113,16 @@ int main(int argc, char **argv)
126113
{
127114
if (PyErr_Occurred())
128115
{
116+
BOOST_ERROR("Python Error detected");
129117
PyErr_Print();
130118
}
131119
else
132120
{
133-
std::cerr << "A C++ exception was thrown for which "
134-
<< "there was no exception handler registered." << std::endl;
121+
BOOST_ERROR("A C++ exception was thrown for which "
122+
"there was no exception handler registered.");
135123
}
136-
success = false;
137124
}
125+
138126
if (python::handle_exception(exec_test_error))
139127
{
140128
if (PyErr_Occurred())
@@ -143,18 +131,18 @@ int main(int argc, char **argv)
143131
}
144132
else
145133
{
146-
std::cerr << "A C++ exception was thrown for which "
147-
<< "there was no exception handler registered." << std::endl;
148-
success = false;
134+
BOOST_ERROR("A C++ exception was thrown for which "
135+
"there was no exception handler registered.");
149136
}
150137
}
151138
else
152139
{
153-
success = false;
140+
BOOST_ERROR("Python exception expected, but not seen.");
154141
}
142+
155143
// Boost.Python doesn't support Py_Finalize yet.
156144
// Py_Finalize();
157-
return success ? 0 : 1;
145+
return boost::report_errors();
158146
}
159147

160148
// Including this file makes sure

test/extract.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
#include <boost/python/implicit.hpp>
1414
#include <string>
1515
#include <boost/lexical_cast.hpp>
16-
#include <cassert>
16+
#define BOOST_ENABLE_ASSERT_HANDLER
17+
#include <boost/assert.hpp>
1718
#include "test_class.hpp"
1819

1920
using namespace boost::python;

test/indirect_traits_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// Software License, Version 1.0. (See accompanying
33
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
44
//#include <stdio.h>
5-
#include <cassert>
5+
#define BOOST_ENABLE_ASSERT_HANDLER
6+
#include <boost/assert.hpp>
67
#include <boost/type_traits/is_member_function_pointer.hpp>
78
#include <boost/mpl/assert.hpp>
89
#include <boost/python/detail/indirect_traits.hpp>

test/list.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Distributed under the Boost Software License, Version 1.0. (See
33
// accompanying file LICENSE_1_0.txt or copy at
44
// http://www.boost.org/LICENSE_1_0.txt)
5+
#include <boost/assert.hpp>
6+
57

68
#include <boost/python/module.hpp>
79
#include <boost/python/def.hpp>
@@ -118,10 +120,10 @@ void exercise(list x, object y, object print)
118120
w.append(5);
119121
w.append(6);
120122
w += "hi";
121-
assert(w[0] == 5);
122-
assert(w[1] == 6);
123-
assert(w[2] == 'h');
124-
assert(w[3] == 'i');
123+
BOOST_ASSERT(w[0] == 5);
124+
BOOST_ASSERT(w[1] == 6);
125+
BOOST_ASSERT(w[2] == 'h');
126+
BOOST_ASSERT(w[3] == 'i');
125127
}
126128

127129
BOOST_PYTHON_MODULE(list_ext)

test/long.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#include <boost/python/def.hpp>
88
#include <boost/python/long.hpp>
99
#include <boost/python/class.hpp>
10-
#include <cassert>
10+
#define BOOST_ENABLE_ASSERT_HANDLER
11+
#include <boost/assert.hpp>
1112

1213
using namespace boost::python;
1314

@@ -30,7 +31,7 @@ char const* is_long1(long_& x)
3031
{
3132
long_ y = x;
3233
x += 50;
33-
assert(x == y + 50);
34+
BOOST_ASSERT(x == y + 50);
3435
return "yes";
3536
}
3637

@@ -59,3 +60,4 @@ BOOST_PYTHON_MODULE(long_ext)
5960
;
6061
}
6162

63+
#include "module_tail.cpp"

test/module_tail.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,29 @@ extern "C" void (*old_translator)(unsigned, EXCEPTION_POINTERS*)
2727

2828
#endif // _WIN32
2929

30+
#include <exception>
31+
#include <boost/python/extract.hpp>
32+
#include <boost/python/str.hpp>
33+
struct test_failure : std::exception
34+
{
35+
test_failure(char const* expr, char const* function, char const* file, unsigned line)
36+
: msg(file + boost::python::str(":%s:") % line + ": Boost.Python assertion failure: " + expr)
37+
{}
38+
39+
char const* what() throw()
40+
{
41+
return boost::python::extract<char const*>(msg)();
42+
}
43+
44+
boost::python::str msg;
45+
};
46+
47+
namespace boost
48+
{
49+
50+
void assertion_failed(char const * expr, char const * function, char const * file, long line)
51+
{
52+
throw ::test_failure(expr,function, file, line);
53+
}
54+
55+
} // namespace boost

0 commit comments

Comments
 (0)