1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/cppexceptions/t_romtable2.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,232 @@
1.4 +// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\cppexceptions\t_romtable2.cpp
1.18 +// Overview:
1.19 +// Check exceptions in RAM loaded image.
1.20 +// API Information:
1.21 +// N/A
1.22 +// Details:
1.23 +// - Throw and catch a variety of exceptions and verify
1.24 +// results are as expected.
1.25 +// Platforms/Drives/Compatibility:
1.26 +// Hardware (Automatic).
1.27 +// Assumptions/Requirement/Pre-requisites:
1.28 +// Failures and causes:
1.29 +// Base Port information:
1.30 +//
1.31 +//
1.32 +
1.33 +
1.34 +#include <e32test.h>
1.35 +
1.36 +GLDEF_D RTest test(_L("T_ROMTABLE"));
1.37 +
1.38 +int catcher(int x);
1.39 +int catcher2(int x);
1.40 +int catcher3(int x);
1.41 +int catcher4(int x);
1.42 +int catcher5(int x);
1.43 +void TestUncaught(void);
1.44 +
1.45 +GLDEF_C TInt E32Main()
1.46 + {
1.47 + test.Start(_L("Check exceptions in RAM loaded image."));
1.48 +
1.49 + test.Printf(_L("Throwing first exception.\n"));
1.50 + int r = catcher(2);
1.51 + test.Printf(_L("Returned %d expected 2\n"), r);
1.52 + test(r==2);
1.53 +
1.54 + test.Printf(_L("Not throwing first exception.\n"));
1.55 + r = catcher(0);
1.56 + test.Printf(_L("Returned %d expected -1\n"), r);
1.57 + test(r==-1);
1.58 +
1.59 + test.Printf(_L("Throwing second exception.\n"));
1.60 + r = catcher2(3);
1.61 + test.Printf(_L("Returned %d expected 3\n"), r);
1.62 + test(r==3);
1.63 +
1.64 + test.Printf(_L("Not throwing second exception.\n"));
1.65 + r = catcher2(0);
1.66 + test.Printf(_L("Returned %d expected -1\n"), r);
1.67 + test(r==-1);
1.68 +
1.69 + test.Printf(_L("Throwing third exception.\n"));
1.70 + r = catcher3(4);
1.71 + test.Printf(_L("Returned %d expected 4\n"), r);
1.72 + test(r==4);
1.73 +
1.74 + test.Printf(_L("Not throwing third exception.\n"));
1.75 + r = catcher3(0);
1.76 + test.Printf(_L("Returned %d expected -1\n"), r);
1.77 + test(r==-1);
1.78 +
1.79 + test.Printf(_L("Throwing fourth exception.\n"));
1.80 + r = catcher4(5);
1.81 + test.Printf(_L("Returned %d expected 5\n"), r);
1.82 + test(r==5);
1.83 +
1.84 + test.Printf(_L("Not throwing fourth exception.\n"));
1.85 + r = catcher4(0);
1.86 + test.Printf(_L("Returned %d expected -1\n"), r);
1.87 + test(r==-1);
1.88 +
1.89 + test.Printf(_L("Throwing fifth exception.\n"));
1.90 + r = catcher5(6);
1.91 + test.Printf(_L("Returned %d expected 6\n"), r);
1.92 + test(r==6);
1.93 +
1.94 + test.Printf(_L("Not throwing fifth exception.\n"));
1.95 + r = catcher5(0);
1.96 + test.Printf(_L("Returned %d expected -1\n"), r);
1.97 + test(r==-1);
1.98 +
1.99 + test.Printf(_L("Testing std::uncaught_exception.\n"));
1.100 + TestUncaught();
1.101 +
1.102 + test.End();
1.103 + test.Close();
1.104 +
1.105 + return 0;
1.106 + }
1.107 +
1.108 +
1.109 +class MyFirstException {
1.110 +public:
1.111 + MyFirstException(int x) { iVal = x; };
1.112 + virtual ~MyFirstException();
1.113 + int iVal;
1.114 +};
1.115 +
1.116 +MyFirstException::~MyFirstException(){}
1.117 +
1.118 +int thrower (int x) {
1.119 +#ifdef __SUPPORT_CPP_EXCEPTIONS__
1.120 + if (x != 0) throw MyFirstException(x);
1.121 +#else //!__SUPPORT_CPP_EXCEPTIONS__
1.122 + if (x != 0)
1.123 + return x;
1.124 + else
1.125 +#endif //__SUPPORT_CPP_EXCEPTIONS__
1.126 + return -1;
1.127 +}
1.128 +
1.129 +int catcher(int x) {
1.130 +#ifdef __SUPPORT_CPP_EXCEPTIONS__
1.131 + try {
1.132 +#endif //__SUPPORT_CPP_EXCEPTIONS__
1.133 + return thrower(x);
1.134 +#ifdef __SUPPORT_CPP_EXCEPTIONS__
1.135 + }
1.136 + catch(MyFirstException& e)
1.137 + {
1.138 + return e.iVal;
1.139 + }
1.140 +#endif //__SUPPORT_CPP_EXCEPTIONS__
1.141 +}
1.142 +
1.143 +
1.144 +#include "second_excp.h"
1.145 +
1.146 +
1.147 +int catcher2(int x) {
1.148 +#ifdef __SUPPORT_CPP_EXCEPTIONS__
1.149 + try {
1.150 +#endif //__SUPPORT_CPP_EXCEPTIONS__
1.151 + return thrower2(x);
1.152 +#ifdef __SUPPORT_CPP_EXCEPTIONS__
1.153 + }
1.154 + catch(MySecondException& e)
1.155 + {
1.156 + return e.iVal;
1.157 + }
1.158 +#endif //__SUPPORT_CPP_EXCEPTIONS__
1.159 +}
1.160 +
1.161 +int catcher3(int x) {
1.162 +#ifdef __SUPPORT_CPP_EXCEPTIONS__
1.163 + try {
1.164 +#endif //__SUPPORT_CPP_EXCEPTIONS__
1.165 + return thrower3(x);
1.166 +#ifdef __SUPPORT_CPP_EXCEPTIONS__
1.167 + }
1.168 + catch(MyThirdException& e)
1.169 + {
1.170 + return e.iVal;
1.171 + }
1.172 +#endif //__SUPPORT_CPP_EXCEPTIONS__
1.173 +}
1.174 +
1.175 +int catcher4(int x) {
1.176 +#ifdef __SUPPORT_CPP_EXCEPTIONS__
1.177 + try {
1.178 +#endif //__SUPPORT_CPP_EXCEPTIONS__
1.179 + return thrower4(x);
1.180 +#ifdef __SUPPORT_CPP_EXCEPTIONS__
1.181 + }
1.182 + catch(MyFourthException& e)
1.183 + {
1.184 + return e.iVal;
1.185 + }
1.186 +#endif //__SUPPORT_CPP_EXCEPTIONS__
1.187 +}
1.188 +
1.189 +int catcher5(int x) {
1.190 +#ifdef __SUPPORT_CPP_EXCEPTIONS__
1.191 + try {
1.192 +#endif //__SUPPORT_CPP_EXCEPTIONS__
1.193 + return thrower5(x);
1.194 +#ifdef __SUPPORT_CPP_EXCEPTIONS__
1.195 + }
1.196 + catch(MyFifthException& e)
1.197 + {
1.198 + return e.iVal;
1.199 + }
1.200 +#endif //__SUPPORT_CPP_EXCEPTIONS__
1.201 +}
1.202 +
1.203 +void TestUncaught(void) {
1.204 + TInt x = 0;
1.205 +#ifdef __SUPPORT_CPP_EXCEPTIONS__
1.206 + try {
1.207 + UncaughtTester aTester(x);
1.208 + test.Printf(_L("Check throw case\n"));
1.209 + thrower(5);
1.210 + test(x == 5);
1.211 + }
1.212 + catch(MyFirstException& /*e*/)
1.213 + {
1.214 +#if defined(__MSVCDOTNET__) || (defined(__CW32__) && (__MWERKS__ > 0x3200)) || defined(__EABI__)
1.215 + test.Printf(_L("~Check x == 2\n"));
1.216 + test(x == 2);
1.217 +#else
1.218 + test.Printf(_L("Checking x == 1, as std::uncaught_exception() broken\n"));
1.219 + test(x == 1);
1.220 +#endif
1.221 + }
1.222 + try
1.223 +#endif //__SUPPORT_CPP_EXCEPTIONS__
1.224 + {
1.225 + UncaughtTester aTester(x);
1.226 + test.Printf(_L("Check no throw case\n"));
1.227 + }
1.228 +#ifdef __SUPPORT_CPP_EXCEPTIONS__
1.229 + catch(MyFirstException& /*e*/)
1.230 + {
1.231 + test.Printf(_L("Whoops!!!\n"));
1.232 + }
1.233 +#endif //__SUPPORT_CPP_EXCEPTIONS__
1.234 + test(x==1);
1.235 +}