os/ossrv/genericopenlibs/cppstdlib/test/runtime/src/exc_interwork.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.
     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 // Name        : exc_interwork.cpp
    15 // Part of     : standard c++ tests.
    16 // 
    17 //
    18 
    19 #include <stdcpp_support.h>
    20 #include <stdexcept>
    21 #include <new>
    22 #include "test_decls.h"
    23 
    24 // Test that the Symbian error codes are converted into propoer 
    25 // Cpp exceptions.
    26 void foo()
    27 {
    28 	int aPass = 0;
    29 	int TotalCases = 0;
    30 
    31 	try{
    32 		TotalCases++;
    33 		TranslateSymErrorToCppException(KErrNoMemory);
    34 	}
    35 	catch(std::bad_alloc a)	{
    36 		aPass++;
    37 	}
    38 	catch(...){
    39 		CPP_TESTS_ASSERT_ALLWAYS(0);
    40 	}
    41 	try{
    42 		TotalCases++;
    43 		TranslateSymErrorToCppException(KErrArgument);
    44 	}
    45 	catch(std::invalid_argument ia)	{
    46 		aPass++;
    47 	}
    48 	catch(...){
    49 		CPP_TESTS_ASSERT_ALLWAYS(0);
    50 	}
    51 	
    52 	try	{
    53 		TotalCases++;
    54 		TranslateSymErrorToCppException(KErrOverflow);
    55 	}
    56 	catch(std::overflow_error oe)	{
    57 		aPass++;
    58 	}
    59 	catch(...){
    60 		CPP_TESTS_ASSERT_ALLWAYS(0);
    61 	}
    62 
    63 	try	{
    64 		TotalCases++;
    65 		TranslateSymErrorToCppException(KErrUnderflow);
    66 	}
    67 	catch(std::underflow_error ue)	{
    68 		aPass++;
    69 	}
    70 	catch(...){
    71 		CPP_TESTS_ASSERT_ALLWAYS(0);
    72 	}
    73 	
    74 	try{
    75 		TotalCases++;
    76 		TranslateSymErrorToCppException(1);
    77 	}
    78 	catch(Symbian_error e)	{
    79 		aPass++;
    80 	}
    81 	catch(...){
    82 		CPP_TESTS_ASSERT_ALLWAYS(0);
    83 	}
    84 
    85 	CPP_TESTS_ASSERT_ALLWAYS(TotalCases == aPass);
    86 }
    87 
    88 // Test that the exception is converted to proper Symbian error code
    89 void bar()
    90 {
    91 	std::bad_alloc a;
    92 	int x = TranslateCppExceptionToSymError(a);
    93 	CPP_TESTS_ASSERT_ALLWAYS(x == KErrNoMemory);
    94 }
    95 
    96 int E32Main()
    97 {
    98 	int *ptr = new int(1);
    99 	foo();
   100 	bar();
   101 	return 0;
   102 }