5 #include "cppunit/cppunit_proxy.h"
7 #if defined (STLPORT) && defined (_STLP_USE_NAMESPACES)
9 * This test case purpose is to check that the exception handling
10 * functions are correctly imported to the STLport namespace only
11 * if they have a right behavior.
12 * Otherwise they are not imported to report the problem as a compile
19 class ExceptionTest
: public CPPUNIT_NS::TestCase
21 CPPUNIT_TEST_SUITE(ExceptionTest
);
22 #if defined (STLPORT) && !defined (_STLP_USE_EXCEPTIONS)
26 #if defined (STLPORT) && defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT)
29 CPPUNIT_TEST(unexpected_except
);
30 #if defined (STLPORT) && defined (_STLP_USE_EXCEPTIONS)
33 #if defined (STLPORT) && defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT)
36 CPPUNIT_TEST(uncaught_except
);
37 #if defined (STLPORT) && defined (_STLP_USE_EXCEPTIONS)
40 CPPUNIT_TEST(exception_emission
);
41 CPPUNIT_TEST_SUITE_END();
45 void unexpected_except();
46 void uncaught_except();
47 void exception_emission();
50 CPPUNIT_TEST_SUITE_REGISTRATION(ExceptionTest
);
52 #if !defined (STLPORT) || !defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT)
53 bool g_unexpected_called
= false;
54 void unexpected_hdl() {
55 g_unexpected_called
= true;
56 throw std::bad_exception();
59 struct special_except
{};
61 throw special_except();
64 void throw_except_func() throw(std::exception
) {
69 void ExceptionTest::what()
72 throw std::runtime_error( std::string( "message" ) );
74 catch ( std::runtime_error
& err
) {
75 CPPUNIT_CHECK( strcmp( err
.what(), "message" ) == 0 );
79 void ExceptionTest::unexpected_except()
81 #if !defined (STLPORT) || !defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT)
82 std::unexpected_handler hdl
= &unexpected_hdl
;
83 std::set_unexpected(hdl
);
88 catch (std::bad_exception
const&) {
89 CPPUNIT_ASSERT( true );
91 catch (special_except
) {
92 CPPUNIT_ASSERT( false );
94 CPPUNIT_ASSERT( g_unexpected_called
);
98 #if !defined (STLPORT) || !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT)
99 struct UncaughtClassTest
101 UncaughtClassTest(int &res
) : _res(res
)
104 ~UncaughtClassTest() {
105 _res
= std::uncaught_exception()?1:0;
112 void ExceptionTest::uncaught_except()
114 #if !defined (STLPORT) || !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT)
115 int uncaught_result
= -1;
117 UncaughtClassTest
test_inst(uncaught_result
);
118 CPPUNIT_ASSERT( uncaught_result
== -1 );
120 CPPUNIT_ASSERT( uncaught_result
== 0 );
124 uncaught_result
= -1;
125 UncaughtClassTest
test_inst(uncaught_result
);
131 CPPUNIT_ASSERT( uncaught_result
== 1 );
135 void ExceptionTest::exception_emission()
137 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
138 std::string foo
= "foo";
140 throw std::runtime_error(foo
);
142 catch (std::runtime_error
const& e
) {
143 CPPUNIT_ASSERT( foo
== e
.what() );
144 std::runtime_error
clone("");
146 CPPUNIT_ASSERT(foo
== clone
.what() );
149 CPPUNIT_ASSERT( false );
153 throw std::runtime_error(foo
);
155 catch (std::runtime_error e
) {
156 CPPUNIT_ASSERT( foo
== e
.what() );
157 std::runtime_error
clone("");
159 CPPUNIT_ASSERT(foo
== clone
.what() );
162 CPPUNIT_ASSERT( false );
165 std::string
msg(512, 'a');
167 throw std::runtime_error(msg
);
169 catch (std::runtime_error
const& e
) {
170 CPPUNIT_ASSERT(msg
== e
.what() );
171 std::runtime_error
clone("");
173 CPPUNIT_ASSERT(msg
== clone
.what() );
176 CPPUNIT_ASSERT( false );
180 throw std::runtime_error(msg
);
182 catch (std::runtime_error e
) {
183 CPPUNIT_ASSERT(msg
== e
.what() );
184 std::runtime_error
clone("");
186 CPPUNIT_ASSERT(msg
== clone
.what() );
189 CPPUNIT_ASSERT( false );