charconvfw/Charconv/ongoing/test/source/utf/TCOMP7.CPP
changeset 0 1fb32624e06b
child 16 56cd22a7a1cb
equal deleted inserted replaced
-1:000000000000 0:1fb32624e06b
       
     1 /*
       
     2 * Copyright (c) 1997-2005 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 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 #include <e32std.h>
       
    26 #include <e32test.h>
       
    27 #include <utf.h>
       
    28 #include <UTF7.H>
       
    29 
       
    30 ///////////////////////////////////////////////////////////////////////////////////////
       
    31 
       
    32 RTest TheTest(_L("TComp7"));
       
    33 	
       
    34 ///////////////////////////////////////////////////////////////////////////////////////
       
    35 ///////////////////////////////////////////////////////////////////////////////////////
       
    36 //Tests macroses and functions.
       
    37 static void Check(TInt aValue, TInt aLine)
       
    38 	{
       
    39 	if(!aValue)
       
    40 		{
       
    41 		TheTest(EFalse, aLine);
       
    42 		}
       
    43 	}
       
    44 #define TEST(arg) ::Check((arg), __LINE__)
       
    45 
       
    46 ///////////////////////////////////////////////////////////////////////////////////////
       
    47 ///////////////////////////////////////////////////////////////////////////////////////
       
    48 	
       
    49 enum TPanic
       
    50 	{
       
    51 	EPanicNoFreeSpaceAtEndOfBuffer1=1,
       
    52 	EPanicNoFreeSpaceAtEndOfBuffer2
       
    53 	};
       
    54 
       
    55 LOCAL_C void Panic(TPanic aPanic)
       
    56 	{
       
    57 	User::Panic(_L("TCOMP7"), aPanic);
       
    58 	}
       
    59 /**
       
    60 @SYMTestCaseID          SYSLIB-CHARCONV-CT-0565
       
    61 @SYMTestCaseDesc        Comparing behaviour with the Unicode CD-ROM UTF-7 sample code
       
    62 @SYMTestPriority        Medium
       
    63 @SYMTestActions        	Tests for CnvUtfConverter::ConvertToUnicodeFromUtf7() function
       
    64 @SYMTestExpectedResults Test must not fail
       
    65 @SYMREQ                 REQ0000
       
    66 */
       
    67 GLDEF_C TInt E32Main()
       
    68 	{
       
    69 	TheTest.Start(_L("Comparing behaviour with the Unicode CD-ROM UTF-7 sample code"));
       
    70 	TDes16* originalUnicode=new TBuf16<512>;
       
    71 	TDes8* generatedUtf7=new TBuf8<1024>;
       
    72 	TDes8* otherGeneratedUtf7=new TBuf8<1024>;
       
    73 	TDes16* generatedUnicode=new TBuf16<512>;
       
    74 	TDes16* otherGeneratedUnicode=new TBuf16<512>;
       
    75 	TInt state=CnvUtfConverter::KStateDefault;
       
    76 	//
       
    77 	*originalUnicode=_L16("");
       
    78 	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);
       
    79 	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);
       
    80 	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);
       
    81 	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);
       
    82 	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);
       
    83 	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);
       
    84 	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);
       
    85 	originalUnicode->AppendFormat(_L16("Here are some odd bits and pieces: =+-/*?#~'@!\"$%%^&\\|()[]{}<>_;:,. "));
       
    86 	enum TUtf7Flags
       
    87 		{
       
    88 		EUtf7FlagOptional		=0x00000001,
       
    89 		EUtf7FlagOtherOptional	=0x00000002,
       
    90 		EUtf7FlagOtherVerbose	=0x00000004,
       
    91 		EUtf7FlagCrossOver		=0x00000008,
       
    92 		EUtf7FlagDone			=0x00000010
       
    93 		};
       
    94 	TheTest.Next(_L("Round-trips via UTF-7"));
       
    95 	for (TInt i=0; ~i&EUtf7FlagDone; ++i)
       
    96 		{
       
    97 		TEST(CnvUtfConverter::ConvertFromUnicodeToUtf7(*generatedUtf7, *originalUnicode, i&EUtf7FlagOptional)==0);
       
    98 		{__ASSERT_ALWAYS(originalUnicode->Length()<originalUnicode->MaxLength(), Panic(EPanicNoFreeSpaceAtEndOfBuffer1));
       
    99 		TUint16* sourceStart=CONST_CAST(TUint16*, originalUnicode->Ptr());
       
   100 		TUint16* sourceEnd=sourceStart+originalUnicode->Length();
       
   101 		char* targetStart=REINTERPRET_CAST(char*, CONST_CAST(TUint8*, otherGeneratedUtf7->Ptr()));
       
   102 		char* targetEnd=targetStart+(otherGeneratedUtf7->MaxLength()-1);
       
   103 		TEST(ConvertUCS2toUTF7(&sourceStart, sourceEnd, &targetStart, targetEnd, i&EUtf7FlagOtherOptional, i&EUtf7FlagOtherVerbose)==ok);
       
   104 		otherGeneratedUtf7->SetLength(targetStart-REINTERPRET_CAST(char*, CONST_CAST(TUint8*, otherGeneratedUtf7->Ptr())));}
       
   105 		TDes8& sourceForReturnTrip=(i&EUtf7FlagCrossOver)? *otherGeneratedUtf7: *generatedUtf7;
       
   106 		TDes8& otherSourceForReturnTrip=(i&EUtf7FlagCrossOver)? *generatedUtf7: *otherGeneratedUtf7;
       
   107 		TEST(CnvUtfConverter::ConvertToUnicodeFromUtf7(*generatedUnicode, sourceForReturnTrip, state)==0);
       
   108 		TEST(state==CnvUtfConverter::KStateDefault);
       
   109 		{__ASSERT_ALWAYS(otherSourceForReturnTrip.Length()<otherSourceForReturnTrip.MaxLength(), Panic(EPanicNoFreeSpaceAtEndOfBuffer2));
       
   110 		char* sourceStart=REINTERPRET_CAST(char*, CONST_CAST(TUint8*, otherSourceForReturnTrip.Ptr()));
       
   111 		char* sourceEnd=sourceStart+otherSourceForReturnTrip.Length();
       
   112 		TUint16* targetStart=CONST_CAST(TUint16*, otherGeneratedUnicode->Ptr());
       
   113 		TUint16* targetEnd=targetStart+(otherGeneratedUnicode->MaxLength()-1);
       
   114 		TEST(ConvertUTF7toUCS2(&sourceStart, sourceEnd, &targetStart, targetEnd)==ok);
       
   115 		otherGeneratedUnicode->SetLength(targetStart-otherGeneratedUnicode->Ptr());}
       
   116 		TEST(*generatedUnicode==*originalUnicode);
       
   117 		TEST(*generatedUnicode==*otherGeneratedUnicode);
       
   118 		}
       
   119 	//
       
   120 	delete originalUnicode;
       
   121 	delete generatedUtf7;
       
   122 	delete otherGeneratedUtf7;
       
   123 	delete generatedUnicode;
       
   124 	delete otherGeneratedUnicode;
       
   125 
       
   126 	TheTest.End();
       
   127 	TheTest.Close();
       
   128 
       
   129 	return KErrNone;
       
   130 	}
       
   131