sl@0: /* sl@0: * Copyright (c) 1997-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "t_gbk.h" sl@0: sl@0: #define test(cond) \ sl@0: { \ sl@0: TBool __bb = (cond); \ sl@0: TEST(__bb); \ sl@0: if (!__bb) \ sl@0: { \ sl@0: ERR_PRINTF1(_L("ERROR: Test Failed")); \ sl@0: User::Leave(1); \ sl@0: } \ sl@0: } sl@0: sl@0: const TInt KMibValue = 113; sl@0: const TInt KBufferLength=100; sl@0: /** sl@0: @SYMTestCaseID SYSLIB-CHARCONV-CT-0509 sl@0: @SYMTestCaseDesc Tests for truncated conversion from Unicode to GBK sl@0: @SYMTestPriority Medium sl@0: @SYMTestActions Tests for truncated conversion from Unicode to Gbk and back to Unicode. sl@0: Tests for the generated Unicode with original Unicode sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: void CT_GBK::TestTruncatedConversionFromUnicodeToGbk(CCnvCharacterSetConverter& aCharacterSetConverter, const TDesC16& aOriginalUnicode) sl@0: { sl@0: INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-CHARCONV-CT-0509 ")); sl@0: for (TInt i=aOriginalUnicode.Length(); i>=0; --i) sl@0: { sl@0: TBuf8 generatedGbk; sl@0: const TInt returnValue=aCharacterSetConverter.ConvertFromUnicode(generatedGbk, aOriginalUnicode.Left(i)); sl@0: test(returnValue>=0); sl@0: TBuf8 generatedsecondPartOfGbk; sl@0: test(aCharacterSetConverter.ConvertFromUnicode(generatedsecondPartOfGbk, aOriginalUnicode.Mid(i-returnValue))==0); sl@0: generatedGbk.Append(generatedsecondPartOfGbk); sl@0: TInt state=CCnvCharacterSetConverter::KStateDefault; sl@0: TBuf16 generatedUnicode; sl@0: test(aCharacterSetConverter.ConvertToUnicode(generatedUnicode, generatedGbk, state)==0); sl@0: test(generatedUnicode==aOriginalUnicode); sl@0: } sl@0: } sl@0: /** sl@0: @SYMTestCaseID SYSLIB-CHARCONV-CT-0510 sl@0: @SYMTestCaseDesc Splitting and converting from Unicode to GBK test sl@0: @SYMTestPriority Medium sl@0: @SYMTestActions Tests for conversion after splitting from Unicode to Gbk and back to Unicode sl@0: Tests for the generated Unicode with original Unicode sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: void CT_GBK::TestSplittingConvertingFromUnicodeToGbk(CCnvCharacterSetConverter& aCharacterSetConverter, TInt aMaximumLengthLowerLimit, TInt aMaximumLengthUpperLimit, TInt aExpectedNumberOfUnicodeCharactersNotConvertedAtSplit, TInt aExpectedLengthOfFirstPartOfGbk, const TDesC8& aExpectedGbk, const TDesC16& aOriginalUnicode) sl@0: { sl@0: INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-CHARCONV-CT-0510 ")); sl@0: test(aMaximumLengthLowerLimit<=aMaximumLengthUpperLimit); sl@0: test(aMaximumLengthUpperLimit<=KBufferLength); sl@0: TUint8 gbkBuffer[KBufferLength]; sl@0: for (TInt i=aMaximumLengthLowerLimit; i<=aMaximumLengthUpperLimit; ++i) sl@0: { sl@0: TPtr8 generatedFirstPartOfGbk(gbkBuffer, i); sl@0: test(aCharacterSetConverter.ConvertFromUnicode(generatedFirstPartOfGbk, aOriginalUnicode)==aExpectedNumberOfUnicodeCharactersNotConvertedAtSplit); sl@0: test(generatedFirstPartOfGbk==aExpectedGbk.Left(aExpectedLengthOfFirstPartOfGbk)); sl@0: TBuf8 generatedSecondPartOfGbk; sl@0: test(aCharacterSetConverter.ConvertFromUnicode(generatedSecondPartOfGbk, aOriginalUnicode.Right(aExpectedNumberOfUnicodeCharactersNotConvertedAtSplit))==0); sl@0: test(generatedSecondPartOfGbk==aExpectedGbk.Mid(aExpectedLengthOfFirstPartOfGbk)); sl@0: TInt state=CCnvCharacterSetConverter::KStateDefault; sl@0: TBuf16 generatedUnicode; sl@0: test(aCharacterSetConverter.ConvertToUnicode(generatedUnicode, generatedFirstPartOfGbk, state)==0); sl@0: test(state==CCnvCharacterSetConverter::KStateDefault); sl@0: TBuf16 generatedSecondPartOfUnicode; sl@0: test(aCharacterSetConverter.ConvertToUnicode(generatedSecondPartOfUnicode, generatedSecondPartOfGbk, state)==0); sl@0: test(state==CCnvCharacterSetConverter::KStateDefault); sl@0: generatedUnicode.Append(generatedSecondPartOfUnicode); sl@0: test(generatedUnicode==aOriginalUnicode); sl@0: } sl@0: } sl@0: /** sl@0: @SYMTestCaseID SYSLIB-CHARCONV-CT-0511 sl@0: @SYMTestCaseDesc Tests for truncated conversion from GBK to Unicode sl@0: @SYMTestPriority Medium sl@0: @SYMTestActions Tests for conversion after truncation from Unicode to Hz and back to Unicode sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: void CT_GBK::TestTruncatedConversionToUnicodeFromGbk(CCnvCharacterSetConverter& aCharacterSetConverter, const TDesC16& aExpectedUnicode, const TDesC8& aOriginalGbk) sl@0: { sl@0: INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-CHARCONV-CT-0511 ")); sl@0: for (TInt i=aOriginalGbk.Length(); i>=2; --i) // 2 is the length of GBK's longest multi-byte characters sl@0: { sl@0: TInt state=CCnvCharacterSetConverter::KStateDefault; sl@0: TBuf16 generatedUnicode; sl@0: const TInt returnValue=aCharacterSetConverter.ConvertToUnicode(generatedUnicode, aOriginalGbk.Left(i), state); sl@0: test(returnValue>=0); sl@0: TBuf16 generatedsecondPartOfUnicode; sl@0: test(aCharacterSetConverter.ConvertToUnicode(generatedsecondPartOfUnicode, aOriginalGbk.Mid(i-returnValue), state)==0); sl@0: generatedUnicode.Append(generatedsecondPartOfUnicode); sl@0: test(generatedUnicode==aExpectedUnicode); sl@0: } sl@0: } sl@0: /** sl@0: @SYMTestCaseID SYSLIB-CHARCONV-CT-0512 sl@0: @SYMTestCaseDesc Splitting and converting from GBK to Unicode test sl@0: @SYMTestPriority Medium sl@0: @SYMTestActions Tests for conversion after splitting from Hz to Unicode and back to Hz sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: void CT_GBK::TestSplittingConvertingToUnicodeFromGbk(CCnvCharacterSetConverter& aCharacterSetConverter, TInt aMaximumLengthLowerLimit, TInt aMaximumLengthUpperLimit, TInt aExpectedNumberOfGbkBytesNotConvertedAtSplit, TInt aExpectedLengthOfFirstPartOfUnicode, const TDesC16& aExpectedUnicode, const TDesC8& aOriginalGbk) sl@0: { sl@0: INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-CHARCONV-CT-0512 ")); sl@0: test(aMaximumLengthLowerLimit<=aMaximumLengthUpperLimit); sl@0: test(aMaximumLengthUpperLimit<=KBufferLength); sl@0: TUint16 unicodeBuffer[KBufferLength]; sl@0: for (TInt i=aMaximumLengthLowerLimit; i<=aMaximumLengthUpperLimit; ++i) sl@0: { sl@0: TPtr16 generatedFirstPartOfUnicode(unicodeBuffer, i); sl@0: TInt state=CCnvCharacterSetConverter::KStateDefault; sl@0: test(aCharacterSetConverter.ConvertToUnicode(generatedFirstPartOfUnicode, aOriginalGbk, state)==aExpectedNumberOfGbkBytesNotConvertedAtSplit); sl@0: test(generatedFirstPartOfUnicode==aExpectedUnicode.Left(aExpectedLengthOfFirstPartOfUnicode)); sl@0: test(state==CCnvCharacterSetConverter::KStateDefault); sl@0: TBuf16 generatedSecondPartOfUnicode; sl@0: test(aCharacterSetConverter.ConvertToUnicode(generatedSecondPartOfUnicode, aOriginalGbk.Right(aExpectedNumberOfGbkBytesNotConvertedAtSplit), state)==0); sl@0: test(generatedSecondPartOfUnicode==aExpectedUnicode.Mid(aExpectedLengthOfFirstPartOfUnicode)); sl@0: test(state==CCnvCharacterSetConverter::KStateDefault); sl@0: TBuf8 generatedGbk; sl@0: test(aCharacterSetConverter.ConvertFromUnicode(generatedGbk, generatedFirstPartOfUnicode)==0); sl@0: TBuf8 generatedSecondPartOfGbk; sl@0: test(aCharacterSetConverter.ConvertFromUnicode(generatedSecondPartOfGbk, generatedSecondPartOfUnicode)==0); sl@0: generatedGbk.Append(generatedSecondPartOfGbk); sl@0: test(generatedGbk==aOriginalGbk); sl@0: } sl@0: } sl@0: sl@0: sl@0: /** sl@0: Tests retreiving the MIB value from the GBK UID sl@0: Tests retreiving the GBK UID from the MIB value sl@0: sl@0: @SYMTestCaseID SYSLIB-CHARCONV-CT-0513 sl@0: @SYMTestCaseDesc Tests for conversion from MIB to UID and UID to MIB sl@0: @SYMTestPriority Medium sl@0: @SYMTestActions Tests CCnvCharacterSetConverter::ConvertMibEnumOfCharacterSetToIdentifierL(), sl@0: CCnvCharacterSetConverter::ConvertCharacterSetIdentifierToMibEnumL() functions sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: void CT_GBK::CharacterSetValueAndMIBTests(CCnvCharacterSetConverter& aCharacterSetConverter, RFs& aFileServerSession) sl@0: { sl@0: INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-CHARCONV-CT-0513 ")); sl@0: // check that the character set value of '113' is converted to the GBK UID (0x10003ecb) sl@0: test(aCharacterSetConverter.ConvertMibEnumOfCharacterSetToIdentifierL(KMibValue,aFileServerSession)==KCharacterSetIdentifierGbk); sl@0: INFO_PRINTF1(_L("\nMIB->Char Set UID - OK")); sl@0: sl@0: // check that the UCS2 GUID (0x10003ecb) is converted to the character set value of '113' sl@0: test(aCharacterSetConverter.ConvertCharacterSetIdentifierToMibEnumL(KCharacterSetIdentifierGbk,aFileServerSession)==KMibValue); sl@0: INFO_PRINTF1(_L("\nChar Set UID->MIB - OK")); sl@0: } sl@0: /** sl@0: @SYMTestCaseID SYSLIB-CHARCONV-CT-0514 sl@0: @SYMTestCaseDesc Conversion tests for GBK to Unicode sl@0: @SYMTestPriority Medium sl@0: @SYMTestActions Tests for conversion from GBK to Unicode and vice versa. sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: void CT_GBK::DoE32MainL() sl@0: { sl@0: RFs fileServerSession; sl@0: CleanupClosePushL(fileServerSession); sl@0: User::LeaveIfError(fileServerSession.Connect()); sl@0: CCnvCharacterSetConverter* characterSetConverter=CCnvCharacterSetConverter::NewLC(); sl@0: CArrayFix* arrayOfCharacterSetsAvailable=CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableLC(fileServerSession); sl@0: INFO_PRINTF1(_L("Available:\n")); sl@0: for (TInt i=arrayOfCharacterSetsAvailable->Count()-1; i>=0; --i) sl@0: { sl@0: const CCnvCharacterSetConverter::SCharacterSet& charactersSet=(*arrayOfCharacterSetsAvailable)[i]; sl@0: characterSetConverter->PrepareToConvertToOrFromL(charactersSet.Identifier(), *arrayOfCharacterSetsAvailable, fileServerSession); sl@0: TPtrC charactersSetName(charactersSet.Name()); sl@0: if (charactersSet.NameIsFileName()) sl@0: { sl@0: charactersSetName.Set(TParsePtrC(charactersSetName).Name()); sl@0: } sl@0: INFO_PRINTF2(_L(" %S\n"), &charactersSetName); sl@0: } sl@0: TPtrC8 originalGbk; sl@0: TBuf16 originalUnicode; sl@0: TBuf8 generatedGbk; sl@0: TBuf16 generatedUnicode; sl@0: TInt state=CCnvCharacterSetConverter::KStateDefault; sl@0: sl@0: // test that we can get MIB and Charset values sl@0: CharacterSetValueAndMIBTests(*characterSetConverter, fileServerSession); sl@0: sl@0: characterSetConverter->PrepareToConvertToOrFromL(KCharacterSetIdentifierGbk, *arrayOfCharacterSetsAvailable, fileServerSession); sl@0: sl@0: INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-CHARCONV-CT-0514 Testing characters shared with GB 2312-80 and characters only in GBK ")); sl@0: originalGbk.Set(_L8("A\xfd\x7d\xdd\xb6\xb1\xc9\xe9\x8e\xe8\x9d""E b\xd3\x59\xd2\x40\x95\xc0")); sl@0: originalUnicode.Format(_L16("A%c%c%c%c%cE b%c%c%c"), 0x9f77, 0x837b, 0x9119, 0x95b9, 0x94e6, 0x89bb, 0x8938, 0x66b2); sl@0: TestTruncatedConversionFromUnicodeToGbk(*characterSetConverter, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 0, 0, 12, 0, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 1, 2, 11, 1, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 3, 4, 10, 3, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 5, 6, 9, 5, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 7, 8, 8, 7, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 9, 10, 7, 9, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 11, 11, 6, 11, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 12, 12, 5, 12, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 13, 13, 4, 13, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 14, 15, 3, 14, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 16, 17, 2, 16, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 18, 19, 1, 18, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 20, 40, 0, 20, originalGbk, originalUnicode); sl@0: TestTruncatedConversionToUnicodeFromGbk(*characterSetConverter, originalUnicode, originalGbk); sl@0: TestTruncatedConversionToUnicodeFromGbk(*characterSetConverter, originalUnicode.Mid(1, 2), originalGbk.Mid(1, 4)); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 0, 0, 20, 0, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 1, 1, 19, 1, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 2, 2, 17, 2, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 3, 3, 15, 3, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 4, 4, 13, 4, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 5, 5, 11, 5, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 6, 6, 9, 6, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 7, 7, 8, 7, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 8, 8, 7, 8, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 9, 9, 6, 9, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 10, 10, 4, 10, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 11, 11, 2, 11, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 12, 30, 0, 12, originalUnicode, originalGbk); sl@0: sl@0: INFO_PRINTF1(_L("Testing GBK characters where the first byte has the high-bit set and the second byte doesn't")); sl@0: originalGbk.Set(_L8("\x20\x5d\xa0\x5d\xa0\xdd")); sl@0: originalUnicode.Format(_L16(" ]%c%c"), 0x71f7, 0x72a6); sl@0: TestTruncatedConversionFromUnicodeToGbk(*characterSetConverter, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 0, 0, 4, 0, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 1, 1, 3, 1, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 2, 3, 2, 2, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 4, 5, 1, 4, originalGbk, originalUnicode); sl@0: TestSplittingConvertingFromUnicodeToGbk(*characterSetConverter, 6, 20, 0, 6, originalGbk, originalUnicode); sl@0: TestTruncatedConversionToUnicodeFromGbk(*characterSetConverter, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 0, 0, 6, 0, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 1, 1, 5, 1, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 2, 2, 4, 2, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 3, 3, 2, 3, originalUnicode, originalGbk); sl@0: TestSplittingConvertingToUnicodeFromGbk(*characterSetConverter, 4, 20, 0, 4, originalUnicode, originalGbk); sl@0: sl@0: INFO_PRINTF1(_L("Testing the many-to-many mapping in GBK")); sl@0: originalGbk.Set(_L8("\xa8\x44\xa1\xaa")); sl@0: test(characterSetConverter->ConvertToUnicode(generatedUnicode, originalGbk, state)==0); sl@0: test(state==CCnvCharacterSetConverter::KStateDefault); sl@0: test(generatedUnicode.Length()==2); sl@0: test(generatedUnicode[0]==0x2015); sl@0: test(generatedUnicode[1]==0x2015); sl@0: originalUnicode.SetLength(2); sl@0: originalUnicode[0]=0x2014; sl@0: originalUnicode[1]=0x2015; sl@0: test(characterSetConverter->ConvertFromUnicode(generatedGbk, originalUnicode)==0); sl@0: test(generatedGbk==_L8("\xa1\xaa\xa1\xaa")); sl@0: sl@0: INFO_PRINTF1(_L("Testing truncated GBK sequences")); sl@0: originalGbk.Set(_L8("qwe\xb5")); sl@0: test(characterSetConverter->ConvertToUnicode(generatedUnicode, originalGbk, state)==1); sl@0: test(state==CCnvCharacterSetConverter::KStateDefault); sl@0: test(generatedUnicode==_L16("qwe")); sl@0: originalGbk.Set(_L8("qwe\x80")); sl@0: test(characterSetConverter->ConvertToUnicode(generatedUnicode, originalGbk, state)==1); sl@0: test(state==CCnvCharacterSetConverter::KStateDefault); sl@0: test(generatedUnicode==_L16("qwe")); sl@0: CleanupStack::PopAndDestroy(3); // arrayOfCharacterSetsAvailable and characterSetConverter and fileServerSession sl@0: } sl@0: sl@0: CT_GBK::CT_GBK() sl@0: { sl@0: SetTestStepName(KTestStep_T_GBK); sl@0: } sl@0: sl@0: TVerdict CT_GBK::doTestStepL() sl@0: { sl@0: SetTestStepResult(EFail); sl@0: sl@0: __UHEAP_MARK; sl@0: TRAPD(error1, DoE32MainL()); sl@0: __UHEAP_MARKEND; sl@0: sl@0: if(error1 == KErrNone) sl@0: { sl@0: SetTestStepResult(EPass); sl@0: } sl@0: sl@0: return TestStepResult(); sl@0: } sl@0: