|
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". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <exception> |
|
17 #include <stdexcept> |
|
18 #include <string> |
|
19 |
|
20 #include "cppunit/cppunit_proxy.h" |
|
21 |
|
22 /* |
|
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 |
|
27 * time error. |
|
28 */ |
|
29 |
|
30 // |
|
31 // TestCase class |
|
32 // |
|
33 class ExceptionTest : public CPPUNIT_NS::TestCase |
|
34 { |
|
35 CPPUNIT_TEST_SUITE(ExceptionTest); |
|
36 #if defined (STLPORT) && !defined (_STLP_USE_EXCEPTIONS) |
|
37 CPPUNIT_IGNORE; |
|
38 #endif |
|
39 #if defined (STLPORT) && defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT) |
|
40 CPPUNIT_IGNORE; |
|
41 #endif |
|
42 CPPUNIT_TEST(unexpected_except); |
|
43 #if defined (STLPORT) && defined (_STLP_USE_EXCEPTIONS) |
|
44 CPPUNIT_STOP_IGNORE; |
|
45 #endif |
|
46 #if defined (STLPORT) && defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT) |
|
47 CPPUNIT_IGNORE; |
|
48 #endif |
|
49 CPPUNIT_TEST(uncaught_except); |
|
50 #if defined (STLPORT) && defined (_STLP_USE_EXCEPTIONS) |
|
51 CPPUNIT_STOP_IGNORE; |
|
52 #endif |
|
53 CPPUNIT_TEST(exception_emission); |
|
54 CPPUNIT_TEST_SUITE_END(); |
|
55 |
|
56 protected: |
|
57 void unexpected_except(); |
|
58 void uncaught_except(); |
|
59 void exception_emission(); |
|
60 }; |
|
61 |
|
62 CPPUNIT_TEST_SUITE_REGISTRATION(ExceptionTest); |
|
63 |
|
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(); |
|
69 } |
|
70 |
|
71 struct special_except {}; |
|
72 void throw_func() { |
|
73 throw special_except(); |
|
74 } |
|
75 |
|
76 void throw_except_func() throw(std::exception) { |
|
77 throw_func(); |
|
78 } |
|
79 #endif |
|
80 |
|
81 void ExceptionTest::unexpected_except() |
|
82 { |
|
83 #if !defined (STLPORT) || !defined (_STLP_NO_UNEXPECTED_EXCEPT_SUPPORT) |
|
84 std::unexpected_handler hdl = &unexpected_hdl; |
|
85 std::set_unexpected(hdl); |
|
86 |
|
87 try { |
|
88 throw_except_func(); |
|
89 } |
|
90 catch (std::bad_exception const&) { |
|
91 CPPUNIT_ASSERT( true ); |
|
92 } |
|
93 catch (special_except) { |
|
94 CPPUNIT_ASSERT( false ); |
|
95 } |
|
96 CPPUNIT_ASSERT( g_unexpected_called ); |
|
97 #endif |
|
98 } |
|
99 |
|
100 #if !defined (STLPORT) || !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT) |
|
101 struct UncaughtClassTest |
|
102 { |
|
103 UncaughtClassTest(int &res) : _res(res) |
|
104 {} |
|
105 |
|
106 ~UncaughtClassTest() { |
|
107 _res = std::uncaught_exception()?1:0; |
|
108 } |
|
109 |
|
110 int &_res; |
|
111 }; |
|
112 #endif |
|
113 |
|
114 void ExceptionTest::uncaught_except() |
|
115 { |
|
116 #if !defined (STLPORT) || !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT) |
|
117 int uncaught_result = -1; |
|
118 { |
|
119 UncaughtClassTest test_inst(uncaught_result); |
|
120 CPPUNIT_ASSERT( uncaught_result == -1 ); |
|
121 } |
|
122 CPPUNIT_ASSERT( uncaught_result == 0 ); |
|
123 |
|
124 { |
|
125 try { |
|
126 uncaught_result = -1; |
|
127 UncaughtClassTest test_inst(uncaught_result); |
|
128 throw "exception"; |
|
129 } |
|
130 catch (...) { |
|
131 } |
|
132 } |
|
133 CPPUNIT_ASSERT( uncaught_result == 1 ); |
|
134 #endif |
|
135 } |
|
136 |
|
137 void runtime_thrower(std::string& str) |
|
138 { |
|
139 throw std::runtime_error(str); |
|
140 } |
|
141 |
|
142 void ExceptionTest::exception_emission() |
|
143 { |
|
144 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) |
|
145 std::string foo = "foo"; |
|
146 try { |
|
147 //throw std::runtime_error(foo); |
|
148 runtime_thrower(foo); |
|
149 } |
|
150 catch (std::runtime_error const& e) { |
|
151 CPPUNIT_ASSERT( foo == e.what() ); |
|
152 } |
|
153 catch (...) { |
|
154 CPPUNIT_ASSERT( false ); |
|
155 } |
|
156 |
|
157 try { |
|
158 std::string msg(512, 'a'); |
|
159 //throw std::runtime_error(msg); |
|
160 runtime_thrower(msg); |
|
161 } |
|
162 catch (std::runtime_error const& e) { |
|
163 const char* c = e.what(); |
|
164 while (*c != 0) { |
|
165 CPPUNIT_ASSERT( *c++ == 'a' ); |
|
166 } |
|
167 } |
|
168 catch (...) { |
|
169 CPPUNIT_ASSERT( false ); |
|
170 } |
|
171 #endif |
|
172 } |