/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- * * The contents of this file are subject to the Netscape Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. * * The Original Code is Mozilla Communicator client code. * * The Initial Developer of the Original Code is Netscape Communications * Corporation. Portions created by Netscape are Copyright (C) 1998 * Netscape Communications Corporation. All Rights Reserved. */ #define NS_IMPL_IDS #include #include "nsRepository.h" #include "nsISupports.h" #include "nsICharsetConverterManager.h" #include "nsConverterCID.h" #include "nsUCVJACID.h" #include "nsUCvLatinCID.h" #ifdef XP_UNIX #define UCONV_DLL "libuconv.so" #define UCVLATIN_DLL "libucvlatin.so" #define UCVJA_DLL "libucvja.so" #else /* XP_UNIX */ #ifdef XP_MAC #define UCONV_DLL "UCONV_DLL" #define UCVLATIN_DLL "UCVLATIN_DLL" #define UCVJA_DLL "UCVJA_DLL" #else /* XP_MAC */ #define UCONV_DLL "uconv.dll" #define UCVLATIN_DLL "ucvlatin.dll" #define UCVJA_DLL "ucvja.dll" #endif #endif #define TABLE_SIZE1 5 nsICharsetConverterManager * ccMan = NULL; nsresult setupRegistry() { nsresult res; res = nsRepository::RegisterFactory(kCharsetConverterManagerCID, UCONV_DLL, PR_FALSE, PR_FALSE); if (NS_FAILED(res) && (NS_ERROR_FACTORY_EXISTS != res)) return res; res = nsRepository::RegisterFactory(kLatin1ToUnicodeCID, UCVLATIN_DLL, PR_FALSE, PR_FALSE); if (NS_FAILED(res) && (NS_ERROR_FACTORY_EXISTS != res)) return res; res = nsRepository::RegisterFactory(kCP1253ToUnicodeCID, UCVLATIN_DLL, PR_FALSE, PR_FALSE); if (NS_FAILED(res) && (NS_ERROR_FACTORY_EXISTS != res)) return res; res = nsRepository::RegisterFactory(kISO88597ToUnicodeCID, UCVLATIN_DLL, PR_FALSE, PR_FALSE); if (NS_FAILED(res) && (NS_ERROR_FACTORY_EXISTS != res)) return res; res = nsRepository::RegisterFactory(kSJIS2UnicodeCID, UCVJA_DLL, PR_FALSE, PR_FALSE); if (NS_FAILED(res) && (NS_ERROR_FACTORY_EXISTS != res)) return res; return NS_OK; } nsresult init() { nsresult res; res = setupRegistry(); if (NS_FAILED(res)) { printf("Error setting up registry: 0x%x",res); return res; } return NS_OK; } nsresult done() { if (ccMan != NULL) NS_RELEASE(ccMan); return NS_OK; } nsresult testCharsetConverterManager() { printf("\n[T1] CharsetConverterManager\n"); nsresult res = nsRepository::CreateInstance(kCharsetConverterManagerCID, NULL, kICharsetConverterManagerIID, (void **)&ccMan); if (NS_FAILED(res)) { printf("ERROR 0x%x: Cannot instantiate.\n",res); return res; } else { printf("Instantiated.\n"); } nsString ** cs; PRInt32 ct; res = ccMan->GetEncodableCharsets(&cs, &ct); if (NS_FAILED(res)) { printf("ERROR 0x%x: GetEncodableCharsets()\n",res); } else { printf("Unicode encoders (%d): ", ct); for (int i=0;iToNewCString(); printf("%s ",cstr); delete [] cstr; } printf("\n"); } delete [] cs; res = ccMan->GetDecodableCharsets(&cs, &ct); if (NS_FAILED(res)) { printf("ERROR 0x%x: GetDecodableCharsets()\n",res); } else { printf("Unicode decoders (%d): ", ct); for (int i=0;iToNewCString(); printf("%s ",cstr); delete [] cstr; } printf("\n"); } delete [] cs; return NS_OK; } nsresult testLatin1Decoder() { printf("\n[T2] Latin1ToUnicode\n"); // create converter nsIUnicodeDecoder * dec; nsAutoString str("iso-8859-1"); nsresult res = ccMan->GetUnicodeDecoder(&str,&dec); if (NS_FAILED(res)) { printf("ERROR 0x%x: Cannot instantiate.\n",res); return res; } else { printf("Instantiated.\n"); } //test converter PRInt32 srcL = TABLE_SIZE1; PRInt32 destL = TABLE_SIZE1; char src [TABLE_SIZE1] = {(char)0,(char)255,(char)13,(char)127,(char)128,}; PRUnichar dest [TABLE_SIZE1]; res=dec->Convert(dest, 0, &destL, src, 0, &srcL); if (NS_FAILED(res)) { printf("ERROR 0x%x: Convert().\n",res); } else { printf("Read %d, write %d.\n",srcL,destL); printf("Converted:"); PRBool failed = PR_FALSE; for (int i=0;i%x", src[i], dest[i]); if (dest[i] != ((PRUint8)src[i])) failed = PR_TRUE; } printf("\n"); if (failed) { printf("Test FAILED!!!\n"); } else { printf("Test Passed.\n"); } } NS_RELEASE(dec); return NS_OK; } #define ISO88597TABLE_SIZE 16 nsresult testISO88597Decoder() { printf("\n[T3] ISO-8859-7 To Unicode\n"); // create converter nsIUnicodeDecoder * dec; nsAutoString str("iso-8859-7"); nsresult res = ccMan->GetUnicodeDecoder(&str,&dec); if (NS_FAILED(res)) { printf("ERROR 0x%x: Cannot instantiate.\n",res); return res; } else { printf("Instantiated.\n"); } //test converter PRInt32 srcL = ISO88597TABLE_SIZE; PRInt32 destL = ISO88597TABLE_SIZE; // some randon C0, ASCII, C1 and ISO-8859-7 code static char src [ISO88597TABLE_SIZE + 1] = { "\x09\x0d\x20\x40" "\x80\x98\xa3\xaf" "\xa7\xb1\xb3\xc9" "\xd9\xe3\xf4\xff" }; static PRUnichar expect [ISO88597TABLE_SIZE] = { 0x0009, 0x000d, 0x0020, 0x0040, 0xfffd, 0xfffd, 0x00a3, 0x2015, 0x00a7, 0x00b1, 0x00b3, 0x0399, 0x03a9, 0x03b3, 0x03c4, 0xfffd }; PRUnichar dest [ISO88597TABLE_SIZE]; res=dec->Convert(dest, 0, &destL, src, 0, &srcL); if (NS_FAILED(res)) { printf("ERROR 0x%x: Convert().\n",res); } else { printf("Read %d, write %d.\n",srcL,destL); printf("Converted:"); PRBool failed = PR_FALSE; for (int i=0;iGetUnicodeDecoder(&str,&dec); if (NS_FAILED(res)) { printf("ERROR 0x%x: Cannot instantiate.\n",res); return res; } else { printf("Instantiated.\n"); } //test converter PRInt32 srcL = SJIS_TEST_SRC_SIZE; PRInt32 destL = SJIS_TEST_DEST_SIZE+ 44; PRBool failed = PR_FALSE; printf("Test Normal Case\n"); res=dec->Convert(dest, 0, &destL, src, 0, &srcL); if (NS_FAILED(res)) { printf("ERROR 0x%x: Convert().\n",res); failed = PR_TRUE; } else { printf("Read %d, write %d.\n",srcL,destL); printf("Converted:"); if(destL != SJIS_TEST_DEST_SIZE) { printf("Dest Size bad. Should be %d but got %d!!\n", SJIS_TEST_DEST_SIZE, destL); failed = PR_TRUE; } for (int i=0;i< destL ;i++) { if (dest[i] != expect[i] ) failed = PR_TRUE; } printf("\n"); if (failed) { printf("Test FAILED!!!\n"); } else { printf("Test Passed.\n"); } failed = PR_FALSE; // Test NS_PARTIAL_MORE_INPUT case printf("Test NS_PARTIAL_MORE_INPUT Case\n"); srcL= SJIS_TEST_SRC_SIZE - 1; destL = SJIS_TEST_DEST_SIZE+ 44; res=dec->Convert(dest, 0, &destL, src, 0, &srcL); if (NS_PARTIAL_MORE_INPUT != res) { printf("ERROR 0x%x: Convert().\n",res); failed = PR_TRUE; } else { if((SJIS_TEST_SRC_SIZE -2 ) != srcL ) { printf("Src Size bad. Should be %d but got %d!!\n", SJIS_TEST_SRC_SIZE-2, srcL); failed = PR_TRUE; } if(destL != (SJIS_TEST_DEST_SIZE-1)) { printf("Dest Size bad. Should be %d but got %d!!\n", SJIS_TEST_DEST_SIZE-1, destL); failed = PR_TRUE; } for (int i=0;i< destL ;i++) { if (dest[i] != expect[i] ) failed = PR_TRUE; } } if (failed) { printf("Test FAILED!!!\n"); } else { printf("Test Passed.\n"); } failed = PR_FALSE; // Test NS_PARTIAL_MORE_OUTPUT case printf("Test NS_PARTIAL_MORE_OUTNPUT Case\n"); srcL= SJIS_TEST_SRC_SIZE; destL = SJIS_TEST_DEST_SIZE-1; res=dec->Convert(dest, 0, &destL, src, 0, &srcL); if (NS_PARTIAL_MORE_OUTPUT != res) { printf("ERROR 0x%x: Convert().\n",res); failed = PR_TRUE; } else { if((SJIS_TEST_SRC_SIZE -2 ) != srcL ) { printf("Src Size bad. Should be %d but got %d!!\n", SJIS_TEST_SRC_SIZE-2, srcL); failed = PR_TRUE; } if(destL != (SJIS_TEST_DEST_SIZE-1)) { printf("Dest Size bad. Should be %d but got %d!!\n", SJIS_TEST_DEST_SIZE-1, destL); failed = PR_TRUE; } for (int i=0;i< destL ;i++) { if (dest[i] != expect[i] ) failed = PR_TRUE; } } // Test NS_ERROR_ILLEGAL_INPUT case //printf("Test NS_ERROR_ILLEGAL_INPUT Case\n"); // To Be Implement if (failed) { printf("Test FAILED!!!\n"); } else { printf("Test Passed.\n"); } } NS_RELEASE(dec); return NS_OK; } nsresult run() { nsresult res; res = testCharsetConverterManager(); if (NS_FAILED(res)) return res; res = testLatin1Decoder(); if (NS_FAILED(res)) return res; res = testSJISDecoder(); if (NS_FAILED(res)) return res; res = testISO88597Decoder(); if (NS_FAILED(res)) return res; return NS_OK; } int main(int argc, char **argv) { nsresult res; printf("*** Unicode Converters Test ***\n"); res = init(); if (NS_FAILED(res)) return -1; run(); done(); printf("\n***--------- Done --------***\n"); return 0; }