testexecfw/stf/api/api_platform/inc/TestclassAssert.h
changeset 2 8bb370ba6d1d
equal deleted inserted replaced
1:bbd31066657e 2:8bb370ba6d1d
       
     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 #ifdef _UNICODE
       
    44 	#define __STF_WIDEN2(x) L ## x
       
    45 	#define __STF_WIDEN(x) __STF_WIDEN2(x)
       
    46 	#define __STF_DBG_FILE__ __STF_WIDEN(__FILE__)
       
    47 #else
       
    48 	#define __STF_DBG_FILE__ __FILE__
       
    49 #endif
       
    50 
       
    51 // Logs to the STIF log file AND to the RDebug
       
    52 #define STIF_LOG( aMessage ) \
       
    53     iLog->Log( _L( aMessage ) ); RDebug::Print( _L( aMessage ) );
       
    54     
       
    55 // Logs to the STF log file AND to the RDebug
       
    56 #define STF_LOG( aMessage ) \
       
    57     iLog->Log( _L( aMessage ) ); RDebug::Print( _L( aMessage ) );	
       
    58 
       
    59 /*********************************************************************************
       
    60  * STF Assert Macros
       
    61  *********************************************************************************/
       
    62 #define __STF_ASSERT_SHARED( aFunction, aMessage ) \
       
    63 	if(!aFunction) \
       
    64 		{ \
       
    65 		iLog->Log( aMessage, __STF_DBG_FILE__, __LINE__ );\
       
    66 		return KErrGeneral;\
       
    67 		}
       
    68 
       
    69 #define __STF_ASSERT_SHARED_RET( aFunction, aMessage, aFailedReturn ) \
       
    70 	if(!aFunction) \
       
    71 		{ \
       
    72 		iLog->Log( aMessage, __STF_DBG_FILE__, __LINE__ );\
       
    73 		return aFailedReturn;\
       
    74 		}
       
    75 		
       
    76 
       
    77 #define STF_ASSERT_EQUALS( aExpected, aActual ) \
       
    78 	__STF_ASSERT_SHARED( AssertEquals( aExpected, aActual ) , KAssertFailedEquals );
       
    79 
       
    80 #define STF_ASSERT_EQUALS_RET( aExpected, aActual, aFailedRet ) \
       
    81 	__STF_ASSERT_SHARED_RET( AssertEquals( aExpected, aActual ) , KAssertFailedEquals, aFailedRet );
       
    82 
       
    83 #define STF_ASSERT_NOT_EQUALS( aExpected, aActual ) \
       
    84 	__STF_ASSERT_SHARED( !AssertEquals( aExpected, aActual ) , KAssertFailedNotEquals );
       
    85 
       
    86 #define STF_ASSERT_NOT_EQUALS_RET( aExpected, aActual, aFailedRet ) \
       
    87 	__STF_ASSERT_SHARED_RET( !AssertEquals( aExpected, aActual ) , KAssertFailedNotEquals, aFailedRet );
       
    88 
       
    89 #define STF_ASSERT_NULL( aPtr ) \
       
    90 	__STF_ASSERT_SHARED( AssertNull( aPtr ), KAssertFailedNull );
       
    91 
       
    92 #define STF_ASSERT_NULL_RET( aPtr, aFailedRet ) \
       
    93 	__STF_ASSERT_SHARED_RET( AssertNull( aPtr ), KAssertFailedNull, aFailedRet );
       
    94 
       
    95 #define STF_ASSERT_NOT_NULL( aPtr ) \
       
    96 	__STF_ASSERT_SHARED( !AssertNull( aPtr ), KAssertFailedNotNull );
       
    97 
       
    98 #define STF_ASSERT_NOT_NULL_RET( aPtr, aFailedRet ) \
       
    99 	__STF_ASSERT_SHARED_RET( !AssertNull( aPtr ), KAssertFailedNotNull, aFailedRet );
       
   100 
       
   101 #define STF_ASSERT_SAME( aExpectedPtr, aActualPtr ) \
       
   102 	__STF_ASSERT_SHARED( AssertSame( aExpectedPtr, aActualPtr ), KAssertFailedSame );
       
   103 
       
   104 #define STF_ASSERT_SAME_RET( aExpectedPtr, aActualPtr, aFailedRet ) \
       
   105 	__STF_ASSERT_SHARED_RET( AssertSame( aExpectedPtr, aActualPtr ), KAssertFailedSame, aFailedRet );
       
   106 
       
   107 #define STF_ASSERT_NOT_SAME( aExpectedPtr, aActualPtr ) \
       
   108 	__STF_ASSERT_SHARED( !AssertSame( aExpectedPtr, aActualPtr ), KAssertFailedNotSame );
       
   109 
       
   110 #define STF_ASSERT_NOT_SAME_RET( aExpectedPtr, aActualPtr, aFailedRet ) \
       
   111 	__STF_ASSERT_SHARED_RET( !AssertSame( aExpectedPtr, aActualPtr ), KAssertFailedNotSame, aFailedRet );
       
   112 
       
   113 #define STF_ASSERT_TRUE( aCondition ) \
       
   114 	__STF_ASSERT_SHARED( AssertTrue( aCondition ), KAssertFailedTrue );
       
   115 
       
   116 #define STF_ASSERT_TRUE_RET( aCondition, aFailedRet ) \
       
   117 	__STF_ASSERT_SHARED_RET( AssertTrue( aCondition ), KAssertFailedTrue, aFailedRet );
       
   118 
       
   119 #define STF_ASSERT_FALSE( aCondition ) \
       
   120 	__STF_ASSERT_SHARED( !AssertTrue( aCondition ), KAssertFailedFalse );
       
   121 
       
   122 #define STF_ASSERT_FALSE_RET( aCondition, aFailedRet ) \
       
   123 	__STF_ASSERT_SHARED_RET( !AssertTrue( aCondition ), KAssertFailedFalse, aFailedRet );
       
   124 
       
   125 // Eclosing block is used to create the scope for the __leaveValue	
       
   126 #define STF_ASSERT_NOT_LEAVES( aStatement ) \
       
   127     { \
       
   128     TRAPD( __leaveValue, aStatement ); \
       
   129     __STF_ASSERT_SHARED( AssertEquals( __leaveValue, KErrNone ), KAssertFailedNotLeaves ); \
       
   130     }
       
   131 
       
   132 #define STF_ASSERT_NOT_LEAVES_RET( aStatement, aFailedRet ) \
       
   133     { \
       
   134     TRAPD( __leaveValue, aStatement ); \
       
   135     __STF_ASSERT_SHARED_RET( AssertEquals( __leaveValue, KErrNone ), KAssertFailedNotLeaves, aFailedRet ); \
       
   136     }
       
   137 
       
   138 // Eclosing block is used to create the scope for the __leaveValue	
       
   139 #define STF_ASSERT_LEAVES( aStatement ) \
       
   140     { \
       
   141     TRAPD( __leaveValue, aStatement ); \
       
   142     __STF_ASSERT_SHARED( !AssertEquals( __leaveValue, KErrNone ), KAssertFailedLeaves ); \
       
   143     }
       
   144 
       
   145 #define STF_ASSERT_LEAVES_RET( aStatement, aFailedRet ) \
       
   146     { \
       
   147     TRAPD( __leaveValue, aStatement ); \
       
   148     __STF_ASSERT_SHARED_RET( !AssertEquals( __leaveValue, KErrNone ), KAssertFailedLeaves, aFailedRet ); \
       
   149     }
       
   150 
       
   151 // Eclosing block is used to create the scope for the __leaveValue	
       
   152 #define STF_ASSERT_LEAVES_WITH( aLeaveCode, aStatement ) \
       
   153     { \
       
   154     TRAPD( __leaveValue, aStatement ); \
       
   155     __STF_ASSERT_SHARED( AssertEquals( __leaveValue, aLeaveCode ), KAssertFailedLeaves ); \
       
   156     }
       
   157 
       
   158 #define STF_ASSERT_LEAVES_WITH_RET( aLeaveCode, aStatement, aFailedRet ) \
       
   159     { \
       
   160     TRAPD( __leaveValue, aStatement ); \
       
   161     __STF_ASSERT_SHARED_RET( AssertEquals( __leaveValue, aLeaveCode ), KAssertFailedLeaves, aFailedRet ); \
       
   162     }
       
   163 
       
   164 #define STF_ASSERT_PANIC( aPanicCode, aStatement ) \
       
   165 	{ \
       
   166 	TestModuleIf().SetExitReason( CTestModuleIf::EPanic, aPanicCode ); \
       
   167 	aStatement; \
       
   168 	}
       
   169 	
       
   170 /*********************************************************************************
       
   171  * STIF Assert Macros
       
   172  *********************************************************************************/
       
   173 #define __STIF_ASSERT_SHARED( aFunction, aMessage ) \
       
   174 	if(!aFunction) \
       
   175 		{ \
       
   176 		iLog->Log( aMessage, __STIF_DBG_FILE__, __LINE__ );\
       
   177 		return KErrGeneral;\
       
   178 		}
       
   179 
       
   180 #define __STIF_ASSERT_SHARED_RET( aFunction, aMessage, aFailedReturn ) \
       
   181 	if(!aFunction) \
       
   182 		{ \
       
   183 		iLog->Log( aMessage, __STIF_DBG_FILE__, __LINE__ );\
       
   184 		return aFailedReturn;\
       
   185 		}
       
   186 		
       
   187 
       
   188 #define STIF_ASSERT_EQUALS( aExpected, aActual ) \
       
   189 	__STIF_ASSERT_SHARED( AssertEquals( aExpected, aActual ) , KAssertFailedEquals );
       
   190 
       
   191 #define STIF_ASSERT_EQUALS_RET( aExpected, aActual, aFailedRet ) \
       
   192 	__STIF_ASSERT_SHARED_RET( AssertEquals( aExpected, aActual ) , KAssertFailedEquals, aFailedRet );
       
   193 
       
   194 #define STIF_ASSERT_NOT_EQUALS( aExpected, aActual ) \
       
   195 	__STIF_ASSERT_SHARED( !AssertEquals( aExpected, aActual ) , KAssertFailedNotEquals );
       
   196 
       
   197 #define STIF_ASSERT_NOT_EQUALS_RET( aExpected, aActual, aFailedRet ) \
       
   198 	__STIF_ASSERT_SHARED_RET( !AssertEquals( aExpected, aActual ) , KAssertFailedNotEquals, aFailedRet );
       
   199 
       
   200 #define STIF_ASSERT_NULL( aPtr ) \
       
   201 	__STIF_ASSERT_SHARED( AssertNull( aPtr ), KAssertFailedNull );
       
   202 
       
   203 #define STIF_ASSERT_NULL_RET( aPtr, aFailedRet ) \
       
   204 	__STIF_ASSERT_SHARED_RET( AssertNull( aPtr ), KAssertFailedNull, aFailedRet );
       
   205 
       
   206 #define STIF_ASSERT_NOT_NULL( aPtr ) \
       
   207 	__STIF_ASSERT_SHARED( !AssertNull( aPtr ), KAssertFailedNotNull );
       
   208 
       
   209 #define STIF_ASSERT_NOT_NULL_RET( aPtr, aFailedRet ) \
       
   210 	__STIF_ASSERT_SHARED_RET( !AssertNull( aPtr ), KAssertFailedNotNull, aFailedRet );
       
   211 
       
   212 #define STIF_ASSERT_SAME( aExpectedPtr, aActualPtr ) \
       
   213 	__STIF_ASSERT_SHARED( AssertSame( aExpectedPtr, aActualPtr ), KAssertFailedSame );
       
   214 
       
   215 #define STIF_ASSERT_SAME_RET( aExpectedPtr, aActualPtr, aFailedRet ) \
       
   216 	__STIF_ASSERT_SHARED_RET( AssertSame( aExpectedPtr, aActualPtr ), KAssertFailedSame, aFailedRet );
       
   217 
       
   218 #define STIF_ASSERT_NOT_SAME( aExpectedPtr, aActualPtr ) \
       
   219 	__STIF_ASSERT_SHARED( !AssertSame( aExpectedPtr, aActualPtr ), KAssertFailedNotSame );
       
   220 
       
   221 #define STIF_ASSERT_NOT_SAME_RET( aExpectedPtr, aActualPtr, aFailedRet ) \
       
   222 	__STIF_ASSERT_SHARED_RET( !AssertSame( aExpectedPtr, aActualPtr ), KAssertFailedNotSame, aFailedRet );
       
   223 
       
   224 #define STIF_ASSERT_TRUE( aCondition ) \
       
   225 	__STIF_ASSERT_SHARED( AssertTrue( aCondition ), KAssertFailedTrue );
       
   226 
       
   227 #define STIF_ASSERT_TRUE_RET( aCondition, aFailedRet ) \
       
   228 	__STIF_ASSERT_SHARED_RET( AssertTrue( aCondition ), KAssertFailedTrue, aFailedRet );
       
   229 
       
   230 #define STIF_ASSERT_FALSE( aCondition ) \
       
   231 	__STIF_ASSERT_SHARED( !AssertTrue( aCondition ), KAssertFailedFalse );
       
   232 
       
   233 #define STIF_ASSERT_FALSE_RET( aCondition, aFailedRet ) \
       
   234 	__STIF_ASSERT_SHARED_RET( !AssertTrue( aCondition ), KAssertFailedFalse, aFailedRet );
       
   235 
       
   236 // Eclosing block is used to create the scope for the __leaveValue	
       
   237 #define STIF_ASSERT_NOT_LEAVES( aStatement ) \
       
   238     { \
       
   239     TRAPD( __leaveValue, aStatement ); \
       
   240     __STIF_ASSERT_SHARED( AssertEquals( __leaveValue, KErrNone ), KAssertFailedNotLeaves ); \
       
   241     }
       
   242 
       
   243 #define STIF_ASSERT_NOT_LEAVES_RET( aStatement, aFailedRet ) \
       
   244     { \
       
   245     TRAPD( __leaveValue, aStatement ); \
       
   246     __STIF_ASSERT_SHARED_RET( AssertEquals( __leaveValue, KErrNone ), KAssertFailedNotLeaves, aFailedRet ); \
       
   247     }
       
   248 
       
   249 // Eclosing block is used to create the scope for the __leaveValue	
       
   250 #define STIF_ASSERT_LEAVES( aStatement ) \
       
   251     { \
       
   252     TRAPD( __leaveValue, aStatement ); \
       
   253     __STIF_ASSERT_SHARED( !AssertEquals( __leaveValue, KErrNone ), KAssertFailedLeaves ); \
       
   254     }
       
   255 
       
   256 #define STIF_ASSERT_LEAVES_RET( aStatement, aFailedRet ) \
       
   257     { \
       
   258     TRAPD( __leaveValue, aStatement ); \
       
   259     __STIF_ASSERT_SHARED_RET( !AssertEquals( __leaveValue, KErrNone ), KAssertFailedLeaves, aFailedRet ); \
       
   260     }
       
   261 
       
   262 // Eclosing block is used to create the scope for the __leaveValue	
       
   263 #define STIF_ASSERT_LEAVES_WITH( aLeaveCode, aStatement ) \
       
   264     { \
       
   265     TRAPD( __leaveValue, aStatement ); \
       
   266     __STIF_ASSERT_SHARED( AssertEquals( __leaveValue, aLeaveCode ), KAssertFailedLeaves ); \
       
   267     }
       
   268 
       
   269 #define STIF_ASSERT_LEAVES_WITH_RET( aLeaveCode, aStatement, aFailedRet ) \
       
   270     { \
       
   271     TRAPD( __leaveValue, aStatement ); \
       
   272     __STIF_ASSERT_SHARED_RET( AssertEquals( __leaveValue, aLeaveCode ), KAssertFailedLeaves, aFailedRet ); \
       
   273     }
       
   274 
       
   275 #define STIF_ASSERT_PANIC( aPanicCode, aStatement ) \
       
   276 	{ \
       
   277 	TestModuleIf().SetExitReason( CTestModuleIf::EPanic, aPanicCode ); \
       
   278 	aStatement; \
       
   279 	}
       
   280 
       
   281 template <class T>
       
   282 inline TBool AssertEquals(const T& aExpected, const T& aActual)
       
   283 /**
       
   284  * AssertEquals
       
   285  *
       
   286  *
       
   287  * @param aExpected - Expected result
       
   288  * @param aActual - Actual result
       
   289  * @return - True if equal
       
   290  */
       
   291 	{
       
   292 	if( aExpected==aActual )
       
   293 		{
       
   294 		return ETrue;
       
   295 		}
       
   296 	return EFalse;
       
   297 	}
       
   298 
       
   299 template <class T>
       
   300 inline TBool AssertNull(const T* aPtr)
       
   301 /**
       
   302  * AssertNull
       
   303  *
       
   304  *
       
   305  * @param aPtr - Pointer
       
   306  * @return - True if NULL
       
   307  */
       
   308 	{
       
   309 	if( aPtr==NULL )
       
   310 		{
       
   311 		return ETrue;
       
   312 		}
       
   313 	return EFalse;
       
   314 	}
       
   315 
       
   316 template <class T>
       
   317 inline TBool AssertSame(const T* aExpectedPtr, const T* aActualPtr)
       
   318 /**
       
   319  * AssertSame
       
   320  *
       
   321  *
       
   322  * @param aExpectedPtr - Expected pointer
       
   323  * @param aActualPtr - Actual pointer
       
   324  * @return - True if equal
       
   325  */
       
   326 	{
       
   327 	if( aExpectedPtr==aActualPtr )
       
   328 		{
       
   329 		return ETrue;
       
   330 		}
       
   331 	return EFalse;
       
   332 	}
       
   333 
       
   334 inline TBool AssertTrue(const TBool& aCondition)
       
   335 /**
       
   336  * AssertTrue
       
   337  *
       
   338  *
       
   339  * @param aCondition - Condition
       
   340  * @return - True if aCondition is true
       
   341  */
       
   342 	{
       
   343 	if( !aCondition )
       
   344 		{
       
   345 		return EFalse;
       
   346 		}
       
   347 	return ETrue;
       
   348 	}
       
   349 
       
   350 #endif