sl@0: // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: // Name        : exc_interwork.cpp
sl@0: // Part of     : standard c++ tests.
sl@0: // 
sl@0: //
sl@0: 
sl@0: #include <stdcpp_support.h>
sl@0: #include <stdexcept>
sl@0: #include <new>
sl@0: #include "test_decls.h"
sl@0: 
sl@0: // Test that the Symbian error codes are converted into propoer 
sl@0: // Cpp exceptions.
sl@0: void foo()
sl@0: {
sl@0: 	int aPass = 0;
sl@0: 	int TotalCases = 0;
sl@0: 
sl@0: 	try{
sl@0: 		TotalCases++;
sl@0: 		TranslateSymErrorToCppException(KErrNoMemory);
sl@0: 	}
sl@0: 	catch(std::bad_alloc a)	{
sl@0: 		aPass++;
sl@0: 	}
sl@0: 	catch(...){
sl@0: 		CPP_TESTS_ASSERT_ALLWAYS(0);
sl@0: 	}
sl@0: 	try{
sl@0: 		TotalCases++;
sl@0: 		TranslateSymErrorToCppException(KErrArgument);
sl@0: 	}
sl@0: 	catch(std::invalid_argument ia)	{
sl@0: 		aPass++;
sl@0: 	}
sl@0: 	catch(...){
sl@0: 		CPP_TESTS_ASSERT_ALLWAYS(0);
sl@0: 	}
sl@0: 	
sl@0: 	try	{
sl@0: 		TotalCases++;
sl@0: 		TranslateSymErrorToCppException(KErrOverflow);
sl@0: 	}
sl@0: 	catch(std::overflow_error oe)	{
sl@0: 		aPass++;
sl@0: 	}
sl@0: 	catch(...){
sl@0: 		CPP_TESTS_ASSERT_ALLWAYS(0);
sl@0: 	}
sl@0: 
sl@0: 	try	{
sl@0: 		TotalCases++;
sl@0: 		TranslateSymErrorToCppException(KErrUnderflow);
sl@0: 	}
sl@0: 	catch(std::underflow_error ue)	{
sl@0: 		aPass++;
sl@0: 	}
sl@0: 	catch(...){
sl@0: 		CPP_TESTS_ASSERT_ALLWAYS(0);
sl@0: 	}
sl@0: 	
sl@0: 	try{
sl@0: 		TotalCases++;
sl@0: 		TranslateSymErrorToCppException(1);
sl@0: 	}
sl@0: 	catch(Symbian_error e)	{
sl@0: 		aPass++;
sl@0: 	}
sl@0: 	catch(...){
sl@0: 		CPP_TESTS_ASSERT_ALLWAYS(0);
sl@0: 	}
sl@0: 
sl@0: 	CPP_TESTS_ASSERT_ALLWAYS(TotalCases == aPass);
sl@0: }
sl@0: 
sl@0: // Test that the exception is converted to proper Symbian error code
sl@0: void bar()
sl@0: {
sl@0: 	std::bad_alloc a;
sl@0: 	int x = TranslateCppExceptionToSymError(a);
sl@0: 	CPP_TESTS_ASSERT_ALLWAYS(x == KErrNoMemory);
sl@0: }
sl@0: 
sl@0: int E32Main()
sl@0: {
sl@0: 	int *ptr = new int(1);
sl@0: 	foo();
sl@0: 	bar();
sl@0: 	return 0;
sl@0: }