stif/stif_plat/inc/TestclassAssert.h
changeset 36 813b186005b6
parent 30 86a2e675b80a
child 41 838cdffd57ce
equal deleted inserted replaced
30:86a2e675b80a 36:813b186005b6
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description: This file contains the header file of the testclass
       
    15 * assert macros.
       
    16 *
       
    17 */
       
    18 
       
    19 #ifndef TestclassAssert_MACROS_H
       
    20 #define TestclassAssert_MACROS_H
       
    21 
       
    22 _LIT( KAssertFailedEquals, "AssertEquals Failed [F:%s][L:%d]" );
       
    23 _LIT( KAssertFailedNotEquals, "AssertNotEquals Failed [F:%s][L:%d]" );
       
    24 _LIT( KAssertFailedNull, "AssertNull Failed [F:%s][L:%d]" );
       
    25 _LIT( KAssertFailedNotNull, "AssertNotNull Failed [F:%s][L:%d]" );
       
    26 _LIT( KAssertFailedSame, "AssertSame Failed [F:%s][L:%d]" );
       
    27 _LIT( KAssertFailedNotSame, "AssertNotSame Failed [F:%s][L:%d]" );
       
    28 _LIT( KAssertFailedTrue, "AssertTrue Failed [F:%s][L:%d]" );
       
    29 _LIT( KAssertFailedFalse, "AssertFalse Failed [F:%s][L:%d]" );
       
    30 _LIT( KAssertFailedNotLeaves, "AssertNotLeaves Failed [F:%s][L:%d]" );
       
    31 _LIT( KAssertFailedLeaves, "AssertLeaves Failed [F:%s][L:%d]" );
       
    32 _LIT( KAssertFailedLeavesWith, "AssertLeavesWith Failed [F:%s][L:%d]" );
       
    33 
       
    34 
       
    35 #ifdef _UNICODE
       
    36 	#define __STIF_WIDEN2(x) L ## x
       
    37 	#define __STIF_WIDEN(x) __STIF_WIDEN2(x)
       
    38 	#define __STIF_DBG_FILE__ __STIF_WIDEN(__FILE__)
       
    39 #else
       
    40 	#define __STIF_DBG_FILE__ __FILE__
       
    41 #endif
       
    42 
       
    43 
       
    44 // Logs to the STIF log file AND to the RDebug
       
    45 #define STIF_LOG( aMessage ) \
       
    46     iLog->Log( _L( aMessage ) ); RDebug::Print( _L( aMessage ) );
       
    47 
       
    48 		
       
    49 /*********************************************************************************
       
    50  * Assert Macros
       
    51  *********************************************************************************/
       
    52 #define __STIF_ASSERT_SHARED( aFunction, aMessage ) \
       
    53 	if(!aFunction) \
       
    54 		{ \
       
    55 		iLog->Log( aMessage, __STIF_DBG_FILE__, __LINE__ );\
       
    56 		return KErrGeneral;\
       
    57 		}
       
    58 
       
    59 #define __STIF_ASSERT_SHARED_RET( aFunction, aMessage, aFailedReturn ) \
       
    60 	if(!aFunction) \
       
    61 		{ \
       
    62 		iLog->Log( aMessage, __STIF_DBG_FILE__, __LINE__ );\
       
    63 		return aFailedReturn;\
       
    64 		}
       
    65 		
       
    66 
       
    67 #define STIF_ASSERT_EQUALS( aExpected, aActual ) \
       
    68 	__STIF_ASSERT_SHARED( AssertEquals( aExpected, aActual ) , KAssertFailedEquals );
       
    69 
       
    70 #define STIF_ASSERT_EQUALS_RET( aExpected, aActual, aFailedRet ) \
       
    71 	__STIF_ASSERT_SHARED_RET( AssertEquals( aExpected, aActual ) , KAssertFailedEquals, aFailedRet );
       
    72 
       
    73 #define STIF_ASSERT_NOT_EQUALS( aExpected, aActual ) \
       
    74 	__STIF_ASSERT_SHARED( !AssertEquals( aExpected, aActual ) , KAssertFailedNotEquals );
       
    75 
       
    76 #define STIF_ASSERT_NOT_EQUALS_RET( aExpected, aActual, aFailedRet ) \
       
    77 	__STIF_ASSERT_SHARED_RET( !AssertEquals( aExpected, aActual ) , KAssertFailedNotEquals, aFailedRet );
       
    78 
       
    79 #define STIF_ASSERT_NULL( aPtr ) \
       
    80 	__STIF_ASSERT_SHARED( AssertNull( aPtr ), KAssertFailedNull );
       
    81 
       
    82 #define STIF_ASSERT_NULL_RET( aPtr, aFailedRet ) \
       
    83 	__STIF_ASSERT_SHARED_RET( AssertNull( aPtr ), KAssertFailedNull, aFailedRet );
       
    84 
       
    85 #define STIF_ASSERT_NOT_NULL( aPtr ) \
       
    86 	__STIF_ASSERT_SHARED( !AssertNull( aPtr ), KAssertFailedNotNull );
       
    87 
       
    88 #define STIF_ASSERT_NOT_NULL_RET( aPtr, aFailedRet ) \
       
    89 	__STIF_ASSERT_SHARED_RET( !AssertNull( aPtr ), KAssertFailedNotNull, aFailedRet );
       
    90 
       
    91 #define STIF_ASSERT_SAME( aExpectedPtr, aActualPtr ) \
       
    92 	__STIF_ASSERT_SHARED( AssertSame( aExpectedPtr, aActualPtr ), KAssertFailedSame );
       
    93 
       
    94 #define STIF_ASSERT_SAME_RET( aExpectedPtr, aActualPtr, aFailedRet ) \
       
    95 	__STIF_ASSERT_SHARED_RET( AssertSame( aExpectedPtr, aActualPtr ), KAssertFailedSame, aFailedRet );
       
    96 
       
    97 #define STIF_ASSERT_NOT_SAME( aExpectedPtr, aActualPtr ) \
       
    98 	__STIF_ASSERT_SHARED( !AssertSame( aExpectedPtr, aActualPtr ), KAssertFailedNotSame );
       
    99 
       
   100 #define STIF_ASSERT_NOT_SAME_RET( aExpectedPtr, aActualPtr, aFailedRet ) \
       
   101 	__STIF_ASSERT_SHARED_RET( !AssertSame( aExpectedPtr, aActualPtr ), KAssertFailedNotSame, aFailedRet );
       
   102 
       
   103 #define STIF_ASSERT_TRUE( aCondition ) \
       
   104 	__STIF_ASSERT_SHARED( AssertTrue( aCondition ), KAssertFailedTrue );
       
   105 
       
   106 #define STIF_ASSERT_TRUE_RET( aCondition, aFailedRet ) \
       
   107 	__STIF_ASSERT_SHARED_RET( AssertTrue( aCondition ), KAssertFailedTrue, aFailedRet );
       
   108 
       
   109 #define STIF_ASSERT_FALSE( aCondition ) \
       
   110 	__STIF_ASSERT_SHARED( !AssertTrue( aCondition ), KAssertFailedFalse );
       
   111 
       
   112 #define STIF_ASSERT_FALSE_RET( aCondition, aFailedRet ) \
       
   113 	__STIF_ASSERT_SHARED_RET( !AssertTrue( aCondition ), KAssertFailedFalse, aFailedRet );
       
   114 
       
   115 // Eclosing block is used to create the scope for the __leaveValue	
       
   116 #define STIF_ASSERT_NOT_LEAVES( aStatement ) \
       
   117     { \
       
   118     TRAPD( __leaveValue, aStatement ); \
       
   119     __STIF_ASSERT_SHARED( AssertEquals( __leaveValue, KErrNone ), KAssertFailedNotLeaves ); \
       
   120     }
       
   121 
       
   122 #define STIF_ASSERT_NOT_LEAVES_RET( aStatement, aFailedRet ) \
       
   123     { \
       
   124     TRAPD( __leaveValue, aStatement ); \
       
   125     __STIF_ASSERT_SHARED_RET( AssertEquals( __leaveValue, KErrNone ), KAssertFailedNotLeaves, aFailedRet ); \
       
   126     }
       
   127 
       
   128 // Eclosing block is used to create the scope for the __leaveValue	
       
   129 #define STIF_ASSERT_LEAVES( aStatement ) \
       
   130     { \
       
   131     TRAPD( __leaveValue, aStatement ); \
       
   132     __STIF_ASSERT_SHARED( !AssertEquals( __leaveValue, KErrNone ), KAssertFailedLeaves ); \
       
   133     }
       
   134 
       
   135 #define STIF_ASSERT_LEAVES_RET( aStatement, aFailedRet ) \
       
   136     { \
       
   137     TRAPD( __leaveValue, aStatement ); \
       
   138     __STIF_ASSERT_SHARED_RET( !AssertEquals( __leaveValue, KErrNone ), KAssertFailedLeaves, aFailedRet ); \
       
   139     }
       
   140 
       
   141 // Eclosing block is used to create the scope for the __leaveValue	
       
   142 #define STIF_ASSERT_LEAVES_WITH( aLeaveCode, aStatement ) \
       
   143     { \
       
   144     TRAPD( __leaveValue, aStatement ); \
       
   145     __STIF_ASSERT_SHARED( AssertEquals( __leaveValue, aLeaveCode ), KAssertFailedLeaves ); \
       
   146     }
       
   147 
       
   148 #define STIF_ASSERT_LEAVES_WITH_RET( aLeaveCode, aStatement, aFailedRet ) \
       
   149     { \
       
   150     TRAPD( __leaveValue, aStatement ); \
       
   151     __STIF_ASSERT_SHARED_RET( AssertEquals( __leaveValue, aLeaveCode ), KAssertFailedLeaves, aFailedRet ); \
       
   152     }
       
   153 
       
   154 #define STIF_ASSERT_PANIC( aPanicCode, aStatement ) \
       
   155 	{ \
       
   156 	TestModuleIf().SetExitReason( CTestModuleIf::EPanic, aPanicCode ); \
       
   157 	aStatement; \
       
   158 	}
       
   159 
       
   160 template <class T>
       
   161 inline TBool AssertEquals(const T& aExpected, const T& aActual)
       
   162 /**
       
   163  * AssertEquals
       
   164  *
       
   165  *
       
   166  * @param aExpected - Expected result
       
   167  * @param aActual - Actual result
       
   168  * @return - True if equal
       
   169  */
       
   170 	{
       
   171 	if( aExpected==aActual )
       
   172 		{
       
   173 		return ETrue;
       
   174 		}
       
   175 	return EFalse;
       
   176 	}
       
   177 
       
   178 template <class T>
       
   179 inline TBool AssertNull(const T* aPtr)
       
   180 /**
       
   181  * AssertNull
       
   182  *
       
   183  *
       
   184  * @param aPtr - Pointer
       
   185  * @return - True if NULL
       
   186  */
       
   187 	{
       
   188 	if( aPtr==NULL )
       
   189 		{
       
   190 		return ETrue;
       
   191 		}
       
   192 	return EFalse;
       
   193 	}
       
   194 
       
   195 template <class T>
       
   196 inline TBool AssertSame(const T* aExpectedPtr, const T* aActualPtr)
       
   197 /**
       
   198  * AssertSame
       
   199  *
       
   200  *
       
   201  * @param aExpectedPtr - Expected pointer
       
   202  * @param aActualPtr - Actual pointer
       
   203  * @return - True if equal
       
   204  */
       
   205 	{
       
   206 	if( aExpectedPtr==aActualPtr )
       
   207 		{
       
   208 		return ETrue;
       
   209 		}
       
   210 	return EFalse;
       
   211 	}
       
   212 
       
   213 inline TBool AssertTrue(const TBool& aCondition)
       
   214 /**
       
   215  * AssertTrue
       
   216  *
       
   217  *
       
   218  * @param aCondition - Condition
       
   219  * @return - True if aCondition is true
       
   220  */
       
   221 	{
       
   222 	if( !aCondition )
       
   223 		{
       
   224 		return EFalse;
       
   225 		}
       
   226 	return ETrue;
       
   227 	}
       
   228 
       
   229 #endif
       
   230 
       
   231 // End of File