os/textandloc/charconvfw/charconv_fw/test/rtest/tsrc/utf/tsimple8.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-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 
    21 #include <utf.h>
    22 #include "t_simple8.h"
    23 ///////////////////////////////////////////////////////////////////////////////////////
    24 
    25 #define test(cond)                                  \
    26     TEST((cond));                                   \
    27     if (!(cond))                                    \
    28         {                                           \
    29         ERR_PRINTF1(_L("ERROR: Test Failed"));      \
    30         User::Leave(1);                             \
    31         }
    32 
    33 ///////////////////////////////////////////////////////////////////////////////////////
    34 ///////////////////////////////////////////////////////////////////////////////////////
    35 
    36 /**
    37 @SYMTestCaseID          SYSLIB-CHARCONV-CT-4002
    38 @SYMTestCaseDesc        Test conversion of a UTF-8 BOM between Unicode and 8-bit 
    39                         encoding.
    40 @SYMTestPriority        Medium
    41 @SYMTestActions        	Tests for CnvUtfConverter::ConvertFromUnicodeToUtf8(),
    42                         CnvUtfConverter::ConvertToUnicodeFromUtf8L() function  
    43 @SYMTestExpectedResults Tests must not fail
    44 @SYMREQ                 REQ0000
    45 */
    46 //INC118714L
    47 void CT_SIMPLE8::TestForUtf8BomL()
    48 	{
    49     INFO_PRINTF1(_L("@SYMTestCaseID:SYSLIB-CHARCONV-CT-4002 Testing converting to and from UTF-8 BOM.")); 	
    50 
    51 	TBuf8<100> generatedUtf8NoGKBWithBom(_L8("\xef\xbb\xbf\x7F\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64"));
    52 	
    53 	//convert to unicode and verify BOM converted properly.
    54 	INFO_PRINTF1(_L("BOM, valid UTF-8 to Unicode conversion test."));
    55 	TBuf16<256> generatedUnicode;
    56 	test(CnvUtfConverter::ConvertToUnicodeFromUtf8(generatedUnicode, generatedUtf8NoGKBWithBom)==0);
    57 	test(generatedUnicode==_L16("\xFEFF\x007fHello World"));
    58    
    59 	INFO_PRINTF1(_L("BOM, valid Unicode to UTF-8 conversion test."));
    60 	TBuf8<256> generatedUtf8;
    61 	test(CnvUtfConverter::ConvertFromUnicodeToUtf8(generatedUtf8, generatedUnicode)==0);
    62 	test(generatedUtf8==generatedUtf8NoGKBWithBom);
    63 	
    64 	}
    65 
    66 
    67 /**
    68 @SYMTestCaseID          SYSLIB-CHARCONV-CT-0563
    69 @SYMTestCaseDesc        Conversion tests from Unicode to UTF-8 character set
    70 @SYMTestPriority        Medium
    71 @SYMTestActions        	Tests for CnvUtfConverter::ConvertFromUnicodeToUtf8(),
    72                         CnvUtfConverter::ConvertToUnicodeFromUtf8L() function
    73 @SYMTestExpectedResults Test must not fail
    74 @SYMREQ                 REQ0000
    75 */
    76 void CT_SIMPLE8::TestSimple8()
    77 	{
    78     INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-CHARCONV-CT-0563 Testing simple UTF-8 round trips "));
    79 	TBuf16<256> originalUnicode;
    80 	TBuf8<256> generatedUtf8;
    81 	TBuf16<256> generatedUnicode;
    82 	//
    83 	INFO_PRINTF1(_L("Empty descriptor"));
    84 	originalUnicode=_L16("");
    85 	test(CnvUtfConverter::ConvertFromUnicodeToUtf8(generatedUtf8, originalUnicode)==0);
    86 	test(generatedUtf8==_L8(""));
    87 	test(CnvUtfConverter::ConvertToUnicodeFromUtf8(generatedUnicode, generatedUtf8)==0);
    88 	test(generatedUnicode==originalUnicode);
    89 	// Testing the new API
    90 	HBufC8* testbuffer8;
    91 	HBufC* testbuffer;
    92 	testbuffer8= CnvUtfConverter::ConvertFromUnicodeToUtf8L(originalUnicode);
    93 	TPtr8 comparebuffer8 = testbuffer8->Des();
    94 	test(comparebuffer8==generatedUtf8);
    95 	testbuffer = CnvUtfConverter::ConvertToUnicodeFromUtf8L(generatedUtf8);
    96 	TPtr compareBuffer = testbuffer->Des();
    97 	test(compareBuffer == originalUnicode);
    98 	delete testbuffer8;
    99 	delete testbuffer;
   100 
   101 	INFO_PRINTF1(_L("English \"Hello!\""));
   102 	originalUnicode=_L16("Hello!");
   103 	test(CnvUtfConverter::ConvertFromUnicodeToUtf8(generatedUtf8, originalUnicode)==0);
   104 	test(generatedUtf8==_L8("Hello!"));
   105 	test(CnvUtfConverter::ConvertToUnicodeFromUtf8(generatedUnicode, generatedUtf8)==0);
   106 	test(generatedUnicode==originalUnicode);
   107 	// Testing the new API
   108 	testbuffer8= CnvUtfConverter::ConvertFromUnicodeToUtf8L(originalUnicode);
   109 	comparebuffer8.Set(testbuffer8->Des());
   110 	test(comparebuffer8==generatedUtf8);
   111 	testbuffer = CnvUtfConverter::ConvertToUnicodeFromUtf8L(generatedUtf8);
   112 	compareBuffer.Set(testbuffer->Des());
   113 	test(compareBuffer == originalUnicode);
   114 	delete testbuffer8;
   115 	delete testbuffer;
   116 
   117 	INFO_PRINTF1(_L("Russian \"Hello!\""));
   118 	originalUnicode.Format(_L16("%c%c%c%c%c%c%c%c%c%c%c%c%c"), 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443, 0x0439, 0x0442, 0x0435, 0x0021);
   119 	test(CnvUtfConverter::ConvertFromUnicodeToUtf8(generatedUtf8, originalUnicode)==0);
   120 	test(generatedUtf8==_L8("\xd0\x97\xd0\xb4\xd1\x80\xd0\xb0\xd0\xb2\xd1\x81\xd1\x82\xd0\xb2\xd1\x83\xd0\xb9\xd1\x82\xd0\xb5\x21"));
   121 	test(CnvUtfConverter::ConvertToUnicodeFromUtf8(generatedUnicode, generatedUtf8)==0);
   122 	test(generatedUnicode==originalUnicode);
   123 	// Testing the new API
   124 	testbuffer8= CnvUtfConverter::ConvertFromUnicodeToUtf8L(originalUnicode);
   125 	comparebuffer8.Set(testbuffer8->Des());
   126 	test(comparebuffer8==generatedUtf8);
   127 	testbuffer = CnvUtfConverter::ConvertToUnicodeFromUtf8L(generatedUtf8);
   128 	compareBuffer.Set(testbuffer->Des());
   129 	test(compareBuffer == originalUnicode);
   130 	delete testbuffer8;
   131 	delete testbuffer;
   132 
   133 	INFO_PRINTF1(_L("Greek \"Hello!\""));
   134 	originalUnicode.Format(_L16("%c%c%c%c%c%c"), 0x0393, 0x03b1, 0x03c3, 0x03bf, 0x03c5, 0x0021);
   135 	test(CnvUtfConverter::ConvertFromUnicodeToUtf8(generatedUtf8, originalUnicode)==0);
   136 	test(generatedUtf8==_L8("\xce\x93\xce\xb1\xcf\x83\xce\xbf\xcf\x85\x21"));
   137 	test(CnvUtfConverter::ConvertToUnicodeFromUtf8(generatedUnicode, generatedUtf8)==0);
   138 	test(generatedUnicode==originalUnicode);
   139 	// Testing the new API
   140 	testbuffer8= CnvUtfConverter::ConvertFromUnicodeToUtf8L(originalUnicode);
   141 	comparebuffer8.Set(testbuffer8->Des());
   142 	test(comparebuffer8==generatedUtf8);
   143 	testbuffer = CnvUtfConverter::ConvertToUnicodeFromUtf8L(generatedUtf8);
   144 	compareBuffer.Set(testbuffer->Des());
   145 	test(compareBuffer == originalUnicode);
   146 	delete testbuffer8;
   147 	delete testbuffer;
   148 
   149 	INFO_PRINTF1(_L("Chinese \"Hello!\""));
   150 	originalUnicode.Format(_L16("%c%c%c"), 0x4f60, 0x597d, 0x0021);
   151 	test(CnvUtfConverter::ConvertFromUnicodeToUtf8(generatedUtf8, originalUnicode)==0);
   152 	test(generatedUtf8==_L8("\xe4\xbd\xa0\xe5\xa5\xbd\x21"));
   153 	test(CnvUtfConverter::ConvertToUnicodeFromUtf8(generatedUnicode, generatedUtf8)==0);
   154 	test(generatedUnicode==originalUnicode);
   155 	// Testing the new API
   156 	testbuffer8= CnvUtfConverter::ConvertFromUnicodeToUtf8L(originalUnicode);
   157 	comparebuffer8.Set(testbuffer8->Des());
   158 	test(comparebuffer8==generatedUtf8);
   159 	testbuffer = CnvUtfConverter::ConvertToUnicodeFromUtf8L(generatedUtf8);
   160 	compareBuffer.Set(testbuffer->Des());
   161 	test(compareBuffer == originalUnicode);
   162 	delete testbuffer8;
   163 	delete testbuffer;
   164 
   165 	INFO_PRINTF1(_L("Japanese \"Hello!\""));
   166 	originalUnicode.Format(_L16("%c%c%c%c"), 0x4eca, 0x65e5, 0x306f, 0x0021);
   167 	test(CnvUtfConverter::ConvertFromUnicodeToUtf8(generatedUtf8, originalUnicode)==0);
   168 	test(generatedUtf8==_L8("\xe4\xbb\x8a\xe6\x97\xa5\xe3\x81\xaf\x21"));
   169 	test(CnvUtfConverter::ConvertToUnicodeFromUtf8(generatedUnicode, generatedUtf8)==0);
   170 	test(generatedUnicode==originalUnicode);
   171 	// Testing the new API
   172 	testbuffer8= CnvUtfConverter::ConvertFromUnicodeToUtf8L(originalUnicode);
   173 	comparebuffer8.Set(testbuffer8->Des());
   174 	test(comparebuffer8==generatedUtf8);
   175 	testbuffer = CnvUtfConverter::ConvertToUnicodeFromUtf8L(generatedUtf8);
   176 	compareBuffer.Set(testbuffer->Des());
   177 	test(compareBuffer == originalUnicode);
   178 	delete testbuffer8;
   179 	delete testbuffer;
   180 	
   181 	
   182 	}
   183 
   184 CT_SIMPLE8::CT_SIMPLE8()
   185     {
   186     SetTestStepName(KTestStep_T_SIMPLE8);
   187     }
   188 
   189 TVerdict CT_SIMPLE8::doTestStepL()
   190     {
   191     SetTestStepResult(EFail);
   192 
   193     __UHEAP_MARK;
   194 
   195     TRAPD(error1, TestSimple8());
   196     TRAPD(error2, TestForUtf8BomL());
   197 
   198 
   199     __UHEAP_MARKEND;
   200 
   201     if(error1 == KErrNone && error2 == KErrNone)
   202         {
   203         SetTestStepResult(EPass);
   204         }
   205 
   206     return TestStepResult();
   207     }
   208