intl/uconv/tests/nsTestUConv.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include <stdio.h>
     7 #include <string.h>
     8 #include "nsXPCOM.h"
     9 #include "nsIComponentManager.h"
    10 #include "nsIServiceManager.h"
    11 #include "nsISupports.h"
    12 #include "nsICharsetConverterManager.h"
    13 #include "nsIPlatformCharset.h"
    14 #include "nsReadableUtils.h"
    17 static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
    18 static NS_DEFINE_CID(kPlatformCharsetCID, NS_PLATFORMCHARSET_CID);
    20 /**
    21  * Test program for the Unicode Converters.
    22  *
    23  * Error messages format inside of a test.
    24  *
    25  * - silent while all is OK.
    26  * 
    27  * - "ERROR at T001.easyConversion.Convert() code=0xfffd.\n"
    28  * - "ERROR at T001.easyConversion.ConvResLen expected=0x02 result=0x04.\n"
    29  * 
    30  * - "Test Passed.\n" for a successful end.
    31  *
    32  * @created         01/Dec/1998
    33  * @author  Catalin Rotaru [CATA]
    34  */
    36 //----------------------------------------------------------------------
    37 // Global variables and macros
    39 #define GENERAL_BUFFER 20000 // general purpose buffer; for Unicode divide by 2
    41 #define ARRAY_SIZE(_array)                                      \
    42      (sizeof(_array) / sizeof(_array[0]))
    44 nsICharsetConverterManager * ccMan = nullptr;
    46 /**
    47  * Test data for Latin1 charset.
    48  */
    50 char bLatin1_d0[] = {
    51   "\x00\x0d\x7f\x80\xff"
    52 };
    54 char16_t cLatin1_d0[] = {
    55   0x0000,0x000d,0x007f,0x20ac,0x00ff
    56 };
    58 int32_t bLatin1_s0 = ARRAY_SIZE(bLatin1_d0)-1;
    59 int32_t cLatin1_s0 = ARRAY_SIZE(cLatin1_d0);
    61 //----------------------------------------------------------------------
    62 // Converter Manager test code
    64 nsresult testCharsetConverterManager()
    65 {
    66   printf("\n[T001] CharsetConverterManager\n");
    68   return NS_OK;
    69 }
    71 //----------------------------------------------------------------------
    72 // Helper functions and macros for testing decoders and encoders
    74 #define CREATE_DECODER(_charset)                                \
    75     nsIUnicodeDecoder * dec;                                    \
    76     nsAutoString str;str.AssignASCII(_charset);                 \
    77     nsresult res = ccMan->GetUnicodeDecoder(&str,&dec);         \
    78     if (NS_FAILED(res)) {                                       \
    79       printf("ERROR at GetUnicodeDecoder() code=0x%x.\n",res);  \
    80       return res;                                               \
    81     }
    83 #define CREATE_ENCODER(_charset)                                \
    84     nsIUnicodeEncoder * enc;                                    \
    85     nsAutoString str; str.AssignASCII(_charset);                \
    86     nsresult res = ccMan->GetUnicodeEncoder(&str,&enc);         \
    87     if (NS_FAILED(res)) {                                       \
    88       printf("ERROR at GetUnicodeEncoder() code=0x%x.\n",res);  \
    89       return res;                                               \
    90     }
    92 /**
    93  * Decoder test.
    94  * 
    95  * This method will test the conversion only.
    96  */
    97 nsresult testDecoder(nsIUnicodeDecoder * aDec, 
    98                      const char * aSrc, int32_t aSrcLength, 
    99                      const char16_t * aRes, int32_t aResLength,
   100                      const char * aTestName)
   101 {
   102   nsresult res;
   104   // prepare for conversion
   105   int32_t srcLen = aSrcLength;
   106   char16_t dest[GENERAL_BUFFER/2];
   107   int32_t destLen = GENERAL_BUFFER/2;
   109   // conversion
   110   res = aDec->Convert(aSrc, &srcLen, dest, &destLen);
   111   // we want a perfect result here - the test data should be complete!
   112   if (res != NS_OK) {
   113     printf("ERROR at %s.easy.Decode() code=0x%x.\n",aTestName,res);
   114     return NS_ERROR_UNEXPECTED;
   115   }
   117   // compare results
   118   if (aResLength != destLen) {
   119       printf("ERROR at %s.easy.DecResLen expected=0x%x result=0x%x.\n", 
   120           aTestName, aResLength, destLen);
   121       return NS_ERROR_UNEXPECTED;
   122   }
   123   for (int32_t i=0; i<aResLength; i++) if (aRes[i] != dest[i]) {
   124       printf("ERROR at %s.easy.DecResChar[%d] expected=0x%x result=0x%x.\n", 
   125           aTestName, i, aRes[i], dest[i]);
   126       return NS_ERROR_UNEXPECTED;
   127   }
   129   return NS_OK;
   130 }
   132 /**
   133  * Encoder test.
   134  * 
   135  * This method will test the conversion only.
   136  */
   137 nsresult testEncoder(nsIUnicodeEncoder * aEnc, 
   138                      const char16_t * aSrc, int32_t aSrcLength, 
   139                      const char * aRes, int32_t aResLength,
   140                      const char * aTestName)
   141 {
   142   nsresult res;
   144   // prepare for conversion
   145   int32_t srcLen = 0;
   146   char dest[GENERAL_BUFFER];
   147   int32_t destLen = 0;
   148   int32_t bcr, bcw;
   150   // conversion
   151   bcr = aSrcLength;
   152   bcw = GENERAL_BUFFER;
   153   res = aEnc->Convert(aSrc, &bcr, dest, &bcw);
   154   srcLen += bcr;
   155   destLen += bcw;
   156   // we want a perfect result here - the test data should be complete!
   157   if (res != NS_OK) {
   158     printf("ERROR at %s.easy.Encode() code=0x%x.\n",aTestName,res);
   159     return NS_ERROR_UNEXPECTED;
   160   }
   162   // finish
   163   bcw = GENERAL_BUFFER - destLen;
   164   res = aEnc->Finish(dest + destLen, &bcw);
   165   destLen += bcw;
   166   // we want a perfect result here - the test data should be complete!
   167   if (res != NS_OK) {
   168     printf("ERROR at %s.easy.Finish() code=0x%x.\n",aTestName,res);
   169     return NS_ERROR_UNEXPECTED;
   170   }
   172   // compare results
   173   if (aResLength != destLen) {
   174       printf("ERROR at %s.easy.EncResLen expected=0x%x result=0x%x.\n", 
   175           aTestName, aResLength, destLen);
   176       return NS_ERROR_UNEXPECTED;
   177   }
   178   for (int32_t i=0; i<aResLength; i++) if (aRes[i] != dest[i]) {
   179       printf("ERROR at %s.easy.EncResChar[%d] expected=0x%x result=0x%x.\n", 
   180           aTestName, i, aRes[i], dest[i]);
   181       return NS_ERROR_UNEXPECTED;
   182   }
   184   return NS_OK;
   185 }
   187 /**
   188  * Decoder test.
   189  * 
   190  * This method will test a given converter under a given set of data and some 
   191  * very stressful conditions.
   192  */
   193 nsresult testStressDecoder(nsIUnicodeDecoder * aDec, 
   194                            const char * aSrc, int32_t aSrcLength, 
   195                            const char16_t * aRes, int32_t aResLength,
   196                            const char * aTestName)
   197 {
   198   nsresult res;
   200   // get estimated length
   201   int32_t estimatedLength;
   202   res = aDec->GetMaxLength(aSrc, aSrcLength, &estimatedLength);
   203   if (NS_FAILED(res)) {
   204     printf("ERROR at %s.stress.Length() code=0x%x.\n",aTestName,res);
   205     return res;
   206   }
   207   bool exactLength = (res == NS_EXACT_LENGTH);
   209   // prepare for conversion
   210   int32_t srcLen = 0;
   211   int32_t srcOff = 0;
   212   char16_t dest[1024];
   213   int32_t destLen = 0;
   214   int32_t destOff = 0;
   216   // controlled conversion
   217   for (;srcOff < aSrcLength;) {
   218     res = aDec->Convert(aSrc + srcOff, &srcLen, dest + destOff, &destLen);
   219     if (NS_FAILED(res)) {
   220       printf("ERROR at %s.stress.Convert() code=0x%x.\n",aTestName,res);
   221       return res;
   222     }
   224     srcOff+=srcLen;
   225     destOff+=destLen;
   227     // give a little input each time; it'll be consumed if enough output space
   228     srcLen = 1;
   229     // give output space only when requested: sadic!
   230     if (res == NS_PARTIAL_MORE_OUTPUT) {
   231       destLen = 1;
   232     } else {
   233       destLen = 0;
   234     }
   235   }
   237   // we want perfect result here - the test data should be complete!
   238   if (res != NS_OK) {
   239     printf("ERROR at %s.stress.postConvert() code=0x%x.\n",aTestName,res);
   240     return NS_ERROR_UNEXPECTED;
   241   }
   243   // compare lengths
   244   if (exactLength) {
   245     if (destOff != estimatedLength) {
   246       printf("ERROR at %s.stress.EstimatedLen expected=0x%x result=0x%x.\n",
   247           aTestName, estimatedLength, destOff);
   248       return NS_ERROR_UNEXPECTED;
   249     }
   250   } else {
   251     if (destOff > estimatedLength) {
   252       printf("ERROR at %s.stress.EstimatedLen expected<=0x%x result=0x%x.\n",
   253           aTestName, estimatedLength, destOff);
   254       return NS_ERROR_UNEXPECTED;
   255     }
   256   }
   258   // compare results
   259   if (aResLength != destOff) {
   260       printf("ERROR at %s.stress.ConvResLen expected=0x%x result=0x%x.\n", 
   261           aTestName, aResLength, destOff);
   262       return NS_ERROR_UNEXPECTED;
   263   }
   264   for (int32_t i=0; i<aResLength; i++) if (aRes[i] != dest[i]) {
   265       printf("ERROR at %s.stress.ConvResChar[%d] expected=0x%x result=0x%x.\n", 
   266           aTestName, i, aRes[i], dest[i]);
   267       return NS_ERROR_UNEXPECTED;
   268   }
   270   return NS_OK;
   271 }
   273 /**
   274  * Encoder test.
   275  * 
   276  * This method will test a given converter under a given set of data and some 
   277  * very stressful conditions.
   278  */
   279 nsresult testStressEncoder(nsIUnicodeEncoder * aEnc, 
   280                            const char16_t * aSrc, int32_t aSrcLength,
   281                            const char * aRes, int32_t aResLength, 
   282                            const char * aTestName)
   283 {
   284   nsresult res;
   286   // get estimated length
   287   int32_t estimatedLength;
   288   res = aEnc->GetMaxLength(aSrc, aSrcLength, &estimatedLength);
   289   if (NS_FAILED(res)) {
   290     printf("ERROR at %s.stress.Length() code=0x%x.\n",aTestName,res);
   291     return res;
   292   }
   293   bool exactLength = (res == NS_OK_UENC_EXACTLENGTH);
   295   // prepare for conversion
   296   int32_t srcLen = 0;
   297   int32_t srcOff = 0;
   298   char dest[GENERAL_BUFFER];
   299   int32_t destLen = 0;
   300   int32_t destOff = 0;
   302   // controlled conversion
   303   for (;srcOff < aSrcLength;) {
   304     res = aEnc->Convert(aSrc + srcOff, &srcLen, dest + destOff, &destLen);
   305     if (NS_FAILED(res)) {
   306       printf("ERROR at %s.stress.Convert() code=0x%x.\n",aTestName,res);
   307       return res;
   308     }
   310     srcOff+=srcLen;
   311     destOff+=destLen;
   313     // give a little input each time; it'll be consumed if enough output space
   314     srcLen = 1;
   315     // give output space only when requested: sadic!
   316     if (res == NS_OK_UENC_MOREOUTPUT) {
   317       destLen = 1;
   318     } else {
   319       destLen = 0;
   320     }
   321   }
   323   if (res != NS_OK) if (res != NS_OK_UENC_MOREOUTPUT) {
   324     printf("ERROR at %s.stress.postConvert() code=0x%x.\n",aTestName,res);
   325     return NS_ERROR_UNEXPECTED;
   326   } 
   328   for (;;) {
   329     res = aEnc->Finish(dest + destOff, &destLen);
   330     if (NS_FAILED(res)) {
   331       printf("ERROR at %s.stress.Finish() code=0x%x.\n",aTestName,res);
   332       return res;
   333     }
   335     destOff+=destLen;
   337     // give output space only when requested: sadic!
   338     if (res == NS_OK_UENC_MOREOUTPUT) {
   339       destLen = 1;
   340     } else break;
   341   }
   343   // compare lengths
   344   if (exactLength) {
   345     if (destOff != estimatedLength) {
   346       printf("ERROR at %s.stress.EstimatedLen expected=0x%x result=0x%x.\n",
   347           aTestName, estimatedLength, destOff);
   348       return NS_ERROR_UNEXPECTED;
   349     }
   350   } else {
   351     if (destOff > estimatedLength) {
   352       printf("ERROR at %s.stress.EstimatedLen expected<=0x%x result=0x%x.\n",
   353           aTestName, estimatedLength, destOff);
   354       return NS_ERROR_UNEXPECTED;
   355     }
   356   }
   358   // compare results
   359   if (aResLength != destOff) {
   360       printf("ERROR at %s.stress.ConvResLen expected=0x%x result=0x%x.\n", 
   361           aTestName, aResLength, destOff);
   362       return NS_ERROR_UNEXPECTED;
   363   }
   364   for (int32_t i=0; i<aResLength; i++) if (aRes[i] != dest[i]) {
   365       printf("ERROR at %s.stress.ConvResChar[%d] expected=0x%x result=0x%x.\n", 
   366           aTestName, i, aRes[i], dest[i]);
   367       return NS_ERROR_UNEXPECTED;
   368   }
   370   return NS_OK;
   371 }
   373 /**
   374  * Reset decoder.
   375  */
   376 nsresult resetDecoder(nsIUnicodeDecoder * aDec, const char * aTestName)
   377 {
   378   nsresult res = aDec->Reset();
   380   if (NS_FAILED(res)) {
   381     printf("ERROR at %s.dec.Reset() code=0x%x.\n",aTestName,res);
   382     return res;
   383   }
   385   return res;
   386 }
   388 /**
   389  * Reset encoder.
   390  */
   391 nsresult resetEncoder(nsIUnicodeEncoder * aEnc, const char * aTestName)
   392 {
   393   nsresult res = aEnc->Reset();
   395   if (NS_FAILED(res)) {
   396     printf("ERROR at %s.enc.Reset() code=0x%x.\n",aTestName,res);
   397     return res;
   398   }
   400   return res;
   401 }
   403 /**
   404  * A standard decoder test.
   405  */
   406 nsresult standardDecoderTest(char * aTestName, char * aCharset, char * aSrc, 
   407   int32_t aSrcLen, char16_t * aRes, int32_t aResLen)
   408 {
   409   printf("\n[%s] Unicode <- %s\n", aTestName, aCharset);
   411   // create converter
   412   CREATE_DECODER(aCharset);
   414   // test converter - easy test
   415   res = testDecoder(dec, aSrc, aSrcLen, aRes, aResLen, aTestName);
   417   // reset converter
   418   if (NS_SUCCEEDED(res)) res = resetDecoder(dec, aTestName);
   420   // test converter - stress test
   421   if (NS_SUCCEEDED(res)) 
   422     res = testStressDecoder(dec, aSrc, aSrcLen, aRes, aResLen, aTestName);
   424   // release converter
   425   NS_RELEASE(dec);
   427   if (NS_FAILED(res)) {
   428     return res;
   429   } else {
   430     printf("Test Passed.\n");
   431     return NS_OK;
   432   }
   433 }
   435 nsresult loadBinaryFile(char * aFile, char * aBuff, int32_t * aBuffLen)
   436 {
   437   FILE * f = fopen(aFile, "rb");
   438   if (!f) {
   439     printf("ERROR at opening file: \"%s\".\n", aFile);
   440     return NS_ERROR_UNEXPECTED;
   441   }
   443   int32_t n = fread(aBuff, 1, *aBuffLen, f);
   444   if (n >= *aBuffLen) {
   445     printf("ERROR at reading from file \"%s\": too much input data.\n", aFile);
   446     return NS_ERROR_UNEXPECTED;
   447   }
   449   *aBuffLen = n;
   450   fclose(f);
   451   return NS_OK;
   452 }
   454 nsresult loadUnicodeFile(char * aFile, char16_t * aBuff, int32_t * aBuffLen)
   455 {
   456   int32_t buffLen = 2*(*aBuffLen);
   458   nsresult res = loadBinaryFile(aFile, (char *)aBuff, &buffLen);
   459   if (NS_FAILED(res)) return res;
   461   *aBuffLen = buffLen/2;
   462   return NS_OK;
   463 }
   465 nsresult testDecoderFromFiles(char * aCharset, char * aSrcFile, char * aResultFile)
   466 {
   467   // create converter
   468   CREATE_DECODER(aCharset);
   470   int32_t srcLen = GENERAL_BUFFER;
   471   char src[GENERAL_BUFFER];
   472   int32_t expLen = GENERAL_BUFFER/2;
   473   char16_t exp[GENERAL_BUFFER/2];
   475   res = loadBinaryFile(aSrcFile, src, &srcLen);
   476   if (NS_FAILED(res)) return res;
   478   res = loadUnicodeFile(aResultFile, exp, &expLen);
   479   if (NS_FAILED(res)) return res;
   481   // test converter - easy test
   482   res = testDecoder(dec, src, srcLen, exp, expLen, "dec");
   484   // release converter
   485   NS_RELEASE(dec);
   487   if (NS_FAILED(res)) {
   488     return res;
   489   } else {
   490     printf("Test Passed.\n");
   491     return NS_OK;
   492   }
   494   return NS_OK;
   495 }
   497 nsresult testEncoderFromFiles(char * aCharset, char * aSrcFile, char * aResultFile)
   498 {
   499   // XXX write me
   500   return NS_OK;
   501 }
   503 //----------------------------------------------------------------------
   504 // Decoders testing functions
   506 /**
   507  * Test the ISO2022JP decoder.
   508  */
   509 nsresult testISO2022JPDecoder()
   510 {
   511   char * testName = "T102";
   512   printf("\n[%s] Unicode <- ISO2022JP\n", testName);
   514   // create converter
   515   CREATE_DECODER("iso-2022-jp");
   517   // test data
   518   char src[] = {"\x0d\x7f\xdd" "\x1b(J\xaa\xdc\x41" "\x1b$B\x21\x21" "\x1b$@\x32\x37" "\x1b(J\x1b(B\xcc"};
   519   char16_t exp[] = {0x000d,0x007f,0xfffd, 0xff6a,0xFF9C,0x0041, 0x3000, 0x5378, 0xfffd};
   521   // test converter - normal operation
   522   res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
   524   // reset converter
   525   if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
   527   // test converter - stress test
   528   if (NS_SUCCEEDED(res)) 
   529     res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
   531   // release converter
   532   NS_RELEASE(dec);
   534   if (NS_FAILED(res)) {
   535     return res;
   536   } else {
   537     printf("Test Passed.\n");
   538     return NS_OK;
   539   }
   540 }
   542 /**
   543  * Test the EUCJP decoder.
   544  */
   545 nsresult testEUCJPDecoder()
   546 {
   547   char * testName = "T103";
   548   printf("\n[%s] Unicode <- EUCJP\n", testName);
   550   // create converter
   551   CREATE_DECODER("euc-jp");
   553   // test data
   554   char src[] = {"\x45"};
   555   char16_t exp[] = {0x0045};
   557   // test converter - normal operation
   558   res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
   560   // reset converter
   561   if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
   563   // test converter - stress test
   564   if (NS_SUCCEEDED(res)) 
   565     res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
   567   // release converter
   568   NS_RELEASE(dec);
   570   if (NS_FAILED(res)) {
   571     return res;
   572   } else {
   573     printf("Test Passed.\n");
   574     return NS_OK;
   575   }
   576 }
   578 /**
   579  * Test the ISO88597 decoder.
   580  */
   581 nsresult testISO88597Decoder()
   582 {
   583   char * testName = "T104";
   584   printf("\n[%s] Unicode <- ISO88597\n", testName);
   586   // create converter
   587   CREATE_DECODER("iso-8859-7");
   589   // test data
   590   char src[] = {
   591     "\x09\x0d\x20\x40"
   592     "\x80\x98\xa3\xaf"
   593     "\xa7\xb1\xb3\xc9"
   594     "\xd9\xe3\xf4\xff"
   595   };
   596   char16_t exp[] = {
   597     0x0009, 0x000d, 0x0020, 0x0040, 
   598     0xfffd, 0xfffd, 0x00a3, 0x2015,
   599     0x00a7, 0x00b1, 0x00b3, 0x0399,
   600     0x03a9, 0x03b3, 0x03c4, 0xfffd
   601   };
   603   // test converter - normal operation
   604   res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
   606   // reset converter
   607   if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
   609   // test converter - stress test
   610   if (NS_SUCCEEDED(res)) 
   611     res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
   613   // release converter
   614   NS_RELEASE(dec);
   616   if (NS_FAILED(res)) {
   617     return res;
   618   } else {
   619     printf("Test Passed.\n");
   620     return NS_OK;
   621   }
   622 }
   624 /**
   625  * Test the SJIS decoder.
   626  */
   627 nsresult testSJISDecoder()
   628 {
   629   char * testName = "T105";
   630   printf("\n[%s] Unicode <- SJIS\n", testName);
   632   // create converter
   633   CREATE_DECODER("Shift_JIS");
   635   // test data
   636   char src[] = {
   637     "Japanese" /* English */
   638     "\x8a\xbf\x8e\x9a" /* Kanji */
   639     "\x83\x4a\x83\x5e\x83\x4a\x83\x69" /* Kantakana */
   640     "\x82\xd0\x82\xe7\x82\xaa\x82\xc8" /* Hiragana */
   641     "\x82\x50\x82\x51\x82\x52\x82\x60\x82\x61\x82\x62" /* full width 123ABC */
   642   };
   643   char16_t exp[] = {
   644     0x004A, 0x0061, 0x0070, 0x0061, 0x006E, 0x0065, 0x0073, 0x0065,
   645     0x6f22, 0x5b57,
   646     0x30ab, 0x30bf, 0x30ab, 0x30ca,
   647     0x3072, 0x3089, 0x304c, 0x306a,
   648     0xff11, 0xff12, 0xff13, 0xff21, 0xff22, 0xff23
   649   };
   651   // test converter - normal operation
   652   res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
   654   // reset converter
   655   if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
   657   // test converter - stress test
   658   if (NS_SUCCEEDED(res)) 
   659     res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
   661   // release converter
   662   NS_RELEASE(dec);
   664   if (NS_FAILED(res)) {
   665     return res;
   666   } else {
   667     printf("Test Passed.\n");
   668     return NS_OK;
   669   }
   670 }
   672 /**
   673  * Test the UTF8 decoder.
   674  */
   675 nsresult testUTF8Decoder()
   676 {
   677   char * testName = "T106";
   678   printf("\n[%s] Unicode <- UTF8\n", testName);
   680   // create converter
   681   CREATE_DECODER("utf-8");
   683 #ifdef NOPE // XXX decomment this when I have test data
   684   // test data
   685   char src[] = {};
   686   char16_t exp[] = {};
   688   // test converter - normal operation
   689   res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
   691   // reset converter
   692   if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
   694   // test converter - stress test
   695   if (NS_SUCCEEDED(res)) 
   696     res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
   697 #endif
   699   // release converter
   700   NS_RELEASE(dec);
   702   if (NS_FAILED(res)) {
   703     return res;
   704   } else {
   705     printf("Test Passed.\n");
   706     return NS_OK;
   707   }
   708 }
   710 //----------------------------------------------------------------------
   711 // Encoders testing functions
   713 /**
   714  * Test the Latin1 encoder.
   715  */
   716 nsresult testLatin1Encoder()
   717 {
   718   char * testName = "T201";
   719   printf("\n[%s] Unicode -> Latin1\n", testName);
   721   // create converter
   722   CREATE_ENCODER("iso-8859-1");
   723   enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc);
   725   // test data
   726   char16_t src[] = {0x0001,0x0002,0xffff,0x00e3};
   727   char exp[] = {"\x01\x02\xcc\xe3"};
   729   // test converter - easy test
   730   res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
   732   // reset converter
   733   if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName);
   735   // test converter - stress test
   736   if (NS_SUCCEEDED(res)) 
   737     res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
   739   // release converter
   740   NS_RELEASE(enc);
   742   if (NS_FAILED(res)) {
   743     return res;
   744   } else {
   745     printf("Test Passed.\n");
   746     return NS_OK;
   747   }
   748 }
   750 /**
   751  * Test the Shift-JIS encoder.
   752  */
   753 nsresult testSJISEncoder()
   754 {
   755   char * testName = "T202";
   756   printf("\n[%s] Unicode -> SJIS\n", testName);
   758   // create converter
   759   CREATE_ENCODER("Shift_JIS");
   760   enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc);
   762   // test data
   763   char16_t src[] = {
   764     0x004A, 0x0061, 0x0070, 0x0061, 0x006E, 0x0065, 0x0073, 0x0065,
   765     0x6f22, 0x5b57,
   766     0x30ab, 0x30bf, 0x30ab, 0x30ca,
   767     0x3072, 0x3089, 0x304c, 0x306a,
   768     0xff11, 0xff12, 0xff13, 0xff21, 0xff22, 0xff23
   769   };
   770   char exp[] = {
   771     "Japanese" /* English */
   772     "\x8a\xbf\x8e\x9a" /* Kanji */
   773     "\x83\x4a\x83\x5e\x83\x4a\x83\x69" /* Kantakana */
   774     "\x82\xd0\x82\xe7\x82\xaa\x82\xc8" /* Hiragana */
   775     "\x82\x50\x82\x51\x82\x52\x82\x60\x82\x61\x82\x62" /* full width 123ABC */
   776   };
   778   // test converter - easy test
   779   res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
   781   // reset converter
   782   if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName);
   784   // test converter - stress test
   785   if (NS_SUCCEEDED(res)) 
   786     res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
   788   // release converter
   789   NS_RELEASE(enc);
   791   if (NS_FAILED(res)) {
   792     return res;
   793   } else {
   794     printf("Test Passed.\n");
   795     return NS_OK;
   796   }
   797 }
   799 /**
   800  * Test the EUC-JP encoder.
   801  */
   802 nsresult testEUCJPEncoder()
   803 {
   804   char * testName = "T203";
   805   printf("\n[%s] Unicode -> EUCJP\n", testName);
   807   // create converter
   808   CREATE_ENCODER("euc-jp");
   809   enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc);
   811   // test data
   812   char16_t src[] = {0x0045, 0x0054};
   813   char exp[] = {"\x45\x54"};
   815   // test converter - easy test
   816   res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
   818   // reset converter
   819   if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName);
   821   // test converter - stress test
   822   if (NS_SUCCEEDED(res)) 
   823     res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
   825   // release converter
   826   NS_RELEASE(enc);
   828   if (NS_FAILED(res)) {
   829     return res;
   830   } else {
   831     printf("Test Passed.\n");
   832     return NS_OK;
   833   }
   834 }
   836 /**
   837  * Test the ISO-2022-JP encoder.
   838  */
   839 nsresult testISO2022JPEncoder()
   840 {
   841   char * testName = "T204";
   842   printf("\n[%s] Unicode -> ISO2022JP\n", testName);
   844   // create converter
   845   CREATE_ENCODER("iso-2022-jp");
   846   enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc);
   848   // test data
   849   char16_t src[] = {0x000d,0x007f, 0xff6a,0xFF9C, 0x3000, 0x5378};
   850   char exp[] = {"\x0d\x7f" "\x1b(J\xaa\xdc" "\x1b$@\x21\x21\x32\x37\x1b(B"};
   852   // test converter - easy test
   853   res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
   855   // reset converter
   856   if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName);
   858   // test converter - stress test
   859   if (NS_SUCCEEDED(res)) 
   860     res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName);
   862   // release converter
   863   NS_RELEASE(enc);
   865   if (NS_FAILED(res)) {
   866     return res;
   867   } else {
   868     printf("Test Passed.\n");
   869     return NS_OK;
   870   }
   871 }
   873 nsresult  testPlatformCharset()
   874 {
   875   nsIPlatformCharset *cinfo;
   876   nsresult res = CallGetService(kPlatformCharsetCID, &cinfo);
   877   if (NS_FAILED(res)) {
   878     printf("ERROR at GetService() code=0x%x.\n",res);
   879     return res;
   880   }
   882   nsString value;
   883   res = cinfo->GetCharset(kPlatformCharsetSel_PlainTextInClipboard , value);
   884   printf("Clipboard plain text encoding = %s\n", NS_LossyConvertUTF16toASCII(value).get());
   886   res = cinfo->GetCharset(kPlatformCharsetSel_FileName , value);
   887   printf("File Name encoding = %s\n", NS_LossyConvertUTF16toASCII(value).get());
   889   res = cinfo->GetCharset(kPlatformCharsetSel_Menu , value);
   890   printf("Menu encoding = %s\n", NS_LossyConvertUTF16toASCII(value).get());
   892   cinfo->Release();
   893   return res;
   895 }
   897 //----------------------------------------------------------------------
   898 // Testing functions
   900 nsresult testAll()
   901 {
   902   nsresult res;
   904   // test the manager(s)
   905   res = testCharsetConverterManager();
   906   if (NS_FAILED(res)) return res;
   908   testPlatformCharset();
   910   // test decoders
   911   standardDecoderTest("T101", "ISO-8859-1", bLatin1_d0, bLatin1_s0, cLatin1_d0, cLatin1_s0);
   912   testISO2022JPDecoder();
   913   testEUCJPDecoder();
   914   testISO88597Decoder();
   915   testSJISDecoder();
   916   testUTF8Decoder();
   917   testMUTF7Decoder();
   918   testUTF7Decoder();
   920   // test encoders
   921   testLatin1Encoder();
   922   testSJISEncoder();
   923   testEUCJPEncoder();
   924   testISO2022JPEncoder();
   925   testMUTF7Encoder();
   926   testUTF7Encoder();
   928   // return
   929   return NS_OK;
   930 }
   932 nsresult testFromArgs(int argc, char **argv)
   933 {
   934   nsresult res = NS_OK;
   935   if ((argc == 5) && (!strcmp(argv[1], "-tdec"))) {
   936     res = testDecoderFromFiles(argv[2], argv[3], argv[4]);
   937   } else if ((argc == 5) && (!strcmp(argv[1], "-tenc"))) {
   938     res = testEncoderFromFiles(argv[2], argv[3], argv[4]);
   939   } else {
   940     printf("Usage:\n");
   941     printf("  TestUConv.exe\n");
   942     printf("  TestUConv.exe -tdec encoding inputEncodedFile expectedResultUnicodeFile\n");
   943     printf("  TestUConv.exe -tenc encoding inputUnicodeFile expectedResultEncodedFile\n");
   944   }
   946   return res;
   947 }
   949 //----------------------------------------------------------------------
   950 // Main program functions
   952 nsresult init()
   953 {
   954   nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
   955   if (NS_FAILED(rv))
   956     return rv;
   957   return CallGetService(kCharsetConverterManagerCID, &ccMan);
   958 }
   960 nsresult done()
   961 {
   962   NS_RELEASE(ccMan);
   963   return NS_OK;
   964 }
   966 int main(int argc, char **argv)
   967 {
   968   nsresult res;
   970   res = init();
   971   if (NS_FAILED(res)) return -1;
   973   if (argc <= 1) {
   974     printf("*** Unicode Converters Test ***\n");
   975     res = testAll();
   976     printf("\n***---------  Done  --------***\n");
   977   } else {
   978     res = testFromArgs(argc, argv);
   979   }
   981   done();
   983   if (NS_FAILED(res)) return -1;
   984   else return 0;
   985 }

mercurial