os/textandloc/charconvfw/charconvplugins/test/rtest/tsrc/main/t_gb2312.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 1997-2010 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 <f32file.h>
    22 #include <charconv.h>
    23 #include "t_gb2312.h"
    24 
    25 #define test(cond)                                  \
    26     {                                               \
    27     TBool __bb = (cond);                            \
    28     TEST(__bb);                                     \
    29     if (!__bb)                                      \
    30         {                                           \
    31         ERR_PRINTF1(_L("ERROR: Test Failed"));      \
    32         User::Leave(1);                             \
    33         }                                           \
    34     }
    35 
    36 
    37 /**
    38 @SYMTestCaseID          SYSLIB-CHARCONV-CT-0502
    39 @SYMTestCaseDesc        Tests for converting GB 2312-80 character set to Unicode
    40 @SYMTestPriority        Medium
    41 @SYMTestActions         Tests for simple GB 2312-80 round trips,
    42 						Tests for varying the default Endianness
    43 @SYMTestExpectedResults Test must not fail
    44 @SYMREQ                 REQ0000
    45 */
    46 void CT_GB2312::DoE32MainL()
    47 	{
    48 	RFs fileServerSession;
    49 	CleanupClosePushL(fileServerSession);
    50 	User::LeaveIfError(fileServerSession.Connect());
    51 	CCnvCharacterSetConverter* characterSetConverter=CCnvCharacterSetConverter::NewLC();
    52 	CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* arrayOfCharacterSetsAvailable=CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableLC(fileServerSession);
    53 	INFO_PRINTF1(_L("Available:\n"));
    54 	for (TInt i=arrayOfCharacterSetsAvailable->Count()-1; i>=0; --i)
    55 		{
    56 		const CCnvCharacterSetConverter::SCharacterSet& charactersSet=(*arrayOfCharacterSetsAvailable)[i];
    57 		characterSetConverter->PrepareToConvertToOrFromL(charactersSet.Identifier(), *arrayOfCharacterSetsAvailable, fileServerSession);
    58 		TPtrC charactersSetName(charactersSet.Name());
    59 		if (charactersSet.NameIsFileName())
    60 			{
    61 			charactersSetName.Set(TParsePtrC(charactersSetName).Name());
    62 			}
    63 		INFO_PRINTF2(_L("    %S\n"), &charactersSetName);
    64 		}
    65 	INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-CHARCONV-CT-0502 Testing simple GB 2312-80 round trips "));
    66 	characterSetConverter->PrepareToConvertToOrFromL(KCharacterSetIdentifierGb2312, *arrayOfCharacterSetsAvailable, fileServerSession);
    67 	TBuf8<256> originalGb2312;
    68 	TBuf16<256> originalUnicode;
    69 	TBuf8<256> generatedGb2312;
    70 	TBuf16<256> generatedUnicode;
    71 	TInt state=CCnvCharacterSetConverter::KStateDefault;
    72 	//
    73 	INFO_PRINTF1(_L("Empty descriptor"));
    74 	originalUnicode=_L16("");
    75 	test(characterSetConverter->ConvertFromUnicode(generatedGb2312, originalUnicode)==0);
    76 	test(generatedGb2312==_L8(""));
    77 	test(characterSetConverter->ConvertToUnicode(generatedUnicode, generatedGb2312, state)==0);
    78 	test(state==CCnvCharacterSetConverter::KStateDefault);
    79 	test(generatedUnicode==originalUnicode);
    80 	INFO_PRINTF1(_L("A couple of sample characters"));
    81 	originalGb2312=_L8("\xb9\xa4\xc8\xcb");
    82 	test(characterSetConverter->ConvertToUnicode(generatedUnicode, originalGb2312, state)==0);
    83 	test(state==CCnvCharacterSetConverter::KStateDefault);
    84 	test(generatedUnicode.Length()==2);
    85 	test(generatedUnicode[0]==0x5de5);
    86 	test(generatedUnicode[1]==0x4eba);
    87 	test(characterSetConverter->ConvertFromUnicode(generatedGb2312, generatedUnicode)==0);
    88 	test(generatedGb2312==originalGb2312);
    89 	INFO_PRINTF1(_L("Converting trunctated GB 2312-80 which ends half-way through a multi-byte character"));
    90 	originalGb2312=_L8("\xb9\xa4\xc8");
    91 	test(characterSetConverter->ConvertToUnicode(generatedUnicode, originalGb2312, state)==1);
    92 	test(state==CCnvCharacterSetConverter::KStateDefault);
    93 	test(generatedUnicode.Length()==1);
    94 	test(generatedUnicode[0]==0x5de5);
    95 	originalGb2312=_L8("\xc8");
    96 	test(characterSetConverter->ConvertToUnicode(generatedUnicode, originalGb2312, state)==CCnvCharacterSetConverter::EErrorIllFormedInput);
    97 	INFO_PRINTF1(_L("Testing varying the default endianness"));
    98 	originalGb2312=_L8("\xb4\xf2");
    99 	characterSetConverter->SetDefaultEndiannessOfForeignCharacters(CCnvCharacterSetConverter::ELittleEndian);
   100 	test(characterSetConverter->ConvertToUnicode(generatedUnicode, originalGb2312, state)==0);
   101 	test(state==CCnvCharacterSetConverter::KStateDefault);
   102 	test(generatedUnicode.Length()==1);
   103 	test(generatedUnicode[0]==0x6253);
   104 	test(characterSetConverter->ConvertFromUnicode(generatedGb2312, generatedUnicode)==0);
   105 	test(generatedGb2312==originalGb2312);
   106 	characterSetConverter->SetDefaultEndiannessOfForeignCharacters(CCnvCharacterSetConverter::EBigEndian);
   107 	test(characterSetConverter->ConvertToUnicode(generatedUnicode, originalGb2312, state)==0);
   108 	test(state==CCnvCharacterSetConverter::KStateDefault);
   109 	test(generatedUnicode.Length()==1);
   110 	test(generatedUnicode[0]==0x6253);
   111 	test(characterSetConverter->ConvertFromUnicode(generatedGb2312, generatedUnicode)==0);
   112 	test(generatedGb2312==originalGb2312);
   113 	INFO_PRINTF1(_L("Testing a many-to-one conversion"));
   114 	originalUnicode.SetLength(4);
   115 	originalUnicode[0]=0x30fb;
   116 	originalUnicode[1]=0x00b7;
   117 	originalUnicode[2]=0x2014;
   118 	originalUnicode[3]=0x2015;
   119 	test(characterSetConverter->ConvertFromUnicode(generatedGb2312, originalUnicode)==0);
   120 	test(generatedGb2312==_L8("\xa1\xa4\xa1\xa4\xa1\xaa\xa1\xaa"));
   121 	test(characterSetConverter->ConvertToUnicode(generatedUnicode, generatedGb2312, state)==0);
   122 	test(state==CCnvCharacterSetConverter::KStateDefault);
   123 	test(generatedUnicode.Length()==4);
   124 	test(generatedUnicode[0]==0x00b7);
   125 	test(generatedUnicode[1]==0x00b7);
   126 	test(generatedUnicode[2]==0x2015);
   127 	test(generatedUnicode[3]==0x2015);
   128 	INFO_PRINTF1(_L("Testing GB2312 being aliased to GBK"));
   129 	TUint charSetId = characterSetConverter->ConvertStandardNameOfCharacterSetToIdentifierL(_L8("GB2312"), fileServerSession);
   130 	test(charSetId==KCharacterSetIdentifierGbk);
   131 	test(characterSetConverter->ConvertStandardNameOfCharacterSetToIdentifierL(_L8("csGB2312"), fileServerSession)==KCharacterSetIdentifierGbk);
   132 	test(characterSetConverter->ConvertStandardNameOfCharacterSetToIdentifierL(_L8("iso-ir-58"), fileServerSession)==KCharacterSetIdentifierGbk);
   133 	test(characterSetConverter->ConvertStandardNameOfCharacterSetToIdentifierL(_L8("chinese"), fileServerSession)==KCharacterSetIdentifierGbk);
   134 	test(characterSetConverter->ConvertStandardNameOfCharacterSetToIdentifierL(_L8("csISO58GB231280"), fileServerSession)==KCharacterSetIdentifierGbk);
   135 	test(characterSetConverter->ConvertMibEnumOfCharacterSetToIdentifierL(57, fileServerSession)==KCharacterSetIdentifierGbk);
   136 	test(characterSetConverter->ConvertMibEnumOfCharacterSetToIdentifierL(2025, fileServerSession)==KCharacterSetIdentifierGbk);
   137 	characterSetConverter->PrepareToConvertToOrFromL(charSetId, *arrayOfCharacterSetsAvailable, fileServerSession);
   138 	originalUnicode.SetLength(3);
   139 	originalUnicode[0]=0x9556; // this has a corresponding GB2312 character
   140 	originalUnicode[1]=0x9555; // this has a corresponding GBK character not in GB2312...
   141 	originalUnicode[2]=0x9394; // ... as does this
   142 	test(characterSetConverter->ConvertFromUnicode(generatedGb2312, originalUnicode)==0);
   143 	test(generatedGb2312.Length()==6);
   144 	test(generatedGb2312==_L8("\xef\xda\xe9\x46\xe6\x67"));
   145 	CleanupStack::PopAndDestroy(3); // arrayOfCharacterSetsAvailable and characterSetConverter and fileServerSession
   146 	}
   147 
   148 CT_GB2312::CT_GB2312()
   149     {
   150     SetTestStepName(KTestStep_T_GB2312);
   151     }
   152 
   153 TVerdict CT_GB2312::doTestStepL()
   154     {
   155     SetTestStepResult(EFail);
   156 
   157     __UHEAP_MARK;
   158     TRAPD(error1, DoE32MainL());    
   159     __UHEAP_MARKEND;
   160 
   161     if(error1 == KErrNone)
   162         {
   163         SetTestStepResult(EPass);
   164         }
   165 
   166     return TestStepResult();
   167     }
   168