| 0 |      1 | // Copyright (c) 2004-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 the License "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 | // e32test\cppexceptions\t_romtable2.cpp
 | 
|  |     15 | // Overview:
 | 
|  |     16 | // Check exceptions in RAM loaded image.
 | 
|  |     17 | // API Information:
 | 
|  |     18 | // N/A
 | 
|  |     19 | // Details:	
 | 
|  |     20 | // - Throw and catch a variety of exceptions and verify 
 | 
|  |     21 | // results are as expected.
 | 
|  |     22 | // Platforms/Drives/Compatibility:
 | 
|  |     23 | // Hardware (Automatic).
 | 
|  |     24 | // Assumptions/Requirement/Pre-requisites:
 | 
|  |     25 | // Failures and causes:
 | 
|  |     26 | // Base Port information:
 | 
|  |     27 | // 
 | 
|  |     28 | //
 | 
|  |     29 | 
 | 
|  |     30 | 
 | 
|  |     31 | #include <e32test.h>
 | 
|  |     32 | 
 | 
|  |     33 | GLDEF_D RTest test(_L("T_ROMTABLE"));
 | 
|  |     34 | 
 | 
|  |     35 | int catcher(int x);
 | 
|  |     36 | int catcher2(int x);
 | 
|  |     37 | int catcher3(int x);
 | 
|  |     38 | int catcher4(int x);
 | 
|  |     39 | int catcher5(int x);
 | 
|  |     40 | void TestUncaught(void);
 | 
|  |     41 | 
 | 
|  |     42 | GLDEF_C TInt E32Main()
 | 
|  |     43 |     {
 | 
|  |     44 |     test.Start(_L("Check exceptions in RAM loaded image."));
 | 
|  |     45 | 
 | 
|  |     46 |     test.Printf(_L("Throwing first exception.\n"));
 | 
|  |     47 |     int r = catcher(2);
 | 
|  |     48 |     test.Printf(_L("Returned %d expected 2\n"), r);
 | 
|  |     49 |     test(r==2);
 | 
|  |     50 | 
 | 
|  |     51 |     test.Printf(_L("Not throwing first exception.\n"));
 | 
|  |     52 |     r = catcher(0);
 | 
|  |     53 |     test.Printf(_L("Returned %d expected -1\n"), r);
 | 
|  |     54 |     test(r==-1);
 | 
|  |     55 | 
 | 
|  |     56 |     test.Printf(_L("Throwing second exception.\n"));
 | 
|  |     57 |     r = catcher2(3);
 | 
|  |     58 |     test.Printf(_L("Returned %d expected 3\n"), r);
 | 
|  |     59 |     test(r==3);
 | 
|  |     60 | 
 | 
|  |     61 |     test.Printf(_L("Not throwing second exception.\n"));
 | 
|  |     62 |     r = catcher2(0);
 | 
|  |     63 |     test.Printf(_L("Returned %d expected -1\n"), r);
 | 
|  |     64 |     test(r==-1);
 | 
|  |     65 | 
 | 
|  |     66 |     test.Printf(_L("Throwing third exception.\n"));
 | 
|  |     67 |     r = catcher3(4);
 | 
|  |     68 |     test.Printf(_L("Returned %d expected 4\n"), r);
 | 
|  |     69 |     test(r==4);
 | 
|  |     70 | 
 | 
|  |     71 |     test.Printf(_L("Not throwing third exception.\n"));
 | 
|  |     72 |     r = catcher3(0);
 | 
|  |     73 |     test.Printf(_L("Returned %d expected -1\n"), r);
 | 
|  |     74 |     test(r==-1);
 | 
|  |     75 | 
 | 
|  |     76 |     test.Printf(_L("Throwing fourth exception.\n"));
 | 
|  |     77 |     r = catcher4(5);
 | 
|  |     78 |     test.Printf(_L("Returned %d expected 5\n"), r);
 | 
|  |     79 |     test(r==5);
 | 
|  |     80 | 
 | 
|  |     81 |     test.Printf(_L("Not throwing fourth exception.\n"));
 | 
|  |     82 |     r = catcher4(0);
 | 
|  |     83 |     test.Printf(_L("Returned %d expected -1\n"), r);
 | 
|  |     84 |     test(r==-1);
 | 
|  |     85 | 
 | 
|  |     86 |     test.Printf(_L("Throwing fifth exception.\n"));
 | 
|  |     87 |     r = catcher5(6);
 | 
|  |     88 |     test.Printf(_L("Returned %d expected 6\n"), r);
 | 
|  |     89 |     test(r==6);
 | 
|  |     90 | 
 | 
|  |     91 |     test.Printf(_L("Not throwing fifth exception.\n"));
 | 
|  |     92 |     r = catcher5(0);
 | 
|  |     93 |     test.Printf(_L("Returned %d expected -1\n"), r);
 | 
|  |     94 |     test(r==-1);
 | 
|  |     95 | 
 | 
|  |     96 |     test.Printf(_L("Testing std::uncaught_exception.\n"));
 | 
|  |     97 |     TestUncaught();
 | 
|  |     98 | 
 | 
|  |     99 | 	test.End();
 | 
|  |    100 | 	test.Close();
 | 
|  |    101 | 
 | 
|  |    102 |     return 0;
 | 
|  |    103 |     }
 | 
|  |    104 | 
 | 
|  |    105 | 
 | 
