Update contrib.
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
20 #include "cppunit/cppunit_proxy.h"
23 * This test case purpose is to check that the exception handling
24 * functions are correctly imported to the STLport namespace only
25 * if they have a right behavior.
26 * Otherwise they are not imported to report the problem as a compile
33 class ExceptionTest : public CPPUNIT_NS::TestCase
35 CPPUNIT_TEST_SUITE(ExceptionTest);
36 #if defined (STLPORT) && !defined (_STLP_USE_EXCEPTIONS)
39 #if defined (STLPORT) && defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT)
42 CPPUNIT_TEST(unexpected_except);
43 #if defined (STLPORT) && defined (_STLP_USE_EXCEPTIONS)
46 #if defined (STLPORT) && defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT)
49 CPPUNIT_TEST(uncaught_except);
50 #if defined (STLPORT) && defined (_STLP_USE_EXCEPTIONS)
53 CPPUNIT_TEST(exception_emission);
54 CPPUNIT_TEST_SUITE_END();
57 void unexpected_except();
58 void uncaught_except();
59 void exception_emission();
62 CPPUNIT_TEST_SUITE_REGISTRATION(ExceptionTest);
64 #if !defined (STLPORT) || !defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT)
65 bool g_unexpected_called = false;
66 void unexpected_hdl() {
67 g_unexpected_called = true;
68 throw std::bad_exception();
71 struct special_except {};
73 throw special_except();
76 void throw_except_func() throw(std::exception) {
81 void ExceptionTest::unexpected_except()
83 #if !defined (STLPORT) || !defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT)
84 std::unexpected_handler hdl = &unexpected_hdl;
85 std::set_unexpected(hdl);
90 catch (std::bad_exception const&) {
91 CPPUNIT_ASSERT( true );
93 catch (special_except) {
94 CPPUNIT_ASSERT( false );
96 CPPUNIT_ASSERT( g_unexpected_called );
100 #if !defined (STLPORT) || !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT)
101 struct UncaughtClassTest
103 UncaughtClassTest(int &res) : _res(res)
106 ~UncaughtClassTest() {
107 _res = std::uncaught_exception()?1:0;
114 void ExceptionTest::uncaught_except()
116 #if !defined (STLPORT) || !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT)
117 int uncaught_result = -1;
119 UncaughtClassTest test_inst(uncaught_result);
120 CPPUNIT_ASSERT( uncaught_result == -1 );
122 CPPUNIT_ASSERT( uncaught_result == 0 );
126 uncaught_result = -1;
127 UncaughtClassTest test_inst(uncaught_result);
133 CPPUNIT_ASSERT( uncaught_result == 1 );
137 void runtime_thrower(std::string& str)
139 throw std::runtime_error(str);
142 void ExceptionTest::exception_emission()
144 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
145 std::string foo = "foo";
147 //throw std::runtime_error(foo);
148 runtime_thrower(foo);
150 catch (std::runtime_error const& e) {
151 CPPUNIT_ASSERT( foo == e.what() );
154 CPPUNIT_ASSERT( false );
158 std::string msg(512, 'a');
159 //throw std::runtime_error(msg);
160 runtime_thrower(msg);
162 catch (std::runtime_error const& e) {
163 const char* c = e.what();
165 CPPUNIT_ASSERT( *c++ == 'a' );
169 CPPUNIT_ASSERT( false );