charconvfw/Charconv/ongoing/test/source/main/TGB2312.CPP
changeset 32 8b9155204a54
equal deleted inserted replaced
31:b9ad20498fb4 32:8b9155204a54
       
     1 /*
       
     2 * Copyright (c) 2312 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 the License "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 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 #include <e32std.h>
       
    26 #include <e32base.h>
       
    27 #include <e32test.h>
       
    28 #include <f32file.h>
       
    29 #include <charconv.h>
       
    30 
       
    31 ///////////////////////////////////////////////////////////////////////////////////////
       
    32 
       
    33 RTest TheTest(_L("TGB2312"));
       
    34 	
       
    35 ///////////////////////////////////////////////////////////////////////////////////////
       
    36 ///////////////////////////////////////////////////////////////////////////////////////
       
    37 //Tests macroses and functions.
       
    38 //If (!aValue) then the test will be panicked, the test data files will be deleted.
       
    39 static void Check(TInt aValue, TInt aLine)
       
    40 	{
       
    41 	if(!aValue)
       
    42 		{
       
    43 		TheTest(EFalse, aLine);
       
    44 		}
       
    45 	}
       
    46 //If (aValue != aExpected) then the test will be panicked, the test data files will be deleted.
       
    47 static void Check(TInt aValue, TInt aExpected, TInt aLine)
       
    48 	{
       
    49 	if(aValue != aExpected)
       
    50 		{
       
    51 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
    52 		TheTest(EFalse, aLine);
       
    53 		}
       
    54 	}
       
    55 //Use these to test conditions.
       
    56 #define TEST(arg) ::Check((arg), __LINE__)
       
    57 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
       
    58 
       
    59 ///////////////////////////////////////////////////////////////////////////////////////
       
    60 ///////////////////////////////////////////////////////////////////////////////////////
       
    61 /**
       
    62 @SYMTestCaseID          SYSLIB-CHARCONV-CT-0502
       
    63 @SYMTestCaseDesc        Tests for converting GB 2312-80 character set to Unicode 
       
    64 @SYMTestPriority        Medium
       
    65 @SYMTestActions         Tests for simple GB 2312-80 round trips,
       
    66 						Tests for varying the default Endianness
       
    67 @SYMTestExpectedResults Test must not fail
       
    68 @SYMREQ                 REQ0000
       
    69 */
       
    70 LOCAL_C void DoE32MainL()
       
    71 	{
       
    72 	RFs fileServerSession;
       
    73 	CleanupClosePushL(fileServerSession);
       
    74 	User::LeaveIfError(fileServerSession.Connect());
       
    75 	CCnvCharacterSetConverter* characterSetConverter=CCnvCharacterSetConverter::NewLC();
       
    76 	CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* arrayOfCharacterSetsAvailable=CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableLC(fileServerSession);
       
    77 	TheTest.Printf(_L("Available:\n"));
       
    78 	for (TInt i=arrayOfCharacterSetsAvailable->Count()-1; i>=0; --i)
       
    79 		{
       
    80 		const CCnvCharacterSetConverter::SCharacterSet& charactersSet=(*arrayOfCharacterSetsAvailable)[i];
       
    81 		characterSetConverter->PrepareToConvertToOrFromL(charactersSet.Identifier(), *arrayOfCharacterSetsAvailable, fileServerSession);
       
    82 		TPtrC charactersSetName(charactersSet.Name());
       
    83 		if (charactersSet.NameIsFileName())
       
    84 			{
       
    85 			charactersSetName.Set(TParsePtrC(charactersSetName).Name());
       
    86 			}
       
    87 		TheTest.Printf(_L("    %S\n"), &charactersSetName);
       
    88 		}
       
    89 	TheTest.Start(_L("Testing simple GB 2312-80 round trips"));
       
    90 	characterSetConverter->PrepareToConvertToOrFromL(KCharacterSetIdentifierGb2312, *arrayOfCharacterSetsAvailable, fileServerSession);
       
    91 	TBuf8<256> originalGb2312;
       
    92 	TBuf16<256> originalUnicode;
       
    93 	TBuf8<256> generatedGb2312;
       
    94 	TBuf16<256> generatedUnicode;
       
    95 	TInt state=CCnvCharacterSetConverter::KStateDefault;
       
    96 	//
       
    97 	TheTest.Next(_L("Empty descriptor"));
       
    98 	originalUnicode=_L16("");
       
    99 	TEST(characterSetConverter->ConvertFromUnicode(generatedGb2312, originalUnicode)==0);
       
   100 	TEST(generatedGb2312==_L8(""));
       
   101 	TEST(characterSetConverter->ConvertToUnicode(generatedUnicode, generatedGb2312, state)==0);
       
   102 	TEST(state==CCnvCharacterSetConverter::KStateDefault);
       
   103 	TEST(generatedUnicode==originalUnicode);
       
   104 	TheTest.Next(_L("A couple of sample characters"));
       
   105 	originalGb2312=_L8("\xb9\xa4\xc8\xcb");
       
   106 	TEST(characterSetConverter->ConvertToUnicode(generatedUnicode, originalGb2312, state)==0);
       
   107 	TEST(state==CCnvCharacterSetConverter::KStateDefault);
       
   108 	TEST(generatedUnicode.Length()==2);
       
   109 	TEST(generatedUnicode[0]==0x5de5);
       
   110 	TEST(generatedUnicode[1]==0x4eba);
       
   111 	TEST(characterSetConverter->ConvertFromUnicode(generatedGb2312, generatedUnicode)==0);
       
   112 	TEST(generatedGb2312==originalGb2312);
       
   113 	TheTest.Next(_L("Converting trunctated GB 2312-80 which ends half-way through a multi-byte character"));
       
   114 	originalGb2312=_L8("\xb9\xa4\xc8");
       
   115 	TEST(characterSetConverter->ConvertToUnicode(generatedUnicode, originalGb2312, state)==1);
       
   116 	TEST(state==CCnvCharacterSetConverter::KStateDefault);
       
   117 	TEST(generatedUnicode.Length()==1);
       
   118 	TEST(generatedUnicode[0]==0x5de5);
       
   119 	originalGb2312=_L8("\xc8");
       
   120 	TEST(characterSetConverter->ConvertToUnicode(generatedUnicode, originalGb2312, state)==CCnvCharacterSetConverter::EErrorIllFormedInput);
       
   121 	TheTest.Next(_L("Testing varying the default endianness"));
       
   122 	originalGb2312=_L8("\xb4\xf2");
       
   123 	characterSetConverter->SetDefaultEndiannessOfForeignCharacters(CCnvCharacterSetConverter::ELittleEndian);
       
   124 	TEST(characterSetConverter->ConvertToUnicode(generatedUnicode, originalGb2312, state)==0);
       
   125 	TEST(state==CCnvCharacterSetConverter::KStateDefault);
       
   126 	TEST(generatedUnicode.Length()==1);
       
   127 	TEST(generatedUnicode[0]==0x6253);
       
   128 	TEST(characterSetConverter->ConvertFromUnicode(generatedGb2312, generatedUnicode)==0);
       
   129 	TEST(generatedGb2312==originalGb2312);
       
   130 	characterSetConverter->SetDefaultEndiannessOfForeignCharacters(CCnvCharacterSetConverter::EBigEndian);
       
   131 	TEST(characterSetConverter->ConvertToUnicode(generatedUnicode, originalGb2312, state)==0);
       
   132 	TEST(state==CCnvCharacterSetConverter::KStateDefault);
       
   133 	TEST(generatedUnicode.Length()==1);
       
   134 	TEST(generatedUnicode[0]==0x6253);
       
   135 	TEST(characterSetConverter->ConvertFromUnicode(generatedGb2312, generatedUnicode)==0);
       
   136 	TEST(generatedGb2312==originalGb2312);
       
   137 	TheTest.Next(_L("Testing a many-to-one conversion"));
       
   138 	originalUnicode.SetLength(4);
       
   139 	originalUnicode[0]=0x30fb;
       
   140 	originalUnicode[1]=0x00b7;
       
   141 	originalUnicode[2]=0x2014;
       
   142 	originalUnicode[3]=0x2015;
       
   143 	TEST(characterSetConverter->ConvertFromUnicode(generatedGb2312, originalUnicode)==0);
       
   144 	TEST(generatedGb2312==_L8("\xa1\xa4\xa1\xa4\xa1\xaa\xa1\xaa"));
       
   145 	TEST(characterSetConverter->ConvertToUnicode(generatedUnicode, generatedGb2312, state)==0);
       
   146 	TEST(state==CCnvCharacterSetConverter::KStateDefault);
       
   147 	TEST(generatedUnicode.Length()==4);
       
   148 	TEST(generatedUnicode[0]==0x00b7);
       
   149 	TEST(generatedUnicode[1]==0x00b7);
       
   150 	TEST(generatedUnicode[2]==0x2015);
       
   151 	TEST(generatedUnicode[3]==0x2015);
       
   152 	CleanupStack::PopAndDestroy(3); // arrayOfCharacterSetsAvailable and characterSetConverter and fileServerSession
       
   153 	}
       
   154 
       
   155 GLDEF_C TInt E32Main()
       
   156 	{
       
   157 	__UHEAP_MARK;
       
   158 
       
   159 	TheTest.Title();
       
   160 
       
   161 	CTrapCleanup* trapCleanup=CTrapCleanup::New();
       
   162 	TEST(trapCleanup != NULL);
       
   163 
       
   164 	TRAPD(error, DoE32MainL());
       
   165 	TEST2(error, KErrNone);
       
   166 
       
   167 	delete trapCleanup;
       
   168 
       
   169 	TheTest.End();
       
   170 	TheTest.Close();
       
   171 
       
   172 	__UHEAP_MARKEND;
       
   173 	return KErrNone;
       
   174 	}
       
   175