2 **********************************************************************
3 * Copyright (c) 2002-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 **********************************************************************
12 #include "unicode/ucnv.h"
13 #include "unicode/uclean.h"
14 #include "unicode/ustring.h"
18 #define CONVERSION_FLAGS (0) /*WC_DEFAULTCHAR WC_COMPOSITECHECK & WC_SEPCHARS*/
19 #define MAX_BUF_SIZE 3048
20 #define LENGTHOF(array) (sizeof(array)/sizeof((array)[0]))
22 class ICUToUnicodePerfFunction
: public UPerfFunction
{
31 ICUToUnicodePerfFunction(const char* name
, const char* source
, int32_t sourceLen
, UErrorCode
& status
){
32 conv
= ucnv_open(name
,&status
);
35 if(U_FAILURE(status
)){
41 int32_t reqdLen
= ucnv_toUChars(conv
, target
, 0,
42 source
, srcLen
, &status
);
43 if(status
==U_BUFFER_OVERFLOW_ERROR
) {
45 target
=(UChar
*)uprv_malloc((reqdLen
) * U_SIZEOF_UCHAR
*2);
46 targetLimit
= target
+ reqdLen
;
48 status
= U_MEMORY_ALLOCATION_ERROR
;
53 virtual void call(UErrorCode
* status
){
54 const char* mySrc
= src
;
55 const char* sourceLimit
= src
+ srcLen
;
56 UChar
* myTarget
= target
;
57 ucnv_toUnicode(conv
, &myTarget
, targetLimit
, &mySrc
, sourceLimit
, NULL
, TRUE
, status
);
59 virtual long getOperationsPerIteration(void){
62 ~ICUToUnicodePerfFunction(){
67 class ICUFromUnicodePerfFunction
: public UPerfFunction
{
77 ICUFromUnicodePerfFunction(const char* name
, const UChar
* source
, int32_t sourceLen
, UErrorCode
& status
){
78 conv
= ucnv_open(name
,&status
);
81 if(U_FAILURE(status
)){
87 int32_t reqdLen
= ucnv_fromUChars(conv
, target
, 0,
88 source
, srcLen
, &status
);
89 if(status
==U_BUFFER_OVERFLOW_ERROR
) {
91 target
=(char*)uprv_malloc((reqdLen
*2));
92 targetLimit
= target
+ reqdLen
;
94 status
= U_MEMORY_ALLOCATION_ERROR
;
99 virtual void call(UErrorCode
* status
){
100 const UChar
* mySrc
= src
;
101 const UChar
* sourceLimit
= src
+ srcLen
;
102 char* myTarget
= target
;
103 ucnv_fromUnicode(conv
,&myTarget
, targetLimit
, &mySrc
, sourceLimit
, NULL
, TRUE
, status
);
105 virtual long getOperationsPerIteration(void){
108 ~ICUFromUnicodePerfFunction(){
114 class ICUOpenAllConvertersFunction
: public UPerfFunction
{
117 int32_t availableConverters
;
118 const char **convNames
;
120 ICUOpenAllConvertersFunction(UBool callCleanup
, UErrorCode
& status
){
122 cleanup
= callCleanup
;
123 availableConverters
= ucnv_countAvailable();
124 convNames
= new const char *[availableConverters
];
125 for (idx
= 0; idx
< availableConverters
; idx
++) {
126 convNames
[idx
] = ucnv_getAvailableName(idx
);
129 virtual void call(UErrorCode
* status
){
134 for (idx
= 0; idx
< availableConverters
; idx
++) {
135 ucnv_close(ucnv_open(convNames
[idx
], status
));
138 virtual long getOperationsPerIteration(void){
139 return availableConverters
;
141 ~ICUOpenAllConvertersFunction(){
146 class WinANSIToUnicodePerfFunction
: public UPerfFunction
{
152 WCHAR dest
[MAX_BUF_SIZE
];
156 WinANSIToUnicodePerfFunction(const char* cpName
, char* pszIn
,UINT szLen
, UErrorCode
& status
){
160 dstLen
= LENGTHOF(dest
);
161 unsigned short bEnc
[30]={'\0'};
162 const char* tenc
=name
;
163 for(int i
=0;*tenc
!='\0';i
++){
167 LPMULTILANGUAGE2 pMulti
;
171 /* create instance of converter object*/
173 __uuidof(CMultiLanguage
),
176 __uuidof(IMultiLanguage2
),
182 MIMECSETINFO mimeInfo
;
184 mimeInfo
.uiCodePage
= 0;
185 mimeInfo
.uiInternetEncoding
=0;
186 /* get the charset info */
187 pMulti
->GetCharsetInfo(bEnc
,&mimeInfo
);
188 uiCodePage
= (mimeInfo
.uiInternetEncoding
==0)?mimeInfo
.uiCodePage
:mimeInfo
.uiInternetEncoding
;
190 virtual void call(UErrorCode
* status
){
191 int winSize
=MultiByteToWideChar(uiCodePage
,CONVERSION_FLAGS
,src
,srcLen
,dest
,dstLen
);
193 virtual long getOperationsPerIteration(void){
198 class WinANSIFromUnicodePerfFunction
: public UPerfFunction
{
204 char dest
[MAX_BUF_SIZE
];
207 BOOL lpUsedDefaultChar
;
210 WinANSIFromUnicodePerfFunction(const char* cpName
, WCHAR
* pszIn
,UINT szLen
, UErrorCode
& status
){
214 dstLen
= LENGTHOF(dest
);
215 lpUsedDefaultChar
=FALSE
;
216 unsigned short bEnc
[30]={'\0'};
217 const char* tenc
=name
;
218 for(int i
=0;*tenc
!='\0';i
++){
222 LPMULTILANGUAGE2 pMulti
;
226 /* create instance of converter object*/
228 __uuidof(CMultiLanguage
),
231 __uuidof(IMultiLanguage2
),
237 MIMECSETINFO mimeInfo
;
238 mimeInfo
.uiCodePage
= 0;
239 mimeInfo
.uiInternetEncoding
=0;
240 /* get the charset info */
241 pMulti
->GetCharsetInfo(bEnc
,&mimeInfo
);
242 uiCodePage
= (mimeInfo
.uiInternetEncoding
==0)?mimeInfo
.uiCodePage
:mimeInfo
.uiInternetEncoding
;
244 virtual void call(UErrorCode
* status
){
245 BOOL
* pUsedDefaultChar
=(uiCodePage
==CP_UTF8
)?NULL
:&lpUsedDefaultChar
;
246 int winSize
= WideCharToMultiByte(uiCodePage
,CONVERSION_FLAGS
,src
,srcLen
,dest
,dstLen
,NULL
, pUsedDefaultChar
);
248 virtual long getOperationsPerIteration(void){
252 static inline void getErr(HRESULT err
, UErrorCode
& status
){
257 //printf("Operation %s successful\n",operation);
260 status
= U_INTERNAL_PROGRAM_ERROR
;
263 status
= U_ILLEGAL_CHAR_FOUND
;
266 class WinIMultiLanguageToUnicodePerfFunction
: public UPerfFunction
{
269 LPMULTILANGUAGE2 pMulti
;
270 LPMLANGCONVERTCHARSET pConvToUni
;
273 WCHAR dst
[MAX_BUF_SIZE
];
278 WinIMultiLanguageToUnicodePerfFunction(const char* name
,char* source
, UINT sourceLen
, UErrorCode
& status
){
282 /* create instance of converter object*/
284 __uuidof(CMultiLanguage
),
287 __uuidof(IMultiLanguage2
),
293 MIMECSETINFO mimeInfo
;
294 mimeInfo
.uiCodePage
= 0;
295 mimeInfo
.uiInternetEncoding
=0;
297 unsigned short bEnc
[30]={'\0'};
298 const char* tenc
=name
;
299 for(int i
=0;*tenc
!='\0';i
++){
303 /* get the charset info */
304 pMulti
->GetCharsetInfo(bEnc
,&mimeInfo
);
305 pMulti
->CreateConvertCharset(mimeInfo
.uiCodePage
, 1200 /*unicode*/, (DWORD
)0,&pConvToUni
);
309 dstLen
= LENGTHOF(dst
);
313 virtual void call(UErrorCode
* status
){
314 HRESULT err
= pConvToUni
->DoConversionToUnicode(src
,&srcLen
,dst
, &dstLen
);
317 virtual long getOperationsPerIteration(void){
322 class WinIMultiLanguageFromUnicodePerfFunction
: public UPerfFunction
{
325 LPMULTILANGUAGE2 pMulti
;
326 LPMLANGCONVERTCHARSET pConvFromUni
;
329 char dst
[MAX_BUF_SIZE
];
334 WinIMultiLanguageFromUnicodePerfFunction(const char* name
,WCHAR
* source
, UINT sourceLen
, UErrorCode
& status
){
338 /* create instance of converter object*/
340 __uuidof(CMultiLanguage
),
343 __uuidof(IMultiLanguage2
),
349 MIMECSETINFO mimeInfo
;
350 mimeInfo
.uiCodePage
= 0;
351 mimeInfo
.uiInternetEncoding
=0;
353 unsigned short bEnc
[30]={'\0'};
354 const char* tenc
=name
;
355 for(int i
=0;*tenc
!='\0';i
++){
359 /* get the charset info */
360 pMulti
->GetCharsetInfo(bEnc
,&mimeInfo
);
361 pMulti
->CreateConvertCharset(1200 /*unicode*/, mimeInfo
.uiCodePage
, (DWORD
)0,&pConvFromUni
);
365 dstLen
= LENGTHOF(dst
);
370 virtual void call(UErrorCode
* status
){
371 HRESULT err
= pConvFromUni
->DoConversionFromUnicode(src
,&srcLen
,dst
, &dstLen
);
374 virtual long getOperationsPerIteration(void){
379 class WinIMultiLanguage2ToUnicodePerfFunction
: public UPerfFunction
{
382 LPMULTILANGUAGE2 pMulti
;
385 WCHAR dst
[MAX_BUF_SIZE
];
390 WinIMultiLanguage2ToUnicodePerfFunction(const char* name
,char* source
, UINT sourceLen
, UErrorCode
& status
){
394 /* create instance of converter object*/
396 __uuidof(CMultiLanguage
),
399 __uuidof(IMultiLanguage2
),
405 dstLen
= LENGTHOF(dst
);
407 unsigned short bEnc
[30]={'\0'};
408 const char* tenc
=name
;
409 for(int i
=0;*tenc
!='\0';i
++){
413 /* get the charset info */
414 MIMECSETINFO mimeInfo
;
415 mimeInfo
.uiCodePage
= 0;
416 mimeInfo
.uiInternetEncoding
=0;
417 pMulti
->GetCharsetInfo(bEnc
,&mimeInfo
);
418 dwEnc
= (mimeInfo
.uiInternetEncoding
==0)?mimeInfo
.uiCodePage
:mimeInfo
.uiInternetEncoding
;
421 virtual void call(UErrorCode
* status
){
423 HRESULT err
= pMulti
->ConvertStringToUnicode(&dwMode
,dwEnc
,(char*)src
,&srcLen
,dst
, &dstLen
);
426 virtual long getOperationsPerIteration(void){
431 class WinIMultiLanguage2FromUnicodePerfFunction
: public UPerfFunction
{
434 LPMULTILANGUAGE2 pMulti
;
435 LPMLANGCONVERTCHARSET pConvFromUni
;
438 char dst
[MAX_BUF_SIZE
];
444 WinIMultiLanguage2FromUnicodePerfFunction(const char* name
,WCHAR
* source
, UINT sourceLen
, UErrorCode
& status
){
448 /* create instance of converter object*/
450 __uuidof(CMultiLanguage
),
453 __uuidof(IMultiLanguage2
),
458 unsigned short bEnc
[30]={'\0'};
459 const char* tenc
=name
;
460 for(int i
=0;*tenc
!='\0';i
++){
466 dstLen
= LENGTHOF(dst
);
468 /* get the charset info */
469 MIMECSETINFO mimeInfo
;
470 mimeInfo
.uiCodePage
= 0;
471 mimeInfo
.uiInternetEncoding
=0;
473 pMulti
->GetCharsetInfo(bEnc
,&mimeInfo
);
474 dwEnc
= (mimeInfo
.uiInternetEncoding
==0)?mimeInfo
.uiCodePage
:mimeInfo
.uiInternetEncoding
;
477 virtual void call(UErrorCode
* status
){
479 HRESULT err
= pMulti
->ConvertStringFromUnicode(&dwMode
,dwEnc
,src
,&srcLen
,dst
, &dstLen
);
482 virtual long getOperationsPerIteration(void){
487 class ConverterPerformanceTest
: public UPerfTest
{
491 ConverterPerformanceTest(int32_t argc
, const char* argv
[], UErrorCode
& status
);
492 ~ConverterPerformanceTest();
493 virtual UPerfFunction
* runIndexedTest(int32_t index
, UBool exec
,const char* &name
, char* par
= NULL
);
495 UPerfFunction
* TestICU_CleanOpenAllConverters();
496 UPerfFunction
* TestICU_OpenAllConverters();
498 UPerfFunction
* TestICU_UTF8_ToUnicode();
499 UPerfFunction
* TestICU_UTF8_FromUnicode();
500 UPerfFunction
* TestWinANSI_UTF8_ToUnicode();
501 UPerfFunction
* TestWinANSI_UTF8_FromUnicode();
502 UPerfFunction
* TestWinIML2_UTF8_ToUnicode();
503 UPerfFunction
* TestWinIML2_UTF8_FromUnicode();
505 UPerfFunction
* TestICU_Latin1_ToUnicode();
506 UPerfFunction
* TestICU_Latin1_FromUnicode();
507 UPerfFunction
* TestWinANSI_Latin1_ToUnicode();
508 UPerfFunction
* TestWinANSI_Latin1_FromUnicode();
509 UPerfFunction
* TestWinIML2_Latin1_ToUnicode();
510 UPerfFunction
* TestWinIML2_Latin1_FromUnicode();
512 UPerfFunction
* TestICU_EBCDIC_Arabic_ToUnicode();
513 UPerfFunction
* TestICU_EBCDIC_Arabic_FromUnicode();
514 UPerfFunction
* TestWinANSI_EBCDIC_Arabic_ToUnicode();
515 UPerfFunction
* TestWinANSI_EBCDIC_Arabic_FromUnicode();
516 UPerfFunction
* TestWinIML2_EBCDIC_Arabic_ToUnicode();
517 UPerfFunction
* TestWinIML2_EBCDIC_Arabic_FromUnicode();
519 UPerfFunction
* TestICU_Latin8_ToUnicode();
520 UPerfFunction
* TestICU_Latin8_FromUnicode();
521 UPerfFunction
* TestWinANSI_Latin8_ToUnicode();
522 UPerfFunction
* TestWinANSI_Latin8_FromUnicode();
523 UPerfFunction
* TestWinIML2_Latin8_ToUnicode();
524 UPerfFunction
* TestWinIML2_Latin8_FromUnicode();
527 UPerfFunction
* TestICU_SJIS_ToUnicode();
528 UPerfFunction
* TestICU_SJIS_FromUnicode();
529 UPerfFunction
* TestWinANSI_SJIS_ToUnicode();
530 UPerfFunction
* TestWinANSI_SJIS_FromUnicode();
531 UPerfFunction
* TestWinIML2_SJIS_ToUnicode();
532 UPerfFunction
* TestWinIML2_SJIS_FromUnicode();
534 UPerfFunction
* TestICU_EUCJP_ToUnicode();
535 UPerfFunction
* TestICU_EUCJP_FromUnicode();
536 UPerfFunction
* TestWinANSI_EUCJP_ToUnicode();
537 UPerfFunction
* TestWinANSI_EUCJP_FromUnicode();
538 UPerfFunction
* TestWinIML2_EUCJP_ToUnicode();
539 UPerfFunction
* TestWinIML2_EUCJP_FromUnicode();
541 UPerfFunction
* TestICU_GB2312_ToUnicode();
542 UPerfFunction
* TestICU_GB2312_FromUnicode();
543 UPerfFunction
* TestWinANSI_GB2312_ToUnicode();
544 UPerfFunction
* TestWinANSI_GB2312_FromUnicode();
545 UPerfFunction
* TestWinIML2_GB2312_ToUnicode();
546 UPerfFunction
* TestWinIML2_GB2312_FromUnicode();
549 UPerfFunction
* TestICU_ISO2022KR_ToUnicode();
550 UPerfFunction
* TestICU_ISO2022KR_FromUnicode();
551 UPerfFunction
* TestWinANSI_ISO2022KR_ToUnicode();
552 UPerfFunction
* TestWinANSI_ISO2022KR_FromUnicode();
553 UPerfFunction
* TestWinIML2_ISO2022KR_ToUnicode();
554 UPerfFunction
* TestWinIML2_ISO2022KR_FromUnicode();
556 UPerfFunction
* TestICU_ISO2022JP_ToUnicode();
557 UPerfFunction
* TestICU_ISO2022JP_FromUnicode();
558 UPerfFunction
* TestWinANSI_ISO2022JP_ToUnicode();
559 UPerfFunction
* TestWinANSI_ISO2022JP_FromUnicode();
560 UPerfFunction
* TestWinIML2_ISO2022JP_ToUnicode();
561 UPerfFunction
* TestWinIML2_ISO2022JP_FromUnicode();