2 ***********************************************************************
3 * © 2016 and later: Unicode, Inc. and others.
4 * License & terms of use: http://www.unicode.org/copyright.html#License
5 ***********************************************************************
6 ***********************************************************************
7 * Copyright (c) 2002-2014, International Business Machines
8 * Corporation and others. All Rights Reserved.
9 ***********************************************************************
10 ***********************************************************************
18 #include "unicode/ucnv.h"
19 #include "unicode/uclean.h"
20 #include "unicode/ustring.h"
21 #include "cmemory.h" // for UPRV_LENGTHOF
23 #include "unicode/uperf.h"
25 #define CONVERSION_FLAGS (0) /*WC_DEFAULTCHAR WC_COMPOSITECHECK & WC_SEPCHARS*/
26 #define MAX_BUF_SIZE 3048
28 class ICUToUnicodePerfFunction
: public UPerfFunction
{
37 ICUToUnicodePerfFunction(const char* name
, const char* source
, int32_t sourceLen
, UErrorCode
& status
){
38 conv
= ucnv_open(name
,&status
);
41 if(U_FAILURE(status
)){
47 int32_t reqdLen
= ucnv_toUChars(conv
, target
, 0,
48 source
, srcLen
, &status
);
49 if(status
==U_BUFFER_OVERFLOW_ERROR
) {
51 target
=(UChar
*)malloc((reqdLen
) * U_SIZEOF_UCHAR
*2);
52 targetLimit
= target
+ reqdLen
;
54 status
= U_MEMORY_ALLOCATION_ERROR
;
59 virtual void call(UErrorCode
* status
){
60 const char* mySrc
= src
;
61 const char* sourceLimit
= src
+ srcLen
;
62 UChar
* myTarget
= target
;
63 ucnv_toUnicode(conv
, &myTarget
, targetLimit
, &mySrc
, sourceLimit
, NULL
, TRUE
, status
);
65 virtual long getOperationsPerIteration(void){
68 ~ICUToUnicodePerfFunction(){
73 class ICUFromUnicodePerfFunction
: public UPerfFunction
{
83 ICUFromUnicodePerfFunction(const char* name
, const UChar
* source
, int32_t sourceLen
, UErrorCode
& status
){
84 conv
= ucnv_open(name
,&status
);
87 if(U_FAILURE(status
)){
93 int32_t reqdLen
= ucnv_fromUChars(conv
, target
, 0,
94 source
, srcLen
, &status
);
95 if(status
==U_BUFFER_OVERFLOW_ERROR
) {
97 target
=(char*)malloc((reqdLen
*2));
98 targetLimit
= target
+ reqdLen
;
100 status
= U_MEMORY_ALLOCATION_ERROR
;
105 virtual void call(UErrorCode
* status
){
106 const UChar
* mySrc
= src
;
107 const UChar
* sourceLimit
= src
+ srcLen
;
108 char* myTarget
= target
;
109 ucnv_fromUnicode(conv
,&myTarget
, targetLimit
, &mySrc
, sourceLimit
, NULL
, TRUE
, status
);
111 virtual long getOperationsPerIteration(void){
114 ~ICUFromUnicodePerfFunction(){
120 class ICUOpenAllConvertersFunction
: public UPerfFunction
{
123 int32_t availableConverters
;
124 const char **convNames
;
126 ICUOpenAllConvertersFunction(UBool callCleanup
, UErrorCode
& status
){
128 cleanup
= callCleanup
;
129 availableConverters
= ucnv_countAvailable();
130 convNames
= new const char *[availableConverters
];
131 for (idx
= 0; idx
< availableConverters
; idx
++) {
132 convNames
[idx
] = ucnv_getAvailableName(idx
);
135 virtual void call(UErrorCode
* status
){
140 for (idx
= 0; idx
< availableConverters
; idx
++) {
141 ucnv_close(ucnv_open(convNames
[idx
], status
));
144 virtual long getOperationsPerIteration(void){
145 return availableConverters
;
147 ~ICUOpenAllConvertersFunction(){
152 class WinANSIToUnicodePerfFunction
: public UPerfFunction
{
158 WCHAR dest
[MAX_BUF_SIZE
];
162 WinANSIToUnicodePerfFunction(const char* cpName
, char* pszIn
,UINT szLen
, UErrorCode
& status
){
166 dstLen
= UPRV_LENGTHOF(dest
);
167 unsigned short bEnc
[30]={'\0'};
168 const char* tenc
=name
;
169 for(int i
=0;*tenc
!='\0';i
++){
173 LPMULTILANGUAGE2 pMulti
;
177 /* create instance of converter object*/
179 __uuidof(CMultiLanguage
),
182 __uuidof(IMultiLanguage2
),
188 MIMECSETINFO mimeInfo
;
190 mimeInfo
.uiCodePage
= 0;
191 mimeInfo
.uiInternetEncoding
=0;
192 /* get the charset info */
193 pMulti
->GetCharsetInfo((wchar_t *)bEnc
,&mimeInfo
);
194 uiCodePage
= (mimeInfo
.uiInternetEncoding
==0)?mimeInfo
.uiCodePage
:mimeInfo
.uiInternetEncoding
;
196 virtual void call(UErrorCode
* status
){
197 int winSize
=MultiByteToWideChar(uiCodePage
,CONVERSION_FLAGS
,src
,srcLen
,dest
,dstLen
);
199 virtual long getOperationsPerIteration(void){
204 class WinANSIFromUnicodePerfFunction
: public UPerfFunction
{
210 char dest
[MAX_BUF_SIZE
];
213 BOOL lpUsedDefaultChar
;
216 WinANSIFromUnicodePerfFunction(const char* cpName
, WCHAR
* pszIn
,UINT szLen
, UErrorCode
& status
){
220 dstLen
= UPRV_LENGTHOF(dest
);
221 lpUsedDefaultChar
=FALSE
;
222 unsigned short bEnc
[30]={'\0'};
223 const char* tenc
=name
;
224 for(int i
=0;*tenc
!='\0';i
++){
228 LPMULTILANGUAGE2 pMulti
;
232 /* create instance of converter object*/
234 __uuidof(CMultiLanguage
),
237 __uuidof(IMultiLanguage2
),
243 MIMECSETINFO mimeInfo
;
244 mimeInfo
.uiCodePage
= 0;
245 mimeInfo
.uiInternetEncoding
=0;
246 /* get the charset info */
247 pMulti
->GetCharsetInfo((wchar_t *)bEnc
,&mimeInfo
);
248 uiCodePage
= (mimeInfo
.uiInternetEncoding
==0)?mimeInfo
.uiCodePage
:mimeInfo
.uiInternetEncoding
;
250 virtual void call(UErrorCode
* status
){
251 BOOL
* pUsedDefaultChar
=(uiCodePage
==CP_UTF8
)?NULL
:&lpUsedDefaultChar
;
252 int winSize
= WideCharToMultiByte(uiCodePage
,CONVERSION_FLAGS
,src
,srcLen
,dest
,dstLen
,NULL
, pUsedDefaultChar
);
254 virtual long getOperationsPerIteration(void){
258 static inline void getErr(HRESULT err
, UErrorCode
& status
){
263 //printf("Operation %s successful\n",operation);
266 status
= U_INTERNAL_PROGRAM_ERROR
;
269 status
= U_ILLEGAL_CHAR_FOUND
;
272 class WinIMultiLanguageToUnicodePerfFunction
: public UPerfFunction
{
275 LPMULTILANGUAGE2 pMulti
;
276 LPMLANGCONVERTCHARSET pConvToUni
;
279 WCHAR dst
[MAX_BUF_SIZE
];
284 WinIMultiLanguageToUnicodePerfFunction(const char* name
,char* source
, UINT sourceLen
, UErrorCode
& status
){
288 /* create instance of converter object*/
290 __uuidof(CMultiLanguage
),
293 __uuidof(IMultiLanguage2
),
299 MIMECSETINFO mimeInfo
;
300 mimeInfo
.uiCodePage
= 0;
301 mimeInfo
.uiInternetEncoding
=0;
303 unsigned short bEnc
[30]={'\0'};
304 const char* tenc
=name
;
305 for(int i
=0;*tenc
!='\0';i
++){
309 /* get the charset info */
310 pMulti
->GetCharsetInfo((wchar_t *)bEnc
,&mimeInfo
);
311 pMulti
->CreateConvertCharset(mimeInfo
.uiCodePage
, 1200 /*unicode*/, (DWORD
)0,&pConvToUni
);
315 dstLen
= UPRV_LENGTHOF(dst
);
319 virtual void call(UErrorCode
* status
){
320 HRESULT err
= pConvToUni
->DoConversionToUnicode(src
,&srcLen
,dst
, &dstLen
);
323 virtual long getOperationsPerIteration(void){
328 class WinIMultiLanguageFromUnicodePerfFunction
: public UPerfFunction
{
331 LPMULTILANGUAGE2 pMulti
;
332 LPMLANGCONVERTCHARSET pConvFromUni
;
335 char dst
[MAX_BUF_SIZE
];
340 WinIMultiLanguageFromUnicodePerfFunction(const char* name
,WCHAR
* source
, UINT sourceLen
, UErrorCode
& status
){
344 /* create instance of converter object*/
346 __uuidof(CMultiLanguage
),
349 __uuidof(IMultiLanguage2
),
355 MIMECSETINFO mimeInfo
;
356 mimeInfo
.uiCodePage
= 0;
357 mimeInfo
.uiInternetEncoding
=0;
359 unsigned short bEnc
[30]={'\0'};
360 const char* tenc
=name
;
361 for(int i
=0;*tenc
!='\0';i
++){
365 /* get the charset info */
366 pMulti
->GetCharsetInfo((wchar_t *)bEnc
,&mimeInfo
);
367 pMulti
->CreateConvertCharset(1200 /*unicode*/, mimeInfo
.uiCodePage
, (DWORD
)0,&pConvFromUni
);
371 dstLen
= UPRV_LENGTHOF(dst
);
376 virtual void call(UErrorCode
* status
){
377 HRESULT err
= pConvFromUni
->DoConversionFromUnicode(src
,&srcLen
,dst
, &dstLen
);
380 virtual long getOperationsPerIteration(void){
385 class WinIMultiLanguage2ToUnicodePerfFunction
: public UPerfFunction
{
388 LPMULTILANGUAGE2 pMulti
;
391 WCHAR dst
[MAX_BUF_SIZE
];
396 WinIMultiLanguage2ToUnicodePerfFunction(const char* name
,char* source
, UINT sourceLen
, UErrorCode
& status
){
400 /* create instance of converter object*/
402 __uuidof(CMultiLanguage
),
405 __uuidof(IMultiLanguage2
),
411 dstLen
= UPRV_LENGTHOF(dst
);
413 unsigned short bEnc
[30]={'\0'};
414 const char* tenc
=name
;
415 for(int i
=0;*tenc
!='\0';i
++){
419 /* get the charset info */
420 MIMECSETINFO mimeInfo
;
421 mimeInfo
.uiCodePage
= 0;
422 mimeInfo
.uiInternetEncoding
=0;
423 pMulti
->GetCharsetInfo((wchar_t *)bEnc
,&mimeInfo
);
424 dwEnc
= (mimeInfo
.uiInternetEncoding
==0)?mimeInfo
.uiCodePage
:mimeInfo
.uiInternetEncoding
;
427 virtual void call(UErrorCode
* status
){
429 HRESULT err
= pMulti
->ConvertStringToUnicode(&dwMode
,dwEnc
,(char*)src
,&srcLen
,dst
, &dstLen
);
432 virtual long getOperationsPerIteration(void){
437 class WinIMultiLanguage2FromUnicodePerfFunction
: public UPerfFunction
{
440 LPMULTILANGUAGE2 pMulti
;
441 LPMLANGCONVERTCHARSET pConvFromUni
;
444 char dst
[MAX_BUF_SIZE
];
450 WinIMultiLanguage2FromUnicodePerfFunction(const char* name
,WCHAR
* source
, UINT sourceLen
, UErrorCode
& status
){
454 /* create instance of converter object*/
456 __uuidof(CMultiLanguage
),
459 __uuidof(IMultiLanguage2
),
464 unsigned short bEnc
[30]={'\0'};
465 const char* tenc
=name
;
466 for(int i
=0;*tenc
!='\0';i
++){
472 dstLen
= UPRV_LENGTHOF(dst
);
474 /* get the charset info */
475 MIMECSETINFO mimeInfo
;
476 mimeInfo
.uiCodePage
= 0;
477 mimeInfo
.uiInternetEncoding
=0;
479 pMulti
->GetCharsetInfo((wchar_t *)bEnc
,&mimeInfo
);
480 dwEnc
= (mimeInfo
.uiInternetEncoding
==0)?mimeInfo
.uiCodePage
:mimeInfo
.uiInternetEncoding
;
483 virtual void call(UErrorCode
* status
){
485 HRESULT err
= pMulti
->ConvertStringFromUnicode(&dwMode
,dwEnc
,src
,&srcLen
,dst
, &dstLen
);
488 virtual long getOperationsPerIteration(void){
493 class ConverterPerformanceTest
: public UPerfTest
{
497 ConverterPerformanceTest(int32_t argc
, const char* argv
[], UErrorCode
& status
);
498 ~ConverterPerformanceTest();
499 virtual UPerfFunction
* runIndexedTest(int32_t index
, UBool exec
,const char* &name
, char* par
= NULL
);
501 UPerfFunction
* TestICU_CleanOpenAllConverters();
502 UPerfFunction
* TestICU_OpenAllConverters();
504 UPerfFunction
* TestICU_UTF8_ToUnicode();
505 UPerfFunction
* TestICU_UTF8_FromUnicode();
506 UPerfFunction
* TestWinANSI_UTF8_ToUnicode();
507 UPerfFunction
* TestWinANSI_UTF8_FromUnicode();
508 UPerfFunction
* TestWinIML2_UTF8_ToUnicode();
509 UPerfFunction
* TestWinIML2_UTF8_FromUnicode();
511 UPerfFunction
* TestICU_Latin1_ToUnicode();
512 UPerfFunction
* TestICU_Latin1_FromUnicode();
513 UPerfFunction
* TestWinANSI_Latin1_ToUnicode();
514 UPerfFunction
* TestWinANSI_Latin1_FromUnicode();
515 UPerfFunction
* TestWinIML2_Latin1_ToUnicode();
516 UPerfFunction
* TestWinIML2_Latin1_FromUnicode();
518 UPerfFunction
* TestICU_EBCDIC_Arabic_ToUnicode();
519 UPerfFunction
* TestICU_EBCDIC_Arabic_FromUnicode();
520 UPerfFunction
* TestWinANSI_EBCDIC_Arabic_ToUnicode();
521 UPerfFunction
* TestWinANSI_EBCDIC_Arabic_FromUnicode();
522 UPerfFunction
* TestWinIML2_EBCDIC_Arabic_ToUnicode();
523 UPerfFunction
* TestWinIML2_EBCDIC_Arabic_FromUnicode();
525 UPerfFunction
* TestICU_Latin8_ToUnicode();
526 UPerfFunction
* TestICU_Latin8_FromUnicode();
527 UPerfFunction
* TestWinANSI_Latin8_ToUnicode();
528 UPerfFunction
* TestWinANSI_Latin8_FromUnicode();
529 UPerfFunction
* TestWinIML2_Latin8_ToUnicode();
530 UPerfFunction
* TestWinIML2_Latin8_FromUnicode();
533 UPerfFunction
* TestICU_SJIS_ToUnicode();
534 UPerfFunction
* TestICU_SJIS_FromUnicode();
535 UPerfFunction
* TestWinANSI_SJIS_ToUnicode();
536 UPerfFunction
* TestWinANSI_SJIS_FromUnicode();
537 UPerfFunction
* TestWinIML2_SJIS_ToUnicode();
538 UPerfFunction
* TestWinIML2_SJIS_FromUnicode();
540 UPerfFunction
* TestICU_EUCJP_ToUnicode();
541 UPerfFunction
* TestICU_EUCJP_FromUnicode();
542 UPerfFunction
* TestWinANSI_EUCJP_ToUnicode();
543 UPerfFunction
* TestWinANSI_EUCJP_FromUnicode();
544 UPerfFunction
* TestWinIML2_EUCJP_ToUnicode();
545 UPerfFunction
* TestWinIML2_EUCJP_FromUnicode();
547 UPerfFunction
* TestICU_GB2312_ToUnicode();
548 UPerfFunction
* TestICU_GB2312_FromUnicode();
549 UPerfFunction
* TestWinANSI_GB2312_ToUnicode();
550 UPerfFunction
* TestWinANSI_GB2312_FromUnicode();
551 UPerfFunction
* TestWinIML2_GB2312_ToUnicode();
552 UPerfFunction
* TestWinIML2_GB2312_FromUnicode();
555 UPerfFunction
* TestICU_ISO2022KR_ToUnicode();
556 UPerfFunction
* TestICU_ISO2022KR_FromUnicode();
557 UPerfFunction
* TestWinANSI_ISO2022KR_ToUnicode();
558 UPerfFunction
* TestWinANSI_ISO2022KR_FromUnicode();
559 UPerfFunction
* TestWinIML2_ISO2022KR_ToUnicode();
560 UPerfFunction
* TestWinIML2_ISO2022KR_FromUnicode();
562 UPerfFunction
* TestICU_ISO2022JP_ToUnicode();
563 UPerfFunction
* TestICU_ISO2022JP_FromUnicode();
564 UPerfFunction
* TestWinANSI_ISO2022JP_ToUnicode();
565 UPerfFunction
* TestWinANSI_ISO2022JP_FromUnicode();
566 UPerfFunction
* TestWinIML2_ISO2022JP_ToUnicode();
567 UPerfFunction
* TestWinIML2_ISO2022JP_FromUnicode();