ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

exception_test.cpp
Go to the documentation of this file.
00001 #include <exception>
00002 #include <stdexcept>
00003 #include <string>
00004 
00005 #include "cppunit/cppunit_proxy.h"
00006 
00007 #if defined (STLPORT) && defined (_STLP_USE_NAMESPACES)
00008 /*
00009  * This test case purpose is to check that the exception handling
00010  * functions are correctly imported to the STLport namespace only
00011  * if they have a right behavior.
00012  * Otherwise they are not imported to report the problem as a compile
00013  * time error.
00014  */
00015 
00016 //
00017 // TestCase class
00018 //
00019 class ExceptionTest : public CPPUNIT_NS::TestCase
00020 {
00021   CPPUNIT_TEST_SUITE(ExceptionTest);
00022 #if defined (STLPORT) && !defined (_STLP_USE_EXCEPTIONS)
00023   CPPUNIT_IGNORE;
00024 #endif
00025   CPPUNIT_TEST(what);
00026 #if defined (STLPORT) && defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT)
00027   CPPUNIT_IGNORE;
00028 #endif
00029   CPPUNIT_TEST(unexpected_except);
00030 #if defined (STLPORT) && defined (_STLP_USE_EXCEPTIONS)
00031   CPPUNIT_STOP_IGNORE;
00032 #endif
00033 #if defined (STLPORT) && defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT)
00034   CPPUNIT_IGNORE;
00035 #endif
00036   CPPUNIT_TEST(uncaught_except);
00037 #if defined (STLPORT) && defined (_STLP_USE_EXCEPTIONS)
00038   CPPUNIT_STOP_IGNORE;
00039 #endif
00040   CPPUNIT_TEST(exception_emission);
00041   CPPUNIT_TEST_SUITE_END();
00042 
00043 protected:
00044   void what();
00045   void unexpected_except();
00046   void uncaught_except();
00047   void exception_emission();
00048 };
00049 
00050 CPPUNIT_TEST_SUITE_REGISTRATION(ExceptionTest);
00051 
00052 #if !defined (STLPORT) || !defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT)
00053 bool g_unexpected_called = false;
00054 void unexpected_hdl() {
00055   g_unexpected_called = true;
00056   throw std::bad_exception();
00057 }
00058 
00059 struct special_except {};
00060 void throw_func() {
00061   throw special_except();
00062 }
00063 
00064 void throw_except_func() throw(std::exception) {
00065   throw_func();
00066 }
00067 #endif
00068 
00069 void ExceptionTest::what()
00070 {
00071   try {
00072     throw std::runtime_error( std::string( "message" ) );
00073   }
00074   catch ( std::runtime_error& err ) {
00075     CPPUNIT_CHECK( strcmp( err.what(), "message" ) == 0 );
00076   }
00077 }
00078 
00079 void ExceptionTest::unexpected_except()
00080 {
00081 #if !defined (STLPORT) || !defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT)
00082   std::unexpected_handler hdl = &unexpected_hdl;
00083   std::set_unexpected(hdl);
00084 
00085   try {
00086     throw_except_func();
00087   }
00088   catch (std::bad_exception const&) {
00089     CPPUNIT_ASSERT( true );
00090   }
00091   catch (special_except) {
00092     CPPUNIT_ASSERT( false );
00093   }
00094   CPPUNIT_ASSERT( g_unexpected_called );
00095 #endif
00096 }
00097 
00098 #if !defined (STLPORT) || !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT)
00099 struct UncaughtClassTest
00100 {
00101   UncaughtClassTest(int &res) : _res(res)
00102   {}
00103 
00104   ~UncaughtClassTest() {
00105     _res = std::uncaught_exception()?1:0;
00106   }
00107 
00108   int &_res;
00109 };
00110 #endif
00111 
00112 void ExceptionTest::uncaught_except()
00113 {
00114 #if !defined (STLPORT) || !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT)
00115   int uncaught_result = -1;
00116   {
00117     UncaughtClassTest test_inst(uncaught_result);
00118     CPPUNIT_ASSERT( uncaught_result == -1 );
00119   }
00120   CPPUNIT_ASSERT( uncaught_result == 0 );
00121 
00122   {
00123     try {
00124       uncaught_result = -1;
00125       UncaughtClassTest test_inst(uncaught_result);
00126       throw "exception";
00127     }
00128     catch (...) {
00129     }
00130   }
00131   CPPUNIT_ASSERT( uncaught_result == 1 );
00132 #endif
00133 }
00134 
00135 void ExceptionTest::exception_emission()
00136 {
00137 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
00138   std::string foo = "foo";
00139   try {
00140     throw std::runtime_error(foo);
00141   }
00142   catch (std::runtime_error const& e) {
00143     CPPUNIT_ASSERT( foo == e.what() );
00144     std::runtime_error clone("");
00145     clone = e;
00146     CPPUNIT_ASSERT(foo == clone.what() );
00147   }
00148   catch (...) {
00149     CPPUNIT_ASSERT( false );
00150   }
00151 
00152   try {
00153     throw std::runtime_error(foo);
00154   }
00155   catch (std::runtime_error e) {
00156     CPPUNIT_ASSERT( foo == e.what() );
00157     std::runtime_error clone("");
00158     clone = e;
00159     CPPUNIT_ASSERT(foo == clone.what() );
00160   }
00161   catch (...) {
00162     CPPUNIT_ASSERT( false );
00163   }
00164 
00165   std::string msg(512, 'a');
00166   try {
00167     throw std::runtime_error(msg);
00168   }
00169   catch (std::runtime_error const& e) {
00170     CPPUNIT_ASSERT(msg == e.what() );
00171     std::runtime_error clone("");
00172     clone = e;
00173     CPPUNIT_ASSERT(msg == clone.what() );
00174   }
00175   catch (...) {
00176     CPPUNIT_ASSERT( false );
00177   }
00178 
00179   try {
00180     throw std::runtime_error(msg);
00181   }
00182   catch (std::runtime_error e) {
00183     CPPUNIT_ASSERT(msg == e.what() );
00184     std::runtime_error clone("");
00185     clone = e;
00186     CPPUNIT_ASSERT(msg == clone.what() );
00187   }
00188   catch (...) {
00189     CPPUNIT_ASSERT( false );
00190   }
00191 #endif
00192 }
00193 #endif

Generated on Sat May 26 2012 04:34:15 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.