os/ossrv/lowlevellibsandfws/genericusabilitylib/test/src/t_lstring.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 // Overview:
    15 // Test methods of the LString16, LString8, LString template class.
    16 // The test cases below are template based allowing the same code to 
    17 // be used for testing both LString16 and LString8 classes.
    18 // Appropriate LString and buffer types are passed as the template
    19 // parameters in the call to each test case.
    20 // API Information:
    21 // LString16, LString8, LString.
    22 // Details:
    23 // For LString8, LString16 and LString objects:
    24 // - Test the Create and CreateMax methods by verifying the return value of
    25 // KErrNone, the initial length and max length. Perform basic write and read
    26 // operations and verify the results.
    27 // - Test the CreateL and CreateMaxL methods by verifying the return value of 
    28 // KErrNone. Also force a heap error and verify return value of KErrNoMemory.
    29 // - Test the Create(const TDesC_& aDesc) and Create(const TDesCX_ aDesc, 
    30 // TInt aMaxLength) methods by verifying the return value of KErrNone. Verify
    31 // initial length, max length and initialisation.
    32 // - Test the CreateL(const TDesC_& aDesc) and CreateMaxL(const TDesCX_ aDesc, 
    33 // TInt aMaxLength) methods by verifying the return value of KErrNone. Also 
    34 // force a heap error and verify return value of KErrNoMemory.
    35 // - Test the Swap method by creating two initialised objects, calling Swap 
    36 // and confirming the results as expected.
    37 // - Test the Assign method by performing an assign from a variety of sources
    38 // and verifying the results are as expected.
    39 // - Test the ReAlloc method in a variety of scenarios that decrease memory, 
    40 // increase memory and zero-length memory. Verify that the results are as
    41 // expected.
    42 // - Test the ReAllocL by verifying the return value of KErrNone. Also force 
    43 // a heap error and verify return value of KErrNoMemory. Verify that the
    44 // object is the same as before the failed ReAllocL call.
    45 // - Test the CleanupClosePushL method via CleanupStack::PopAndDestroy().
    46 // - Force the CleanupClosePushL to leave to check cleanup of LString.
    47 // Platforms/Drives/Compatibility:
    48 // All 
    49 // Assumptions/Requirement/Pre-requisites:
    50 // Failures and causes:
    51 // Base Port information:
    52 // 
    53 //
    54 
    55 #include <e32base.h>
    56 #include <e32math.h>
    57 #include <e32test.h>
    58 #include <euserhl.h>
    59 #include <u32std.h>
    60 #include <f32file.h>
    61 #include <s32mem.h>
    62 
    63 LOCAL_D RTest test(_L("T_LSTRING"));
    64 
    65 /* This macro generates a TTEXT* from the string passed in.  
    66  * The TTEXT type is a template parameter defined for the calling function
    67  * and an appropriate 8 or 16 bit string variant is created by this macro
    68  */
    69 #undef _TS
    70 #define _TS(a) ((const TTEXT*)RTest::String(sizeof(TTEXT),(TText8*)a,(TText16*)L ## a))
    71 
    72 /* This macro generates a CHAR* from the string passed in.  
    73  * The CHAR type is a template parameter defined for the calling function
    74  * and an appropriate 8(char)or 16(wchar_t) bit string variant is created by this macro
    75  */
    76 #undef _CS
    77 #define _CS(a) ((const CHAR*)RTest::String(sizeof(TTEXT),(TText8*)a,(TText16*)L ## a))
    78 
    79 /**
    80 @SYMTestCaseID SYSLIB-EUSERHL-UT-4001
    81 @SYMTestCaseDesc Tests constructors of LString classes
    82 @SYMTestPriority High
    83 @SYMTestActions Creates LString objects using the different 
    84 				constructors. 
    85 @SYMTestExpectedResults All constructors create object object as expected
    86 @SYMREQ	10372
    87 */
    88 template<class LSTRING, class TBUF, class TTEXT, class HBUF, class CHAR >
    89 LOCAL_C void TestConstructors(LSTRING*)
    90 {
    91 	
    92 	test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4001"));
    93 	
    94 	test.Next(_L("LString_(const TDesC& aDes) constructor"));
    95 	const TBUF des (_TS("123456"));
    96 	const LSTRING lStr(des);
    97 	test(lStr.Length() == 6);
    98 	test(lStr.MaxLength() >= 6);
    99 	
   100 	test.Next(_L("LString_(const LString16& aDes) constructor"));
   101 	LSTRING lStr1(lStr);
   102 	test(lStr1.Length() == 6);
   103 	test(lStr1.MaxLength() >= 6);
   104 	
   105 	test.Next(_L("LString_(const TUInt16* aString) constructor"));
   106 	LSTRING lStr2(lStr1.PtrZL());
   107 	test(lStr2.Length() == 6);
   108 	test(lStr2.MaxLength() >= 6);
   109 	
   110 	test.Next(_L("LString_(HBufC_* aHBuf) constructor"));
   111 
   112 	HBUF* hBuf = HBUF::NewMax(12);	//Create LString as EBufCPtr
   113 	LSTRING lStr3(hBuf);
   114 	test(lStr3.Length() == 12);
   115 	test(lStr3.MaxLength() >= 12);
   116 
   117 	hBuf = HBUF::NewMax(0);
   118 	LSTRING lStr4(hBuf);			//The length of aHBuf is zero
   119 	test(lStr4.Length() == 0);
   120 
   121 	hBuf = NULL;				//aHBuf is NULL
   122 	LSTRING lStr5((HBUF*)hBuf);
   123 	test(lStr5.Length() == 0);
   124 	test(lStr5.MaxLength() == 0);
   125 	
   126 	test.Next(_L("LString_(TUInt16* aHeapCell, TInt aLength, TInt aMaxLength) constructor"));
   127 	TTEXT* heap = NULL;
   128 
   129 	heap = (TTEXT*)User::Alloc(24*(TInt)sizeof(TTEXT)); //Allocate 48 bytes for 24 long LString16
   130 	LSTRING lStr6(heap, 12,24);
   131 	test(lStr6.Length() == 12);		
   132 	test(lStr6.MaxLength() >= 24);		
   133 
   134 	test.Next(_L("LString_(TUint* aHeapCell, TInt aMaxLength ) method"));
   135 	
   136 	heap = (TTEXT*)User::Alloc(24*(TInt)sizeof(TTEXT)); //Allocate 48 bytes for 24 long LString16
   137 	LSTRING lStr7(heap, 24);
   138 	test(lStr7.Length() == 0);		
   139 	test(lStr7.MaxLength() >= 24);		
   140 	
   141 	test.Next(_L("LString_(char/wchar_t * aCharStr) constructor"));
   142 	
   143 	LSTRING lStr8(_CS("0123456789"));
   144 	test(lStr8.Length() == 10);		
   145 	test(lStr8.MaxLength() >= 10);
   146 	
   147 	LSTRING lStr9(_CS("01234567890"));
   148 	test(lStr9.Length() == 11);		
   149 	test(lStr9.MaxLength() >= 11);
   150 }
   151 
   152 
   153 
   154 /**
   155 Tests the following methods:
   156  - TInt Assign(const LString_& lStr);
   157  - TInt Assign(TUint* aHeapCell, TInt aMaxLength);
   158  - TInt Assign(TUint* aHeapCell, TInt aLength, TInt aMaxLength);
   159  - TInt Assign(HBufC& aHBuf);
   160  - LString(HBufC_&) constructor.
   161 */
   162 /**
   163 @SYMTestCaseID SYSLIB-EUSERHL-UT-4002
   164 @SYMTestCaseDesc Tests Assign methods of LString
   165 @SYMTestPriority High
   166 @SYMTestActions Creates LString objects and assigns data to the LString using 
   167 				the LString::Assign overloads.
   168 				Checks that the data is assigned as expected.
   169 				Creates LString objects from HBufC* and verifies that object
   170 				is created as expected
   171 @SYMTestExpectedResults LString objects are created and assigned as expected
   172 @SYMREQ	10372
   173 */
   174 template<class LSTRING, class TBUF, class TTEXT, class HBUF, class RBUF>	
   175 LOCAL_C void TestAssign(LSTRING*)
   176 {
   177 
   178 	test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4002"));
   179 	
   180 	TBUF des (_TS("123456"));
   181 	LSTRING lStr2(des);
   182 	TTEXT* heap = NULL;
   183 	RBUF buf;
   184 
   185 	test.Next(_L("Assign(const LString_& aLString) method"));
   186 
   187 	LSTRING lStr;
   188 	lStr.Assign(lStr2);
   189 	// the assignment clears lStr2 so the two strings should be unequal
   190 	test(lStr != lStr2);
   191 
   192 	test.Next(_L("Assign(TUint* aHeapCell, TInt aLength, TInt aMaxLength ) method"));
   193 
   194 	heap = (TTEXT*)User::Alloc(24*(TInt)sizeof(TTEXT)); //Allocate 48 bytes for 24 long LString16
   195 	lStr.Assign(heap, 12,24);
   196 	test(lStr.Length() == 12);		
   197 	test(lStr.MaxLength() >= 24);		
   198 
   199 	heap = NULL;
   200 	lStr.Assign(heap, 0,0);
   201 	test(lStr.Length() == 0);		
   202 	test(lStr.MaxLength() == 0);		
   203 	
   204 	test.Next(_L("Assign(TUint* aHeapCell, TInt aMaxLength ) method"));
   205 	
   206 	heap = (TTEXT*)User::Alloc(24*(TInt)sizeof(TTEXT)); //Allocate 48 bytes for 24 long LString16
   207 	lStr.Assign(heap, 24);
   208 	test(lStr.Length() == 0);		
   209 	test(lStr.MaxLength() >= 24);		
   210 
   211 	test.Next(_L("Assign(HBufC_* aHBuf) method"));
   212 
   213 	HBUF* hBuf = HBUF::NewMax(11);
   214 	lStr.Assign(hBuf);			//Create LString as EBufCPtr type
   215 	test(lStr.Length() == 11);
   216 	test(lStr.MaxLength() >= 11); //There could me more allocated memory - see HBufC8::Des()
   217 	
   218 	HBUF* hBuf2 = HBUF::NewMax(5);
   219 	lStr = hBuf2;			//Create LString as EBufCPtr type
   220 	test(lStr.Length() == 5);
   221 	test(lStr.MaxLength() >= 5); //There could me more allocated memory - see HBufC8::Des()
   222 
   223 	test.Next(_L("Assign(const RBuf_& aRBuf) method"));
   224 
   225 	buf.Create(des);
   226 	lStr.Assign(buf);
   227 	
   228 	// the assignment trasnfers buf so the two strings should be equal
   229 	test(lStr == des);
   230 	test(lStr != buf);
   231 	
   232 }
   233 
   234 /**
   235 @SYMTestCaseID SYSLIB-EUSERHL-UT-4003
   236 @SYMTestCaseDesc Tests AppendL methods of LString
   237 @SYMTestPriority High
   238 @SYMTestActions Creates LString objects and uses AppendL methods to append 
   239 				data to the LString.
   240 				Checks that the data is appended as expected.
   241 @SYMTestExpectedResults LString objects are created and data appended as 
   242 				expected with the strings grouwing as necessary to accomodate 
   243 				the new data.
   244 @SYMREQ	10372
   245 */
   246 template<class LSTRING, class TBUF, class TTEXT, class CHAR>
   247 LOCAL_C void TestAppendL(LSTRING*)
   248 {
   249 	test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4003"));
   250 
   251 	LSTRING lStr;
   252 	LSTRING lStr2;
   253 	TBUF des(_TS("123456"));
   254 	TBUF des2(_TS("123456A"));
   255 	
   256 
   257 	test.Next(_L("AppendL(const TDesc_& aDes) method"));
   258 
   259 	lStr.AppendL(des);
   260 	test(lStr == des);
   261 	
   262 	test.Next(_L("AppendL(const TUint16* aBuf, TInt aLength) method"));
   263 
   264 	lStr2 = des;
   265 	lStr.Reset();
   266 	lStr.AppendL(lStr2.PtrZL(),lStr2.Length());
   267 	test(lStr == des);
   268 	
   269 	test.Next(_L("AppendL(TChar aChar) method"));
   270 
   271 	TChar c = 'A';
   272 	lStr = des;
   273 	lStr.AppendL(c);
   274 	test(lStr == des2);
   275 	
   276 	test.Next(_L("ZeroTerminateL() method"));
   277 
   278 	lStr = des;
   279 	lStr.ZeroTerminateL();
   280 	test(lStr == des);
   281 	
   282 	test.Next(_L("AppendL(char/wchar_t* aCharStr) method"));
   283 	
   284 	LSTRING lStr3(_CS("0123456789"));
   285 	test(lStr3.Length() == 10);		
   286 	test(lStr3.MaxLength() >= 10);
   287 	
   288 	LSTRING lStr4(_CS("01234567890"));
   289 	test(lStr4.Length() == 11);		
   290 	test(lStr4.MaxLength() >= 11);
   291 
   292 	lStr3.AppendL(_CS("0"),1);
   293 	test(lStr3 == lStr4);
   294 	
   295 	lStr4.AppendL(_CS("0123456789"),10);
   296 	test(lStr4.Length() == 21);		
   297 	test(lStr4.MaxLength() >= 21);
   298 	
   299 	test.Next(_L("operator + (char/wchar_t* aCharStr) method"));
   300 	
   301 	LSTRING lStr5(_CS("0123456789"));
   302 	test(lStr5.Length() == 10);		
   303 	test(lStr5.MaxLength() >= 10);
   304 	
   305 	LSTRING lStr6(_CS("01234567890"));
   306 	test(lStr6.Length() == 11);		
   307 	test(lStr6.MaxLength() >= 11);
   308 
   309 	lStr5+=_CS("0");
   310 	test(lStr5 == lStr6);
   311 	
   312 	lStr6 +=_CS("0123456789");
   313 	test(lStr6.Length() == 21);		
   314 	test(lStr6.MaxLength() >= 21);
   315 	
   316 }
   317 
   318 /**
   319 Tests the following methods. 
   320  - TInt ReAlloc(TInt aMaxLength);
   321 */
   322 /**
   323 @SYMTestCaseID SYSLIB-EUSERHL-UT-4004 
   324 @SYMTestCaseDesc Tests ReAlloc methods of LString
   325 @SYMTestPriority High
   326 @SYMTestActions Creates LString objects and uses ReAlloc to decrease,
   327 				increase and zero memory. Validates that length and 
   328 				maxlength are changed as expected 
   329 @SYMTestExpectedResults ReAlloc should change the length and maxLength 
   330 				of the LString data as expected
   331 @SYMREQ	10372
   332 */
   333 template<class LSTRING, class TBUF, class TTEXT, class HBUF>	
   334 LOCAL_C void TestReAllocL(LSTRING*)
   335 {
   336 	test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4004"));
   337 
   338 	test.Next(_L("ReAlloc(TInt aMaxLength) method"));
   339 
   340 	TBUF des (_TS("0123456"));
   341 	
   342 	//reallocate EPtr type - decrease memory
   343 	LSTRING lStr(des);
   344 	lStr.SetLengthL(3);
   345 	test(lStr.ReAlloc(3)==KErrNone);					//ReAlloc to EPtr
   346 	test(lStr.MaxLength()>=3);
   347 	test(lStr.Length()==3);
   348 	test(lStr[0] == (TTEXT)('0'));
   349 	test(lStr[2] == (TTEXT)('2'));
   350 
   351 	//reallocate EBufCPtr type - decrease memory
   352 	HBUF* hBuf = HBUF::NewMax(9);
   353 	*hBuf = _TS("012345678");
   354 	lStr.Assign(hBuf);						//Create as EBufCPtr
   355 	lStr.SetLengthL(5);
   356 	test(lStr.ReAlloc(5)==KErrNone);		//ReAlloc to EBufCPtr
   357 	test(lStr.MaxLength()>=5);//There could be more allocated memory - see HBufC8::Des()
   358 	test(lStr.Length()==5);
   359 	test(lStr[0] == (TTEXT)('0'));
   360 	test(lStr[4] == (TTEXT)('4'));
   361 
   362 	//reallocate EBufCPtr type - increase memory
   363 	hBuf = HBUF::NewMax(9);
   364 	*hBuf = _TS("012345678");
   365 	lStr.Assign(hBuf);						//Create as EBufCPtr
   366 	test(lStr.ReAlloc(15)==KErrNone);		//ReAlloc to EBufCPtr
   367 	test(lStr.MaxLength()>=15);//There could be more allocated memory - see HBufC8::Des()
   368 	test(lStr.Length()==9);
   369 	test(lStr[0] == (TTEXT)('0'));
   370 	test(lStr[8] == (TTEXT)('8'));
   371 
   372 	//reallocate EPtr type - to zero-length
   373 	lStr = des;
   374 	lStr.SetLengthL(0);
   375 	test(lStr.ReAlloc(0)==KErrNone);		//ReAlloc to EPtr
   376 	test(lStr.MaxLength()==0);
   377 	test(lStr.Length()==0);
   378 
   379 	//reallocate EBufCPtr type to zero-length
   380 	hBuf = HBUF::NewMax(9);
   381 	*hBuf = _TS("012345678");
   382 	lStr.Assign(hBuf);						//Create as EBufCPtr
   383 	lStr.SetLengthL(0);
   384 	test(lStr.ReAlloc(0)==KErrNone);		//ReAlloc to EPtr
   385 	test(lStr.MaxLength()==0);
   386 	test(lStr.Length()==0);
   387 
   388 	//reallocate from zero-length
   389 	lStr.Reset();						
   390 	test(lStr.ReAlloc(9)==KErrNone);		//ReAlloc to EPtr
   391 	test(lStr.MaxLength() >=9);
   392 	test(lStr.Length()==0);
   393 
   394 	//reallocate from zero-length to zero-length
   395 	lStr.Reset();							
   396 	test(lStr.ReAlloc(0)==KErrNone);		//ReAlloc to EPtr
   397 	test(lStr.Length() == 0);		
   398 	test(lStr.MaxLength() == 0);		
   399 }
   400 
   401 /**
   402 Tests the following methods. 
   403  - TInt ReAllocL(TInt aMaxLength);
   404 */
   405 /**
   406 @SYMTestCaseID SYSLIB-EUSERHL-UT-4005
   407 @SYMTestCaseDesc Tests Leaving variant ReAllocL of LString
   408 @SYMTestPriority High
   409 @SYMTestActions Creates LString objects and uses ReAllocL to 
   410 				increase memory under OOM conditions
   411 				Verifies that ReAllocL leaves with KErrNoMemory
   412 @SYMTestExpectedResults Calls to ReAllocL that increase memory should 
   413 				leave with KErrNoMemory under OOM conditions
   414 @SYMREQ	10372
   415 */
   416 template<class LSTRING, class TBUF, class TTEXT>	
   417 LOCAL_C void TestReAllocLeaving(LSTRING*)
   418 {
   419 
   420 	test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4005"));
   421 	
   422 	test.Next(_L("ReAllocL(TInt aMaxLength) method"));
   423 	TBUF des(_TS("01"));
   424 
   425 	LSTRING lStr(des);
   426 	TRAPD(ret, lStr.ReAllocL(6));	//ReAlloc buffer
   427 	test(KErrNone == ret);
   428 
   429 #if defined(_DEBUG)
   430 	__UHEAP_FAILNEXT(1);
   431 	TRAP(ret, lStr.ReAllocL(100));	//Realloc buffer. This should fail.
   432 	test(KErrNoMemory == ret);
   433 #endif //(_DEBUG)
   434 
   435 	test(lStr.MaxLength() >=6);		//Check LString is the same as before ... 
   436 	test(lStr.Length()==2);			//... ReAlloc that failed.
   437 	test(lStr[0] == (TTEXT)('0'));
   438 	test(lStr[1] == (TTEXT)('1'));
   439 }
   440 
   441 
   442 /**
   443 Tests the following methods. 
   444  - void SwapL(LString& aBuf);
   445  - void SwapL(TDes& aBuf);
   446 */
   447 /**
   448 @SYMTestCaseID SYSLIB-EUSERHL-UT-4006
   449 @SYMTestCaseDesc Tests SwapL methods of LString
   450 @SYMTestPriority High
   451 @SYMTestActions Creates LString objects and uses SwapL to 
   452 				swap string contents.
   453 				Verifies that contents are swapped and memory 
   454 				reallocated where necessary
   455 @SYMTestExpectedResults Calls to SwapL should swap string contents 
   456 				and buffer should be automatically reallocated to fit the new data.
   457 @SYMREQ	10372
   458 */
   459 template<class LSTRING, class TBUF, class TTEXT>
   460 LOCAL_C void TestSwapL(LSTRING*)
   461 {
   462 	test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4006"));
   463 
   464 	LSTRING lStr1, lStr2;
   465 	TBUF des1(_TS("12"));
   466 	TBUF des2 (_TS("345678"));
   467 	TBUF des3 (_TS("12345678"));
   468 	TBUF swap_des = des2;
   469 
   470 	test.Next(_L("Swap(LString_& aLString) method"));
   471 
   472 	// assignment operation; implies deep copying
   473 	lStr1 = des1;
   474 	lStr2 = des2;
   475 
   476 	// swap LStrings with TDes
   477 	// lStr1 should grow to accommodate swap_des
   478 	lStr1.SwapL(swap_des);
   479 	test(lStr1==des2);
   480 	test(swap_des==des1);
   481 
   482 	// swap two LStrings
   483 	// lStr2 should grow to accommodate lStr1
   484 	lStr1 = des3;
   485 	lStr1.SwapL(lStr2);
   486 
   487 	test(lStr1==des2);
   488 	test(lStr2==des3);
   489 
   490 }
   491 
   492 
   493 /**
   494 Test assignemnt operator.
   495 */
   496 /**
   497 @SYMTestCaseID SYSLIB-EUSERHL-UT-4007
   498 @SYMTestCaseDesc Tests Assignment operator for LString
   499 @SYMTestPriority High
   500 @SYMTestActions Creates LString objects and uses assignment to 
   501 				change string contents.
   502 				Verifies that contents are swapped and memory 
   503 				reallocated where necessary
   504 @SYMTestExpectedResults Assignment operator should change string contents
   505 				and buffer should be automatically reallocated to fit the new data.
   506 @SYMREQ	10372
   507 */
   508 template<class LSTRING, class TBUF, class TBUFC, class TTEXT, class CHAR>
   509 LOCAL_C void TestAssignmentOperatorL()
   510 {
   511 	test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4007"));	
   512 
   513 	test.Next(_L("Assignment operator"));
   514 
   515 	TBUF tdes(_TS("Modifiable descriptor"));
   516 	TBUFC tdesc(_TS("Non-modifiable descriptor"));
   517 
   518 	LSTRING lStr(32);
   519 	LSTRING lStr2(32);
   520 	lStr2.CopyL(_TS("Buffer descriptor"), 17);
   521 
   522 	lStr = tdesc;	test(lStr == tdesc);
   523 	lStr = tdes;	test(lStr == tdes);
   524 	lStr = lStr2;	test(lStr == lStr2);
   525 	
   526 	LSTRING lStr3(tdes);
   527 	lStr = lStr3.PtrZL(); test(lStr == tdes);
   528 	
   529 	test.Next(_L("operator=(char/wchar_t* aCharStr) method"));
   530 	LSTRING lStr4(_CS("123456"));
   531 	LSTRING lStr5 = _CS("123456");
   532 	test(lStr4 == lStr5);
   533 	
   534 	LSTRING lStr6;
   535 	lStr6 =  _CS("123456");
   536 	test(lStr4 == lStr6);
   537 	}
   538 
   539 /** 
   540 Test Capacity growth and compression
   541 */
   542 /**
   543 @SYMTestCaseID SYSLIB-EUSERHL-UT-4008
   544 @SYMTestCaseDesc Tests capacity growth and compression for LString
   545 @SYMTestPriority High
   546 @SYMTestActions Creates an LString object then calls SetMaxLength and Compress
   547 				to increase or decrease underlying buffer size.
   548 				Tests that Length and Maxlength are modified as expected 
   549 @SYMTestExpectedResults SetMaxLength and Compress should  resize the underlying
   550 				buffer as expected.
   551 @SYMREQ	10372
   552 */
   553 template<class LSTRING, class TBUF, class TTEXT> 
   554 LOCAL_C void TestCapacityChangesL(LSTRING*)
   555 {
   556 	test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4008"));
   557 	
   558 	test.Next(_L("Test capacity growth and compression on LString"));
   559 
   560 	LSTRING lStr(_TS("0123456"));
   561 	test(lStr.Length()==7);
   562 	test(lStr.MaxLength() >=7);
   563 
   564  	lStr.SetMaxLengthL(10);
   565 	test(lStr.Length()==7);
   566 	test(lStr.MaxLength() >=10);
   567 
   568  	lStr.SetMaxLengthL(6);
   569 	test(lStr.Length()==6);
   570 	test(lStr.MaxLength() >=6);
   571  	
   572  	lStr.SetMaxLengthL(10);
   573 	test(lStr.Length()==6);
   574 	test(lStr.MaxLength() >=10);
   575 	
   576 	//Call the same thing again to check the condition
   577 	//that required length is already set
   578  	lStr.SetMaxLengthL(10);
   579 	test(lStr.Length()==6);
   580 	test(lStr.MaxLength() >=10);
   581 
   582  	lStr.Compress();
   583  	test(lStr.Length()==6);
   584 	test(lStr.MaxLength() >= 6);
   585 	
   586 	//Call the same thing again to check the condition
   587 	//that the string is already compressed
   588  	lStr.Compress();
   589  	test(lStr.Length()==6);
   590 	test(lStr.MaxLength() >= 6);
   591 	
   592 	lStr.ReserveFreeCapacityL(15);
   593  	test(lStr.Length()==6);
   594 	test(lStr.MaxLength() >= 32);
   595 	
   596 	lStr.Reset();
   597  	test(lStr.Length()==0);
   598 	test(lStr.MaxLength() == 0);
   599 
   600 }
   601 
   602 
   603 /** 
   604 Test copying from 16bit to 8bit and vice versa
   605 */
   606 /**
   607 @SYMTestCaseID SYSLIB-EUSERHL-UT-4009
   608 @SYMTestCaseDesc Tests Copying between 8 and 16 bit LString variants
   609 @SYMTestPriority High
   610 @SYMTestActions Creates an LString8 and LString16 object and uses the cross-variant 
   611 				Copy functions to copy data between the variants.
   612 @SYMTestExpectedResults Data is successfully copied between the variants.
   613 @SYMREQ	10372
   614 */
   615 LOCAL_C void TestCrossCopyingL()
   616 	{
   617 	
   618 	test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4009"));
   619 	
   620 	test.Next(_L("Test cross copying on LString"));
   621 	
   622 	LString8 lStr8("0123");
   623 	LString16 lStr16(L"01234567");
   624 
   625 	LString8 targetStr8;
   626 	LString16 targetStr16;
   627 	
   628 	targetStr8.CopyL(lStr16);
   629 	targetStr16.CopyL(lStr8);
   630 	}	
   631 
   632 
   633 /** 
   634 Test creating an LString from a stream
   635 */
   636 /**
   637 @SYMTestCaseID SYSLIB-EUSERHL-UT-4010
   638 @SYMTestCaseDesc Tests Creating an LString from a stream
   639 @SYMTestPriority High
   640 @SYMTestActions Creates a in-memory stream and writes some data to it.
   641 				Creates an LString from a readstream and verifies that the
   642 				contents of the LString match the data written to the stream.
   643 @SYMTestExpectedResults The LString contents should match the data written 
   644 				to the stream.
   645 @SYMREQ	10372
   646 */
   647 template<class LSTRING, class TTEXT>
   648 LOCAL_C void TestReadFromStreamL()
   649 	{
   650 	test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4010"));
   651 	
   652 	test.Next(_L("Test creating LString from a stream"));
   653 	
   654 	RFs fs;
   655 	fs.Connect() OR_LEAVE;
   656 	
   657 	LSTRING outString = _TS("This is a test string written to a stream");
   658 	
   659 	//Create a buffer to contain the stream
   660 	CBufFlat* buf = CBufFlat::NewL(outString.MaxLength());
   661 	
   662 	//Create a write stream
   663 	RBufWriteStream outStream;
   664 	outStream.Open(*buf);
   665 	
   666 	//write some data to the stream
   667 	outStream << outString;
   668 	outStream.CommitL();
   669 	
   670 	//Open a readstream
   671 	RBufReadStream inStream;	
   672 	inStream.Open(*buf);
   673 	
   674 	//Create an LString from the stream
   675 	LSTRING inString;
   676 	inString.CreateL(inStream,outString.Length());	
   677 	test(inString == outString);
   678 	
   679 	delete buf;	
   680 	}
   681 
   682 /** 
   683 Test support for [wide]character strings.
   684 APIs that modify data.
   685 */
   686 /**
   687 @SYMTestCaseID BASESRVCS-EUSERHL-UT-4068
   688 @SYMTestCaseDesc Test the APIs provided to support wchar_t and char strings
   689 @SYMTestPriority High
   690 @SYMTestActions 1)Construct LString object from the supplied null terminated 
   691 				character string.
   692 				2)Assign new string to the constructed LString object
   693 				3)Appends data onto the end of this LString object's data.
   694 				The length of this descriptor is incremented to reflect the new content.
   695 				4)Copy new data into the LString object, replacing any existing
   696 				data, and expanding its heap buffer to accommodate if necessary.
   697 				5)Insert contents into the LString
   698 				6)Replace data to the end of the LString object and justify it.
   699 				7)Append data of specified length, to the end of the LString object.
   700 				8)Justify data, to the end of the LString object.
   701 				9)Appends data onto the end of this descriptor's data and justifies it.
   702 @SYMTestExpectedResults The LString contents should match the data expected after the operation.
   703 @SYMREQ	10372
   704 */
   705 
   706 template<class LSTRING, class TTEXT, class CHAR>
   707 LOCAL_C void TestCharacterStringSupport_Modifiers(LSTRING*)
   708 	{
   709 	test.Next (_L ("@SYMTestCaseID:BASESRVCS-EUSERHL-UT-4068"));
   710 	
   711 	// 1. test Constructor 
   712 	// Constructs LString object from the supplied null terminated 
   713 	// character string
   714 	test.Next(_L("LString_(char/wchar_t * aCharStr) constructor"));
   715 	LSTRING lStr(_CS("0123456789"));
   716 	test(lStr.Length() == 10);		
   717 	test(lStr.MaxLength() >= 10);
   718 	test(lStr.Compare(_CS("0123456789")) == 0 );
   719 	// try strings ending with 0
   720 	LSTRING lStr1(_CS("01234567890"));
   721 	test(lStr1.Length() == 11);		
   722 	test(lStr1.MaxLength() >= 11);
   723 	test(lStr1.Compare(_CS("01234567890")) == 0 );
   724 	
   725 	// 2. test '=' operator
   726 	LSTRING lTestStr;
   727 	// Assign new string to the constructed LString object
   728 	test.Next(_L("LString_ operator '=' "));
   729 	lTestStr = _CS("Try a New String");
   730 	test(lTestStr.Compare(_CS("Try a New String")) == 0 );
   731 	test(lTestStr.Length() == 16);		
   732 	test(lTestStr.MaxLength() >= 16);
   733 	
   734 	// 3. test '+=' operator
   735 	// Appends data onto the end of this LString object's data.
   736 	// The length of this descriptor is incremented to reflect the new content.
   737 	test.Next(_L("LString_ operator '+=' "));
   738 	lTestStr += _CS("!!!");
   739 	test(lTestStr.Compare(_CS("Try a New String!!!")) == 0 );
   740 	test(lTestStr.Length() == 19);		
   741 	test(lTestStr.MaxLength() >= 19);
   742 		
   743 	// 4.Test "Copy()" Variants
   744 	LSTRING lTestStr1;
   745 	LSTRING lTestStr2;
   746 	// Copy new data into the LString object, replacing any existing
   747 	// data, and expanding its heap buffer to accommodate if necessary.
   748 	test.Next(_L("LString_ CopyL "));
   749 	lTestStr1.CopyL(_TS("Try a New String"));
   750 	test(lTestStr1.Compare(_TS("Try a New String")) == 0 );
   751 	test(lTestStr1.Length() == 16);		
   752 	test(lTestStr1.MaxLength() >= 16);
   753 	
   754 	// Copy folded(normalized) content
   755 	test.Next(_L("LString_ CopyFL "));
   756 	lTestStr1.CopyFL(_CS("Some RaNDom STRING"));
   757 	lTestStr2.CopyFL(_CS("SOME RaNDom string"));
   758 	test(lTestStr1.Compare(lTestStr2) == 0);
   759 	
   760 	// Copy contents in Lower case
   761 	test.Next(_L("LString_ CopyLCL "));
   762 	lTestStr1.CopyLCL(_CS("SOME STRING IN UPPER CASE"));
   763 	test(lTestStr1 == _CS("some string in upper case"));
   764 
   765 	// Copy contents in Upper case
   766 	test.Next(_L("LString_ CopyUCL "));
   767 	lTestStr1.CopyUCL(_CS("some string in lower case"));
   768 	test(lTestStr1 == _CS("SOME STRING IN LOWER CASE"));
   769 		
   770 	// Copy Capitalized contents
   771 	test.Next(_L("LString_ CopyCPL "));
   772 	lTestStr1.CopyCPL(_CS("some string in lower case"));
   773 	test(lTestStr1 == _CS("Some string in lower case"));
   774 			
   775 	// 5. Test Insert()
   776 	LSTRING lTestStr3;
   777 	// Insert contents into a string
   778 	test.Next(_L("LString_ InsertL "));
   779 	lTestStr3 = _CS("Some Content Can Be Into This String");
   780 	lTestStr3.InsertL(20,_CS("Inserted "));
   781 	test(lTestStr3 == _CS("Some Content Can Be Inserted Into This String"));
   782 	
   783 	// 6. Test Replace()
   784 	LSTRING lTestStr4;
   785 	// Replace contents form the string
   786 	test.Next(_L("LString_ ReplaceL "));
   787 	lTestStr4 = _CS("Some Content Can Be Decalper");
   788 	lTestStr4.ReplaceL(20,8,_CS("Replaced"));
   789 	test(lTestStr4 == _CS("Some Content Can Be Replaced"));
   790 	
   791 	// 7. Test Append()
   792 	LSTRING lTestStr5;
   793 	//Append data of specified length, to the end of the LString object.	
   794 	test.Next(_L("LString_ AppendL(src,length)"));
   795 	lTestStr5.CopyL( _CS("Try appending "));
   796 	lTestStr5.AppendL(_CS("Try appending some more"),3);
   797 	test(lTestStr5 == _CS("Try appending Try"));
   798 		
   799 	//Append data , to the end of the LString object.	
   800 	test.Next(_L("LString_ AppendL(src)"));
   801 	lTestStr5.CopyL( _CS("Try appending "));
   802 	lTestStr5.AppendL(_CS("Try appending some more"));
   803 	test(lTestStr5 == _CS("Try appending Try appending some more"));
   804 		
   805 	// 8. Test Justify()
   806 	LSTRING lTestStr6;
   807 	//Copy data into this descriptor and justifies it, replacing any existing data	
   808 	test.Next(_L("LString_ JustifyL "));
   809 	lTestStr6.CopyL(_CS("Justified"));
   810 	lTestStr6.JustifyL(_CS("Just"),9,ERight,*(_TS("x")));
   811 	test(lTestStr6 == _CS("xxxxxJust"));
   812 	
   813 	// 9. Test AppendJustify variants
   814 	LSTRING lTestStr7;
   815 	// Append data to the end of the LString object and justify it.
   816 	test.Next(_L("LString_ AppendJustifyL(const char*,TInt aLength,TInt aWidth,TAlign anAlignment,TChar aFill)"));
   817 	lTestStr7.CopyL(_CS("One "));
   818 	lTestStr7.AppendJustifyL(_CS("Two "),KDefaultJustifyWidth,ERight,*(_TS("x")));
   819 	test(lTestStr7 == _TS("One Two "));
   820 	
   821 	lTestStr7.CopyL(_CS("One "));
   822 	lTestStr7.AppendJustifyL(_CS("Two Three"),3,7,ERight,*(_TS("x")));
   823 	test(lTestStr7 == _CS("One xxxxTwo") );
   824 	
   825 	// Append data to the end of the LString object and justify it.
   826 	test.Next(_L("LString_ AppendJustifyL(const char* aCharStr,TInt aWidth,TAlign anAlignment,TChar aFill)"));
   827 	lTestStr7.CopyL(_CS("One "));
   828 	lTestStr7.AppendJustifyL(_CS("Two Three"),KDefaultJustifyWidth,ERight,*(_TS("x")));
   829 	test(lTestStr7 == _TS("One Two Three"));
   830 	
   831 	lTestStr7.CopyL(_CS("One "));
   832 	lTestStr7.AppendJustifyL(_CS("Two Three"),13,ERight,*(_TS("x")));
   833 	test(lTestStr7 == _CS("One xxxxTwo Three") );
   834 	}
   835 
   836 /** 
   837 Test support for [wide]character strings.
   838 APIs that do not modify any data.
   839 */
   840 /**
   841 @SYMTestCaseID BASESRVCS-EUSERHL-UT-4069
   842 @SYMTestCaseDesc Test the APIs provided to support wchar_t and char strings
   843 @SYMTestPriority High
   844 @SYMTestActions 1)Determine whether this descriptor's data is equal to the specified string's data.
   845 				2)Determine whether this descriptor's data is less than the specified string's data.
   846 				3)Determine whether this descriptor's data is less than or equal to the specified string's data.
   847 				4)Determine whether this descriptor's data is greater than the specified string's data.
   848 				5)Determine whether this descriptor's data is greater than or equal to the specified string's data.
   849 				6)Determine whether this descriptor's data is not equal to the specified string's data.
   850 				7)Compare this descriptor's data with the specified string's data.
   851 				8)Search this descriptor's data for a match with the match pattern supplied in the specified string's
   852 				9)Searches for the first occurrence of the specified data sequence within this descriptor
   853 @SYMTestExpectedResults The operation/comparision  must result in the desired output
   854 @SYMREQ	10372
   855 */
   856 template<class LSTRING,class TTEXT, class CHAR>
   857 LOCAL_C void TestCharacterStringSupport_NonModifiers(LSTRING*)
   858 	{
   859 	test.Next (_L ("@SYMTestCaseID:BASESRVCS-EUSERHL-UT-4069"));
   860 	
   861 	// 1.test '==' operator
   862 	LSTRING lTestStr1;
   863 	lTestStr1.CopyL(_CS("Are they equal?? "));
   864 	test(lTestStr1 == _CS("Are they equal?? "));
   865 	lTestStr1.CopyL(_CS("12345670"));
   866 	test(lTestStr1 == _CS("12345670"));
   867 	
   868 	// 2.test "<" operator
   869 	LSTRING lTestStr2;
   870 	lTestStr2.CopyL(_CS("ABCDEFGH"));
   871 	test(lTestStr2 < _CS("abcdefgh"));
   872 	lTestStr2.CopyL(_CS(" Is this is smaller"));
   873 	test(lTestStr2 < _CS("No, larger of the string is greater than the smaller one"));
   874 	
   875 	// 3.test "<=" operator
   876 	LSTRING lTestStr3;
   877 	lTestStr3.CopyL(_CS("ABCDEFGH"));
   878 	test(lTestStr3 <= _CS("abcdefgh"));
   879 	lTestStr3.CopyL(_CS("equals"));
   880 	test(lTestStr3 <= _CS("equals"));
   881 	
   882 	// 4.test ">" operator
   883 	LSTRING lTestStr4;
   884 	lTestStr4.CopyL(_CS("abcdefgh"));
   885 	test(lTestStr4 > _CS("ABCDEFGH"));
   886 	lTestStr4.CopyL(_CS("No, larger of the string is greater than the smaller one"));
   887 	test(lTestStr4 > _CS("Is this smaller??"));
   888 	
   889 	// 5.test ">=" operator
   890 	LSTRING lTestStr5;
   891 	lTestStr5.CopyL(_CS("abcdefgh"));
   892 	test(lTestStr5 >= _CS("ABCDEFGH"));
   893 	lTestStr5.CopyL(_CS("equals"));
   894 	test(lTestStr5 >= _CS("equals"));
   895 	
   896 	// 6.test "!="
   897 	LSTRING lTestStr6;
   898 	lTestStr6.CopyL(_CS("abcdefgh"));
   899 	test(lTestStr6 != _CS("ABCDEFGH"));
   900 	lTestStr6.CopyL(_CS("equals"));
   901 	test(!(lTestStr6 != _CS("equals")));
   902 	
   903 	// 7.test Compare variants
   904 	LSTRING lTestStr7;
   905 	// Compare strict
   906 	lTestStr7.CopyL(_CS("abcdefgh"));
   907 	test(lTestStr7.Compare(_CS("ABCDEFGH")) > 0);	
   908 	lTestStr7.CopyL(_CS("ABCDEFGH"));
   909 	test(lTestStr7.Compare(_CS("abcdefgh")) < 0);
   910 	lTestStr5.CopyL(_CS("equals"));
   911 	test(lTestStr5.Compare( _CS("equals")) == 0);
   912 	// Compare foalded
   913 	lTestStr7.CopyL(_CS("abcdefgh"));
   914 	test(lTestStr7.CompareF(_CS("ABcDeFgH")) == 0);	
   915 	
   916 	// 8.test Match variants
   917 	LSTRING lTestStr8;
   918 	// Match strict
   919 	lTestStr8.CopyL(_CS("abcdefghijklmnopqrstuvwxyz"));
   920 	test(lTestStr8.Match(_CS("*ijk*"))== 8);
   921 	test(lTestStr8.Match(_CS("*i?k*"))== 8);
   922 	test(lTestStr8.Match(_CS("ijk*"))== KErrNotFound);
   923 	// Match Folded
   924 	test(lTestStr8.MatchF(_CS("*IjK*"))== 8);
   925 	test(lTestStr8.MatchF(_CS("*I?k*"))== 8);
   926 	test(lTestStr8.MatchF(_CS("ijK*"))== KErrNotFound);
   927 	
   928 	// 9.test Find variants
   929 	LSTRING lTestStr9;
   930 	// Find strict
   931 	lTestStr9.CopyL(_CS("abcdefghijklmnopqrstuvwxyz"));
   932 	test(lTestStr9.Find(_CS("abcde")) == 0);
   933 	test(lTestStr9.Find(_CS("cde")) == 2);
   934 	test(lTestStr9.Find(_CS("efg22")) == KErrNotFound);
   935 	test(lTestStr9.Find(_CS("efg22"),3) == 4);
   936 	// Find folded
   937 	test(lTestStr9.FindF(_CS("aBcDe")) == 0);
   938 	test(lTestStr9.FindF(_CS("cDe")) == 2);
   939 	test(lTestStr9.FindF(_CS("eFg22")) == KErrNotFound);
   940 	test(lTestStr9.FindF(_CS("efG22"),3) == 4);
   941 	}
   942 
   943 void RunTestsL()
   944 	{
   945 	
   946 	LString8* r8=0;
   947 	LString16* r16=0;
   948 
   949 	// LString8 Tests
   950 	TestConstructors<LString8,TBuf8<11>,TText8, HBufC8, char>(r8);
   951 	TestSwapL<LString8,TBuf8<11>,TText8>(r8);
   952 	TestAssign<LString8,TBuf8<11>,TText8,HBufC8,RBuf8>(r8);
   953 	TestAppendL<LString8,TBuf8<11>,TText8, char>(r8);
   954 	TestReAllocL<LString8,TBuf8<11>,TText8,HBufC8>(r8);
   955 	TestReAllocLeaving<LString8,TBuf8<11>,TText8>(r8);
   956 	TestAssignmentOperatorL<LString8,TBuf8<32>,TBufC8<32>,TText8, char>();
   957 	TestCapacityChangesL<LString8,TBuf8<11>,TText8>(r8);
   958 	TestReadFromStreamL<LString8,TText8>();
   959 	TestCharacterStringSupport_Modifiers<LString8,TText8, char>(r8);
   960 	TestCharacterStringSupport_NonModifiers<LString8,TText8, char>(r8);
   961 	
   962 	// LString16 Tests
   963 	TestConstructors<LString16,TBuf16<11>,TText16,HBufC16, wchar_t>(r16);
   964 	TestSwapL<LString16,TBuf16<11>,TText16>(r16);
   965 	TestAssign<LString16,TBuf16<11>,TText16,HBufC16,RBuf16>(r16);
   966 	TestAppendL<LString16,TBuf16<11>, TText16,wchar_t>(r16);
   967 	TestReAllocL<LString16,TBuf16<11>,TText16,HBufC16>(r16);
   968 	TestReAllocLeaving<LString16,TBuf16<11>,TText16>(r16);
   969 	TestAssignmentOperatorL<LString16,TBuf16<32>,TBufC16<32>,TText16,wchar_t>();
   970 	TestCapacityChangesL<LString16,TBuf16<11>,TText16>(r16);
   971 	TestReadFromStreamL<LString16,TText16>();
   972 	TestCharacterStringSupport_Modifiers<LString16,TText16, wchar_t>(r16);
   973 	TestCharacterStringSupport_NonModifiers<LString16,TText16, wchar_t>(r16);
   974 	TestCrossCopyingL();
   975 	}
   976 
   977 
   978 
   979 
   980 /** 
   981 Test arithmetric operator overloads
   982 */
   983 
   984 
   985 GLDEF_C TInt E32Main()
   986     {
   987 
   988 	CTrapCleanup* trapHandler=CTrapCleanup::New();
   989 	test(trapHandler!=NULL);
   990 
   991 	test.Title();
   992 	test.Start(_L("Testing LString8 & LString16 classes"));
   993 
   994 	__UHEAP_MARK;
   995 
   996 	TRAPD(err, RunTestsL());
   997 	
   998 	__UHEAP_MARKEND;
   999 
  1000 	test.End();
  1001 
  1002 	delete trapHandler;
  1003 	return(err);
  1004     }
  1005