ISO2022JP to Unicode converter adedd.

This commit is contained in:
cata%netscape.com 1999-02-12 19:50:47 +00:00
Родитель a5721b39ed
Коммит 3e584889b2
3 изменённых файлов: 304 добавлений и 82 удалений

Просмотреть файл

@ -32,7 +32,8 @@ SIMPLE_PROGRAMS = $(CPPSRCS:.cpp=)
include $(topsrcdir)/config/rules.mk
INCLUDES += -I$(srcdir)/../ucvlatin \
-I$(srcdir)/../ucvja
-I$(srcdir)/../ucvja \
-I$(srcdir)/../ucvja2
LIBS += $(NSPR_LIBS) -L$(DIST)/bin/ -lxpcom -lraptorbase -lreg -luconv

Просмотреть файл

@ -39,15 +39,12 @@ OBJS = \
LINCS= \
-I$(PUBLIC)\xpcom \
-I$(PUBLIC)\raptor \
-I..\public \
-I..\ucvja \
-I..\ucvlatin \
-I$(PUBLIC)\uconv \
$(NULL)
LLIBS= \
$(DIST)\lib\xpcom32.lib \
$(DIST)\lib\raptorbase.lib \
$(DIST)\lib\uconv.lib \
-SUBSYSTEM:CONSOLE
include <$(DEPTH)\config\rules.mak>

Просмотреть файл

