michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include "nsXPCOM.h" michael@0: #include "nsIComponentManager.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsISupports.h" michael@0: #include "nsICharsetConverterManager.h" michael@0: #include "nsIPlatformCharset.h" michael@0: #include "nsReadableUtils.h" michael@0: michael@0: michael@0: static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID); michael@0: static NS_DEFINE_CID(kPlatformCharsetCID, NS_PLATFORMCHARSET_CID); michael@0: michael@0: /** michael@0: * Test program for the Unicode Converters. michael@0: * michael@0: * Error messages format inside of a test. michael@0: * michael@0: * - silent while all is OK. michael@0: * michael@0: * - "ERROR at T001.easyConversion.Convert() code=0xfffd.\n" michael@0: * - "ERROR at T001.easyConversion.ConvResLen expected=0x02 result=0x04.\n" michael@0: * michael@0: * - "Test Passed.\n" for a successful end. michael@0: * michael@0: * @created 01/Dec/1998 michael@0: * @author Catalin Rotaru [CATA] michael@0: */ michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Global variables and macros michael@0: michael@0: #define GENERAL_BUFFER 20000 // general purpose buffer; for Unicode divide by 2 michael@0: michael@0: #define ARRAY_SIZE(_array) \ michael@0: (sizeof(_array) / sizeof(_array[0])) michael@0: michael@0: nsICharsetConverterManager * ccMan = nullptr; michael@0: michael@0: /** michael@0: * Test data for Latin1 charset. michael@0: */ michael@0: michael@0: char bLatin1_d0[] = { michael@0: "\x00\x0d\x7f\x80\xff" michael@0: }; michael@0: michael@0: char16_t cLatin1_d0[] = { michael@0: 0x0000,0x000d,0x007f,0x20ac,0x00ff michael@0: }; michael@0: michael@0: int32_t bLatin1_s0 = ARRAY_SIZE(bLatin1_d0)-1; michael@0: int32_t cLatin1_s0 = ARRAY_SIZE(cLatin1_d0); michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Converter Manager test code michael@0: michael@0: nsresult testCharsetConverterManager() michael@0: { michael@0: printf("\n[T001] CharsetConverterManager\n"); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Helper functions and macros for testing decoders and encoders michael@0: michael@0: #define CREATE_DECODER(_charset) \ michael@0: nsIUnicodeDecoder * dec; \ michael@0: nsAutoString str;str.AssignASCII(_charset); \ michael@0: nsresult res = ccMan->GetUnicodeDecoder(&str,&dec); \ michael@0: if (NS_FAILED(res)) { \ michael@0: printf("ERROR at GetUnicodeDecoder() code=0x%x.\n",res); \ michael@0: return res; \ michael@0: } michael@0: michael@0: #define CREATE_ENCODER(_charset) \ michael@0: nsIUnicodeEncoder * enc; \ michael@0: nsAutoString str; str.AssignASCII(_charset); \ michael@0: nsresult res = ccMan->GetUnicodeEncoder(&str,&enc); \ michael@0: if (NS_FAILED(res)) { \ michael@0: printf("ERROR at GetUnicodeEncoder() code=0x%x.\n",res); \ michael@0: return res; \ michael@0: } michael@0: michael@0: /** michael@0: * Decoder test. michael@0: * michael@0: * This method will test the conversion only. michael@0: */ michael@0: nsresult testDecoder(nsIUnicodeDecoder * aDec, michael@0: const char * aSrc, int32_t aSrcLength, michael@0: const char16_t * aRes, int32_t aResLength, michael@0: const char * aTestName) michael@0: { michael@0: nsresult res; michael@0: michael@0: // prepare for conversion michael@0: int32_t srcLen = aSrcLength; michael@0: char16_t dest[GENERAL_BUFFER/2]; michael@0: int32_t destLen = GENERAL_BUFFER/2; michael@0: michael@0: // conversion michael@0: res = aDec->Convert(aSrc, &srcLen, dest, &destLen); michael@0: // we want a perfect result here - the test data should be complete! michael@0: if (res != NS_OK) { michael@0: printf("ERROR at %s.easy.Decode() code=0x%x.\n",aTestName,res); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: // compare results michael@0: if (aResLength != destLen) { michael@0: printf("ERROR at %s.easy.DecResLen expected=0x%x result=0x%x.\n", michael@0: aTestName, aResLength, destLen); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: for (int32_t i=0; iConvert(aSrc, &bcr, dest, &bcw); michael@0: srcLen += bcr; michael@0: destLen += bcw; michael@0: // we want a perfect result here - the test data should be complete! michael@0: if (res != NS_OK) { michael@0: printf("ERROR at %s.easy.Encode() code=0x%x.\n",aTestName,res); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: // finish michael@0: bcw = GENERAL_BUFFER - destLen; michael@0: res = aEnc->Finish(dest + destLen, &bcw); michael@0: destLen += bcw; michael@0: // we want a perfect result here - the test data should be complete! michael@0: if (res != NS_OK) { michael@0: printf("ERROR at %s.easy.Finish() code=0x%x.\n",aTestName,res); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: // compare results michael@0: if (aResLength != destLen) { michael@0: printf("ERROR at %s.easy.EncResLen expected=0x%x result=0x%x.\n", michael@0: aTestName, aResLength, destLen); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: for (int32_t i=0; iGetMaxLength(aSrc, aSrcLength, &estimatedLength); michael@0: if (NS_FAILED(res)) { michael@0: printf("ERROR at %s.stress.Length() code=0x%x.\n",aTestName,res); michael@0: return res; michael@0: } michael@0: bool exactLength = (res == NS_EXACT_LENGTH); michael@0: michael@0: // prepare for conversion michael@0: int32_t srcLen = 0; michael@0: int32_t srcOff = 0; michael@0: char16_t dest[1024]; michael@0: int32_t destLen = 0; michael@0: int32_t destOff = 0; michael@0: michael@0: // controlled conversion michael@0: for (;srcOff < aSrcLength;) { michael@0: res = aDec->Convert(aSrc + srcOff, &srcLen, dest + destOff, &destLen); michael@0: if (NS_FAILED(res)) { michael@0: printf("ERROR at %s.stress.Convert() code=0x%x.\n",aTestName,res); michael@0: return res; michael@0: } michael@0: michael@0: srcOff+=srcLen; michael@0: destOff+=destLen; michael@0: michael@0: // give a little input each time; it'll be consumed if enough output space michael@0: srcLen = 1; michael@0: // give output space only when requested: sadic! michael@0: if (res == NS_PARTIAL_MORE_OUTPUT) { michael@0: destLen = 1; michael@0: } else { michael@0: destLen = 0; michael@0: } michael@0: } michael@0: michael@0: // we want perfect result here - the test data should be complete! michael@0: if (res != NS_OK) { michael@0: printf("ERROR at %s.stress.postConvert() code=0x%x.\n",aTestName,res); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: // compare lengths michael@0: if (exactLength) { michael@0: if (destOff != estimatedLength) { michael@0: printf("ERROR at %s.stress.EstimatedLen expected=0x%x result=0x%x.\n", michael@0: aTestName, estimatedLength, destOff); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: } else { michael@0: if (destOff > estimatedLength) { michael@0: printf("ERROR at %s.stress.EstimatedLen expected<=0x%x result=0x%x.\n", michael@0: aTestName, estimatedLength, destOff); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: } michael@0: michael@0: // compare results michael@0: if (aResLength != destOff) { michael@0: printf("ERROR at %s.stress.ConvResLen expected=0x%x result=0x%x.\n", michael@0: aTestName, aResLength, destOff); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: for (int32_t i=0; iGetMaxLength(aSrc, aSrcLength, &estimatedLength); michael@0: if (NS_FAILED(res)) { michael@0: printf("ERROR at %s.stress.Length() code=0x%x.\n",aTestName,res); michael@0: return res; michael@0: } michael@0: bool exactLength = (res == NS_OK_UENC_EXACTLENGTH); michael@0: michael@0: // prepare for conversion michael@0: int32_t srcLen = 0; michael@0: int32_t srcOff = 0; michael@0: char dest[GENERAL_BUFFER]; michael@0: int32_t destLen = 0; michael@0: int32_t destOff = 0; michael@0: michael@0: // controlled conversion michael@0: for (;srcOff < aSrcLength;) { michael@0: res = aEnc->Convert(aSrc + srcOff, &srcLen, dest + destOff, &destLen); michael@0: if (NS_FAILED(res)) { michael@0: printf("ERROR at %s.stress.Convert() code=0x%x.\n",aTestName,res); michael@0: return res; michael@0: } michael@0: michael@0: srcOff+=srcLen; michael@0: destOff+=destLen; michael@0: michael@0: // give a little input each time; it'll be consumed if enough output space michael@0: srcLen = 1; michael@0: // give output space only when requested: sadic! michael@0: if (res == NS_OK_UENC_MOREOUTPUT) { michael@0: destLen = 1; michael@0: } else { michael@0: destLen = 0; michael@0: } michael@0: } michael@0: michael@0: if (res != NS_OK) if (res != NS_OK_UENC_MOREOUTPUT) { michael@0: printf("ERROR at %s.stress.postConvert() code=0x%x.\n",aTestName,res); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: for (;;) { michael@0: res = aEnc->Finish(dest + destOff, &destLen); michael@0: if (NS_FAILED(res)) { michael@0: printf("ERROR at %s.stress.Finish() code=0x%x.\n",aTestName,res); michael@0: return res; michael@0: } michael@0: michael@0: destOff+=destLen; michael@0: michael@0: // give output space only when requested: sadic! michael@0: if (res == NS_OK_UENC_MOREOUTPUT) { michael@0: destLen = 1; michael@0: } else break; michael@0: } michael@0: michael@0: // compare lengths michael@0: if (exactLength) { michael@0: if (destOff != estimatedLength) { michael@0: printf("ERROR at %s.stress.EstimatedLen expected=0x%x result=0x%x.\n", michael@0: aTestName, estimatedLength, destOff); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: } else { michael@0: if (destOff > estimatedLength) { michael@0: printf("ERROR at %s.stress.EstimatedLen expected<=0x%x result=0x%x.\n", michael@0: aTestName, estimatedLength, destOff); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: } michael@0: michael@0: // compare results michael@0: if (aResLength != destOff) { michael@0: printf("ERROR at %s.stress.ConvResLen expected=0x%x result=0x%x.\n", michael@0: aTestName, aResLength, destOff); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: for (int32_t i=0; iReset(); michael@0: michael@0: if (NS_FAILED(res)) { michael@0: printf("ERROR at %s.dec.Reset() code=0x%x.\n",aTestName,res); michael@0: return res; michael@0: } michael@0: michael@0: return res; michael@0: } michael@0: michael@0: /** michael@0: * Reset encoder. michael@0: */ michael@0: nsresult resetEncoder(nsIUnicodeEncoder * aEnc, const char * aTestName) michael@0: { michael@0: nsresult res = aEnc->Reset(); michael@0: michael@0: if (NS_FAILED(res)) { michael@0: printf("ERROR at %s.enc.Reset() code=0x%x.\n",aTestName,res); michael@0: return res; michael@0: } michael@0: michael@0: return res; michael@0: } michael@0: michael@0: /** michael@0: * A standard decoder test. michael@0: */ michael@0: nsresult standardDecoderTest(char * aTestName, char * aCharset, char * aSrc, michael@0: int32_t aSrcLen, char16_t * aRes, int32_t aResLen) michael@0: { michael@0: printf("\n[%s] Unicode <- %s\n", aTestName, aCharset); michael@0: michael@0: // create converter michael@0: CREATE_DECODER(aCharset); michael@0: michael@0: // test converter - easy test michael@0: res = testDecoder(dec, aSrc, aSrcLen, aRes, aResLen, aTestName); michael@0: michael@0: // reset converter michael@0: if (NS_SUCCEEDED(res)) res = resetDecoder(dec, aTestName); michael@0: michael@0: // test converter - stress test michael@0: if (NS_SUCCEEDED(res)) michael@0: res = testStressDecoder(dec, aSrc, aSrcLen, aRes, aResLen, aTestName); michael@0: michael@0: // release converter michael@0: NS_RELEASE(dec); michael@0: michael@0: if (NS_FAILED(res)) { michael@0: return res; michael@0: } else { michael@0: printf("Test Passed.\n"); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: nsresult loadBinaryFile(char * aFile, char * aBuff, int32_t * aBuffLen) michael@0: { michael@0: FILE * f = fopen(aFile, "rb"); michael@0: if (!f) { michael@0: printf("ERROR at opening file: \"%s\".\n", aFile); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: int32_t n = fread(aBuff, 1, *aBuffLen, f); michael@0: if (n >= *aBuffLen) { michael@0: printf("ERROR at reading from file \"%s\": too much input data.\n", aFile); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: *aBuffLen = n; michael@0: fclose(f); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult loadUnicodeFile(char * aFile, char16_t * aBuff, int32_t * aBuffLen) michael@0: { michael@0: int32_t buffLen = 2*(*aBuffLen); michael@0: michael@0: nsresult res = loadBinaryFile(aFile, (char *)aBuff, &buffLen); michael@0: if (NS_FAILED(res)) return res; michael@0: michael@0: *aBuffLen = buffLen/2; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult testDecoderFromFiles(char * aCharset, char * aSrcFile, char * aResultFile) michael@0: { michael@0: // create converter michael@0: CREATE_DECODER(aCharset); michael@0: michael@0: int32_t srcLen = GENERAL_BUFFER; michael@0: char src[GENERAL_BUFFER]; michael@0: int32_t expLen = GENERAL_BUFFER/2; michael@0: char16_t exp[GENERAL_BUFFER/2]; michael@0: michael@0: res = loadBinaryFile(aSrcFile, src, &srcLen); michael@0: if (NS_FAILED(res)) return res; michael@0: michael@0: res = loadUnicodeFile(aResultFile, exp, &expLen); michael@0: if (NS_FAILED(res)) return res; michael@0: michael@0: // test converter - easy test michael@0: res = testDecoder(dec, src, srcLen, exp, expLen, "dec"); michael@0: michael@0: // release converter michael@0: NS_RELEASE(dec); michael@0: michael@0: if (NS_FAILED(res)) { michael@0: return res; michael@0: } else { michael@0: printf("Test Passed.\n"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult testEncoderFromFiles(char * aCharset, char * aSrcFile, char * aResultFile) michael@0: { michael@0: // XXX write me michael@0: return NS_OK; michael@0: } michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Decoders testing functions michael@0: michael@0: /** michael@0: * Test the ISO2022JP decoder. michael@0: */ michael@0: nsresult testISO2022JPDecoder() michael@0: { michael@0: char * testName = "T102"; michael@0: printf("\n[%s] Unicode <- ISO2022JP\n", testName); michael@0: michael@0: // create converter michael@0: CREATE_DECODER("iso-2022-jp"); michael@0: michael@0: // test data michael@0: char src[] = {"\x0d\x7f\xdd" "\x1b(J\xaa\xdc\x41" "\x1b$B\x21\x21" "\x1b$@\x32\x37" "\x1b(J\x1b(B\xcc"}; michael@0: char16_t exp[] = {0x000d,0x007f,0xfffd, 0xff6a,0xFF9C,0x0041, 0x3000, 0x5378, 0xfffd}; michael@0: michael@0: // test converter - normal operation michael@0: res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); michael@0: michael@0: // reset converter michael@0: if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName); michael@0: michael@0: // test converter - stress test michael@0: if (NS_SUCCEEDED(res)) michael@0: res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); michael@0: michael@0: // release converter michael@0: NS_RELEASE(dec); michael@0: michael@0: if (NS_FAILED(res)) { michael@0: return res; michael@0: } else { michael@0: printf("Test Passed.\n"); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Test the EUCJP decoder. michael@0: */ michael@0: nsresult testEUCJPDecoder() michael@0: { michael@0: char * testName = "T103"; michael@0: printf("\n[%s] Unicode <- EUCJP\n", testName); michael@0: michael@0: // create converter michael@0: CREATE_DECODER("euc-jp"); michael@0: michael@0: // test data michael@0: char src[] = {"\x45"}; michael@0: char16_t exp[] = {0x0045}; michael@0: michael@0: // test converter - normal operation michael@0: res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); michael@0: michael@0: // reset converter michael@0: if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName); michael@0: michael@0: // test converter - stress test michael@0: if (NS_SUCCEEDED(res)) michael@0: res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); michael@0: michael@0: // release converter michael@0: NS_RELEASE(dec); michael@0: michael@0: if (NS_FAILED(res)) { michael@0: return res; michael@0: } else { michael@0: printf("Test Passed.\n"); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Test the ISO88597 decoder. michael@0: */ michael@0: nsresult testISO88597Decoder() michael@0: { michael@0: char * testName = "T104"; michael@0: printf("\n[%s] Unicode <- ISO88597\n", testName); michael@0: michael@0: // create converter michael@0: CREATE_DECODER("iso-8859-7"); michael@0: michael@0: // test data michael@0: char src[] = { michael@0: "\x09\x0d\x20\x40" michael@0: "\x80\x98\xa3\xaf" michael@0: "\xa7\xb1\xb3\xc9" michael@0: "\xd9\xe3\xf4\xff" michael@0: }; michael@0: char16_t exp[] = { michael@0: 0x0009, 0x000d, 0x0020, 0x0040, michael@0: 0xfffd, 0xfffd, 0x00a3, 0x2015, michael@0: 0x00a7, 0x00b1, 0x00b3, 0x0399, michael@0: 0x03a9, 0x03b3, 0x03c4, 0xfffd michael@0: }; michael@0: michael@0: // test converter - normal operation michael@0: res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); michael@0: michael@0: // reset converter michael@0: if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName); michael@0: michael@0: // test converter - stress test michael@0: if (NS_SUCCEEDED(res)) michael@0: res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); michael@0: michael@0: // release converter michael@0: NS_RELEASE(dec); michael@0: michael@0: if (NS_FAILED(res)) { michael@0: return res; michael@0: } else { michael@0: printf("Test Passed.\n"); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Test the SJIS decoder. michael@0: */ michael@0: nsresult testSJISDecoder() michael@0: { michael@0: char * testName = "T105"; michael@0: printf("\n[%s] Unicode <- SJIS\n", testName); michael@0: michael@0: // create converter michael@0: CREATE_DECODER("Shift_JIS"); michael@0: michael@0: // test data michael@0: char src[] = { michael@0: "Japanese" /* English */ michael@0: "\x8a\xbf\x8e\x9a" /* Kanji */ michael@0: "\x83\x4a\x83\x5e\x83\x4a\x83\x69" /* Kantakana */ michael@0: "\x82\xd0\x82\xe7\x82\xaa\x82\xc8" /* Hiragana */ michael@0: "\x82\x50\x82\x51\x82\x52\x82\x60\x82\x61\x82\x62" /* full width 123ABC */ michael@0: }; michael@0: char16_t exp[] = { michael@0: 0x004A, 0x0061, 0x0070, 0x0061, 0x006E, 0x0065, 0x0073, 0x0065, michael@0: 0x6f22, 0x5b57, michael@0: 0x30ab, 0x30bf, 0x30ab, 0x30ca, michael@0: 0x3072, 0x3089, 0x304c, 0x306a, michael@0: 0xff11, 0xff12, 0xff13, 0xff21, 0xff22, 0xff23 michael@0: }; michael@0: michael@0: // test converter - normal operation michael@0: res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); michael@0: michael@0: // reset converter michael@0: if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName); michael@0: michael@0: // test converter - stress test michael@0: if (NS_SUCCEEDED(res)) michael@0: res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); michael@0: michael@0: // release converter michael@0: NS_RELEASE(dec); michael@0: michael@0: if (NS_FAILED(res)) { michael@0: return res; michael@0: } else { michael@0: printf("Test Passed.\n"); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Test the UTF8 decoder. michael@0: */ michael@0: nsresult testUTF8Decoder() michael@0: { michael@0: char * testName = "T106"; michael@0: printf("\n[%s] Unicode <- UTF8\n", testName); michael@0: michael@0: // create converter michael@0: CREATE_DECODER("utf-8"); michael@0: michael@0: #ifdef NOPE // XXX decomment this when I have test data michael@0: // test data michael@0: char src[] = {}; michael@0: char16_t exp[] = {}; michael@0: michael@0: // test converter - normal operation michael@0: res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); michael@0: michael@0: // reset converter michael@0: if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName); michael@0: michael@0: // test converter - stress test michael@0: if (NS_SUCCEEDED(res)) michael@0: res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); michael@0: #endif michael@0: michael@0: // release converter michael@0: NS_RELEASE(dec); michael@0: michael@0: if (NS_FAILED(res)) { michael@0: return res; michael@0: } else { michael@0: printf("Test Passed.\n"); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Encoders testing functions michael@0: michael@0: /** michael@0: * Test the Latin1 encoder. michael@0: */ michael@0: nsresult testLatin1Encoder() michael@0: { michael@0: char * testName = "T201"; michael@0: printf("\n[%s] Unicode -> Latin1\n", testName); michael@0: michael@0: // create converter michael@0: CREATE_ENCODER("iso-8859-1"); michael@0: enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc); michael@0: michael@0: // test data michael@0: char16_t src[] = {0x0001,0x0002,0xffff,0x00e3}; michael@0: char exp[] = {"\x01\x02\xcc\xe3"}; michael@0: michael@0: // test converter - easy test michael@0: res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); michael@0: michael@0: // reset converter michael@0: if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName); michael@0: michael@0: // test converter - stress test michael@0: if (NS_SUCCEEDED(res)) michael@0: res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); michael@0: michael@0: // release converter michael@0: NS_RELEASE(enc); michael@0: michael@0: if (NS_FAILED(res)) { michael@0: return res; michael@0: } else { michael@0: printf("Test Passed.\n"); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Test the Shift-JIS encoder. michael@0: */ michael@0: nsresult testSJISEncoder() michael@0: { michael@0: char * testName = "T202"; michael@0: printf("\n[%s] Unicode -> SJIS\n", testName); michael@0: michael@0: // create converter michael@0: CREATE_ENCODER("Shift_JIS"); michael@0: enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc); michael@0: michael@0: // test data michael@0: char16_t src[] = { michael@0: 0x004A, 0x0061, 0x0070, 0x0061, 0x006E, 0x0065, 0x0073, 0x0065, michael@0: 0x6f22, 0x5b57, michael@0: 0x30ab, 0x30bf, 0x30ab, 0x30ca, michael@0: 0x3072, 0x3089, 0x304c, 0x306a, michael@0: 0xff11, 0xff12, 0xff13, 0xff21, 0xff22, 0xff23 michael@0: }; michael@0: char exp[] = { michael@0: "Japanese" /* English */ michael@0: "\x8a\xbf\x8e\x9a" /* Kanji */ michael@0: "\x83\x4a\x83\x5e\x83\x4a\x83\x69" /* Kantakana */ michael@0: "\x82\xd0\x82\xe7\x82\xaa\x82\xc8" /* Hiragana */ michael@0: "\x82\x50\x82\x51\x82\x52\x82\x60\x82\x61\x82\x62" /* full width 123ABC */ michael@0: }; michael@0: michael@0: // test converter - easy test michael@0: res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); michael@0: michael@0: // reset converter michael@0: if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName); michael@0: michael@0: // test converter - stress test michael@0: if (NS_SUCCEEDED(res)) michael@0: res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); michael@0: michael@0: // release converter michael@0: NS_RELEASE(enc); michael@0: michael@0: if (NS_FAILED(res)) { michael@0: return res; michael@0: } else { michael@0: printf("Test Passed.\n"); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Test the EUC-JP encoder. michael@0: */ michael@0: nsresult testEUCJPEncoder() michael@0: { michael@0: char * testName = "T203"; michael@0: printf("\n[%s] Unicode -> EUCJP\n", testName); michael@0: michael@0: // create converter michael@0: CREATE_ENCODER("euc-jp"); michael@0: enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc); michael@0: michael@0: // test data michael@0: char16_t src[] = {0x0045, 0x0054}; michael@0: char exp[] = {"\x45\x54"}; michael@0: michael@0: // test converter - easy test michael@0: res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); michael@0: michael@0: // reset converter michael@0: if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName); michael@0: michael@0: // test converter - stress test michael@0: if (NS_SUCCEEDED(res)) michael@0: res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); michael@0: michael@0: // release converter michael@0: NS_RELEASE(enc); michael@0: michael@0: if (NS_FAILED(res)) { michael@0: return res; michael@0: } else { michael@0: printf("Test Passed.\n"); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Test the ISO-2022-JP encoder. michael@0: */ michael@0: nsresult testISO2022JPEncoder() michael@0: { michael@0: char * testName = "T204"; michael@0: printf("\n[%s] Unicode -> ISO2022JP\n", testName); michael@0: michael@0: // create converter michael@0: CREATE_ENCODER("iso-2022-jp"); michael@0: enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc); michael@0: michael@0: // test data michael@0: char16_t src[] = {0x000d,0x007f, 0xff6a,0xFF9C, 0x3000, 0x5378}; michael@0: char exp[] = {"\x0d\x7f" "\x1b(J\xaa\xdc" "\x1b$@\x21\x21\x32\x37\x1b(B"}; michael@0: michael@0: // test converter - easy test michael@0: res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); michael@0: michael@0: // reset converter michael@0: if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName); michael@0: michael@0: // test converter - stress test michael@0: if (NS_SUCCEEDED(res)) michael@0: res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); michael@0: michael@0: // release converter michael@0: NS_RELEASE(enc); michael@0: michael@0: if (NS_FAILED(res)) { michael@0: return res; michael@0: } else { michael@0: printf("Test Passed.\n"); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: nsresult testPlatformCharset() michael@0: { michael@0: nsIPlatformCharset *cinfo; michael@0: nsresult res = CallGetService(kPlatformCharsetCID, &cinfo); michael@0: if (NS_FAILED(res)) { michael@0: printf("ERROR at GetService() code=0x%x.\n",res); michael@0: return res; michael@0: } michael@0: michael@0: nsString value; michael@0: res = cinfo->GetCharset(kPlatformCharsetSel_PlainTextInClipboard , value); michael@0: printf("Clipboard plain text encoding = %s\n", NS_LossyConvertUTF16toASCII(value).get()); michael@0: michael@0: res = cinfo->GetCharset(kPlatformCharsetSel_FileName , value); michael@0: printf("File Name encoding = %s\n", NS_LossyConvertUTF16toASCII(value).get()); michael@0: michael@0: res = cinfo->GetCharset(kPlatformCharsetSel_Menu , value); michael@0: printf("Menu encoding = %s\n", NS_LossyConvertUTF16toASCII(value).get()); michael@0: michael@0: cinfo->Release(); michael@0: return res; michael@0: michael@0: } michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Testing functions michael@0: michael@0: nsresult testAll() michael@0: { michael@0: nsresult res; michael@0: michael@0: // test the manager(s) michael@0: res = testCharsetConverterManager(); michael@0: if (NS_FAILED(res)) return res; michael@0: michael@0: testPlatformCharset(); michael@0: michael@0: // test decoders michael@0: standardDecoderTest("T101", "ISO-8859-1", bLatin1_d0, bLatin1_s0, cLatin1_d0, cLatin1_s0); michael@0: testISO2022JPDecoder(); michael@0: testEUCJPDecoder(); michael@0: testISO88597Decoder(); michael@0: testSJISDecoder(); michael@0: testUTF8Decoder(); michael@0: testMUTF7Decoder(); michael@0: testUTF7Decoder(); michael@0: michael@0: // test encoders michael@0: testLatin1Encoder(); michael@0: testSJISEncoder(); michael@0: testEUCJPEncoder(); michael@0: testISO2022JPEncoder(); michael@0: testMUTF7Encoder(); michael@0: testUTF7Encoder(); michael@0: michael@0: // return michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult testFromArgs(int argc, char **argv) michael@0: { michael@0: nsresult res = NS_OK; michael@0: if ((argc == 5) && (!strcmp(argv[1], "-tdec"))) { michael@0: res = testDecoderFromFiles(argv[2], argv[3], argv[4]); michael@0: } else if ((argc == 5) && (!strcmp(argv[1], "-tenc"))) { michael@0: res = testEncoderFromFiles(argv[2], argv[3], argv[4]); michael@0: } else { michael@0: printf("Usage:\n"); michael@0: printf(" TestUConv.exe\n"); michael@0: printf(" TestUConv.exe -tdec encoding inputEncodedFile expectedResultUnicodeFile\n"); michael@0: printf(" TestUConv.exe -tenc encoding inputUnicodeFile expectedResultEncodedFile\n"); michael@0: } michael@0: michael@0: return res; michael@0: } michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Main program functions michael@0: michael@0: nsresult init() michael@0: { michael@0: nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: return CallGetService(kCharsetConverterManagerCID, &ccMan); michael@0: } michael@0: michael@0: nsresult done() michael@0: { michael@0: NS_RELEASE(ccMan); michael@0: return NS_OK; michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: nsresult res; michael@0: michael@0: res = init(); michael@0: if (NS_FAILED(res)) return -1; michael@0: michael@0: if (argc <= 1) { michael@0: printf("*** Unicode Converters Test ***\n"); michael@0: res = testAll(); michael@0: printf("\n***--------- Done --------***\n"); michael@0: } else { michael@0: res = testFromArgs(argc, argv); michael@0: } michael@0: michael@0: done(); michael@0: michael@0: if (NS_FAILED(res)) return -1; michael@0: else return 0; michael@0: }