os/textandloc/charconvfw/charconv_fw/test/rtest/tsrc/utf/tcomp7.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 1997-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 <utf.h>
    21 #include "utf7.h"
    22 #include "t_comp7.h"
    23 
    24 ///////////////////////////////////////////////////////////////////////////////////////
    25 
    26 
    27 #define test(cond)                                  \
    28     TEST((cond));                                   \
    29     if (!(cond))                                    \
    30         {                                           \
    31         ERR_PRINTF1(_L("ERROR: Test Failed"));      \
    32         User::Leave(1);                             \
    33         }
    34 
    35 
    36 
    37 ///////////////////////////////////////////////////////////////////////////////////////
    38 ///////////////////////////////////////////////////////////////////////////////////////
    39 
    40 enum TPanic
    41 	{
    42 	EPanicNoFreeSpaceAtEndOfBuffer1=1,
    43 	EPanicNoFreeSpaceAtEndOfBuffer2
    44 	};
    45 
    46 LOCAL_C void Panic(TPanic aPanic)
    47 	{
    48 	User::Panic(_L("TCOMP7"), aPanic);
    49 	}
    50 /**
    51 @SYMTestCaseID          SYSLIB-CHARCONV-CT-0565
    52 @SYMTestCaseDesc        Comparing behaviour with the Unicode CD-ROM UTF-7 sample code
    53 @SYMTestPriority        Medium
    54 @SYMTestActions        	Tests for CnvUtfConverter::ConvertToUnicodeFromUtf7() function
    55 @SYMTestExpectedResults Test must not fail
    56 @SYMREQ                 REQ0000
    57 */
    58 void CT_COMP7::TestCOMP7()
    59 	{
    60     INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-CHARCONV-CT-0565 Comparing behaviour with the Unicode CD-ROM UTF-7 sample code "));
    61 	TDes16* originalUnicode=new TBuf16<512>;
    62 	TDes8* generatedUtf7=new TBuf8<1024>;
    63 	TDes8* otherGeneratedUtf7=new TBuf8<1024>;
    64 	TDes16* generatedUnicode=new TBuf16<512>;
    65 	TDes16* otherGeneratedUnicode=new TBuf16<512>;
    66 	TInt state=CnvUtfConverter::KStateDefault;
    67 	//
    68 	*originalUnicode=_L16("");
    69 	originalUnicode->AppendFormat(_L16("Here are some European letters: %c%c%c%c%c%c%c%c%c%c. "), 0x0117, 0x010d, 0x00f1, 0x00df, 0x00d8, 0x0142, 0x0151, 0x016f, 0x0131, 0x00c9);
    70 	originalUnicode->AppendFormat(_L16("Here are some Greek letters: %c%c%c%c%c%c%c%c%c%c. "),    0x03a3, 0x03bf, 0x03bc, 0x03b4, 0x03b5, 0x0345, 0x03a9, 0x0392, 0x0395, 0x03a1);
    71 	originalUnicode->AppendFormat(_L16("Here are some Cyrillic letters: %c%c%c%c%c%c%c%c%c%c. "), 0x0437, 0x0430, 0x0440, 0x0435, 0x0438, 0x0306, 0x0411, 0x041e, 0x0423, 0x0416);
    72 	originalUnicode->AppendFormat(_L16("Here are some Georgian letters: %c%c%c%c%c%c%c%c%c%c. "), 0x10da, 0x10e0, 0x10e9, 0x10ef, 0x10d2, 0x10d7, 0x10e2, 0x10d0, 0x10ed, 0x10e8);
    73 	originalUnicode->AppendFormat(_L16("Here are some Japanese kana: %c%c%c%c%c%c%c%c%c%c. "),    0x30a2, 0x30c0, 0x30d5, 0x30b7, 0x30ba, 0x3091, 0x3093, 0x3055, 0x3074, 0x3080);
    74 	originalUnicode->AppendFormat(_L16("Here are some Han characters: %c%c%c%c%c%c%c%c%c%c. "),   0x7f0c, 0x7a92, 0x6d90, 0x6272, 0x5934, 0x5973, 0x55ce, 0x516c, 0x4ec0, 0x4e00);
    75 	originalUnicode->AppendFormat(_L16("Here are some surrogate pairs: %c%c%c%c%c%c%c%c%c%c. "),   0xd800, 0xdc00, 0xdbff, 0xdfff, 0xdb80, 0xdddd, 0xdbff, 0xdcba, 0xdbeb, 0xdeb0);
    76 	originalUnicode->AppendFormat(_L16("Here are some odd bits and pieces: =+-/*?#~'@!\"$%%^&\\|()[]{}<>_;:,. "));
    77 	enum TUtf7Flags
    78 		{
    79 		EUtf7FlagOptional		=0x00000001,
    80 		EUtf7FlagOtherOptional	=0x00000002,
    81 		EUtf7FlagOtherVerbose	=0x00000004,
    82 		EUtf7FlagCrossOver		=0x00000008,
    83 		EUtf7FlagDone			=0x00000010
    84 		};
    85 	INFO_PRINTF1(_L("Round-trips via UTF-7"));
    86 	for (TInt i=0; ~i&EUtf7FlagDone; ++i)
    87 		{
    88         test(CnvUtfConverter::ConvertFromUnicodeToUtf7(*generatedUtf7, *originalUnicode, i&EUtf7FlagOptional)==0);
    89 		{__ASSERT_ALWAYS(originalUnicode->Length()<originalUnicode->MaxLength(), Panic(EPanicNoFreeSpaceAtEndOfBuffer1));
    90 		TUint16* sourceStart=CONST_CAST(TUint16*, originalUnicode->Ptr());
    91 		TUint16* sourceEnd=sourceStart+originalUnicode->Length();
    92 		char* targetStart=REINTERPRET_CAST(char*, CONST_CAST(TUint8*, otherGeneratedUtf7->Ptr()));
    93 		char* targetEnd=targetStart+(otherGeneratedUtf7->MaxLength()-1);
    94 		test(ConvertUCS2toUTF7(&sourceStart, sourceEnd, &targetStart, targetEnd, i&EUtf7FlagOtherOptional, i&EUtf7FlagOtherVerbose)==ok);
    95 		otherGeneratedUtf7->SetLength(targetStart-REINTERPRET_CAST(char*, CONST_CAST(TUint8*, otherGeneratedUtf7->Ptr())));}
    96 		TDes8& sourceForReturnTrip=(i&EUtf7FlagCrossOver)? *otherGeneratedUtf7: *generatedUtf7;
    97 		TDes8& otherSourceForReturnTrip=(i&EUtf7FlagCrossOver)? *generatedUtf7: *otherGeneratedUtf7;
    98 		test(CnvUtfConverter::ConvertToUnicodeFromUtf7(*generatedUnicode, sourceForReturnTrip, state)==0);
    99 		test(state==CnvUtfConverter::KStateDefault);
   100 		{__ASSERT_ALWAYS(otherSourceForReturnTrip.Length()<otherSourceForReturnTrip.MaxLength(), Panic(EPanicNoFreeSpaceAtEndOfBuffer2));
   101 		char* sourceStart=REINTERPRET_CAST(char*, CONST_CAST(TUint8*, otherSourceForReturnTrip.Ptr()));
   102 		char* sourceEnd=sourceStart+otherSourceForReturnTrip.Length();
   103 		TUint16* targetStart=CONST_CAST(TUint16*, otherGeneratedUnicode->Ptr());
   104 		TUint16* targetEnd=targetStart+(otherGeneratedUnicode->MaxLength()-1);
   105 		test(ConvertUTF7toUCS2(&sourceStart, sourceEnd, &targetStart, targetEnd)==ok);
   106 		otherGeneratedUnicode->SetLength(targetStart-otherGeneratedUnicode->Ptr());}
   107 		test(*generatedUnicode==*originalUnicode);
   108 		test(*generatedUnicode==*otherGeneratedUnicode);
   109 		}
   110 	//
   111 	delete originalUnicode;
   112 	delete generatedUtf7;
   113 	delete otherGeneratedUtf7;
   114 	delete generatedUnicode;
   115 	delete otherGeneratedUnicode;
   116 
   117 	}
   118 
   119 CT_COMP7::CT_COMP7()
   120     {
   121     SetTestStepName(KTestStep_T_COMP7);
   122     }
   123 
   124 TVerdict CT_COMP7::doTestStepL()
   125     {
   126     SetTestStepResult(EFail);
   127 
   128     __UHEAP_MARK;
   129 
   130     TRAPD(error1, TestCOMP7());
   131 
   132     __UHEAP_MARKEND;
   133 
   134     if(error1 == KErrNone )
   135         {
   136         SetTestStepResult(EPass);
   137         }
   138 
   139     return TestStepResult();
   140     }