symport/e32/include/e32test.h
changeset 1 0a7b44b10206
child 2 806186ab5e14
equal deleted inserted replaced
0:c55016431358 1:0a7b44b10206
       
     1 // Copyright (c) 1994-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 the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32\include\e32test.h
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file e32test.h
       
    20  @publishedAll
       
    21  @released
       
    22 */
       
    23 
       
    24 #ifndef __E32TEST_H__
       
    25 #define __E32TEST_H__
       
    26 #include <e32std.h>
       
    27 #include <e32std_private.h>
       
    28 #include <e32base_private.h>
       
    29 #include <e32base.h>
       
    30 #include <e32cons.h>
       
    31 #include <e32ver.h>
       
    32 #include <e32kpan.h>
       
    33 #include <cpudefs.h>
       
    34 #include <e32debug.h>
       
    35 #include <e32def_private.h>
       
    36 #include <e32event_private.h>
       
    37 
       
    38 
       
    39 /**
       
    40 Test console.
       
    41 
       
    42 The class creates a console window to which test results can be logged
       
    43 through the various overloads of the operator().
       
    44 */
       
    45 class RTest
       
    46 	{
       
    47 public:
       
    48 	IMPORT_C RTest(const TDesC &aTitle,TInt aThrowaway,const TText* anOtherThrowaway);
       
    49 	IMPORT_C RTest(const TDesC &aTitle,TInt aThrowaway);
       
    50 	IMPORT_C RTest(const TDesC &aTitle);
       
    51 	IMPORT_C void Close();
       
    52 	IMPORT_C void Title();
       
    53 	IMPORT_C void Start(const TDesC &aHeading);
       
    54 	IMPORT_C void Next(const TDesC &aHeading);
       
    55 	IMPORT_C void End();
       
    56 	IMPORT_C void operator()(TInt aResult,TInt aLineNum,const TText* aFileName);
       
    57 	IMPORT_C void operator()(TInt aResult,TInt aLineNum);
       
    58 	IMPORT_C void operator()(TInt aResult);
       
    59 	IMPORT_C void Panic(TInt anError,TRefByValue<const TDesC> aFmt,...);
       
    60 	IMPORT_C void Panic(TRefByValue<const TDesC> aFmt,...);
       
    61 	IMPORT_C void Printf(TRefByValue<const TDesC> aFmt,...);
       
    62 	IMPORT_C TKeyCode Getch();
       
    63 	inline static const TAny* String(TInt aSel,const TText8 *aBuf1,const TText16 *aBuf2);
       
    64 	inline CConsoleBase* Console() const;
       
    65 	inline void SetConsole(CConsoleBase* aConsole);
       
    66 	inline TBool Logged() const;
       
    67 	inline void SetLogged(TBool aToLog);
       
    68 	inline void HandleError(TInt aError, TInt aLine, const TText* aFileName);
       
    69 	inline void HandleNull(TInt aLine, const TText* aFileName);
       
    70 	inline void HandleNotEqual(TInt aExpected, TInt aActual, TInt aLine, const TText* aFileName);
       
    71 	inline void HandleFailedCompare(TInt aLeft, const TText* aComp, TInt aRight, TInt aLine, const TText* aFileName);
       
    72 	inline void HandleValue(TInt aValue,  TInt aLine, const TText* aFileName);
       
    73 
       
    74 	IMPORT_C static TInt CloseHandleAndWaitForDestruction(RHandleBase& aH);	/**< @internalTechnology */
       
    75 
       
    76 protected:
       
    77 	void CheckConsoleCreated();
       
    78 	void DisplayLevel();
       
    79 	inline void Push();
       
    80 	inline void Pop();
       
    81 private:
       
    82 	enum {EMaxStack=0x100,EMaxBuffer=0x100};
       
    83 private:
       
    84 	TInt	iTest;
       
    85 	TInt	iCheck;
       
    86 	TInt	iLevel;
       
    87 	TBool	iLogging;
       
    88 	CConsoleBase *iConsole;
       
    89 	TBuf<0x40> iTitle;
       
    90 	TInt iStack[EMaxStack];
       
    91 	TText iBuf[EMaxBuffer];
       
    92 	};
       
    93 
       
    94 
       
    95 
       
    96 
       
    97 /**
       
    98 Gets the console.
       
    99 
       
   100 @return A pointer to the console object.
       
   101 */
       
   102 inline CConsoleBase* RTest::Console() const
       
   103 	{ return(iConsole); }
       
   104 
       
   105 	
       
   106 	
       
   107 	
       
   108 /**
       
   109 Utility function that returns a pointer to the specified TText8* argument
       
   110 or the TText16* argument depending on the value of the aSel argument.
       
   111 
       
   112 @param aSel  An integer containing the size of a TText8 type or TText16 type.
       
   113 @param aBuf1 A pointer to 8-bit text.
       
   114 @param aBuf2 A pointer to 16-bit text.
       
   115 
       
   116 @return A pointer to aBuf1, if the value of aSel is the size of a TText8 type,
       
   117         otherwise a pointer to aBuf2.
       
   118 */	
       
   119 inline const TAny *RTest::String(TInt aSel,const TText8 *aBuf1,const TText16 *aBuf2)
       
   120 	{ return(aSel == sizeof(TText8) ? (TAny *)aBuf1 : (TAny *)aBuf2); }
       
   121 
       
   122 
       
   123 
       
   124 /**
       
   125 @internalComponent
       
   126 */
       
   127 inline void RTest::Push()
       
   128 	{ iStack[iLevel++] = iTest; iTest = 0; }
       
   129 
       
   130 
       
   131 
       
   132 /**
       
   133 @internalComponent
       
   134 */
       
   135 inline void RTest::Pop()
       
   136 	{ iTest = iStack[--iLevel]; }
       
   137 
       
   138 
       
   139 
       
   140 
       
   141 /**
       
   142 Sets the console.
       
   143 
       
   144 @param aConsole A pointer to the console object to be used.
       
   145 */	
       
   146 inline void RTest::SetConsole(CConsoleBase* aConsole)
       
   147     { iConsole = aConsole; }
       
   148 
       
   149 
       
   150 
       
   151 
       
   152 /**
       
   153 Tests whether the logging flag is set.
       
   154 
       
   155 If the logging flag is set, console output is also written to
       
   156 the debug output as represented by a RDebug object.
       
   157 
       
   158 @return True, if the logging flag is set, false otherwise.
       
   159 */	
       
   160 inline TBool RTest::Logged() const
       
   161 	{ return(iLogging); }
       
   162 
       
   163 
       
   164 
       
   165 
       
   166 /**
       
   167 Sets the logging flag.
       
   168 
       
   169 If the logging flag is set, console output is also written to
       
   170 the debug output as represented by a RDebug object.
       
   171 
       
   172 @param aToLog ETrue, if the logging flag is to be set, EFalse, otherwise.
       
   173 */	
       
   174 inline void RTest::SetLogged(TBool aToLog)
       
   175 	{ iLogging = aToLog; }
       
   176 
       
   177 
       
   178 
       
   179 
       
   180 // test equivalent of _L
       
   181 /**
       
   182 @internalComponent
       
   183 */
       
   184 #define _TL(a) (S*)RTest::String(sizeof(S),(TText8*)a,(TText16*)L ## a) 
       
   185 
       
   186 // the next two, slightly confusing, macros are necessary in order
       
   187 // to enable proper string merging with certain compilers.
       
   188 
       
   189 /**
       
   190 @internalComponent
       
   191 */
       
   192 #define __test(x,l,f) test(x,l,_S(f))
       
   193 
       
   194 /**
       
   195 @internalComponent
       
   196 */
       
   197 #define test(x) __test(x,__LINE__,__FILE__)
       
   198 
       
   199 
       
   200 #ifdef __E32TEST_EXTENSION__
       
   201 
       
   202 /**
       
   203 @internalComponent
       
   204 */
       
   205 #define __S(f) _S(f)
       
   206 
       
   207 /**
       
   208 @internalComponent
       
   209 
       
   210 Panics and displays an appropriate error message if x is less then zero (Indicating an error code).
       
   211 */
       
   212 #define test_NotNegative(x) { TInt _r = (x); if (_r < 0) test.HandleError(_r, __LINE__,__S(__FILE__)); }
       
   213 
       
   214 /**
       
   215 @internalComponent
       
   216 
       
   217 Panics and displays an appropriate error message if x is not equal to KErrNone.
       
   218 */
       
   219 #define test_KErrNone(x) { TInt _r = (x); if (_r !=KErrNone) test.HandleError(_r, __LINE__,__S(__FILE__)); }
       
   220 
       
   221 /**
       
   222 @internalComponent
       
   223 
       
   224 Panics and displays an appropriate error message if the trapped statement/block x leaves.
       
   225 */
       
   226 #define test_TRAP(x) { TRAPD(_r, x); if (_r != KErrNone) test.HandleError(_r, __LINE__,__S(__FILE__)); }
       
   227 
       
   228 /**
       
   229 @internalComponent
       
   230 
       
   231 Panics and displays an appropriate error message if x is not equal to NULL.
       
   232 */
       
   233 #define test_NotNull(x) { TAny* _a = (TAny*)(x); if (_a == NULL) test.HandleNull(__LINE__,__S(__FILE__)); }
       
   234 /**
       
   235 @internalComponent
       
   236 
       
   237 Panics and displays an appropriate error message if e (expected) is not equal to a (actual).
       
   238 */
       
   239 #define test_Equal(e, a) { TInt _e = TInt(e); TInt _a = TInt(a); if (_e != _a) test.HandleNotEqual(_e, _a, __LINE__,__S(__FILE__)); }
       
   240 
       
   241 /**
       
   242 @internalComponent
       
   243 
       
   244 Panics and displays an appropriate error message if the comparison specified with operator b, between a and c, is EFalse.
       
   245 */
       
   246 #define test_Compare(a,b,c)  {TInt _a = TInt(a); TInt _c = TInt(c); if (!(_a b _c)) test.HandleFailedCompare(_a, __S(#b), _c, __LINE__,__S(__FILE__)); }
       
   247 
       
   248 
       
   249 /**
       
   250 @internalComponent
       
   251 
       
   252 Panics and displays an appropriate error message displaying v, if the expression e is false.
       
   253 */
       
   254 #define test_Value(v, e) if (!(e)) test.HandleValue(v,  __LINE__,__S(__FILE__));
       
   255 
       
   256 /**
       
   257 @internalComponent
       
   258 
       
   259 If expression e is false, statement s is executed then a Panic is raised.
       
   260 */
       
   261 #define test_Assert(e,s) if(!(e)) {s; test.operator()(EFalse, __LINE__,__S(__FILE__)); }
       
   262 	
       
   263 	
       
   264 
       
   265 #endif
       
   266 
       
   267 
       
   268 /**
       
   269 Prints a failure message, including an error code at the console and raises a panic.
       
   270 
       
   271 
       
   272 @param aError	 The error code to be printed in the failure massage.
       
   273 @param aLineNum  A line number that is printed in the failure message.
       
   274 @param aFileName A file name that is printed in the failure message.
       
   275                  
       
   276 @panic USER 84 Always.
       
   277 */
       
   278 inline void RTest::HandleError(TInt aError, TInt aLine, const TText* aFileName)
       
   279 	{
       
   280 	RDebug::Printf("RTEST: Error %d at line %d", aError,aLine);
       
   281 	Printf(_L("RTEST: Error %d\n"), aError);
       
   282 	operator()(EFalse, aLine, aFileName);
       
   283 	}
       
   284 /**
       
   285 Prints a failure message indicating null was encountered, at the console and raises a panic.
       
   286 
       
   287 @param aLineNum  A line number that is printed in the failure message.
       
   288 @param aFileName A file name that is printed in the failure message.
       
   289                  
       
   290 @panic USER 84 Always.
       
   291 */
       
   292 
       
   293 inline void RTest::HandleNull(TInt aLine, const TText* aFileName)
       
   294 	{
       
   295 	RDebug::Printf("RTEST: Null value at line %d", aLine);
       
   296 	Printf(_L("RTEST: Null value\n"));
       
   297 	operator()(EFalse, aLine, aFileName);
       
   298 	}
       
   299 
       
   300 
       
   301 /**
       
   302 Prints a failure message indicating that two value (also printed) where not equal, at the console and raises a panic.
       
   303 
       
   304 @param aExpected The value that is to be printed as expected.
       
   305 @param aActual	 The value that is to be printed as being actually received.
       
   306 @param aLineNum  A line number that is printed in the failure message.
       
   307 @param aFileName A file name that is printed in the failure message.
       
   308                  
       
   309 @panic USER 84 Always.
       
   310 */
       
   311 
       
   312 inline void RTest::HandleNotEqual(TInt aExpected, TInt aActual, TInt aLine, const TText* aFileName)
       
   313 	{
       
   314 	RDebug::Printf("RTEST: Expected 0x%x (%d) but got 0x%x (%d) at line %d", aExpected,aExpected,aActual,aActual,aLine);
       
   315 	Printf(_L("RTEST: Expected 0x%x (%d) but got 0x%x (%d)\n"), aExpected,aExpected,aActual,aActual);
       
   316 	operator()(EFalse, aLine, aFileName);
       
   317 	}
       
   318 
       
   319 
       
   320 /**
       
   321 Prints a failure message indicating that a comparison between two values (also printed) resulted in EFalse,
       
   322 at the console and raises a panic.
       
   323 
       
   324 @param aLeft 	 The left value of the comparison.
       
   325 @param aComp	 A string representing the comparison operator.
       
   326 @param aRight	 The right value of the comparison.
       
   327 @param aLineNum  A line number that is printed in the failure message.
       
   328 @param aFileName A file name that is printed in the failure message.
       
   329                  
       
   330 @panic USER 84 Always.
       
   331 */
       
   332 inline void RTest::HandleFailedCompare(TInt aLeft, const TText* aComp, TInt aRight, TInt aLine, const TText* aFileName)
       
   333 	{
       
   334 	RDebug::Printf("RTEST: (0x%x (%d) %s 0x%x (%d)) == EFalse at line %d", aLeft,aLeft,aComp,aRight,aRight,aLine);
       
   335 	Printf(_L("RTEST: (0x%x (%d) %s 0x%x (%d)) == EFalse\n"), aLeft,aLeft,aComp, aRight,aRight);
       
   336 	operator()(EFalse, aLine, aFileName);
       
   337 	}
       
   338 
       
   339 
       
   340 /**
       
   341 Prints a failure message indicating that aValue was not an expected value, at the console and raises a panic.
       
   342 
       
   343 @param aValue The value that is to be printed as not being an expected value.
       
   344 @param aLineNum  A line number that is printed in the failure message.
       
   345 @param aFileName A file name that is printed in the failure message.
       
   346                  
       
   347 @panic USER 84 Always.
       
   348 */
       
   349 inline void RTest::HandleValue(TInt aValue,  TInt aLine, const TText* aFileName)
       
   350 	{
       
   351 	Printf(_L("RTEST: %d (0x%x) was not an expected value.\n"), aValue, aValue);
       
   352 	operator()(EFalse, aLine, aFileName);
       
   353 	}
       
   354 
       
   355 
       
   356 /**
       
   357 @internalTechnology
       
   358 */
       
   359 _LIT(KLitCloseAndWait,"Close&Wait");
       
   360 
       
   361 /**
       
   362 @internalTechnology
       
   363 */
       
   364 #define CLOSE_AND_WAIT(h)	((void)(RTest::CloseHandleAndWaitForDestruction(h) && (User::Panic(KLitCloseAndWait,__LINE__),1)))
       
   365 
       
   366 
       
   367 
       
   368 #endif
       
   369