Back to index

lightning-sunbird  0.9+nobinonly
nsTestUConv.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <stdio.h>
00040 #include <string.h>
00041 #include "nsXPCOM.h"
00042 #include "nsIComponentManager.h"
00043 #include "nsIServiceManager.h"
00044 #include "nsISupports.h"
00045 #include "nsICharsetConverterManager.h"
00046 #include "nsIPlatformCharset.h"
00047 #include "nsICharRepresentable.h"
00048 #include "prmem.h"
00049 #include "nsReadableUtils.h"
00050 
00051 
00052 static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
00053 static NS_DEFINE_CID(kPlatformCharsetCID, NS_PLATFORMCHARSET_CID);
00054 
00055 //#define TEST_IS_REPRESENTABLE
00056 
00073 //----------------------------------------------------------------------
00074 // Global variables and macros
00075 
00076 #define GENERAL_BUFFER 20000 // general purpose buffer; for Unicode divide by 2
00077 
00078 #define ARRAY_SIZE(_array)                                      \
00079      (sizeof(_array) / sizeof(_array[0]))
00080 
00081 nsICharsetConverterManager * ccMan = NULL;
00082 
00087 char bLatin1_d0[] = {
00088   "\x00\x0d\x7f\x80\xff"
00089 };
00090 
00091 PRUnichar cLatin1_d0[] = {
00092   0x0000,0x000d,0x007f,0x20ac,0x00ff
00093 };
00094 
00095 PRInt32 bLatin1_s0 = ARRAY_SIZE(bLatin1_d0)-1;
00096 PRInt32 cLatin1_s0 = ARRAY_SIZE(cLatin1_d0);
00097 
00098 //----------------------------------------------------------------------
00099 // Converter Manager test code
00100 
00101 nsresult testCharsetConverterManager()
00102 {
00103   printf("\n[T001] CharsetConverterManager\n");
00104 
00105   return NS_OK;
00106 }
00107 
00108 //----------------------------------------------------------------------
00109 // Helper functions and macros for testing decoders and encoders
00110 
00111 #define CREATE_DECODER(_charset)                                \
00112     nsIUnicodeDecoder * dec;                                    \
00113     nsAutoString str;str.AssignWithConversion(_charset);        \
00114     nsresult res = ccMan->GetUnicodeDecoder(&str,&dec);         \
00115     if (NS_FAILED(res)) {                                       \
00116       printf("ERROR at GetUnicodeDecoder() code=0x%x.\n",res);  \
00117       return res;                                               \
00118     }
00119 
00120 #define CREATE_ENCODER(_charset)                                \
00121     nsIUnicodeEncoder * enc;                                    \
00122     nsAutoString str; str.AssignWithConversion(_charset);       \
00123     nsresult res = ccMan->GetUnicodeEncoder(&str,&enc);         \
00124     if (NS_FAILED(res)) {                                       \
00125       printf("ERROR at GetUnicodeEncoder() code=0x%x.\n",res);  \
00126       return res;                                               \
00127     }
00128 
00134 nsresult testDecoder(nsIUnicodeDecoder * aDec, 
00135                      const char * aSrc, PRInt32 aSrcLength, 
00136                      const PRUnichar * aRes, PRInt32 aResLength,
00137                      const char * aTestName)
00138 {
00139   nsresult res;
00140 
00141   // prepare for conversion
00142   PRInt32 srcLen = aSrcLength;
00143   PRUnichar dest[GENERAL_BUFFER/2];
00144   PRInt32 destLen = GENERAL_BUFFER/2;
00145 
00146   // conversion
00147   res = aDec->Convert(aSrc, &srcLen, dest, &destLen);
00148   // we want a perfect result here - the test data should be complete!
00149   if (res != NS_OK) {
00150     printf("ERROR at %s.easy.Decode() code=0x%x.\n",aTestName,res);
00151     return NS_ERROR_UNEXPECTED;
00152   }
00153 
00154   // compare results
00155   if (aResLength != destLen) {
00156       printf("ERROR at %s.easy.DecResLen expected=0x%x result=0x%x.\n", 
00157           aTestName, aResLength, destLen);
00158       return NS_ERROR_UNEXPECTED;
00159   }
00160   for (PRInt32 i=0; i<aResLength; i++) if (aRes[i] != dest[i]) {
00161       printf("ERROR at %s.easy.DecResChar[%d] expected=0x%x result=0x%x.\n", 
00162           aTestName, i, aRes[i], dest[i]);
00163       return NS_ERROR_UNEXPECTED;
00164   }
00165 
00166   return NS_OK;
00167 }
00168 
00174 nsresult testEncoder(nsIUnicodeEncoder * aEnc, 
00175                      const PRUnichar * aSrc, PRInt32 aSrcLength, 
00176                      const char * aRes, PRInt32 aResLength,
00177                      const char * aTestName)
00178 {
00179   nsresult res;
00180 
00181   // prepare for conversion
00182   PRInt32 srcLen = 0;
00183   char dest[GENERAL_BUFFER];
00184   PRInt32 destLen = 0;
00185   PRInt32 bcr, bcw;
00186 
00187   // conversion
00188   bcr = aSrcLength;
00189   bcw = GENERAL_BUFFER;
00190   res = aEnc->Convert(aSrc, &bcr, dest, &bcw);
00191   srcLen += bcr;
00192   destLen += bcw;
00193   // we want a perfect result here - the test data should be complete!
00194   if (res != NS_OK) {
00195     printf("ERROR at %s.easy.Encode() code=0x%x.\n",aTestName,res);
00196     return NS_ERROR_UNEXPECTED;
00197   }
00198 
00199   // finish
00200   bcw = GENERAL_BUFFER - destLen;
00201   res = aEnc->Finish(dest + destLen, &bcw);
00202   destLen += bcw;
00203   // we want a perfect result here - the test data should be complete!
00204   if (res != NS_OK) {
00205     printf("ERROR at %s.easy.Finish() code=0x%x.\n",aTestName,res);
00206     return NS_ERROR_UNEXPECTED;
00207   }
00208 
00209   // compare results
00210   if (aResLength != destLen) {
00211       printf("ERROR at %s.easy.EncResLen expected=0x%x result=0x%x.\n", 
00212           aTestName, aResLength, destLen);
00213       return NS_ERROR_UNEXPECTED;
00214   }
00215   for (PRInt32 i=0; i<aResLength; i++) if (aRes[i] != dest[i]) {
00216       printf("ERROR at %s.easy.EncResChar[%d] expected=0x%x result=0x%x.\n", 
00217           aTestName, i, aRes[i], dest[i]);
00218       return NS_ERROR_UNEXPECTED;
00219   }
00220   
00221 #ifdef TEST_IS_REPRESENTABLE
00222   nsICharRepresentable* rp = nsnull;
00223   res = aEnc->QueryInterface(NS_GET_IID(nsICharRepresentable),(void**) &rp);
00224   if(NS_SUCCEEDED(res))  {
00225     PRUint32 *info= (PRUint32*)PR_Calloc((0x10000 >> 5), 4);
00226     rp->FillInfo(info);
00227     for(int i=0;i< 0x10000;i++)
00228     {
00229        if(IS_REPRESENTABLE(info, i)) 
00230            printf("%4x\n", i);
00231     }
00232   }
00233 #endif
00234 
00235   return NS_OK;
00236 }
00237 
00244 nsresult testStressDecoder(nsIUnicodeDecoder * aDec, 
00245                            const char * aSrc, PRInt32 aSrcLength, 
00246                            const PRUnichar * aRes, PRInt32 aResLength,
00247                            const char * aTestName)
00248 {
00249   nsresult res;
00250 
00251   // get estimated length
00252   PRInt32 estimatedLength;
00253   res = aDec->GetMaxLength(aSrc, aSrcLength, &estimatedLength);
00254   if (NS_FAILED(res)) {
00255     printf("ERROR at %s.stress.Length() code=0x%x.\n",aTestName,res);
00256     return res;
00257   }
00258   PRBool exactLength = (res == NS_EXACT_LENGTH);
00259 
00260   // prepare for conversion
00261   PRInt32 srcLen = 0;
00262   PRInt32 srcOff = 0;
00263   PRUnichar dest[1024];
00264   PRInt32 destLen = 0;
00265   PRInt32 destOff = 0;
00266 
00267   // controlled conversion
00268   for (;srcOff < aSrcLength;) {
00269     res = aDec->Convert(aSrc + srcOff, &srcLen, dest + destOff, &destLen);
00270     if (NS_FAILED(res)) {
00271       printf("ERROR at %s.stress.Convert() code=0x%x.\n",aTestName,res);
00272       return res;
00273     }
00274 
00275     srcOff+=srcLen;
00276     destOff+=destLen;
00277 
00278     // give a little input each time; it'll be consumed if enough output space
00279     srcLen = 1;
00280     // give output space only when requested: sadic!
00281     if (res == NS_PARTIAL_MORE_OUTPUT) {
00282       destLen = 1;
00283     } else {
00284       destLen = 0;
00285     }
00286   }
00287 
00288   // we want perfect result here - the test data should be complete!
00289   if (res != NS_OK) {
00290     printf("ERROR at %s.stress.postConvert() code=0x%x.\n",aTestName,res);
00291     return NS_ERROR_UNEXPECTED;
00292   }
00293 
00294   // compare lengths
00295   if (exactLength) {
00296     if (destOff != estimatedLength) {
00297       printf("ERROR at %s.stress.EstimatedLen expected=0x%x result=0x%x.\n",
00298           aTestName, estimatedLength, destOff);
00299       return NS_ERROR_UNEXPECTED;
00300     }
00301   } else {
00302     if (destOff > estimatedLength) {
00303       printf("ERROR at %s.stress.EstimatedLen expected<=0x%x result=0x%x.\n",
00304           aTestName, estimatedLength, destOff);
00305       return NS_ERROR_UNEXPECTED;
00306     }
00307   }
00308 
00309   // compare results
00310   if (aResLength != destOff) {
00311       printf("ERROR at %s.stress.ConvResLen expected=0x%x result=0x%x.\n", 
00312           aTestName, aResLength, destOff);
00313       return NS_ERROR_UNEXPECTED;
00314   }
00315   for (PRInt32 i=0; i<aResLength; i++) if (aRes[i] != dest[i]) {
00316       printf("ERROR at %s.stress.ConvResChar[%d] expected=0x%x result=0x%x.\n", 
00317           aTestName, i, aRes[i], dest[i]);
00318       return NS_ERROR_UNEXPECTED;
00319   }
00320 
00321   return NS_OK;
00322 }
00323 
00330 nsresult testStressEncoder(nsIUnicodeEncoder * aEnc, 
00331                            const PRUnichar * aSrc, PRInt32 aSrcLength,
00332                            const char * aRes, PRInt32 aResLength, 
00333                            const char * aTestName)
00334 {
00335   nsresult res;
00336 
00337   // get estimated length
00338   PRInt32 estimatedLength;
00339   res = aEnc->GetMaxLength(aSrc, aSrcLength, &estimatedLength);
00340   if (NS_FAILED(res)) {
00341     printf("ERROR at %s.stress.Length() code=0x%x.\n",aTestName,res);
00342     return res;
00343   }
00344   PRBool exactLength = (res == NS_OK_UENC_EXACTLENGTH);
00345 
00346   // prepare for conversion
00347   PRInt32 srcLen = 0;
00348   PRInt32 srcOff = 0;
00349   char dest[GENERAL_BUFFER];
00350   PRInt32 destLen = 0;
00351   PRInt32 destOff = 0;
00352 
00353   // controlled conversion
00354   for (;srcOff < aSrcLength;) {
00355     res = aEnc->Convert(aSrc + srcOff, &srcLen, dest + destOff, &destLen);
00356     if (NS_FAILED(res)) {
00357       printf("ERROR at %s.stress.Convert() code=0x%x.\n",aTestName,res);
00358       return res;
00359     }
00360 
00361     srcOff+=srcLen;
00362     destOff+=destLen;
00363 
00364     // give a little input each time; it'll be consumed if enough output space
00365     srcLen = 1;
00366     // give output space only when requested: sadic!
00367     if (res == NS_OK_UENC_MOREOUTPUT) {
00368       destLen = 1;
00369     } else {
00370       destLen = 0;
00371     }
00372   }
00373 
00374   if (res != NS_OK) if (res != NS_OK_UENC_MOREOUTPUT) {
00375     printf("ERROR at %s.stress.postConvert() code=0x%x.\n",aTestName,res);
00376     return NS_ERROR_UNEXPECTED;
00377   } 
00378   
00379   for (;;) {
00380     res = aEnc->Finish(dest + destOff, &destLen);
00381     if (NS_FAILED(res)) {
00382       printf("ERROR at %s.stress.Finish() code=0x%x.\n",aTestName,res);
00383       return res;
00384     }
00385 
00386     destOff+=destLen;
00387 
00388     // give output space only when requested: sadic!
00389     if (res == NS_OK_UENC_MOREOUTPUT) {
00390       destLen = 1;
00391     } else break;
00392   }
00393 
00394   // compare lengths
00395   if (exactLength) {
00396     if (destOff != estimatedLength) {
00397       printf("ERROR at %s.stress.EstimatedLen expected=0x%x result=0x%x.\n",
00398           aTestName, estimatedLength, destOff);
00399       return NS_ERROR_UNEXPECTED;
00400     }
00401   } else {
00402     if (destOff > estimatedLength) {
00403       printf("ERROR at %s.stress.EstimatedLen expected<=0x%x result=0x%x.\n",
00404           aTestName, estimatedLength, destOff);
00405       return NS_ERROR_UNEXPECTED;
00406     }
00407   }
00408 
00409   // compare results
00410   if (aResLength != destOff) {
00411       printf("ERROR at %s.stress.ConvResLen expected=0x%x result=0x%x.\n", 
00412           aTestName, aResLength, destOff);
00413       return NS_ERROR_UNEXPECTED;
00414   }
00415   for (PRInt32 i=0; i<aResLength; i++) if (aRes[i] != dest[i]) {
00416       printf("ERROR at %s.stress.ConvResChar[%d] expected=0x%x result=0x%x.\n", 
00417           aTestName, i, aRes[i], dest[i]);
00418       return NS_ERROR_UNEXPECTED;
00419   }
00420 
00421   return NS_OK;
00422 }
00423 
00427 nsresult resetDecoder(nsIUnicodeDecoder * aDec, const char * aTestName)
00428 {
00429   nsresult res = aDec->Reset();
00430 
00431   if (NS_FAILED(res)) {
00432     printf("ERROR at %s.dec.Reset() code=0x%x.\n",aTestName,res);
00433     return res;
00434   }
00435 
00436   return res;
00437 }
00438 
00442 nsresult resetEncoder(nsIUnicodeEncoder * aEnc, const char * aTestName)
00443 {
00444   nsresult res = aEnc->Reset();
00445 
00446   if (NS_FAILED(res)) {
00447     printf("ERROR at %s.enc.Reset() code=0x%x.\n",aTestName,res);
00448     return res;
00449   }
00450 
00451   return res;
00452 }
00453 
00457 nsresult standardDecoderTest(char * aTestName, char * aCharset, char * aSrc, 
00458   PRInt32 aSrcLen, PRUnichar * aRes, PRInt32 aResLen)
00459 {
00460   printf("\n[%s] Unicode <- %s\n", aTestName, aCharset);
00461 
00462   // create converter
00463   CREATE_DECODER(aCharset);
00464 
00465   // test converter - easy test
00466   res = testDecoder(dec, aSrc, aSrcLen, aRes, aResLen, aTestName);
00467 
00468   // reset converter
00469   if (NS_SUCCEEDED(res)) res = resetDecoder(dec, aTestName);
00470 
00471   // test converter - stress test
00472   if (NS_SUCCEEDED(res)) 
00473     res = testStressDecoder(dec, aSrc, aSrcLen, aRes, aResLen, aTestName);
00474 
00475   // release converter
00476   NS_RELEASE(dec);
00477 
00478   if (NS_FAILED(res)) {
00479     return res;
00480   } else {
00481     printf("Test Passed.\n");
00482     return NS_OK;
00483   }
00484 }
00485 
00486 nsresult loadBinaryFile(char * aFile, char * aBuff, PRInt32 * aBuffLen)
00487 {
00488   FILE * f = fopen(aFile, "rb");
00489   if (f == NULL) {
00490     printf("ERROR at opening file: \"%s\".\n", aFile);
00491     return NS_ERROR_UNEXPECTED;
00492   }
00493 
00494   PRInt32 n = fread(aBuff, 1, *aBuffLen, f);
00495   if (n >= *aBuffLen) {
00496     printf("ERROR at reading from file \"%s\": too much input data.\n", aFile);
00497     return NS_ERROR_UNEXPECTED;
00498   }
00499 
00500   *aBuffLen = n;
00501   fclose(f);
00502   return NS_OK;
00503 }
00504 
00505 nsresult loadUnicodeFile(char * aFile, PRUnichar * aBuff, PRInt32 * aBuffLen)
00506 {
00507   PRInt32 buffLen = 2*(*aBuffLen);
00508 
00509   nsresult res = loadBinaryFile(aFile, (char *)aBuff, &buffLen);
00510   if (NS_FAILED(res)) return res;
00511 
00512   *aBuffLen = buffLen/2;
00513   return NS_OK;
00514 }
00515 
00516 nsresult testDecoderFromFiles(char * aCharset, char * aSrcFile, char * aResultFile)
00517 {
00518   // create converter
00519   CREATE_DECODER(aCharset);
00520 
00521   PRInt32 srcLen = GENERAL_BUFFER;
00522   char src[GENERAL_BUFFER];
00523   PRInt32 expLen = GENERAL_BUFFER/2;
00524   PRUnichar exp[GENERAL_BUFFER/2];
00525 
00526   res = loadBinaryFile(aSrcFile, src, &srcLen);
00527   if (NS_FAILED(res)) return res;
00528 
00529   res = loadUnicodeFile(aResultFile, exp, &expLen);
00530   if (NS_FAILED(res)) return res;
00531 
00532   // test converter - easy test
00533   res = testDecoder(dec, src, srcLen, exp, expLen, "dec");
00534 
00535   // release converter
00536   NS_RELEASE(dec);
00537 
00538   if (NS_FAILED(res)) {
00539     return res;
00540   } else {
00541     printf("Test Passed.\n");
00542     return NS_OK;
00543   }
00544 
00545   return NS_OK;
00546 }
00547 
00548 nsresult testEncoderFromFiles(char * aCharset, char * aSrcFile, char * aResultFile)
00549 {
00550   // XXX write me
00551   return NS_OK;
00552 }
00553 
00554 //----------------------------------------------------------------------
00555 // Decoders testing functions
00556 
00560 nsresult testISO2022JPDecoder()
00561 {
00562   char * testName = "T102";
00563   printf("\n[%s] Unicode <- ISO2022JP\n", testName);
00564 
00565   // create converter
00566   CREATE_DECODER("iso-2022-jp");
00567 
00568   // test data
00569   char src[] = {"\x0d\x7f\xdd" "\x1b(J\xaa\xdc\x41" "\x1b$B\x21\x21" "\x1b$@\x32\x37" "\x1b(J\x1b(B\xcc"};
00570   PRUnichar exp[] = {0x000d,0x007f,0xfffd, 0xff6a,0xFF9C,0x0041, 0x3000, 0x5378, 0xfffd};
00571 
00572   // test converter - normal operation
00573   res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
00574 
00575   // reset converter
00576   if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
00577 
00578   // test converter - stress test
00579   if (NS_SUCCEEDED(res)) 
00580     res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
00581 
00582   // release converter
00583   NS_RELEASE(dec);
00584 
00585   if (NS_FAILED(res)) {
00586     return res;
00587   } else {
00588     printf("Test Passed.\n");
00589     return NS_OK;
00590   }
00591 }
00592 
00596 nsresult testEUCJPDecoder()
00597 {
00598   char * testName = "T103";
00599   printf("\n[%s] Unicode <- EUCJP\n", testName);
00600 
00601   // create converter
00602   CREATE_DECODER("euc-jp");
00603 
00604   // test data
00605   char src[] = {"\x45"};
00606   PRUnichar exp[] = {0x0045};
00607 
00608   // test converter - normal operation
00609   res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
00610 
00611   // reset converter
00612   if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
00613 
00614   // test converter - stress test
00615   if (NS_SUCCEEDED(res)) 
00616     res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
00617 
00618   // release converter
00619   NS_RELEASE(dec);
00620 
00621   if (NS_FAILED(res)) {
00622     return res;
00623   } else {
00624     printf("Test Passed.\n");
00625     return NS_OK;
00626   }
00627 }
00628 
00632 nsresult testISO88597Decoder()
00633 {
00634   char * testName = "T104";
00635   printf("\n[%s] Unicode <- ISO88597\n", testName);
00636 
00637   // create converter
00638   CREATE_DECODER("iso-8859-7");
00639 
00640   // test data
00641   char src[] = {
00642     "\x09\x0d\x20\x40"
00643     "\x80\x98\xa3\xaf"
00644     "\xa7\xb1\xb3\xc9"
00645     "\xd9\xe3\xf4\xff"
00646   };
00647   PRUnichar exp[] = {
00648     0x0009, 0x000d, 0x0020, 0x0040, 
00649     0xfffd, 0xfffd, 0x00a3, 0x2015,
00650     0x00a7, 0x00b1, 0x00b3, 0x0399,
00651     0x03a9, 0x03b3, 0x03c4, 0xfffd
00652   };
00653 
00654   // test converter - normal operation
00655   res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
00656 
00657   // reset converter
00658   if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
00659 
00660   // test converter - stress test
00661   if (NS_SUCCEEDED(res)) 
00662     res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
00663 
00664   // release converter
00665   NS_RELEASE(dec);
00666 
00667   if (NS_FAILED(res)) {
00668     return res;
00669   } else {
00670     printf("Test Passed.\n");
00671     return NS_OK;
00672   }
00673 }
00674 
00678 nsresult testSJISDecoder()
00679 {
00680   char * testName = "T105";
00681   printf("\n[%s] Unicode <- SJIS\n", testName);
00682 
00683   // create converter
00684   CREATE_DECODER("Shift_JIS");
00685 
00686   // test data
00687   char src[] = {
00688     "Japanese" /* English */
00689     "\x8a\xbf\x8e\x9a" /* Kanji */
00690     "\x83\x4a\x83\x5e\x83\x4a\x83\x69" /* Kantakana */
00691     "\x82\xd0\x82\xe7\x82\xaa\x82\xc8" /* Hiragana */
00692     "\x82\x50\x82\x51\x82\x52\x82\x60\x82\x61\x82\x62" /* full width 123ABC */
00693   };
00694   PRUnichar exp[] = {
00695     0x004A, 0x0061, 0x0070, 0x0061, 0x006E, 0x0065, 0x0073, 0x0065,
00696     0x6f22, 0x5b57,
00697     0x30ab, 0x30bf, 0x30ab, 0x30ca,
00698     0x3072, 0x3089, 0x304c, 0x306a,
00699     0xff11, 0xff12, 0xff13, 0xff21, 0xff22, 0xff23
00700   };
00701 
00702   // test converter - normal operation
00703   res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
00704 
00705   // reset converter
00706   if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
00707 
00708   // test converter - stress test
00709   if (NS_SUCCEEDED(res)) 
00710     res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
00711 
00712   // release converter
00713   NS_RELEASE(dec);
00714 
00715   if (NS_FAILED(res)) {
00716     return res;
00717   } else {
00718     printf("Test Passed.\n");
00719     return NS_OK;
00720   }
00721 }
00722 
00726 nsresult testUTF8Decoder()
00727 {
00728   char * testName = "T106";
00729   printf("\n[%s] Unicode <- UTF8\n", testName);
00730 
00731   // create converter
00732   CREATE_DECODER("utf-8");
00733 
00734 #ifdef NOPE // XXX decomment this when I have test data
00735   // test data
00736   char src[] = {};
00737   PRUnichar exp[] = {};
00738 
00739   // test converter - normal operation
00740   res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
00741 
00742   // reset converter
00743   if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
00744 
00745   // test converter - stress test
00746   if (NS_SUCCEEDED(res)) 
00747     res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
00748 #endif
00749 
00750   // release converter
00751   NS_RELEASE(dec);
00752 
00753   if (NS_FAILED(res)) {
00754     return res;
00755   } else {
00756     printf("Test Passed.\n");
00757     return NS_OK;
00758   }
00759 }
00760 
00764 nsresult testMUTF7Decoder()
00765 {
00766   char * testName = "T107";
00767   printf("\n[%s] Unicode <- MUTF7\n", testName);
00768 
00769   // create converter
00770   CREATE_DECODER("x-imap4-modified-utf7");
00771 
00772   // test data
00773   char src[] = {"\x50\x51\x52\x53&AAAAAAAA-&-&AAA-"};
00774   PRUnichar exp[] = {0x0050,0x0051,0x0052,0x0053,0x0000,0x0000,0x0000,'&',0x0000};
00775 
00776   // test converter - normal operation
00777   res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
00778 
00779   // reset converter
00780   if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
00781 
00782   // test converter - stress test
00783   if (NS_SUCCEEDED(res)) 
00784     res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
00785 
00786   // release converter
00787   NS_RELEASE(dec);
00788 
00789   if (NS_FAILED(res)) {
00790     return res;
00791   } else {
00792     printf("Test Passed.\n");
00793     return NS_OK;
00794   }
00795 }
00796 
00800 nsresult testUTF7Decoder()
00801 {
00802   char * testName = "T108";
00803   printf("\n[%s] Unicode <- UTF7\n", testName);
00804 
00805   // create converter
00806   CREATE_DECODER("utf-7");
00807 
00808   // test data
00809   char src[] = {"+ADwAIQ-DOC"};
00810   PRUnichar exp[] = {'<','!','D','O','C'};
00811 
00812   // test converter - normal operation
00813   res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
00814 
00815   // reset converter
00816   if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
00817 
00818   // test converter - stress test
00819   if (NS_SUCCEEDED(res)) 
00820     res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
00821 
00822   // release converter
00823   NS_RELEASE(dec);
00824 
00825   if (NS_FAILED(res)) {
00826     return res;
00827   } else {
00828     printf("Test Passed.\n");
00829     return NS_OK;
00830   }
00831 }
00832 
00833 //----------------------------------------------------------------------
00834 // Encoders testing functions
00835 
00839 nsresult testLatin1Encoder()
00840 {
00841   char * testName = "T201";
00842   printf("\n[%s] Unicode -> Latin1\n", testName);
00843 
00844   // create converter
00845   CREATE_ENCODER("iso-8859-1");
00846   enc->SetOutputErrorBehavior(enc->kOnError_Replace, NULL, 0x00cc);
00847 
00848   // test data
00849   PRUnichar src[] = {0x0001,0x0002,0xffff,0x00e3};
00850   char exp[] = {"\x01\x02\xcc\xe3"};
00851 
00852   // test converter - easy test
00853   res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
00854 
00855   // reset converter
00856   if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName);
00857 
00858   // test converter - stress test
00859   if (NS_SUCCEEDED(res)) 
00860     res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
00861 
00862   // release converter
00863   NS_RELEASE(enc);
00864 
00865   if (NS_FAILED(res)) {
00866     return res;
00867   } else {
00868     printf("Test Passed.\n");
00869     return NS_OK;
00870   }
00871 }
00872 
00876 nsresult testSJISEncoder()
00877 {
00878   char * testName = "T202";
00879   printf("\n[%s] Unicode -> SJIS\n", testName);
00880 
00881   // create converter
00882   CREATE_ENCODER("Shift_JIS");
00883   enc->SetOutputErrorBehavior(enc->kOnError_Replace, NULL, 0x00cc);
00884 
00885   // test data
00886   PRUnichar src[] = {
00887     0x004A, 0x0061, 0x0070, 0x0061, 0x006E, 0x0065, 0x0073, 0x0065,
00888     0x6f22, 0x5b57,
00889     0x30ab, 0x30bf, 0x30ab, 0x30ca,
00890     0x3072, 0x3089, 0x304c, 0x306a,
00891     0xff11, 0xff12, 0xff13, 0xff21, 0xff22, 0xff23
00892   };
00893   char exp[] = {
00894     "Japanese" /* English */
00895     "\x8a\xbf\x8e\x9a" /* Kanji */
00896     "\x83\x4a\x83\x5e\x83\x4a\x83\x69" /* Kantakana */
00897     "\x82\xd0\x82\xe7\x82\xaa\x82\xc8" /* Hiragana */
00898     "\x82\x50\x82\x51\x82\x52\x82\x60\x82\x61\x82\x62" /* full width 123ABC */
00899   };
00900 
00901   // test converter - easy test
00902   res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
00903 
00904   // reset converter
00905   if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName);
00906 
00907   // test converter - stress test
00908   if (NS_SUCCEEDED(res)) 
00909     res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
00910 
00911   // release converter
00912   NS_RELEASE(enc);
00913 
00914   if (NS_FAILED(res)) {
00915     return res;
00916   } else {
00917     printf("Test Passed.\n");
00918     return NS_OK;
00919   }
00920 }
00921 
00925 nsresult testEUCJPEncoder()
00926 {
00927   char * testName = "T203";
00928   printf("\n[%s] Unicode -> EUCJP\n", testName);
00929 
00930   // create converter
00931   CREATE_ENCODER("euc-jp");
00932   enc->SetOutputErrorBehavior(enc->kOnError_Replace, NULL, 0x00cc);
00933 
00934   // test data
00935   PRUnichar src[] = {0x0045, 0x0054};
00936   char exp[] = {"\x45\x54"};
00937 
00938   // test converter - easy test
00939   res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
00940 
00941   // reset converter
00942   if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName);
00943 
00944   // test converter - stress test
00945   if (NS_SUCCEEDED(res)) 
00946     res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
00947 
00948   // release converter
00949   NS_RELEASE(enc);
00950 
00951   if (NS_FAILED(res)) {
00952     return res;
00953   } else {
00954     printf("Test Passed.\n");
00955     return NS_OK;
00956   }
00957 }
00958 
00962 nsresult testISO2022JPEncoder()
00963 {
00964   char * testName = "T204";
00965   printf("\n[%s] Unicode -> ISO2022JP\n", testName);
00966 
00967   // create converter
00968   CREATE_ENCODER("iso-2022-jp");
00969   enc->SetOutputErrorBehavior(enc->kOnError_Replace, NULL, 0x00cc);
00970 
00971   // test data
00972   PRUnichar src[] = {0x000d,0x007f, 0xff6a,0xFF9C, 0x3000, 0x5378};
00973   char exp[] = {"\x0d\x7f" "\x1b(J\xaa\xdc" "\x1b$@\x21\x21\x32\x37\x1b(B"};
00974 
00975   // test converter - easy test
00976   res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
00977 
00978   // reset converter
00979   if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName);
00980 
00981   // test converter - stress test
00982   if (NS_SUCCEEDED(res)) 
00983     res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
00984 
00985   // release converter
00986   NS_RELEASE(enc);
00987 
00988   if (NS_FAILED(res)) {
00989     return res;
00990   } else {
00991     printf("Test Passed.\n");
00992     return NS_OK;
00993   }
00994 }
00995 
00999 nsresult testMUTF7Encoder()
01000 {
01001   char * testName = "T205";
01002   printf("\n[%s] Unicode -> MUTF-7\n", testName);
01003 
01004   // create converter
01005   CREATE_ENCODER("x-imap4-modified-utf7");
01006   enc->SetOutputErrorBehavior(enc->kOnError_Replace, NULL, 0x00cc);
01007 
01008   // test data
01009   PRUnichar src[] = {0x0050,0x0051,0x0052,0x0053,0x0000,0x0000,0x0000,'&',0x0000};
01010   char exp[] = {"\x50\x51\x52\x53&AAAAAAAA-&-&AAA-"};
01011 
01012   // test converter - easy test
01013   res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
01014 
01015   // reset converter
01016   if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName);
01017 
01018   // test converter - stress test
01019   if (NS_SUCCEEDED(res)) 
01020     res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
01021 
01022   // release converter
01023   NS_RELEASE(enc);
01024 
01025   if (NS_FAILED(res)) {
01026     return res;
01027   } else {
01028     printf("Test Passed.\n");
01029     return NS_OK;
01030   }
01031 }
01032 
01036 nsresult testUTF7Encoder()
01037 {
01038   char * testName = "T206";
01039   printf("\n[%s] Unicode -> UTF-7\n", testName);
01040 
01041   // create converter
01042   CREATE_ENCODER("utf-7");
01043   enc->SetOutputErrorBehavior(enc->kOnError_Replace, NULL, 0x00cc);
01044 
01045   // test data
01046   PRUnichar src[] = {'e','t','i','r','a',0x0a};
01047   char exp[] = {"etira\x0a"};
01048 
01049   // test converter - easy test
01050   res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
01051 
01052   // reset converter
01053   if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName);
01054 
01055   // test converter - stress test
01056   if (NS_SUCCEEDED(res)) 
01057     res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
01058 
01059   // release converter
01060   NS_RELEASE(enc);
01061 
01062   if (NS_FAILED(res)) {
01063     return res;
01064   } else {
01065     printf("Test Passed.\n");
01066     return NS_OK;
01067   }
01068 }
01069 
01070 nsresult  testPlatformCharset()
01071 {
01072   nsIPlatformCharset *cinfo;
01073   nsresult res = CallGetService(kPlatformCharsetCID, &cinfo);
01074   if (NS_FAILED(res)) {
01075     printf("ERROR at GetService() code=0x%x.\n",res);
01076     return res;
01077   }
01078 
01079   nsString value;
01080   res = cinfo->GetCharset(kPlatformCharsetSel_PlainTextInClipboard , value);
01081   printf("Clipboard plain text encoding = %s\n", NS_LossyConvertUCS2toASCII(value).get());
01082   
01083   res = cinfo->GetCharset(kPlatformCharsetSel_FileName , value);
01084   printf("File Name encoding = %s\n", NS_LossyConvertUCS2toASCII(value).get());
01085 
01086   res = cinfo->GetCharset(kPlatformCharsetSel_Menu , value);
01087   printf("Menu encoding = %s\n", NS_LossyConvertUCS2toASCII(value).get());
01088 
01089   cinfo->Release();
01090   return res;
01091   
01092 }
01093 
01094 //----------------------------------------------------------------------
01095 // Testing functions
01096 
01097 nsresult testAll()
01098 {
01099   nsresult res;
01100 
01101   // test the manager(s)
01102   res = testCharsetConverterManager();
01103   if (NS_FAILED(res)) return res;
01104 
01105   testPlatformCharset();
01106 
01107   // test decoders
01108   standardDecoderTest("T101", "ISO-8859-1", bLatin1_d0, bLatin1_s0, cLatin1_d0, cLatin1_s0);
01109   testISO2022JPDecoder();
01110   testEUCJPDecoder();
01111   testISO88597Decoder();
01112   testSJISDecoder();
01113   testUTF8Decoder();
01114   testMUTF7Decoder();
01115   testUTF7Decoder();
01116 
01117   // test encoders
01118   testLatin1Encoder();
01119   testSJISEncoder();
01120   testEUCJPEncoder();
01121   testISO2022JPEncoder();
01122   testMUTF7Encoder();
01123   testUTF7Encoder();
01124 
01125   // return
01126   return NS_OK;
01127 }
01128 
01129 nsresult testFromArgs(int argc, char **argv)
01130 {
01131   nsresult res = NS_OK;
01132   if ((argc == 5) && (!strcmp(argv[1], "-tdec"))) {
01133     res = testDecoderFromFiles(argv[2], argv[3], argv[4]);
01134   } else if ((argc == 5) && (!strcmp(argv[1], "-tenc"))) {
01135     res = testEncoderFromFiles(argv[2], argv[3], argv[4]);
01136   } else {
01137     printf("Usage:\n");
01138     printf("  TestUConv.exe\n");
01139     printf("  TestUConv.exe -tdec encoding inputEncodedFile expectedResultUnicodeFile\n");
01140     printf("  TestUConv.exe -tenc encoding inputUnicodeFile expectedResultEncodedFile\n");
01141   }
01142 
01143   return res;
01144 }
01145 
01146 //----------------------------------------------------------------------
01147 // Main program functions
01148 
01149 nsresult init()
01150 {
01151   nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
01152   if (NS_FAILED(rv))
01153     return rv;
01154   return CallGetService(kCharsetConverterManagerCID, &ccMan);
01155 }
01156 
01157 nsresult done()
01158 {
01159   NS_RELEASE(ccMan);
01160   return NS_OK;
01161 }
01162 
01163 int main(int argc, char **argv)
01164 {
01165   nsresult res;
01166 
01167   res = init();
01168   if (NS_FAILED(res)) return -1;
01169 
01170   if (argc <= 1) {
01171     printf("*** Unicode Converters Test ***\n");
01172     res = testAll();
01173     printf("\n***---------  Done  --------***\n");
01174   } else {
01175     res = testFromArgs(argc, argv);
01176   }
01177 
01178   done();
01179 
01180   if (NS_FAILED(res)) return -1;
01181   else return 0;
01182 }