First public contribution.
1 // Copyright (c) 2001-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include "CTextUtilsTest.h"
18 #include <uriutilscommon.h>
19 #include <inetprottextutils.h>
21 #include "TextUtilsTestCommon.h"
22 #include "TextUtilsTests.h"
24 CTextUtilsTest* CTextUtilsTest::NewLC(CIpuTestHarness* aTestHarness)
26 CTextUtilsTest* self = new (ELeave) CTextUtilsTest(aTestHarness);
27 CleanupStack::PushL(self);
32 CTextUtilsTest* CTextUtilsTest::NewL(CIpuTestHarness* aTestHarness)
34 CTextUtilsTest* self = CTextUtilsTest::NewLC(aTestHarness);
35 CleanupStack::Pop(self);
39 CTextUtilsTest::CTextUtilsTest(CIpuTestHarness* aTestHarness)
40 : iTestHarness(aTestHarness)
44 void CTextUtilsTest::ConstructL()
48 CTextUtilsTest::~CTextUtilsTest()
52 void CTextUtilsTest::DoTestsL()
55 TestTextUtilsRemoveL(KRemove0_base, KRemove0_left, KRemove0_left_consumed,
56 KRemove0_right, KRemove0_right_consumed,
57 KRemove0_both, KRemove0_both_consumed);
59 TestTextUtilsDesToIntConversionL(KDecRep0, KNumericValue0, KIsValid0);
60 TestTextUtilsDesToIntConversionL(KDecRep1, KNumericValue1, KIsValid1);
61 TestTextUtilsDesToIntConversionL(KDecRep2, KNumericValue2, KIsValid2);
62 TestTextUtilsDesToIntConversionL(KDecRep3, KNumericValue3, KIsValid3);
64 TestTextUtilsIntToDesConversionL(KNumericValue0, KDecRep0);
65 TestTextUtilsIntToDesConversionL(KNumericValue1, KDecRep1);
67 _LIT(KWhiteSpace," \t");
68 TestTextUtilsDesToHexConversionL(KWhiteSpace, 0, EFalse);
70 TestTextUtilsDesToHexConversionL(KHexRep0, KNumericValue0, KIsValid0);
71 TestTextUtilsDesToHexConversionL(KHexRep1, KNumericValue1, KIsValid1);
72 TestTextUtilsDesToHexConversionL(KHexRep2, KNumericValue2, KIsValid2);
73 TestTextUtilsDesToHexConversionL(KHexRep3, KNumericValue3, KIsValid3);
75 TestTextUtilsHexToDesConversionL(KNumericValue0, KHexRep0);
76 TestTextUtilsHexToDesConversionL(KNumericValue1, KHexRep1);
78 TestTextUtilsExtractQuotedStringL(KQuotedBuffer0, KQuotedString0, KQuotedRemainder0, KExpectedError0);
79 TestTextUtilsExtractQuotedStringL(KQuotedBuffer1, KQuotedString1, KQuotedRemainder1, KExpectedError1);
80 TestTextUtilsExtractQuotedStringL(KQuotedBuffer2, KQuotedString2, KQuotedRemainder2, KExpectedError2);
81 TestTextUtilsExtractQuotedStringL(KQuotedBuffer3, KQuotedString3, KQuotedRemainder3, KExpectedError3);
83 TestTextUtilsExtractTokenFromListL(
92 TestTextUtilsExtractTokenFromListL(
101 TestTextUtilsExtractTokenFromListL(
110 TestTextUtilsExtractTokenFromListL(
120 void CTextUtilsTest::TestTextUtilsRemoveL(const TDesC& aBase,
121 const TDesC& aRemoveLeft, TInt aConsumedLeft,
122 const TDesC& aRemoveRight, TInt aConsumedRight,
123 const TDesC& aRemoveBoth, TInt aConsumedBoth)
125 iTestHarness->StartTestL(_L("Test TextUtils Remove (16-bit)"));
127 TInt error = DoTestTextUtilsRemove<TPtrC16>(aBase, aRemoveLeft, aConsumedLeft,
128 aRemoveRight, aConsumedRight,
129 aRemoveBoth, aConsumedBoth);
131 iTestHarness->EndTest(error);
134 HBufC8* baseBuf = HBufC8::NewLC(aBase.Length());
135 TPtr8 base8Bit = baseBuf->Des();
136 base8Bit.Copy(aBase);
138 HBufC8* removeLeftBuf = HBufC8::NewLC(aRemoveLeft.Length());
139 TPtr8 removeLeft8Bit = removeLeftBuf->Des();
140 removeLeft8Bit.Copy(aRemoveLeft);
142 HBufC8* removeRightBuf = HBufC8::NewLC(aRemoveRight.Length());
143 TPtr8 removeRight8Bit = removeRightBuf->Des();
144 removeRight8Bit.Copy(aRemoveRight);
146 HBufC8* removeBothBuf = HBufC8::NewLC(aRemoveBoth.Length());
147 TPtr8 removeBoth8Bit = removeBothBuf->Des();
148 removeBoth8Bit.Copy(aRemoveBoth);
150 iTestHarness->StartTestL(_L("Test TextUtils Remove (8-bit)"));
152 error = DoTestTextUtilsRemove<TPtrC8>(base8Bit, removeLeft8Bit, aConsumedLeft,
153 removeRight8Bit, aConsumedRight,
154 removeBoth8Bit, aConsumedBoth);
156 iTestHarness->EndTest(error);
158 CleanupStack::PopAndDestroy(4, baseBuf); // baseBuf, removeLeftBuf, removeRightBuf, removeBothBuf
161 void CTextUtilsTest::TestTextUtilsDesToIntConversionL(const TDesC& aDes, TInt aInt, TBool aValid)
163 iTestHarness->StartTestL(_L("Test TextUtils ConvertDescriptorToInt (16-bit)"));
165 TInt error = DoTestTextUtilsDesToIntConversion(aDes, aInt, aValid);
167 iTestHarness->EndTest(error);
170 HBufC8* desBuf = HBufC8::NewLC(aDes.Length());
171 TPtr8 des8Bit = desBuf->Des();
174 iTestHarness->StartTestL(_L("Test TextUtils ConvertDescriptorToInt (8-bit)"));
176 error = DoTestTextUtilsDesToIntConversion(des8Bit, aInt, aValid);
178 iTestHarness->EndTest(error);
180 CleanupStack::PopAndDestroy(desBuf); // desBuf
183 void CTextUtilsTest::TestTextUtilsIntToDesConversionL(TInt aInt, const TDesC& aDes)
185 iTestHarness->StartTestL(_L("Test TextUtils ConvertIntToDescriptor (16-bit)"));
187 TInt error = DoTestTextUtilsIntToDesConversion<HBufC16>(aInt, aDes);
189 iTestHarness->EndTest(error);
192 HBufC8* desBuf = HBufC8::NewLC(aDes.Length());
193 TPtr8 des8Bit = desBuf->Des();
196 iTestHarness->StartTestL(_L("Test TextUtils ConvertIntToDescriptor (8-bit)"));
198 error = DoTestTextUtilsIntToDesConversion<HBufC8>(aInt, des8Bit);
200 iTestHarness->EndTest(error);
202 CleanupStack::PopAndDestroy(desBuf); // desBuf
207 void CTextUtilsTest::TestTextUtilsDesToHexConversionL(const TDesC& aDes, TInt aHexNumber, TBool aValid)
209 iTestHarness->StartTestL(_L("Test TextUtils ConvertDescriptorToHex (16-bit)"));
211 TInt error = DoTestTextUtilsDesToHexConversion(aDes, aHexNumber, aValid);
212 iTestHarness->EndTest(error);
214 // Test 8 bit version
215 TestTextUtilsDesToHexConversion8L(aDes, aHexNumber, aValid);
219 void CTextUtilsTest::TestTextUtilsDesToHexConversion8L(const TDesC& aDes, TInt aHexNumber, TBool aValid)
221 HBufC8* desBuf = HBufC8::NewLC(aDes.Length());
222 TPtr8 des8Bit = desBuf->Des();
225 iTestHarness->StartTestL(_L("Test TextUtils ConvertDescriptorToHex (8-bit)"));
227 TInt error = DoTestTextUtilsDesToHexConversion(des8Bit, aHexNumber, aValid);
229 iTestHarness->EndTest(error);
231 CleanupStack::PopAndDestroy(desBuf); // desBuf
236 void CTextUtilsTest::TestTextUtilsHexToDesConversionL(TInt aHex, const TDesC& aDes)
238 iTestHarness->StartTestL(_L("Test TextUtils ConvertHexToDescriptor (16-bit)"));
240 TInt error = DoTestTextUtilsHexToDesConversion<HBufC16>(aHex, aDes);
242 iTestHarness->EndTest(error);
245 HBufC8* desBuf = HBufC8::NewLC(aDes.Length());
246 TPtr8 des8Bit = desBuf->Des();
249 iTestHarness->StartTestL(_L("Test TextUtils ConvertHexToDescriptor (8-bit)"));
251 error = DoTestTextUtilsHexToDesConversion<HBufC8>(aHex, des8Bit);
253 iTestHarness->EndTest(error);
255 CleanupStack::PopAndDestroy(desBuf); // desBuf
258 void CTextUtilsTest::TestTextUtilsExtractQuotedStringL(
259 const TDesC& aBuffer,
260 const TDesC& aString,
261 const TDesC& aRemainder,
265 iTestHarness->StartTestL(_L("Test TextUtils ExtractQuotedStringL (16-bit)"));
267 TInt error = DoTestExtractQuotedStringL<TPtrC>(aBuffer, aString, aRemainder, aExpectedError);
269 iTestHarness->EndTest(error);
272 HBufC8* buffer = HBufC8::NewLC(aBuffer.Length());
273 buffer->Des().Copy(aBuffer);
275 HBufC8* string = HBufC8::NewLC(aString.Length());
276 string->Des().Copy(aString);
278 HBufC8* remainder = HBufC8::NewLC(aRemainder.Length());
279 remainder->Des().Copy(aRemainder);
281 iTestHarness->StartTestL(_L("Test TextUtils ExtractQuotedStringL (8-bit)"));
283 error = DoTestExtractQuotedStringL<TPtrC8>(*buffer, *string, *remainder, aExpectedError);
285 iTestHarness->EndTest(error);
287 CleanupStack::PopAndDestroy(3, buffer);
290 void CTextUtilsTest::TestTextUtilsExtractTokenFromListL(
291 const TDesC& aBuffer,
292 const TDesC& aToken0,
293 const TDesC& aToken1,
294 const TDesC& aToken2,
295 const TDesC& aRemainder,
296 const TDesC& aSeparator
299 iTestHarness->StartTestL(_L("Test TextUtils ExtractTokenFromList, multiple separator (16-bit)"));
301 TInt error = DoExtractTokenFromListMultipleSeparator<TPtrC>(aBuffer, aToken0, aToken1, aToken2, aRemainder, aSeparator);
303 iTestHarness->EndTest(error);
306 HBufC8* buffer = HBufC8::NewLC(aBuffer.Length());
307 buffer->Des().Copy(aBuffer);
309 HBufC8* token0 = HBufC8::NewLC(aToken0.Length());
310 token0->Des().Copy(aToken0);
312 HBufC8* token1 = HBufC8::NewLC(aToken1.Length());
313 token1->Des().Copy(aToken1);
315 HBufC8* token2 = HBufC8::NewLC(aToken2.Length());
316 token2->Des().Copy(aToken2);
318 HBufC8* remainder = HBufC8::NewLC(aRemainder.Length());
319 remainder->Des().Copy(aRemainder);
321 HBufC8* separator = HBufC8::NewLC(aSeparator.Length());
322 separator->Des().Copy(aSeparator);
324 iTestHarness->StartTestL(_L("Test TextUtils ExtractTokenFromList, multiple separator (8-bit)"));
326 error = DoExtractTokenFromListMultipleSeparator<TPtrC8>(*buffer, *token0, *token1, *token2, *remainder, *separator);
328 iTestHarness->EndTest(error);
330 CleanupStack::PopAndDestroy(6, buffer);
333 void CTextUtilsTest::TestTextUtilsExtractTokenFromListL(
334 const TDesC& aBuffer,
335 const TDesC& aToken0,
336 const TDesC& aToken1,
337 const TDesC& aToken2,
338 const TDesC& aRemainder,
342 iTestHarness->StartTestL(_L("Test TextUtils ExtractTokenFromList, single separator (16-bit)"));
344 TInt error = DoExtractTokenFromListSingleSeparator<TPtrC>(aBuffer, aToken0, aToken1, aToken2, aRemainder, aSeparator);
346 iTestHarness->EndTest(error);
349 HBufC8* buffer = HBufC8::NewLC(aBuffer.Length());
350 buffer->Des().Copy(aBuffer);
352 HBufC8* token0 = HBufC8::NewLC(aToken0.Length());
353 token0->Des().Copy(aToken0);
355 HBufC8* token1 = HBufC8::NewLC(aToken1.Length());
356 token1->Des().Copy(aToken1);
358 HBufC8* token2 = HBufC8::NewLC(aToken2.Length());
359 token2->Des().Copy(aToken2);
361 HBufC8* remainder = HBufC8::NewLC(aRemainder.Length());
362 remainder->Des().Copy(aRemainder);
364 iTestHarness->StartTestL(_L("Test TextUtils ExtractTokenFromList, single separator (8-bit)"));
366 error = DoExtractTokenFromListSingleSeparator<TPtrC8>(*buffer, *token0, *token1, *token2, *remainder, aSeparator);
368 iTestHarness->EndTest(error);
370 CleanupStack::PopAndDestroy(5, buffer);
377 template<class TPtrCType, class TDesCType>
378 LOCAL_C TInt DoTestTextUtilsRemove(const TDesCType& aBase,
379 const TDesCType& aRemoveLeft, TInt aConsumedLeft,
380 const TDesCType& aRemoveRight, TInt aConsumedRight,
381 const TDesCType& aRemoveBoth, TInt aConsumedBoth)
384 TPtrCType data = aBase;
385 TInt consumed = InetProtTextUtils::RemoveWhiteSpace(data, InetProtTextUtils::ERemoveLeft);
387 if( data.Compare(aRemoveLeft) != 0 || consumed != aConsumedLeft )
391 // Do remove right...
393 consumed = InetProtTextUtils::RemoveWhiteSpace(data, InetProtTextUtils::ERemoveRight);
395 if( data.Compare(aRemoveRight) != 0 || consumed != aConsumedRight )
401 consumed = InetProtTextUtils::RemoveWhiteSpace(data, InetProtTextUtils::ERemoveBoth);
403 if( data.Compare(aRemoveBoth) != 0 || consumed != aConsumedBoth )
410 template<class TDesCType>
411 LOCAL_C TInt DoTestTextUtilsDesToIntConversion(const TDesCType& aDes, TInt aInt, TBool aValid)
414 TInt error = InetProtTextUtils::ConvertDescriptorToInt(aDes, theInt);
415 if( (error < KErrNone && aValid) || (error >= KErrNone && !aValid) )
419 if( aValid && theInt != aInt )
426 template<class HBufCType, class TDesCType>
427 LOCAL_C TInt DoTestTextUtilsIntToDesConversion(TInt aInt, const TDesCType& aDes)
429 HBufCType* theDes = NULL;
430 TRAPD(error, InetProtTextUtils::ConvertIntToDescriptorL(aInt, theDes));
431 if( error != KErrNone )
435 if( theDes->CompareF(aDes) != 0 )
444 template<class TDesCType>
445 LOCAL_C TInt DoTestTextUtilsDesToHexConversion(const TDesCType& aDes, TInt aHex, TBool aValid)
448 TInt error = InetProtTextUtils::ConvertDescriptorToHex(aDes, theHex);
449 if( (error < KErrNone && aValid) || (error >= KErrNone && !aValid) )
453 if( aValid && theHex != aHex )
460 template<class HBufCType, class TDesCType>
461 LOCAL_C TInt DoTestTextUtilsHexToDesConversion(TInt aHex, const TDesCType& aDes)
463 HBufCType* theDes = NULL;
464 TRAPD(error, InetProtTextUtils::ConvertHexToDescriptorL(aHex, theDes));
465 if( error != KErrNone )
469 if( theDes->CompareF(aDes) != 0 )
478 template<class TPtrCType, class TDesCType>
479 LOCAL_C TInt DoTestExtractQuotedStringL(const TDesCType& aBuffer, const TDesCType& aString, const TDesCType& aRemainder, TInt aExpectedError)
481 TPtrCType buffer(aBuffer);
483 TRAPD( error, InetProtTextUtils::ExtractQuotedStringL(buffer, string) );
485 if( error == KErrNone && aExpectedError == KErrNone)
487 if( aString.Compare(string) == 0 && aRemainder.Compare(buffer) == 0 )
498 else if( error == aExpectedError )
500 // Got expected error - passed
510 template<class TPtrCType, class TDesCType>
511 LOCAL_C TInt DoExtractTokenFromListMultipleSeparator(
512 const TDesCType& aBuffer,
513 const TDesCType& aToken0,
514 const TDesCType& aToken1,
515 const TDesCType& aToken2,
516 const TDesCType& aRemainder,
517 const TDesCType& aSeparator
520 // get the first token...
521 TPtrCType buffer = aBuffer;
523 InetProtTextUtils::ExtractNextTokenFromList(buffer, token, aSeparator);
526 if( token.Compare(aToken0) != 0 )
531 // get the second token
532 InetProtTextUtils::ExtractNextTokenFromList(buffer, token, aSeparator);
535 if( token.Compare(aToken1) != 0 )
540 // get the third token
541 InetProtTextUtils::ExtractNextTokenFromList(buffer, token, aSeparator);
544 if( token.Compare(aToken2) != 0 )
549 // Check that the remainder is correct
550 if( buffer.Compare(aRemainder) != 0 )
558 template<class TPtrCType, class TDesCType>
559 LOCAL_C TInt DoExtractTokenFromListSingleSeparator(
560 const TDesCType& aBuffer,
561 const TDesCType& aToken0,
562 const TDesCType& aToken1,
563 const TDesCType& aToken2,
564 const TDesCType& aRemainder,
568 // get the first token...
569 TPtrCType buffer = aBuffer;
571 InetProtTextUtils::ExtractNextTokenFromList(buffer, token, aSeparator);
574 if( token.Compare(aToken0) != 0 )
579 // get the second token
580 InetProtTextUtils::ExtractNextTokenFromList(buffer, token, aSeparator);
583 if( token.Compare(aToken1) != 0 )
588 // get the third token
589 InetProtTextUtils::ExtractNextTokenFromList(buffer, token, aSeparator);
592 if( token.Compare(aToken2) != 0 )
597 // Check that the remainder is correct
598 if( buffer.Compare(aRemainder) != 0 )