sl@0: // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include "cppunit/cppunit_proxy.h" sl@0: sl@0: /* sl@0: * This test case purpose is to check that the exception handling sl@0: * functions are correctly imported to the STLport namespace only sl@0: * if they have a right behavior. sl@0: * Otherwise they are not imported to report the problem as a compile sl@0: * time error. sl@0: */ sl@0: sl@0: // sl@0: // TestCase class sl@0: // sl@0: class ExceptionTest : public CPPUNIT_NS::TestCase sl@0: { sl@0: CPPUNIT_TEST_SUITE(ExceptionTest); sl@0: #if defined (STLPORT) && !defined (_STLP_USE_EXCEPTIONS) sl@0: CPPUNIT_IGNORE; sl@0: #endif sl@0: #if defined (STLPORT) && defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT) sl@0: CPPUNIT_IGNORE; sl@0: #endif sl@0: CPPUNIT_TEST(unexpected_except); sl@0: #if defined (STLPORT) && defined (_STLP_USE_EXCEPTIONS) sl@0: CPPUNIT_STOP_IGNORE; sl@0: #endif sl@0: #if defined (STLPORT) && defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT) sl@0: CPPUNIT_IGNORE; sl@0: #endif sl@0: CPPUNIT_TEST(uncaught_except); sl@0: #if defined (STLPORT) && defined (_STLP_USE_EXCEPTIONS) sl@0: CPPUNIT_STOP_IGNORE; sl@0: #endif sl@0: CPPUNIT_TEST(exception_emission); sl@0: CPPUNIT_TEST_SUITE_END(); sl@0: sl@0: protected: sl@0: void unexpected_except(); sl@0: void uncaught_except(); sl@0: void exception_emission(); sl@0: }; sl@0: sl@0: CPPUNIT_TEST_SUITE_REGISTRATION(ExceptionTest); sl@0: sl@0: #if !defined (STLPORT) || !defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT) sl@0: bool g_unexpected_called = false; sl@0: void unexpected_hdl() { sl@0: g_unexpected_called = true; sl@0: throw std::bad_exception(); sl@0: } sl@0: sl@0: struct special_except {}; sl@0: void throw_func() { sl@0: throw special_except(); sl@0: } sl@0: sl@0: void throw_except_func() throw(std::exception) { sl@0: throw_func(); sl@0: } sl@0: #endif sl@0: sl@0: void ExceptionTest::unexpected_except() sl@0: { sl@0: #if !defined (STLPORT) || !defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT) sl@0: std::unexpected_handler hdl = &unexpected_hdl; sl@0: std::set_unexpected(hdl); sl@0: sl@0: try { sl@0: throw_except_func(); sl@0: } sl@0: catch (std::bad_exception const&) { sl@0: CPPUNIT_ASSERT( true ); sl@0: } sl@0: catch (special_except) { sl@0: CPPUNIT_ASSERT( false ); sl@0: } sl@0: CPPUNIT_ASSERT( g_unexpected_called ); sl@0: #endif sl@0: } sl@0: sl@0: #if !defined (STLPORT) || !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT) sl@0: struct UncaughtClassTest sl@0: { sl@0: UncaughtClassTest(int &res) : _res(res) sl@0: {} sl@0: sl@0: ~UncaughtClassTest() { sl@0: _res = std::uncaught_exception()?1:0; sl@0: } sl@0: sl@0: int &_res; sl@0: }; sl@0: #endif sl@0: sl@0: void ExceptionTest::uncaught_except() sl@0: { sl@0: #if !defined (STLPORT) || !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT) sl@0: int uncaught_result = -1; sl@0: { sl@0: UncaughtClassTest test_inst(uncaught_result); sl@0: CPPUNIT_ASSERT( uncaught_result == -1 ); sl@0: } sl@0: CPPUNIT_ASSERT( uncaught_result == 0 ); sl@0: sl@0: { sl@0: try { sl@0: uncaught_result = -1; sl@0: UncaughtClassTest test_inst(uncaught_result); sl@0: throw "exception"; sl@0: } sl@0: catch (...) { sl@0: } sl@0: } sl@0: CPPUNIT_ASSERT( uncaught_result == 1 ); sl@0: #endif sl@0: } sl@0: sl@0: void runtime_thrower(std::string& str) sl@0: { sl@0: throw std::runtime_error(str); sl@0: } sl@0: sl@0: void ExceptionTest::exception_emission() sl@0: { sl@0: #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) sl@0: std::string foo = "foo"; sl@0: try { sl@0: //throw std::runtime_error(foo); sl@0: runtime_thrower(foo); sl@0: } sl@0: catch (std::runtime_error const& e) { sl@0: CPPUNIT_ASSERT( foo == e.what() ); sl@0: } sl@0: catch (...) { sl@0: CPPUNIT_ASSERT( false ); sl@0: } sl@0: sl@0: try { sl@0: std::string msg(512, 'a'); sl@0: //throw std::runtime_error(msg); sl@0: runtime_thrower(msg); sl@0: } sl@0: catch (std::runtime_error const& e) { sl@0: const char* c = e.what(); sl@0: while (*c != 0) { sl@0: CPPUNIT_ASSERT( *c++ == 'a' ); sl@0: } sl@0: } sl@0: catch (...) { sl@0: CPPUNIT_ASSERT( false ); sl@0: } sl@0: #endif sl@0: }