|  |    106 | class MyFirstException {
 | 
|  |    107 | public:
 | 
|  |    108 |   MyFirstException(int x) { iVal = x; };
 | 
|  |    109 |   virtual ~MyFirstException();
 | 
|  |    110 |   int iVal;
 | 
|  |    111 | };
 | 
|  |    112 | 
 | 
|  |    113 | MyFirstException::~MyFirstException(){}
 | 
|  |    114 | 
 | 
|  |    115 | int thrower (int x) {
 | 
|  |    116 | #ifdef __SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    117 |   if (x != 0) throw MyFirstException(x);
 | 
|  |    118 | #else //!__SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    119 |   if (x != 0)
 | 
|  |    120 | 	  return x;
 | 
|  |    121 |   else
 | 
|  |    122 | #endif //__SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    123 |   return -1;
 | 
|  |    124 | }
 | 
|  |    125 | 
 | 
|  |    126 | int catcher(int x) {
 | 
|  |    127 | #ifdef __SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    128 |   try {
 | 
|  |    129 | #endif //__SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    130 |     return thrower(x);
 | 
|  |    131 | #ifdef __SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    132 |   }
 | 
|  |    133 |   catch(MyFirstException& e) 
 | 
|  |    134 |     {
 | 
|  |    135 |       return e.iVal;
 | 
|  |    136 |     }
 | 
|  |    137 | #endif //__SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    138 | }
 | 
|  |    139 | 
 | 
|  |    140 | 
 | 
|  |    141 | #include "second_excp.h"
 | 
|  |    142 | 
 | 
|  |    143 | 
 | 
|  |    144 | int catcher2(int x) {
 | 
|  |    145 | #ifdef __SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    146 |   try {
 | 
|  |    147 | #endif //__SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    148 |     return thrower2(x);
 | 
|  |    149 | #ifdef __SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    150 |   }
 | 
|  |    151 |   catch(MySecondException& e) 
 | 
|  |    152 |     {
 | 
|  |    153 |       return e.iVal;
 | 
|  |    154 |     }
 | 
|  |    155 | #endif //__SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    156 | }
 | 
|  |    157 | 
 | 
|  |    158 | int catcher3(int x) {
 | 
|  |    159 | #ifdef __SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    160 |   try {
 | 
|  |    161 | #endif //__SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    162 |     return thrower3(x);
 | 
|  |    163 | #ifdef __SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    164 |   }
 | 
|  |    165 |   catch(MyThirdException& e) 
 | 
|  |    166 |     {
 | 
|  |    167 |       return e.iVal;
 | 
|  |    168 |     }
 | 
|  |    169 | #endif //__SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    170 | }
 | 
|  |    171 | 
 | 
|  |    172 | int catcher4(int x) {
 | 
|  |    173 | #ifdef __SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    174 |   try {
 | 
|  |    175 | #endif //__SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    176 |     return thrower4(x);
 | 
|  |    177 | #ifdef __SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    178 |   }
 | 
|  |    179 |   catch(MyFourthException& e) 
 | 
|  |    180 |     {
 | 
|  |    181 |       return e.iVal;
 | 
|  |    182 |     }
 | 
|  |    183 | #endif //__SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    184 | }
 | 
|  |    185 | 
 | 
|  |    186 | int catcher5(int x) {
 | 
|  |    187 | #ifdef __SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    188 |   try {
 | 
|  |    189 | #endif //__SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    190 |     return thrower5(x);
 | 
|  |    191 | #ifdef __SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    192 |   }
 | 
|  |    193 |   catch(MyFifthException& e) 
 | 
|  |    194 |     {
 | 
|  |    195 |       return e.iVal;
 | 
|  |    196 |     }
 | 
|  |    197 | #endif //__SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    198 | }
 | 
|  |    199 | 
 | 
|  |    200 | void TestUncaught(void) {
 | 
|  |    201 |   TInt x = 0;
 | 
|  |    202 | #ifdef __SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    203 |   try {
 | 
|  |    204 |     UncaughtTester aTester(x);
 | 
|  |    205 |     test.Printf(_L("Check throw case\n"));
 | 
|  |    206 |     thrower(5);
 | 
|  |    207 | 	test(x == 5);
 | 
|  |    208 |   }
 | 
|  |    209 |   catch(MyFirstException& /*e*/) 
 | 
|  |    210 |     {
 | 
|  |    211 | #if defined(__MSVCDOTNET__) || (defined(__CW32__) && (__MWERKS__ > 0x3200)) || defined(__EABI__)
 | 
|  |    212 |       test.Printf(_L("~Check x == 2\n"));
 | 
|  |    213 | 	  test(x == 2);
 | 
|  |    214 | #else
 | 
|  |    215 | 	  test.Printf(_L("Checking x == 1, as std::uncaught_exception() broken\n"));
 | 
|  |    216 | 	  test(x == 1);
 | 
|  |    217 | #endif
 | 
|  |    218 |     }
 | 
|  |    219 |   try
 | 
|  |    220 | #endif //__SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    221 | 	{
 | 
|  |    222 |     UncaughtTester aTester(x);
 | 
|  |    223 |     test.Printf(_L("Check no throw case\n"));
 | 
|  |    224 |     }
 | 
|  |    225 | #ifdef __SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    226 |   catch(MyFirstException& /*e*/) 
 | 
|  |    227 |     {
 | 
|  |    228 |       test.Printf(_L("Whoops!!!\n"));
 | 
|  |    229 |     }
 | 
|  |    230 | #endif //__SUPPORT_CPP_EXCEPTIONS__
 | 
|  |    231 |   test(x==1);
 | 
|  |    232 | }
 |