@ -23,31 +23,55 @@
#include "nsRepository.h"
#include "nsISupports.h"
#include "nsICharsetConverterManager.h"
#include "nsConverterCID.h"
#include "nsUCVJACID.h"
// include the CIDs
#include "nsUCvLatinCID.h"
#include "nsUCVJACID.h"
#include "nsUCVJA2CID.h"
#ifdef XP_UNIX
#define UCONV_DLL "libuconv.so"
#define UCVLATIN_DLL "libucvlatin.so"
#define UCVJA_DLL "libucvja.so"
#define UCVJA2_DLL "libucvja2.so"
#else /* XP_UNIX */
#ifdef XP_MAC
#define UCONV_DLL "UCONV_DLL"
#define UCVLATIN_DLL "UCVLATIN_DLL"
#define UCVJA_DLL "UCVJA_DLL"
#define UCVJA2_DLL "UCVJA2_DLL"
#else /* XP_MAC */
#define UCONV_DLL "uconv.dll"
#define UCVLATIN_DLL "ucvlatin.dll"
#define UCVJA_DLL "ucvja.dll"
#define UCVJA2_DLL "ucvja2.dll"
#endif
#endif
#define TABLE_SIZE1 5
/**
* Test program for the Unicode Converters.
*
* Error messages format inside of a test.
*
* - silent while all is OK.
*
* - "ERROR at T11.easyConversion.Convert() code=0xfffd.\n"
* - "ERROR at T11.easyConversion.ConvResLen expected=0x02 result=0x04.\n"
*
* - "Test Passed.\n" for a successful end.
*
* @created 01/Dec/1998
* @author Catalin Rotaru [CATA]
*/
//----------------------------------------------------------------------
// Global variables and macros
nsICharsetConverterManager * ccMan = NULL;
//----------------------------------------------------------------------
// Registry setup function(s)
nsresult setupRegistry()
{
nsresult res;
@ -67,30 +91,18 @@ nsresult setupRegistry()
res = nsRepository::RegisterFactory(kSJIS2UnicodeCID, UCVJA_DLL, PR_FALSE, PR_FALSE);
if (NS_FAILED(res) && (NS_ERROR_FACTORY_EXISTS != res)) return res;
return NS_OK;
}
nsresult init()
{
nsresult res;
res = setupRegistry();
if (NS_FAILED(res)) {
printf("Error setting up registry: 0x%x",res);
return res;
}
res = nsRepository::RegisterFactory(kISO2022JPToUnicodeCID, UCVJA2_DLL, PR_FALSE, PR_FALSE);
if (NS_FAILED(res) && (NS_ERROR_FACTORY_EXISTS != res)) return res;
return NS_OK;
}
nsresult done()
{
if (ccMan != NULL) NS_RELEASE(ccMan);
return NS_OK;
}
//----------------------------------------------------------------------
// Converter Manager test code
nsresult testCharsetConverterManager()
{
// XXX use the Service Manager to get the ccm
printf("\n[T1] CharsetConverterManager\n");
nsresult res = nsRepository::CreateInstance(kCharsetConverterManagerCID,
@ -136,55 +148,10 @@ nsresult testCharsetConverterManager()
return NS_OK;
}
nsresult testLatin1Decoder()
{
printf("\n[T2] Latin1ToUnicode\n");
// create converter
nsIUnicodeDecoder * dec;
nsAutoString str("iso-8859-1");
nsresult res = ccMan->GetUnicodeDecoder(&str,&dec);
if (NS_FAILED(res)) {
printf("ERROR 0x%x: Cannot instantiate.\n",res);
return res;
} else {
printf("Instantiated.\n");
}
//test converter
PRInt32 srcL = TABLE_SIZE1;
PRInt32 destL = TABLE_SIZE1;
char src [TABLE_SIZE1] = {(char)0,(char)255,(char)13,(char)127,(char)128,};
PRUnichar dest [TABLE_SIZE1];
res=dec->Convert(dest, 0, &destL, src, 0, &srcL);
if (NS_FAILED(res)) {
printf("ERROR 0x%x: Convert().\n",res);
} else {
printf("Read %d, write %d.\n",srcL,destL);
printf("Converted:");
PRBool failed = PR_FALSE;
for (int i=0;i<TABLE_SIZE1;i++) {
printf(" %d->%x", src[i], dest[i]);
if (dest[i] != ((PRUint8)src[i])) failed = PR_TRUE;
}
printf("\n");
if (failed) {
printf("Test FAILED!!!\n");
} else {
printf("Test Passed.\n");
}
}
NS_RELEASE(dec);
return NS_OK;
}
//----------------------------------------------------------------------
// Old SHIT !!! Don't copy this crap, go to the new stuff down in the file.
#define TABLE_SIZE1 5
#define ISO88597TABLE_SIZE 16
nsresult testISO88597Decoder()
@ -456,26 +423,283 @@ nsresult testAsciiEncoder()
}
//----------------------------------------------------------------------
// Helper functions and macros for testing decoders and encoders
#define CREATE_DECODER(_charset) \
nsIUnicodeDecoder * dec; \
nsAutoString str(_charset); \
nsresult res = ccMan->GetUnicodeDecoder(&str,&dec); \
if (NS_FAILED(res)) { \
printf("ERROR 0x%x: Cannot instantiate.\n",res); \
return res; \
}
#define ARRAY_SIZE(_array) \
(sizeof(_array) / sizeof(_array[0]))
/**
* Decoder test.
*
* This method will test the conversion only.
*/
nsresult testDecoder(nsIUnicodeDecoder * aDec,
const char * aSrc, PRInt32 aSrcLength,
const PRUnichar * aRes, PRInt32 aResLength,
const char * aTestName)
{
nsresult res;
// prepare for conversion
PRInt32 srcLen = aSrcLength;
PRUnichar dest[1024];
PRInt32 destLen = 1024;
// conversion
res = aDec->Convert(dest, 0, &destLen, aSrc, 0, &srcLen);
// we want perfect result here - the test data should be complete!
if (res != NS_OK) {
printf("ERROR at %s.easy.Convert() code=0x%x.\n",aTestName,res);
return NS_ERROR_UNEXPECTED;
}
// compare results
if (aResLength != destLen) {
printf("ERROR at %s.easy.ConvResLen expected=0x%x result=0x%x.\n",
aTestName, aResLength, destLen);
return NS_ERROR_UNEXPECTED;
}
for (PRInt32 i=0; i<aResLength; i++) if (aRes[i] != dest[i]) {
printf("ERROR at %s.easy.ConvResChar[%d] expected=0x%x result=0x%x.\n",
aTestName, i, aRes[i], dest[i]);
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
}
/**
* Decoder test.
*
* This method will test a given converter under a given set of data and some
* very stressful conditions.
*/
nsresult testStressDecoder(nsIUnicodeDecoder * aDec,
const char * aSrc, PRInt32 aSrcLength,
const PRUnichar * aRes, PRInt32 aResLength,
const char * aTestName)
{
nsresult res;
// get estimated length
PRInt32 estimatedLength;
res = aDec->Length(aSrc, 0, aSrcLength, &estimatedLength);
if (NS_FAILED(res)) {
printf("ERROR at %s.stress.Length() code=0x%x.\n",aTestName,res);
return res;
}
PRBool exactLength = (res == NS_EXACT_LENGTH);
// prepare for conversion
PRInt32 srcLen = 0;
PRInt32 srcOff = 0;
PRUnichar dest[1024];
PRInt32 destLen = 0;
PRInt32 destOff = 0;
// controlled conversion
for (;srcOff < aSrcLength;) {
res = aDec->Convert(dest, destOff, &destLen, aSrc, srcOff, &srcLen);
if (NS_FAILED(res)) {
printf("ERROR at %s.stress.Convert() code=0x%x.\n",aTestName,res);
return res;
}
srcOff+=srcLen;
destOff+=destLen;
// give a little input each time; it'll be consumed if enough output space
srcLen = 1;
// give output space only when requested: sadic!
if (res == NS_PARTIAL_MORE_OUTPUT) {
destLen = 1;
} else {
destLen = 0;
}
}
// we want perfect result here - the test data should be complete!
if (res != NS_OK) {
printf("ERROR at %s.stress.postConvert() code=0x%x.\n",aTestName,res);
return NS_ERROR_UNEXPECTED;
}
// compare lengths
if (exactLength) {
if (destOff != estimatedLength) {
printf("ERROR at %s.stress.EstimatedLen expected=0x%x result=0x%x.\n",
aTestName, estimatedLength, destOff);
return NS_ERROR_UNEXPECTED;
}
} else {
if (destOff > estimatedLength) {
printf("ERROR at %s.stress.EstimatedLen expected<=0x%x result=0x%x.\n",
aTestName, estimatedLength, destOff);
return NS_ERROR_UNEXPECTED;
}
}
// compare results
if (aResLength != destOff) {
printf("ERROR at %s.stress.ConvResLen expected=0x%x result=0x%x.\n",
aTestName, aResLength, destOff);
return NS_ERROR_UNEXPECTED;
}
for (PRInt32 i=0; i<aResLength; i++) if (aRes[i] != dest[i]) {
printf("ERROR at %s.stress.ConvResChar[%d] expected=0x%x result=0x%x.\n",
aTestName, i, aRes[i], dest[i]);
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
}
/**
* Reset decoder.
*/
nsresult resetDecoder(nsIUnicodeDecoder * aDec, const char * aTestName)
{
nsresult res = aDec->Reset();
if (NS_FAILED(res)) {
printf("ERROR at %s.reset.Reset() code=0x%x.\n",aTestName,res);
return res;
}
return res;
}
//----------------------------------------------------------------------
// Decoders testing functions
/**
* Test the Latin1 decoder.
*/
nsresult testLatin1Decoder()
{
char * testName = "T2";
printf("\n[%s] Latin1ToUnicode\n", testName);
// create converter
CREATE_DECODER("iso-8859-1");
// test data
char src[] = {"\x00\x0d\x7f\x80\xff"};
PRUnichar exp[] = {0x0000,0x000d,0x007F,0x0080,0x00FF};
// test converter - easy test
res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
// reset converter
if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
// test converter - stress test
if (NS_SUCCEEDED(res))
res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
// release converter
NS_RELEASE(dec);
if (NS_FAILED(res)) {
return res;
} else {
printf("Test Passed.\n");
return NS_OK;
}
}
/**
* Test the ISO2022JP decoder.
*/
nsresult testISO2022JPDecoder()
{
char * testName = "T4";
printf("\n[%s] ISO2022JPToUnicode\n", testName);
// create converter
CREATE_DECODER("iso-2022-jp");
// test data
// char src[] = {"\x00\x0d\x7f" "\x1b$B\x12\x34" "\x1b(J\xaa" "\x1b$@\xaa\xbb" "\x1b(J\x1b(B\xcc"};
// PRUnichar exp[] = {0x0000,0x000d,0x007f,0x1234,0x00aa,0xaabb,0x00cc};
char src[] = {"\x0d\x7f\xdd" "\x1b(J\xaa\xdc\x41" "\x1b$B\x21\x21" "\x1b$@\x32\x37" "\x1b(J\x1b(B\xcc"};
PRUnichar exp[] = {0x000d,0x007f,0xfffd, 0xff6a,0xFF9C,0x0041, 0x3000, 0x5378, 0xfffd};
// test converter - normal operation
res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
// reset converter
if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName);
// test converter - stress test
if (NS_SUCCEEDED(res))
res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
// release converter
NS_RELEASE(dec);
if (NS_FAILED(res)) {
return res;
} else {
printf("Test Passed.\n");
return NS_OK;
}
}
//----------------------------------------------------------------------
// Encoders testing functions
//----------------------------------------------------------------------
// Main program functions
nsresult run()
{
nsresult res;
// test the manager(s)
res = testCharsetConverterManager();
if (NS_FAILED(res)) return res;
res = testLatin1Decoder();
if (NS_FAILED(res)) return res;
// test decoders
testLatin1Decoder();
testSJISDecoder();
testISO88597Decoder();
testISO2022JPDecoder();
res = testSJISDecoder();
if (NS_FAILED(res)) return res;
// test encoders
testAsciiEncoder();
res = testISO88597Decoder();
if (NS_FAILED(res)) return res;
// return
return NS_OK;
}
res = testAsciiEncoder();
if (NS_FAILED(res)) return res;
nsresult init()
{
nsresult res;
res = setupRegistry();
if (NS_FAILED(res)) {
printf("ERROR 0x%x: setting up registry.\n",res);
return res;
}
return NS_OK;
}
nsresult done()
{
if (ccMan != NULL) NS_RELEASE(ccMan);
return NS_OK;
}