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: // Overview: sl@0: // Test methods of the LString16, LString8, LString template class. sl@0: // The test cases below are template based allowing the same code to sl@0: // be used for testing both LString16 and LString8 classes. sl@0: // Appropriate LString and buffer types are passed as the template sl@0: // parameters in the call to each test case. sl@0: // API Information: sl@0: // LString16, LString8, LString. sl@0: // Details: sl@0: // For LString8, LString16 and LString objects: sl@0: // - Test the Create and CreateMax methods by verifying the return value of sl@0: // KErrNone, the initial length and max length. Perform basic write and read sl@0: // operations and verify the results. sl@0: // - Test the CreateL and CreateMaxL methods by verifying the return value of sl@0: // KErrNone. Also force a heap error and verify return value of KErrNoMemory. sl@0: // - Test the Create(const TDesC_& aDesc) and Create(const TDesCX_ aDesc, sl@0: // TInt aMaxLength) methods by verifying the return value of KErrNone. Verify sl@0: // initial length, max length and initialisation. sl@0: // - Test the CreateL(const TDesC_& aDesc) and CreateMaxL(const TDesCX_ aDesc, sl@0: // TInt aMaxLength) methods by verifying the return value of KErrNone. Also sl@0: // force a heap error and verify return value of KErrNoMemory. sl@0: // - Test the Swap method by creating two initialised objects, calling Swap sl@0: // and confirming the results as expected. sl@0: // - Test the Assign method by performing an assign from a variety of sources sl@0: // and verifying the results are as expected. sl@0: // - Test the ReAlloc method in a variety of scenarios that decrease memory, sl@0: // increase memory and zero-length memory. Verify that the results are as sl@0: // expected. sl@0: // - Test the ReAllocL by verifying the return value of KErrNone. Also force sl@0: // a heap error and verify return value of KErrNoMemory. Verify that the sl@0: // object is the same as before the failed ReAllocL call. sl@0: // - Test the CleanupClosePushL method via CleanupStack::PopAndDestroy(). sl@0: // - Force the CleanupClosePushL to leave to check cleanup of LString. sl@0: // Platforms/Drives/Compatibility: sl@0: // All sl@0: // Assumptions/Requirement/Pre-requisites: sl@0: // Failures and causes: sl@0: // Base Port information: sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: LOCAL_D RTest test(_L("T_LSTRING")); sl@0: sl@0: /* This macro generates a TTEXT* from the string passed in. sl@0: * The TTEXT type is a template parameter defined for the calling function sl@0: * and an appropriate 8 or 16 bit string variant is created by this macro sl@0: */ sl@0: #undef _TS sl@0: #define _TS(a) ((const TTEXT*)RTest::String(sizeof(TTEXT),(TText8*)a,(TText16*)L ## a)) sl@0: sl@0: /* This macro generates a CHAR* from the string passed in. sl@0: * The CHAR type is a template parameter defined for the calling function sl@0: * and an appropriate 8(char)or 16(wchar_t) bit string variant is created by this macro sl@0: */ sl@0: #undef _CS sl@0: #define _CS(a) ((const CHAR*)RTest::String(sizeof(TTEXT),(TText8*)a,(TText16*)L ## a)) sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EUSERHL-UT-4001 sl@0: @SYMTestCaseDesc Tests constructors of LString classes sl@0: @SYMTestPriority High sl@0: @SYMTestActions Creates LString objects using the different sl@0: constructors. sl@0: @SYMTestExpectedResults All constructors create object object as expected sl@0: @SYMREQ 10372 sl@0: */ sl@0: template sl@0: LOCAL_C void TestConstructors(LSTRING*) sl@0: { sl@0: sl@0: test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4001")); sl@0: sl@0: test.Next(_L("LString_(const TDesC& aDes) constructor")); sl@0: const TBUF des (_TS("123456")); sl@0: const LSTRING lStr(des); sl@0: test(lStr.Length() == 6); sl@0: test(lStr.MaxLength() >= 6); sl@0: sl@0: test.Next(_L("LString_(const LString16& aDes) constructor")); sl@0: LSTRING lStr1(lStr); sl@0: test(lStr1.Length() == 6); sl@0: test(lStr1.MaxLength() >= 6); sl@0: sl@0: test.Next(_L("LString_(const TUInt16* aString) constructor")); sl@0: LSTRING lStr2(lStr1.PtrZL()); sl@0: test(lStr2.Length() == 6); sl@0: test(lStr2.MaxLength() >= 6); sl@0: sl@0: test.Next(_L("LString_(HBufC_* aHBuf) constructor")); sl@0: sl@0: HBUF* hBuf = HBUF::NewMax(12); //Create LString as EBufCPtr sl@0: LSTRING lStr3(hBuf); sl@0: test(lStr3.Length() == 12); sl@0: test(lStr3.MaxLength() >= 12); sl@0: sl@0: hBuf = HBUF::NewMax(0); sl@0: LSTRING lStr4(hBuf); //The length of aHBuf is zero sl@0: test(lStr4.Length() == 0); sl@0: sl@0: hBuf = NULL; //aHBuf is NULL sl@0: LSTRING lStr5((HBUF*)hBuf); sl@0: test(lStr5.Length() == 0); sl@0: test(lStr5.MaxLength() == 0); sl@0: sl@0: test.Next(_L("LString_(TUInt16* aHeapCell, TInt aLength, TInt aMaxLength) constructor")); sl@0: TTEXT* heap = NULL; sl@0: sl@0: heap = (TTEXT*)User::Alloc(24*(TInt)sizeof(TTEXT)); //Allocate 48 bytes for 24 long LString16 sl@0: LSTRING lStr6(heap, 12,24); sl@0: test(lStr6.Length() == 12); sl@0: test(lStr6.MaxLength() >= 24); sl@0: sl@0: test.Next(_L("LString_(TUint* aHeapCell, TInt aMaxLength ) method")); sl@0: sl@0: heap = (TTEXT*)User::Alloc(24*(TInt)sizeof(TTEXT)); //Allocate 48 bytes for 24 long LString16 sl@0: LSTRING lStr7(heap, 24); sl@0: test(lStr7.Length() == 0); sl@0: test(lStr7.MaxLength() >= 24); sl@0: sl@0: test.Next(_L("LString_(char/wchar_t * aCharStr) constructor")); sl@0: sl@0: LSTRING lStr8(_CS("0123456789")); sl@0: test(lStr8.Length() == 10); sl@0: test(lStr8.MaxLength() >= 10); sl@0: sl@0: LSTRING lStr9(_CS("01234567890")); sl@0: test(lStr9.Length() == 11); sl@0: test(lStr9.MaxLength() >= 11); sl@0: } sl@0: sl@0: sl@0: sl@0: /** sl@0: Tests the following methods: sl@0: - TInt Assign(const LString_& lStr); sl@0: - TInt Assign(TUint* aHeapCell, TInt aMaxLength); sl@0: - TInt Assign(TUint* aHeapCell, TInt aLength, TInt aMaxLength); sl@0: - TInt Assign(HBufC& aHBuf); sl@0: - LString(HBufC_&) constructor. sl@0: */ sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EUSERHL-UT-4002 sl@0: @SYMTestCaseDesc Tests Assign methods of LString sl@0: @SYMTestPriority High sl@0: @SYMTestActions Creates LString objects and assigns data to the LString using sl@0: the LString::Assign overloads. sl@0: Checks that the data is assigned as expected. sl@0: Creates LString objects from HBufC* and verifies that object sl@0: is created as expected sl@0: @SYMTestExpectedResults LString objects are created and assigned as expected sl@0: @SYMREQ 10372 sl@0: */ sl@0: template sl@0: LOCAL_C void TestAssign(LSTRING*) sl@0: { sl@0: sl@0: test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4002")); sl@0: sl@0: TBUF des (_TS("123456")); sl@0: LSTRING lStr2(des); sl@0: TTEXT* heap = NULL; sl@0: RBUF buf; sl@0: sl@0: test.Next(_L("Assign(const LString_& aLString) method")); sl@0: sl@0: LSTRING lStr; sl@0: lStr.Assign(lStr2); sl@0: // the assignment clears lStr2 so the two strings should be unequal sl@0: test(lStr != lStr2); sl@0: sl@0: test.Next(_L("Assign(TUint* aHeapCell, TInt aLength, TInt aMaxLength ) method")); sl@0: sl@0: heap = (TTEXT*)User::Alloc(24*(TInt)sizeof(TTEXT)); //Allocate 48 bytes for 24 long LString16 sl@0: lStr.Assign(heap, 12,24); sl@0: test(lStr.Length() == 12); sl@0: test(lStr.MaxLength() >= 24); sl@0: sl@0: heap = NULL; sl@0: lStr.Assign(heap, 0,0); sl@0: test(lStr.Length() == 0); sl@0: test(lStr.MaxLength() == 0); sl@0: sl@0: test.Next(_L("Assign(TUint* aHeapCell, TInt aMaxLength ) method")); sl@0: sl@0: heap = (TTEXT*)User::Alloc(24*(TInt)sizeof(TTEXT)); //Allocate 48 bytes for 24 long LString16 sl@0: lStr.Assign(heap, 24); sl@0: test(lStr.Length() == 0); sl@0: test(lStr.MaxLength() >= 24); sl@0: sl@0: test.Next(_L("Assign(HBufC_* aHBuf) method")); sl@0: sl@0: HBUF* hBuf = HBUF::NewMax(11); sl@0: lStr.Assign(hBuf); //Create LString as EBufCPtr type sl@0: test(lStr.Length() == 11); sl@0: test(lStr.MaxLength() >= 11); //There could me more allocated memory - see HBufC8::Des() sl@0: sl@0: HBUF* hBuf2 = HBUF::NewMax(5); sl@0: lStr = hBuf2; //Create LString as EBufCPtr type sl@0: test(lStr.Length() == 5); sl@0: test(lStr.MaxLength() >= 5); //There could me more allocated memory - see HBufC8::Des() sl@0: sl@0: test.Next(_L("Assign(const RBuf_& aRBuf) method")); sl@0: sl@0: buf.Create(des); sl@0: lStr.Assign(buf); sl@0: sl@0: // the assignment trasnfers buf so the two strings should be equal sl@0: test(lStr == des); sl@0: test(lStr != buf); sl@0: sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EUSERHL-UT-4003 sl@0: @SYMTestCaseDesc Tests AppendL methods of LString sl@0: @SYMTestPriority High sl@0: @SYMTestActions Creates LString objects and uses AppendL methods to append sl@0: data to the LString. sl@0: Checks that the data is appended as expected. sl@0: @SYMTestExpectedResults LString objects are created and data appended as sl@0: expected with the strings grouwing as necessary to accomodate sl@0: the new data. sl@0: @SYMREQ 10372 sl@0: */ sl@0: template sl@0: LOCAL_C void TestAppendL(LSTRING*) sl@0: { sl@0: test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4003")); sl@0: sl@0: LSTRING lStr; sl@0: LSTRING lStr2; sl@0: TBUF des(_TS("123456")); sl@0: TBUF des2(_TS("123456A")); sl@0: sl@0: sl@0: test.Next(_L("AppendL(const TDesc_& aDes) method")); sl@0: sl@0: lStr.AppendL(des); sl@0: test(lStr == des); sl@0: sl@0: test.Next(_L("AppendL(const TUint16* aBuf, TInt aLength) method")); sl@0: sl@0: lStr2 = des; sl@0: lStr.Reset(); sl@0: lStr.AppendL(lStr2.PtrZL(),lStr2.Length()); sl@0: test(lStr == des); sl@0: sl@0: test.Next(_L("AppendL(TChar aChar) method")); sl@0: sl@0: TChar c = 'A'; sl@0: lStr = des; sl@0: lStr.AppendL(c); sl@0: test(lStr == des2); sl@0: sl@0: test.Next(_L("ZeroTerminateL() method")); sl@0: sl@0: lStr = des; sl@0: lStr.ZeroTerminateL(); sl@0: test(lStr == des); sl@0: sl@0: test.Next(_L("AppendL(char/wchar_t* aCharStr) method")); sl@0: sl@0: LSTRING lStr3(_CS("0123456789")); sl@0: test(lStr3.Length() == 10); sl@0: test(lStr3.MaxLength() >= 10); sl@0: sl@0: LSTRING lStr4(_CS("01234567890")); sl@0: test(lStr4.Length() == 11); sl@0: test(lStr4.MaxLength() >= 11); sl@0: sl@0: lStr3.AppendL(_CS("0"),1); sl@0: test(lStr3 == lStr4); sl@0: sl@0: lStr4.AppendL(_CS("0123456789"),10); sl@0: test(lStr4.Length() == 21); sl@0: test(lStr4.MaxLength() >= 21); sl@0: sl@0: test.Next(_L("operator + (char/wchar_t* aCharStr) method")); sl@0: sl@0: LSTRING lStr5(_CS("0123456789")); sl@0: test(lStr5.Length() == 10); sl@0: test(lStr5.MaxLength() >= 10); sl@0: sl@0: LSTRING lStr6(_CS("01234567890")); sl@0: test(lStr6.Length() == 11); sl@0: test(lStr6.MaxLength() >= 11); sl@0: sl@0: lStr5+=_CS("0"); sl@0: test(lStr5 == lStr6); sl@0: sl@0: lStr6 +=_CS("0123456789"); sl@0: test(lStr6.Length() == 21); sl@0: test(lStr6.MaxLength() >= 21); sl@0: sl@0: } sl@0: sl@0: /** sl@0: Tests the following methods. sl@0: - TInt ReAlloc(TInt aMaxLength); sl@0: */ sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EUSERHL-UT-4004 sl@0: @SYMTestCaseDesc Tests ReAlloc methods of LString sl@0: @SYMTestPriority High sl@0: @SYMTestActions Creates LString objects and uses ReAlloc to decrease, sl@0: increase and zero memory. Validates that length and sl@0: maxlength are changed as expected sl@0: @SYMTestExpectedResults ReAlloc should change the length and maxLength sl@0: of the LString data as expected sl@0: @SYMREQ 10372 sl@0: */ sl@0: template sl@0: LOCAL_C void TestReAllocL(LSTRING*) sl@0: { sl@0: test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4004")); sl@0: sl@0: test.Next(_L("ReAlloc(TInt aMaxLength) method")); sl@0: sl@0: TBUF des (_TS("0123456")); sl@0: sl@0: //reallocate EPtr type - decrease memory sl@0: LSTRING lStr(des); sl@0: lStr.SetLengthL(3); sl@0: test(lStr.ReAlloc(3)==KErrNone); //ReAlloc to EPtr sl@0: test(lStr.MaxLength()>=3); sl@0: test(lStr.Length()==3); sl@0: test(lStr[0] == (TTEXT)('0')); sl@0: test(lStr[2] == (TTEXT)('2')); sl@0: sl@0: //reallocate EBufCPtr type - decrease memory sl@0: HBUF* hBuf = HBUF::NewMax(9); sl@0: *hBuf = _TS("012345678"); sl@0: lStr.Assign(hBuf); //Create as EBufCPtr sl@0: lStr.SetLengthL(5); sl@0: test(lStr.ReAlloc(5)==KErrNone); //ReAlloc to EBufCPtr sl@0: test(lStr.MaxLength()>=5);//There could be more allocated memory - see HBufC8::Des() sl@0: test(lStr.Length()==5); sl@0: test(lStr[0] == (TTEXT)('0')); sl@0: test(lStr[4] == (TTEXT)('4')); sl@0: sl@0: //reallocate EBufCPtr type - increase memory sl@0: hBuf = HBUF::NewMax(9); sl@0: *hBuf = _TS("012345678"); sl@0: lStr.Assign(hBuf); //Create as EBufCPtr sl@0: test(lStr.ReAlloc(15)==KErrNone); //ReAlloc to EBufCPtr sl@0: test(lStr.MaxLength()>=15);//There could be more allocated memory - see HBufC8::Des() sl@0: test(lStr.Length()==9); sl@0: test(lStr[0] == (TTEXT)('0')); sl@0: test(lStr[8] == (TTEXT)('8')); sl@0: sl@0: //reallocate EPtr type - to zero-length sl@0: lStr = des; sl@0: lStr.SetLengthL(0); sl@0: test(lStr.ReAlloc(0)==KErrNone); //ReAlloc to EPtr sl@0: test(lStr.MaxLength()==0); sl@0: test(lStr.Length()==0); sl@0: sl@0: //reallocate EBufCPtr type to zero-length sl@0: hBuf = HBUF::NewMax(9); sl@0: *hBuf = _TS("012345678"); sl@0: lStr.Assign(hBuf); //Create as EBufCPtr sl@0: lStr.SetLengthL(0); sl@0: test(lStr.ReAlloc(0)==KErrNone); //ReAlloc to EPtr sl@0: test(lStr.MaxLength()==0); sl@0: test(lStr.Length()==0); sl@0: sl@0: //reallocate from zero-length sl@0: lStr.Reset(); sl@0: test(lStr.ReAlloc(9)==KErrNone); //ReAlloc to EPtr sl@0: test(lStr.MaxLength() >=9); sl@0: test(lStr.Length()==0); sl@0: sl@0: //reallocate from zero-length to zero-length sl@0: lStr.Reset(); sl@0: test(lStr.ReAlloc(0)==KErrNone); //ReAlloc to EPtr sl@0: test(lStr.Length() == 0); sl@0: test(lStr.MaxLength() == 0); sl@0: } sl@0: sl@0: /** sl@0: Tests the following methods. sl@0: - TInt ReAllocL(TInt aMaxLength); sl@0: */ sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EUSERHL-UT-4005 sl@0: @SYMTestCaseDesc Tests Leaving variant ReAllocL of LString sl@0: @SYMTestPriority High sl@0: @SYMTestActions Creates LString objects and uses ReAllocL to sl@0: increase memory under OOM conditions sl@0: Verifies that ReAllocL leaves with KErrNoMemory sl@0: @SYMTestExpectedResults Calls to ReAllocL that increase memory should sl@0: leave with KErrNoMemory under OOM conditions sl@0: @SYMREQ 10372 sl@0: */ sl@0: template sl@0: LOCAL_C void TestReAllocLeaving(LSTRING*) sl@0: { sl@0: sl@0: test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4005")); sl@0: sl@0: test.Next(_L("ReAllocL(TInt aMaxLength) method")); sl@0: TBUF des(_TS("01")); sl@0: sl@0: LSTRING lStr(des); sl@0: TRAPD(ret, lStr.ReAllocL(6)); //ReAlloc buffer sl@0: test(KErrNone == ret); sl@0: sl@0: #if defined(_DEBUG) sl@0: __UHEAP_FAILNEXT(1); sl@0: TRAP(ret, lStr.ReAllocL(100)); //Realloc buffer. This should fail. sl@0: test(KErrNoMemory == ret); sl@0: #endif //(_DEBUG) sl@0: sl@0: test(lStr.MaxLength() >=6); //Check LString is the same as before ... sl@0: test(lStr.Length()==2); //... ReAlloc that failed. sl@0: test(lStr[0] == (TTEXT)('0')); sl@0: test(lStr[1] == (TTEXT)('1')); sl@0: } sl@0: sl@0: sl@0: /** sl@0: Tests the following methods. sl@0: - void SwapL(LString& aBuf); sl@0: - void SwapL(TDes& aBuf); sl@0: */ sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EUSERHL-UT-4006 sl@0: @SYMTestCaseDesc Tests SwapL methods of LString sl@0: @SYMTestPriority High sl@0: @SYMTestActions Creates LString objects and uses SwapL to sl@0: swap string contents. sl@0: Verifies that contents are swapped and memory sl@0: reallocated where necessary sl@0: @SYMTestExpectedResults Calls to SwapL should swap string contents sl@0: and buffer should be automatically reallocated to fit the new data. sl@0: @SYMREQ 10372 sl@0: */ sl@0: template sl@0: LOCAL_C void TestSwapL(LSTRING*) sl@0: { sl@0: test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4006")); sl@0: sl@0: LSTRING lStr1, lStr2; sl@0: TBUF des1(_TS("12")); sl@0: TBUF des2 (_TS("345678")); sl@0: TBUF des3 (_TS("12345678")); sl@0: TBUF swap_des = des2; sl@0: sl@0: test.Next(_L("Swap(LString_& aLString) method")); sl@0: sl@0: // assignment operation; implies deep copying sl@0: lStr1 = des1; sl@0: lStr2 = des2; sl@0: sl@0: // swap LStrings with TDes sl@0: // lStr1 should grow to accommodate swap_des sl@0: lStr1.SwapL(swap_des); sl@0: test(lStr1==des2); sl@0: test(swap_des==des1); sl@0: sl@0: // swap two LStrings sl@0: // lStr2 should grow to accommodate lStr1 sl@0: lStr1 = des3; sl@0: lStr1.SwapL(lStr2); sl@0: sl@0: test(lStr1==des2); sl@0: test(lStr2==des3); sl@0: sl@0: } sl@0: sl@0: sl@0: /** sl@0: Test assignemnt operator. sl@0: */ sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EUSERHL-UT-4007 sl@0: @SYMTestCaseDesc Tests Assignment operator for LString sl@0: @SYMTestPriority High sl@0: @SYMTestActions Creates LString objects and uses assignment to sl@0: change string contents. sl@0: Verifies that contents are swapped and memory sl@0: reallocated where necessary sl@0: @SYMTestExpectedResults Assignment operator should change string contents sl@0: and buffer should be automatically reallocated to fit the new data. sl@0: @SYMREQ 10372 sl@0: */ sl@0: template sl@0: LOCAL_C void TestAssignmentOperatorL() sl@0: { sl@0: test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4007")); sl@0: sl@0: test.Next(_L("Assignment operator")); sl@0: sl@0: TBUF tdes(_TS("Modifiable descriptor")); sl@0: TBUFC tdesc(_TS("Non-modifiable descriptor")); sl@0: sl@0: LSTRING lStr(32); sl@0: LSTRING lStr2(32); sl@0: lStr2.CopyL(_TS("Buffer descriptor"), 17); sl@0: sl@0: lStr = tdesc; test(lStr == tdesc); sl@0: lStr = tdes; test(lStr == tdes); sl@0: lStr = lStr2; test(lStr == lStr2); sl@0: sl@0: LSTRING lStr3(tdes); sl@0: lStr = lStr3.PtrZL(); test(lStr == tdes); sl@0: sl@0: test.Next(_L("operator=(char/wchar_t* aCharStr) method")); sl@0: LSTRING lStr4(_CS("123456")); sl@0: LSTRING lStr5 = _CS("123456"); sl@0: test(lStr4 == lStr5); sl@0: sl@0: LSTRING lStr6; sl@0: lStr6 = _CS("123456"); sl@0: test(lStr4 == lStr6); sl@0: } sl@0: sl@0: /** sl@0: Test Capacity growth and compression sl@0: */ sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EUSERHL-UT-4008 sl@0: @SYMTestCaseDesc Tests capacity growth and compression for LString sl@0: @SYMTestPriority High sl@0: @SYMTestActions Creates an LString object then calls SetMaxLength and Compress sl@0: to increase or decrease underlying buffer size. sl@0: Tests that Length and Maxlength are modified as expected sl@0: @SYMTestExpectedResults SetMaxLength and Compress should resize the underlying sl@0: buffer as expected. sl@0: @SYMREQ 10372 sl@0: */ sl@0: template sl@0: LOCAL_C void TestCapacityChangesL(LSTRING*) sl@0: { sl@0: test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4008")); sl@0: sl@0: test.Next(_L("Test capacity growth and compression on LString")); sl@0: sl@0: LSTRING lStr(_TS("0123456")); sl@0: test(lStr.Length()==7); sl@0: test(lStr.MaxLength() >=7); sl@0: sl@0: lStr.SetMaxLengthL(10); sl@0: test(lStr.Length()==7); sl@0: test(lStr.MaxLength() >=10); sl@0: sl@0: lStr.SetMaxLengthL(6); sl@0: test(lStr.Length()==6); sl@0: test(lStr.MaxLength() >=6); sl@0: sl@0: lStr.SetMaxLengthL(10); sl@0: test(lStr.Length()==6); sl@0: test(lStr.MaxLength() >=10); sl@0: sl@0: //Call the same thing again to check the condition sl@0: //that required length is already set sl@0: lStr.SetMaxLengthL(10); sl@0: test(lStr.Length()==6); sl@0: test(lStr.MaxLength() >=10); sl@0: sl@0: lStr.Compress(); sl@0: test(lStr.Length()==6); sl@0: test(lStr.MaxLength() >= 6); sl@0: sl@0: //Call the same thing again to check the condition sl@0: //that the string is already compressed sl@0: lStr.Compress(); sl@0: test(lStr.Length()==6); sl@0: test(lStr.MaxLength() >= 6); sl@0: sl@0: lStr.ReserveFreeCapacityL(15); sl@0: test(lStr.Length()==6); sl@0: test(lStr.MaxLength() >= 32); sl@0: sl@0: lStr.Reset(); sl@0: test(lStr.Length()==0); sl@0: test(lStr.MaxLength() == 0); sl@0: sl@0: } sl@0: sl@0: sl@0: /** sl@0: Test copying from 16bit to 8bit and vice versa sl@0: */ sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EUSERHL-UT-4009 sl@0: @SYMTestCaseDesc Tests Copying between 8 and 16 bit LString variants sl@0: @SYMTestPriority High sl@0: @SYMTestActions Creates an LString8 and LString16 object and uses the cross-variant sl@0: Copy functions to copy data between the variants. sl@0: @SYMTestExpectedResults Data is successfully copied between the variants. sl@0: @SYMREQ 10372 sl@0: */ sl@0: LOCAL_C void TestCrossCopyingL() sl@0: { sl@0: sl@0: test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4009")); sl@0: sl@0: test.Next(_L("Test cross copying on LString")); sl@0: sl@0: LString8 lStr8("0123"); sl@0: LString16 lStr16(L"01234567"); sl@0: sl@0: LString8 targetStr8; sl@0: LString16 targetStr16; sl@0: sl@0: targetStr8.CopyL(lStr16); sl@0: targetStr16.CopyL(lStr8); sl@0: } sl@0: sl@0: sl@0: /** sl@0: Test creating an LString from a stream sl@0: */ sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EUSERHL-UT-4010 sl@0: @SYMTestCaseDesc Tests Creating an LString from a stream sl@0: @SYMTestPriority High sl@0: @SYMTestActions Creates a in-memory stream and writes some data to it. sl@0: Creates an LString from a readstream and verifies that the sl@0: contents of the LString match the data written to the stream. sl@0: @SYMTestExpectedResults The LString contents should match the data written sl@0: to the stream. sl@0: @SYMREQ 10372 sl@0: */ sl@0: template sl@0: LOCAL_C void TestReadFromStreamL() sl@0: { sl@0: test.Next (_L ("@SYMTestCaseID:SYSLIB-EUSERHL-UT-4010")); sl@0: sl@0: test.Next(_L("Test creating LString from a stream")); sl@0: sl@0: RFs fs; sl@0: fs.Connect() OR_LEAVE; sl@0: sl@0: LSTRING outString = _TS("This is a test string written to a stream"); sl@0: sl@0: //Create a buffer to contain the stream sl@0: CBufFlat* buf = CBufFlat::NewL(outString.MaxLength()); sl@0: sl@0: //Create a write stream sl@0: RBufWriteStream outStream; sl@0: outStream.Open(*buf); sl@0: sl@0: //write some data to the stream sl@0: outStream << outString; sl@0: outStream.CommitL(); sl@0: sl@0: //Open a readstream sl@0: RBufReadStream inStream; sl@0: inStream.Open(*buf); sl@0: sl@0: //Create an LString from the stream sl@0: LSTRING inString; sl@0: inString.CreateL(inStream,outString.Length()); sl@0: test(inString == outString); sl@0: sl@0: delete buf; sl@0: } sl@0: sl@0: /** sl@0: Test support for [wide]character strings. sl@0: APIs that modify data. sl@0: */ sl@0: /** sl@0: @SYMTestCaseID BASESRVCS-EUSERHL-UT-4068 sl@0: @SYMTestCaseDesc Test the APIs provided to support wchar_t and char strings sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1)Construct LString object from the supplied null terminated sl@0: character string. sl@0: 2)Assign new string to the constructed LString object sl@0: 3)Appends data onto the end of this LString object's data. sl@0: The length of this descriptor is incremented to reflect the new content. sl@0: 4)Copy new data into the LString object, replacing any existing sl@0: data, and expanding its heap buffer to accommodate if necessary. sl@0: 5)Insert contents into the LString sl@0: 6)Replace data to the end of the LString object and justify it. sl@0: 7)Append data of specified length, to the end of the LString object. sl@0: 8)Justify data, to the end of the LString object. sl@0: 9)Appends data onto the end of this descriptor's data and justifies it. sl@0: @SYMTestExpectedResults The LString contents should match the data expected after the operation. sl@0: @SYMREQ 10372 sl@0: */ sl@0: sl@0: template sl@0: LOCAL_C void TestCharacterStringSupport_Modifiers(LSTRING*) sl@0: { sl@0: test.Next (_L ("@SYMTestCaseID:BASESRVCS-EUSERHL-UT-4068")); sl@0: sl@0: // 1. test Constructor sl@0: // Constructs LString object from the supplied null terminated sl@0: // character string sl@0: test.Next(_L("LString_(char/wchar_t * aCharStr) constructor")); sl@0: LSTRING lStr(_CS("0123456789")); sl@0: test(lStr.Length() == 10); sl@0: test(lStr.MaxLength() >= 10); sl@0: test(lStr.Compare(_CS("0123456789")) == 0 ); sl@0: // try strings ending with 0 sl@0: LSTRING lStr1(_CS("01234567890")); sl@0: test(lStr1.Length() == 11); sl@0: test(lStr1.MaxLength() >= 11); sl@0: test(lStr1.Compare(_CS("01234567890")) == 0 ); sl@0: sl@0: // 2. test '=' operator sl@0: LSTRING lTestStr; sl@0: // Assign new string to the constructed LString object sl@0: test.Next(_L("LString_ operator '=' ")); sl@0: lTestStr = _CS("Try a New String"); sl@0: test(lTestStr.Compare(_CS("Try a New String")) == 0 ); sl@0: test(lTestStr.Length() == 16); sl@0: test(lTestStr.MaxLength() >= 16); sl@0: sl@0: // 3. test '+=' operator sl@0: // Appends data onto the end of this LString object's data. sl@0: // The length of this descriptor is incremented to reflect the new content. sl@0: test.Next(_L("LString_ operator '+=' ")); sl@0: lTestStr += _CS("!!!"); sl@0: test(lTestStr.Compare(_CS("Try a New String!!!")) == 0 ); sl@0: test(lTestStr.Length() == 19); sl@0: test(lTestStr.MaxLength() >= 19); sl@0: sl@0: // 4.Test "Copy()" Variants sl@0: LSTRING lTestStr1; sl@0: LSTRING lTestStr2; sl@0: // Copy new data into the LString object, replacing any existing sl@0: // data, and expanding its heap buffer to accommodate if necessary. sl@0: test.Next(_L("LString_ CopyL ")); sl@0: lTestStr1.CopyL(_TS("Try a New String")); sl@0: test(lTestStr1.Compare(_TS("Try a New String")) == 0 ); sl@0: test(lTestStr1.Length() == 16); sl@0: test(lTestStr1.MaxLength() >= 16); sl@0: sl@0: // Copy folded(normalized) content sl@0: test.Next(_L("LString_ CopyFL ")); sl@0: lTestStr1.CopyFL(_CS("Some RaNDom STRING")); sl@0: lTestStr2.CopyFL(_CS("SOME RaNDom string")); sl@0: test(lTestStr1.Compare(lTestStr2) == 0); sl@0: sl@0: // Copy contents in Lower case sl@0: test.Next(_L("LString_ CopyLCL ")); sl@0: lTestStr1.CopyLCL(_CS("SOME STRING IN UPPER CASE")); sl@0: test(lTestStr1 == _CS("some string in upper case")); sl@0: sl@0: // Copy contents in Upper case sl@0: test.Next(_L("LString_ CopyUCL ")); sl@0: lTestStr1.CopyUCL(_CS("some string in lower case")); sl@0: test(lTestStr1 == _CS("SOME STRING IN LOWER CASE")); sl@0: sl@0: // Copy Capitalized contents sl@0: test.Next(_L("LString_ CopyCPL ")); sl@0: lTestStr1.CopyCPL(_CS("some string in lower case")); sl@0: test(lTestStr1 == _CS("Some string in lower case")); sl@0: sl@0: // 5. Test Insert() sl@0: LSTRING lTestStr3; sl@0: // Insert contents into a string sl@0: test.Next(_L("LString_ InsertL ")); sl@0: lTestStr3 = _CS("Some Content Can Be Into This String"); sl@0: lTestStr3.InsertL(20,_CS("Inserted ")); sl@0: test(lTestStr3 == _CS("Some Content Can Be Inserted Into This String")); sl@0: sl@0: // 6. Test Replace() sl@0: LSTRING lTestStr4; sl@0: // Replace contents form the string sl@0: test.Next(_L("LString_ ReplaceL ")); sl@0: lTestStr4 = _CS("Some Content Can Be Decalper"); sl@0: lTestStr4.ReplaceL(20,8,_CS("Replaced")); sl@0: test(lTestStr4 == _CS("Some Content Can Be Replaced")); sl@0: sl@0: // 7. Test Append() sl@0: LSTRING lTestStr5; sl@0: //Append data of specified length, to the end of the LString object. sl@0: test.Next(_L("LString_ AppendL(src,length)")); sl@0: lTestStr5.CopyL( _CS("Try appending ")); sl@0: lTestStr5.AppendL(_CS("Try appending some more"),3); sl@0: test(lTestStr5 == _CS("Try appending Try")); sl@0: sl@0: //Append data , to the end of the LString object. sl@0: test.Next(_L("LString_ AppendL(src)")); sl@0: lTestStr5.CopyL( _CS("Try appending ")); sl@0: lTestStr5.AppendL(_CS("Try appending some more")); sl@0: test(lTestStr5 == _CS("Try appending Try appending some more")); sl@0: sl@0: // 8. Test Justify() sl@0: LSTRING lTestStr6; sl@0: //Copy data into this descriptor and justifies it, replacing any existing data sl@0: test.Next(_L("LString_ JustifyL ")); sl@0: lTestStr6.CopyL(_CS("Justified")); sl@0: lTestStr6.JustifyL(_CS("Just"),9,ERight,*(_TS("x"))); sl@0: test(lTestStr6 == _CS("xxxxxJust")); sl@0: sl@0: // 9. Test AppendJustify variants sl@0: LSTRING lTestStr7; sl@0: // Append data to the end of the LString object and justify it. sl@0: test.Next(_L("LString_ AppendJustifyL(const char*,TInt aLength,TInt aWidth,TAlign anAlignment,TChar aFill)")); sl@0: lTestStr7.CopyL(_CS("One ")); sl@0: lTestStr7.AppendJustifyL(_CS("Two "),KDefaultJustifyWidth,ERight,*(_TS("x"))); sl@0: test(lTestStr7 == _TS("One Two ")); sl@0: sl@0: lTestStr7.CopyL(_CS("One ")); sl@0: lTestStr7.AppendJustifyL(_CS("Two Three"),3,7,ERight,*(_TS("x"))); sl@0: test(lTestStr7 == _CS("One xxxxTwo") ); sl@0: sl@0: // Append data to the end of the LString object and justify it. sl@0: test.Next(_L("LString_ AppendJustifyL(const char* aCharStr,TInt aWidth,TAlign anAlignment,TChar aFill)")); sl@0: lTestStr7.CopyL(_CS("One ")); sl@0: lTestStr7.AppendJustifyL(_CS("Two Three"),KDefaultJustifyWidth,ERight,*(_TS("x"))); sl@0: test(lTestStr7 == _TS("One Two Three")); sl@0: sl@0: lTestStr7.CopyL(_CS("One ")); sl@0: lTestStr7.AppendJustifyL(_CS("Two Three"),13,ERight,*(_TS("x"))); sl@0: test(lTestStr7 == _CS("One xxxxTwo Three") ); sl@0: } sl@0: sl@0: /** sl@0: Test support for [wide]character strings. sl@0: APIs that do not modify any data. sl@0: */ sl@0: /** sl@0: @SYMTestCaseID BASESRVCS-EUSERHL-UT-4069 sl@0: @SYMTestCaseDesc Test the APIs provided to support wchar_t and char strings sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1)Determine whether this descriptor's data is equal to the specified string's data. sl@0: 2)Determine whether this descriptor's data is less than the specified string's data. sl@0: 3)Determine whether this descriptor's data is less than or equal to the specified string's data. sl@0: 4)Determine whether this descriptor's data is greater than the specified string's data. sl@0: 5)Determine whether this descriptor's data is greater than or equal to the specified string's data. sl@0: 6)Determine whether this descriptor's data is not equal to the specified string's data. sl@0: 7)Compare this descriptor's data with the specified string's data. sl@0: 8)Search this descriptor's data for a match with the match pattern supplied in the specified string's sl@0: 9)Searches for the first occurrence of the specified data sequence within this descriptor sl@0: @SYMTestExpectedResults The operation/comparision must result in the desired output sl@0: @SYMREQ 10372 sl@0: */ sl@0: template sl@0: LOCAL_C void TestCharacterStringSupport_NonModifiers(LSTRING*) sl@0: { sl@0: test.Next (_L ("@SYMTestCaseID:BASESRVCS-EUSERHL-UT-4069")); sl@0: sl@0: // 1.test '==' operator sl@0: LSTRING lTestStr1; sl@0: lTestStr1.CopyL(_CS("Are they equal?? ")); sl@0: test(lTestStr1 == _CS("Are they equal?? ")); sl@0: lTestStr1.CopyL(_CS("12345670")); sl@0: test(lTestStr1 == _CS("12345670")); sl@0: sl@0: // 2.test "<" operator sl@0: LSTRING lTestStr2; sl@0: lTestStr2.CopyL(_CS("ABCDEFGH")); sl@0: test(lTestStr2 < _CS("abcdefgh")); sl@0: lTestStr2.CopyL(_CS(" Is this is smaller")); sl@0: test(lTestStr2 < _CS("No, larger of the string is greater than the smaller one")); sl@0: sl@0: // 3.test "<=" operator sl@0: LSTRING lTestStr3; sl@0: lTestStr3.CopyL(_CS("ABCDEFGH")); sl@0: test(lTestStr3 <= _CS("abcdefgh")); sl@0: lTestStr3.CopyL(_CS("equals")); sl@0: test(lTestStr3 <= _CS("equals")); sl@0: sl@0: // 4.test ">" operator sl@0: LSTRING lTestStr4; sl@0: lTestStr4.CopyL(_CS("abcdefgh")); sl@0: test(lTestStr4 > _CS("ABCDEFGH")); sl@0: lTestStr4.CopyL(_CS("No, larger of the string is greater than the smaller one")); sl@0: test(lTestStr4 > _CS("Is this smaller??")); sl@0: sl@0: // 5.test ">=" operator sl@0: LSTRING lTestStr5; sl@0: lTestStr5.CopyL(_CS("abcdefgh")); sl@0: test(lTestStr5 >= _CS("ABCDEFGH")); sl@0: lTestStr5.CopyL(_CS("equals")); sl@0: test(lTestStr5 >= _CS("equals")); sl@0: sl@0: // 6.test "!=" sl@0: LSTRING lTestStr6; sl@0: lTestStr6.CopyL(_CS("abcdefgh")); sl@0: test(lTestStr6 != _CS("ABCDEFGH")); sl@0: lTestStr6.CopyL(_CS("equals")); sl@0: test(!(lTestStr6 != _CS("equals"))); sl@0: sl@0: // 7.test Compare variants sl@0: LSTRING lTestStr7; sl@0: // Compare strict sl@0: lTestStr7.CopyL(_CS("abcdefgh")); sl@0: test(lTestStr7.Compare(_CS("ABCDEFGH")) > 0); sl@0: lTestStr7.CopyL(_CS("ABCDEFGH")); sl@0: test(lTestStr7.Compare(_CS("abcdefgh")) < 0); sl@0: lTestStr5.CopyL(_CS("equals")); sl@0: test(lTestStr5.Compare( _CS("equals")) == 0); sl@0: // Compare foalded sl@0: lTestStr7.CopyL(_CS("abcdefgh")); sl@0: test(lTestStr7.CompareF(_CS("ABcDeFgH")) == 0); sl@0: sl@0: // 8.test Match variants sl@0: LSTRING lTestStr8; sl@0: // Match strict sl@0: lTestStr8.CopyL(_CS("abcdefghijklmnopqrstuvwxyz")); sl@0: test(lTestStr8.Match(_CS("*ijk*"))== 8); sl@0: test(lTestStr8.Match(_CS("*i?k*"))== 8); sl@0: test(lTestStr8.Match(_CS("ijk*"))== KErrNotFound); sl@0: // Match Folded sl@0: test(lTestStr8.MatchF(_CS("*IjK*"))== 8); sl@0: test(lTestStr8.MatchF(_CS("*I?k*"))== 8); sl@0: test(lTestStr8.MatchF(_CS("ijK*"))== KErrNotFound); sl@0: sl@0: // 9.test Find variants sl@0: LSTRING lTestStr9; sl@0: // Find strict sl@0: lTestStr9.CopyL(_CS("abcdefghijklmnopqrstuvwxyz")); sl@0: test(lTestStr9.Find(_CS("abcde")) == 0); sl@0: test(lTestStr9.Find(_CS("cde")) == 2); sl@0: test(lTestStr9.Find(_CS("efg22")) == KErrNotFound); sl@0: test(lTestStr9.Find(_CS("efg22"),3) == 4); sl@0: // Find folded sl@0: test(lTestStr9.FindF(_CS("aBcDe")) == 0); sl@0: test(lTestStr9.FindF(_CS("cDe")) == 2); sl@0: test(lTestStr9.FindF(_CS("eFg22")) == KErrNotFound); sl@0: test(lTestStr9.FindF(_CS("efG22"),3) == 4); sl@0: } sl@0: sl@0: void RunTestsL() sl@0: { sl@0: sl@0: LString8* r8=0; sl@0: LString16* r16=0; sl@0: sl@0: // LString8 Tests sl@0: TestConstructors,TText8, HBufC8, char>(r8); sl@0: TestSwapL,TText8>(r8); sl@0: TestAssign,TText8,HBufC8,RBuf8>(r8); sl@0: TestAppendL,TText8, char>(r8); sl@0: TestReAllocL,TText8,HBufC8>(r8); sl@0: TestReAllocLeaving,TText8>(r8); sl@0: TestAssignmentOperatorL,TBufC8<32>,TText8, char>(); sl@0: TestCapacityChangesL,TText8>(r8); sl@0: TestReadFromStreamL(); sl@0: TestCharacterStringSupport_Modifiers(r8); sl@0: TestCharacterStringSupport_NonModifiers(r8); sl@0: sl@0: // LString16 Tests sl@0: TestConstructors,TText16,HBufC16, wchar_t>(r16); sl@0: TestSwapL,TText16>(r16); sl@0: TestAssign,TText16,HBufC16,RBuf16>(r16); sl@0: TestAppendL, TText16,wchar_t>(r16); sl@0: TestReAllocL,TText16,HBufC16>(r16); sl@0: TestReAllocLeaving,TText16>(r16); sl@0: TestAssignmentOperatorL,TBufC16<32>,TText16,wchar_t>(); sl@0: TestCapacityChangesL,TText16>(r16); sl@0: TestReadFromStreamL(); sl@0: TestCharacterStringSupport_Modifiers(r16); sl@0: TestCharacterStringSupport_NonModifiers(r16); sl@0: TestCrossCopyingL(); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: /** sl@0: Test arithmetric operator overloads sl@0: */ sl@0: sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: sl@0: CTrapCleanup* trapHandler=CTrapCleanup::New(); sl@0: test(trapHandler!=NULL); sl@0: sl@0: test.Title(); sl@0: test.Start(_L("Testing LString8 & LString16 classes")); sl@0: sl@0: __UHEAP_MARK; sl@0: sl@0: TRAPD(err, RunTestsL()); sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: test.End(); sl@0: sl@0: delete trapHandler; sl@0: return(err); sl@0: } sl@0: