charconvfw/charconvplugins/test/rtest/tsrc/main/treplacement.cpp
changeset 53 11e2bb0d14ba
parent 46 6124ff6478cc
child 59 7d891bb52a7d
equal deleted inserted replaced
46:6124ff6478cc 53:11e2bb0d14ba
     1 /*
       
     2 * Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <e32base.h>
       
    21 #include <e32test.h>
       
    22 #include <f32file.h>
       
    23 #include <charconv.h>
       
    24 
       
    25 ///////////////////////////////////////////////////////////////////////////////////////
       
    26 
       
    27 RTest TheTest(_L("TReplacement"));
       
    28 
       
    29 ///////////////////////////////////////////////////////////////////////////////////////
       
    30 ///////////////////////////////////////////////////////////////////////////////////////
       
    31 //Tests macroses and functions.
       
    32 //If (!aValue) then the test will be panicked, the test data files will be deleted.
       
    33 static void Check(TInt aValue, TInt aLine)
       
    34 	{
       
    35 	if(!aValue)
       
    36 		{
       
    37 		TheTest(EFalse, aLine);
       
    38 		}
       
    39 	}
       
    40 //If (aValue != aExpected) then the test will be panicked, the test data files will be deleted.
       
    41 static void Check(TInt aValue, TInt aExpected, TInt aLine)
       
    42 	{
       
    43 	if(aValue != aExpected)
       
    44 		{
       
    45 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
    46 		TheTest(EFalse, aLine);
       
    47 		}
       
    48 	}
       
    49 //Use these to test conditions.
       
    50 #define TEST(arg) ::Check((arg), __LINE__)
       
    51 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
       
    52 
       
    53 ///////////////////////////////////////////////////////////////////////////////////////
       
    54 ///////////////////////////////////////////////////////////////////////////////////////
       
    55 /**
       
    56 @SYMTestCaseID          SYSLIB-CHARCONV-CT-0546
       
    57 @SYMTestCaseDesc        Tests for the conversion of uncovertible Unicode characters
       
    58 @SYMTestPriority        Medium
       
    59 @SYMTestActions         Tests for the replacement of uncovertible Unicode characters
       
    60 @SYMTestExpectedResults Test must not fail
       
    61 @SYMREQ                 REQ0000
       
    62 */
       
    63 LOCAL_C void DoE32MainL()
       
    64 	{
       
    65 	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-CHARCONV-CT-0546 "));
       
    66 	RFs fileServerSession;
       
    67 	CleanupClosePushL(fileServerSession);
       
    68 	User::LeaveIfError(fileServerSession.Connect());
       
    69 	CCnvCharacterSetConverter* characterSetConverter=CCnvCharacterSetConverter::NewLC();
       
    70 	CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* arrayOfCharacterSetsAvailable=CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableLC(fileServerSession);
       
    71 	TheTest.Printf(_L("Available:\n"));
       
    72 	for (TInt i=arrayOfCharacterSetsAvailable->Count()-1; i>=0; --i)
       
    73 		{
       
    74 		const CCnvCharacterSetConverter::SCharacterSet& charactersSet=(*arrayOfCharacterSetsAvailable)[i];
       
    75 		characterSetConverter->PrepareToConvertToOrFromL(charactersSet.Identifier(), *arrayOfCharacterSetsAvailable, fileServerSession);
       
    76 		TPtrC charactersSetName(charactersSet.Name());
       
    77 		if (charactersSet.NameIsFileName())
       
    78 			{
       
    79 			charactersSetName.Set(TParsePtrC(charactersSetName).Name());
       
    80 			}
       
    81 		TheTest.Printf(_L("    %S\n"), &charactersSetName);
       
    82 		}
       
    83 	characterSetConverter->PrepareToConvertToOrFromL(KCharacterSetIdentifierGb2312, *arrayOfCharacterSetsAvailable, fileServerSession);
       
    84 	TheTest.Next(_L("Testing various values for iReplacementForUnconvertibleUnicodeCharacters"));
       
    85 	//
       
    86 	TheTest.Next(_L("Testing iReplacementForUnconvertibleUnicodeCharacters being long"));
       
    87 	{
       
    88 	TBuf16<50> originalUnicode;
       
    89 	originalUnicode.Format(_L16("Here is a Georgian character - %c"), 0x10da);
       
    90 	characterSetConverter->SetReplacementForUnconvertibleUnicodeCharactersL(_L8("[something quite long]"));
       
    91 	const TInt KLengthOfForeignBuffer=100;
       
    92 	TUint8 foreignBuffer[KLengthOfForeignBuffer];
       
    93 	for (TInt i=31; ; ++i)
       
    94 		{
       
    95 		TEST(i<=KLengthOfForeignBuffer);
       
    96 		TPtr8 generatedForeign(foreignBuffer, i);
       
    97 		TInt numberOfUnconvertibleCharacters;
       
    98 		TInt indexOfFirstUnconvertibleCharacter;
       
    99 		const TInt returnValue=characterSetConverter->ConvertFromUnicode(generatedForeign, originalUnicode, numberOfUnconvertibleCharacters, indexOfFirstUnconvertibleCharacter);
       
   100 		if (i==53)
       
   101 			{
       
   102 			TEST(returnValue==0);
       
   103 			TEST(generatedForeign==_L8("Here is a Georgian character - [something quite long]"));
       
   104 			TEST(numberOfUnconvertibleCharacters==1);
       
   105 			TEST(indexOfFirstUnconvertibleCharacter==31);
       
   106 			break;
       
   107 			}
       
   108 		TEST(returnValue==1);
       
   109 		TEST(generatedForeign==_L8("Here is a Georgian character - "));
       
   110 		TEST(numberOfUnconvertibleCharacters==0);
       
   111 		TEST(indexOfFirstUnconvertibleCharacter==-1); // not something that the API guarantees (it's undefined if numberOfUnconvertibleCharacters==0), but we'll check it any
       
   112 		}
       
   113 	}
       
   114 	TheTest.Next(_L("Testing lots of unconvertible Unicode characters"));
       
   115 	{
       
   116 	characterSetConverter->SetReplacementForUnconvertibleUnicodeCharactersL(_L8("?Q"));
       
   117 	TInt numberOfUnconvertibleCharacters;
       
   118 	TInt indexOfFirstUnconvertibleCharacter;
       
   119 	TInt i;
       
   120 	TPtr16 originalUnicode(HBufC16::NewMaxLC(10+(sizeof(CCnvCharacterSetConverter::TArrayOfAscendingIndices)/sizeof(TUint16)))->Des());
       
   121 	TPtr8 generatedForeign(HBufC8::NewLC(originalUnicode.Length()*sizeof(TUint16))->Des());
       
   122 	originalUnicode.Fill(0x7535);
       
   123 	TEST(characterSetConverter->ConvertFromUnicode(generatedForeign, originalUnicode, numberOfUnconvertibleCharacters, indexOfFirstUnconvertibleCharacter)==0);
       
   124 	TEST(generatedForeign.Length()>0);
       
   125 	TEST(generatedForeign.Length()==originalUnicode.Length()*STATIC_CAST(TInt, sizeof(TUint16)));
       
   126 	TEST(numberOfUnconvertibleCharacters==0);
       
   127 	for (i=generatedForeign.Length()-1; i>=0; i-=2)
       
   128 		{
       
   129 		TEST(generatedForeign[i-1]==0xb5);
       
   130 		TEST(generatedForeign[i]==0xe7);
       
   131 		}
       
   132 	originalUnicode.Fill(0x0f0a); // a Tibetan character, not in GB 2312-80
       
   133 	const TInt returnValue=characterSetConverter->ConvertFromUnicode(generatedForeign, originalUnicode, numberOfUnconvertibleCharacters, indexOfFirstUnconvertibleCharacter);
       
   134 	TEST(returnValue==0);
       
   135 	TEST(generatedForeign.Length()>0);
       
   136 	TEST(generatedForeign.Length()==(originalUnicode.Length()-returnValue)*STATIC_CAST(TInt, sizeof(TUint16)));
       
   137 	TEST(numberOfUnconvertibleCharacters==25);
       
   138 	TEST(indexOfFirstUnconvertibleCharacter==0);
       
   139 	for (i=generatedForeign.Length()-1; i>=0; i-=2)
       
   140 		{
       
   141 		TEST(generatedForeign[i-1]=='?');
       
   142 		TEST(generatedForeign[i]=='Q');
       
   143 		}
       
   144 	CleanupStack::PopAndDestroy(2); // generatedForeign and originalUnicode
       
   145 	}
       
   146 	CleanupStack::PopAndDestroy(3); // arrayOfCharacterSetsAvailable and characterSetConverter and fileServerSession
       
   147 	}
       
   148 
       
   149 GLDEF_C TInt E32Main()
       
   150 	{
       
   151 	__UHEAP_MARK;
       
   152 
       
   153 	TheTest.Title();
       
   154 
       
   155 	CTrapCleanup* trapCleanup=CTrapCleanup::New();
       
   156 	TEST(trapCleanup != NULL);
       
   157 
       
   158 	TRAPD(error, DoE32MainL());
       
   159 	TEST2(error, KErrNone);
       
   160 
       
   161 	delete trapCleanup;
       
   162 
       
   163 	TheTest.End();
       
   164 	TheTest.Close();
       
   165 
       
   166 	__UHEAP_MARKEND;
       
   167 	return KErrNone;
       
   168 	}
       
   169