os/textandloc/charconvfw/charconvplugins/test/rtest/tsrc/main/t_win1256.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2009-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_win1256.h"
    24 
    25 const TUint KCharacterSetIdentifier=KCharacterSetIdentifierWin1256;
    26 
    27 #ifdef __WINS__
    28 _LIT(KInputUnicodeFilename, "c:\\test\\data\\win1256_uni_input.dat");
    29 _LIT(KInputForeignFilename, "c:\\test\\data\\win1256_for_input.dat");
    30 _LIT(KExpectUnicodeFilename, "c:\\test\\data\\win1256_uni_expect.dat");
    31 _LIT(KExpectForeignFilename, "c:\\test\\data\\win1256_for_expect.dat");
    32 #else
    33 _LIT(KInputUnicodeFilename, "z:\\test\\data\\win1256_uni_input.dat");
    34 _LIT(KInputForeignFilename, "z:\\test\\data\\win1256_for_input.dat");
    35 _LIT(KExpectUnicodeFilename, "z:\\test\\data\\win1256_uni_expect.dat");
    36 _LIT(KExpectForeignFilename, "z:\\test\\data\\win1256_for_expect.dat");
    37 #endif
    38 
    39 #define test(cond)                                  \
    40     {                                               \
    41     TBool __bb = (cond);                            \
    42     TEST(__bb);                                     \
    43     if (!__bb)                                      \
    44         {                                           \
    45         ERR_PRINTF1(_L("ERROR: Test Failed"));      \
    46         User::Leave(1);                             \
    47         }                                           \
    48     }
    49 
    50 void CT_WIN1256::ReadDescL(TDes8& aDes, const TDesC& aFilename, RFs& aFs)
    51     {
    52     RFile file;
    53     TInt err = file.Open(aFs, aFilename, EFileRead);
    54     test(err == KErrNone);
    55     CleanupClosePushL(file);    
    56     err = file.Read(aDes);
    57     test(err == KErrNone);
    58     CleanupStack::PopAndDestroy(&file);
    59     }
    60         
    61 void CT_WIN1256::Merge_Big(TDesC8& aSource, TDes16& aTarget)
    62     {
    63     TInt length = aSource.Length();
    64     TInt i = 0;
    65     for(i=0;i<length-1;i++)
    66         {
    67         TInt64 temp = *(aSource.Ptr()+(i))*16*16 + *(aSource.Ptr()+i+1);
    68         aTarget.Append(temp);
    69         i++;
    70         }   
    71     }
    72         
    73 void CT_WIN1256::DoE32MainL()
    74     {
    75     RFs fileServerSession;
    76     CleanupClosePushL(fileServerSession);
    77     User::LeaveIfError(fileServerSession.Connect());
    78     CCnvCharacterSetConverter* characterSetConverter=CCnvCharacterSetConverter::NewLC();
    79     CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* arrayOfCharacterSetsAvailable=\
    80         CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableLC(fileServerSession);
    81     
    82     INFO_PRINTF1(_L("Available:\n"));
    83     for (TInt i=arrayOfCharacterSetsAvailable->Count()-1; i>=0; --i)
    84         {
    85         const CCnvCharacterSetConverter::SCharacterSet& charactersSet=(*arrayOfCharacterSetsAvailable)[i];
    86         characterSetConverter->PrepareToConvertToOrFromL(charactersSet.Identifier(), *arrayOfCharacterSetsAvailable, fileServerSession);
    87         TPtrC charactersSetName(charactersSet.Name());
    88         if (charactersSet.NameIsFileName())
    89             {
    90             charactersSetName.Set(TParsePtrC(charactersSetName).Name());
    91             }
    92         INFO_PRINTF2(_L("    %S\n"), &charactersSetName);
    93         }
    94     
    95     INFO_PRINTF1(_L("Encoding from Unicode to Foreign"));
    96     characterSetConverter->PrepareToConvertToOrFromL(KCharacterSetIdentifier, *arrayOfCharacterSetsAvailable, fileServerSession);
    97     TBuf8<512> temp;
    98     TBuf16<256> originalUnicode;
    99     TBuf8<256> generatedForeign;
   100     TBuf16<256> generatedUnicode;
   101     ReadDescL(temp, KInputUnicodeFilename, fileServerSession);
   102     Merge_Big(temp, originalUnicode);
   103     test(characterSetConverter->ConvertFromUnicode(generatedForeign, originalUnicode) == 0);
   104     ReadDescL(temp, KExpectForeignFilename, fileServerSession);
   105     test(generatedForeign == temp);
   106 
   107     INFO_PRINTF1(_L("Encoding from Foreign to Unicode"));   
   108     ReadDescL(generatedForeign, KInputForeignFilename, fileServerSession);
   109     TInt state=CCnvCharacterSetConverter::KStateDefault;
   110     test( 0 == characterSetConverter->ConvertToUnicode(generatedUnicode, generatedForeign, state));
   111     ReadDescL(temp, KExpectUnicodeFilename, fileServerSession);
   112     originalUnicode.Zero();
   113     Merge_Big(temp, originalUnicode); 
   114     test(generatedUnicode == originalUnicode);
   115     
   116     CleanupStack::PopAndDestroy(3);
   117     }
   118 
   119 CT_WIN1256::CT_WIN1256()
   120     {
   121     SetTestStepName(KTestStep_T_WIN1256);
   122     }
   123 
   124 TVerdict CT_WIN1256::doTestStepL()
   125     {
   126     SetTestStepResult(EFail);
   127 
   128     __UHEAP_MARK;
   129     TRAPD(error1, DoE32MainL());
   130     __UHEAP_MARKEND;
   131 
   132     if(error1 == KErrNone)
   133         {
   134         SetTestStepResult(EPass);
   135         }
   136 
   137     return TestStepResult();
   138     }