os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/exception_test.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include <exception>
sl@0
    17
#include <stdexcept>
sl@0
    18
#include <string>
sl@0
    19
sl@0
    20
#include "cppunit/cppunit_proxy.h"
sl@0
    21
sl@0
    22
/*
sl@0
    23
 * This test case purpose is to check that the exception handling
sl@0
    24
 * functions are correctly imported to the STLport namespace only
sl@0
    25
 * if they have a right behavior.
sl@0
    26
 * Otherwise they are not imported to report the problem as a compile
sl@0
    27
 * time error.
sl@0
    28
 */
sl@0
    29
sl@0
    30
//
sl@0
    31
// TestCase class
sl@0
    32
//
sl@0
    33
class ExceptionTest : public CPPUNIT_NS::TestCase
sl@0
    34
{
sl@0
    35
  CPPUNIT_TEST_SUITE(ExceptionTest);
sl@0
    36
#if defined (STLPORT) && !defined (_STLP_USE_EXCEPTIONS)
sl@0
    37
  CPPUNIT_IGNORE;
sl@0
    38
#endif
sl@0
    39
#if defined (STLPORT) && defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT)
sl@0
    40
  CPPUNIT_IGNORE;
sl@0
    41
#endif
sl@0
    42
  CPPUNIT_TEST(unexpected_except);
sl@0
    43
#if defined (STLPORT) && defined (_STLP_USE_EXCEPTIONS)
sl@0
    44
  CPPUNIT_STOP_IGNORE;
sl@0
    45
#endif
sl@0
    46
#if defined (STLPORT) && defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT)
sl@0
    47
  CPPUNIT_IGNORE;
sl@0
    48
#endif
sl@0
    49
  CPPUNIT_TEST(uncaught_except);
sl@0
    50
#if defined (STLPORT) && defined (_STLP_USE_EXCEPTIONS)
sl@0
    51
  CPPUNIT_STOP_IGNORE;
sl@0
    52
#endif
sl@0
    53
  CPPUNIT_TEST(exception_emission);
sl@0
    54
  CPPUNIT_TEST_SUITE_END();
sl@0
    55
sl@0
    56
protected:
sl@0
    57
  void unexpected_except();
sl@0
    58
  void uncaught_except();
sl@0
    59
  void exception_emission();
sl@0
    60
};
sl@0
    61
sl@0
    62
CPPUNIT_TEST_SUITE_REGISTRATION(ExceptionTest);
sl@0
    63
sl@0
    64
#if !defined (STLPORT) || !defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT)
sl@0
    65
bool g_unexpected_called = false;
sl@0
    66
void unexpected_hdl() {
sl@0
    67
  g_unexpected_called = true;
sl@0
    68
  throw std::bad_exception();
sl@0
    69
}
sl@0
    70
sl@0
    71
struct special_except {};
sl@0
    72
void throw_func() {
sl@0
    73
  throw special_except();
sl@0
    74
}
sl@0
    75
sl@0
    76
void throw_except_func() throw(std::exception) {
sl@0
    77
  throw_func();
sl@0
    78
}
sl@0
    79
#endif
sl@0
    80
sl@0
    81
void ExceptionTest::unexpected_except()
sl@0
    82
{
sl@0
    83
#if !defined (STLPORT) || !defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT)
sl@0
    84
  std::unexpected_handler hdl = &unexpected_hdl;
sl@0
    85
  std::set_unexpected(hdl);
sl@0
    86
sl@0
    87
  try {
sl@0
    88
    throw_except_func();
sl@0
    89
  }
sl@0
    90
  catch (std::bad_exception const&) {
sl@0
    91
    CPPUNIT_ASSERT( true );
sl@0
    92
  }
sl@0
    93
  catch (special_except) {
sl@0
    94
    CPPUNIT_ASSERT( false );
sl@0
    95
  }
sl@0
    96
  CPPUNIT_ASSERT( g_unexpected_called );
sl@0
    97
#endif
sl@0
    98
}
sl@0
    99
sl@0
   100
#if !defined (STLPORT) || !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT)
sl@0
   101
struct UncaughtClassTest
sl@0
   102
{
sl@0
   103
  UncaughtClassTest(int &res) : _res(res)
sl@0
   104
  {}
sl@0
   105
sl@0
   106
  ~UncaughtClassTest() {
sl@0
   107
    _res = std::uncaught_exception()?1:0;
sl@0
   108
  }
sl@0
   109
sl@0
   110
  int &_res;
sl@0
   111
};
sl@0
   112
#endif
sl@0
   113
sl@0
   114
void ExceptionTest::uncaught_except()
sl@0
   115
{
sl@0
   116
#if !defined (STLPORT) || !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT)
sl@0
   117
  int uncaught_result = -1;
sl@0
   118
  {
sl@0
   119
    UncaughtClassTest test_inst(uncaught_result);
sl@0
   120
    CPPUNIT_ASSERT( uncaught_result == -1 );
sl@0
   121
  }
sl@0
   122
  CPPUNIT_ASSERT( uncaught_result == 0 );
sl@0
   123
sl@0
   124
  {
sl@0
   125
    try {
sl@0
   126
      uncaught_result = -1;
sl@0
   127
      UncaughtClassTest test_inst(uncaught_result);
sl@0
   128
      throw "exception";
sl@0
   129
    }
sl@0
   130
    catch (...) {
sl@0
   131
    }
sl@0
   132
  }
sl@0
   133
  CPPUNIT_ASSERT( uncaught_result == 1 );
sl@0
   134
#endif
sl@0
   135
}
sl@0
   136
sl@0
   137
void runtime_thrower(std::string& str)
sl@0
   138
{
sl@0
   139
	throw std::runtime_error(str);
sl@0
   140
}
sl@0
   141
sl@0
   142
void ExceptionTest::exception_emission()
sl@0
   143
{
sl@0
   144
#if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
sl@0
   145
  std::string foo = "foo";
sl@0
   146
  try {
sl@0
   147
    //throw std::runtime_error(foo);
sl@0
   148
	  runtime_thrower(foo);
sl@0
   149
  }
sl@0
   150
  catch (std::runtime_error const& e) {
sl@0
   151
    CPPUNIT_ASSERT( foo == e.what() );
sl@0
   152
  }
sl@0
   153
  catch (...) {
sl@0
   154
    CPPUNIT_ASSERT( false );
sl@0
   155
  }
sl@0
   156
sl@0
   157
  try {
sl@0
   158
    std::string msg(512, 'a');
sl@0
   159
    //throw std::runtime_error(msg);
sl@0
   160
	runtime_thrower(msg);
sl@0
   161
  }
sl@0
   162
  catch (std::runtime_error const& e) {
sl@0
   163
    const char* c = e.what();
sl@0
   164
    while (*c != 0) {
sl@0
   165
      CPPUNIT_ASSERT( *c++ == 'a' );
sl@0
   166
    }
sl@0
   167
  }
sl@0
   168
  catch (...) {
sl@0
   169
    CPPUNIT_ASSERT( false );
sl@0
   170
  }
sl@0
   171
#endif
sl@0
   172
}