2 **********************************************************************
3 * Copyright (c) 2002-2014, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 **********************************************************************
14 #include "unicode/ucnv.h"
15 #include "unicode/uclean.h"
16 #include "unicode/ustring.h"
17 #include "cmemory.h" // for UPRV_LENGTHOF
19 #include "unicode/uperf.h"
21 #define CONVERSION_FLAGS (0) /*WC_DEFAULTCHAR WC_COMPOSITECHECK & WC_SEPCHARS*/
22 #define MAX_BUF_SIZE 3048
24 class ICUToUnicodePerfFunction
: public UPerfFunction
{
33 ICUToUnicodePerfFunction(const char* name
, const char* source
, int32_t sourceLen
, UErrorCode
& status
){
34 conv
= ucnv_open(name
,&status
);
37 if(U_FAILURE(status
)){
43 int32_t reqdLen
= ucnv_toUChars(conv
, target
, 0,
44 source
, srcLen
, &status
);
45 if(status
==U_BUFFER_OVERFLOW_ERROR
) {
47 target
=(UChar
*)malloc((reqdLen
) * U_SIZEOF_UCHAR
*2);
48 targetLimit
= target
+ reqdLen
;
50 status
= U_MEMORY_ALLOCATION_ERROR
;
55 virtual void call(UErrorCode
* status
){
56 const char* mySrc
= src
;
57 const char* sourceLimit
= src
+ srcLen
;
58 UChar
* myTarget
= target
;
59 ucnv_toUnicode(conv
, &myTarget
, targetLimit
, &mySrc
, sourceLimit
, NULL
, TRUE
, status
);
61 virtual long getOperationsPerIteration(void){
64 ~ICUToUnicodePerfFunction(){
69 class ICUFromUnicodePerfFunction
: public UPerfFunction
{
79 ICUFromUnicodePerfFunction(const char* name
, const UChar
* source
, int32_t sourceLen
, UErrorCode
& status
){
80 conv
= ucnv_open(name
,&status
);
83 if(U_FAILURE(status
)){
89 int32_t reqdLen
= ucnv_fromUChars(conv
, target
, 0,
90 source
, srcLen
, &status
);
91 if(status
==U_BUFFER_OVERFLOW_ERROR
) {
93 target
=(char*)malloc((reqdLen
*2));
94 targetLimit
= target
+ reqdLen
;
96 status
= U_MEMORY_ALLOCATION_ERROR
;
101 virtual void call(UErrorCode
* status
){
102 const UChar
* mySrc
= src
;
103 const UChar
* sourceLimit
= src
+ srcLen
;
104 char* myTarget
= target
;
105 ucnv_fromUnicode(conv
,&myTarget
, targetLimit
, &mySrc
, sourceLimit
, NULL
, TRUE
, status
);
107 virtual long getOperationsPerIteration(void){
110 ~ICUFromUnicodePerfFunction(){
116 class ICUOpenAllConvertersFunction
: public UPerfFunction
{
119 int32_t availableConverters
;
120 const char **convNames
;
122 ICUOpenAllConvertersFunction(UBool callCleanup
, UErrorCode
& status
){
124 cleanup
= callCleanup
;
125 availableConverters
= ucnv_countAvailable();
126 convNames
= new const char *[availableConverters
];
127 for (idx
= 0; idx
< availableConverters
; idx
++) {
128 convNames
[idx
] = ucnv_getAvailableName(idx
);
131 virtual void call(UErrorCode
* status
){
136 for (idx
= 0; idx
< availableConverters
; idx
++) {
137 ucnv_close(ucnv_open(convNames
[idx
], status
));
140 virtual long getOperationsPerIteration(void){
141 return availableConverters
;
143 ~ICUOpenAllConvertersFunction(){
148 class WinANSIToUnicodePerfFunction
: public UPerfFunction
{
154 WCHAR dest
[MAX_BUF_SIZE
];
158 WinANSIToUnicodePerfFunction(const char* cpName
, char* pszIn
,UINT szLen
, UErrorCode
& status
){
162 dstLen
= UPRV_LENGTHOF(dest
);
163 unsigned short bEnc
[30]={'\0'};
164 const char* tenc
=name
;
165 for(int i
=0;*tenc
!='\0';i
++){
169 LPMULTILANGUAGE2 pMulti
;
173 /* create instance of converter object*/
175 __uuidof(CMultiLanguage
),
178 __uuidof(IMultiLanguage2
),
184 MIMECSETINFO mimeInfo
;
186 mimeInfo
.uiCodePage
= 0;
187 mimeInfo
.uiInternetEncoding
=0;
188 /* get the charset info */
189 pMulti
->GetCharsetInfo((wchar_t *)bEnc
,&mimeInfo
);
190 uiCodePage
= (mimeInfo
.uiInternetEncoding
==0)?mimeInfo
.uiCodePage
:mimeInfo
.uiInternetEncoding
;
192 virtual void call(UErrorCode
* status
){
193 int winSize
=MultiByteToWideChar(uiCodePage
,CONVERSION_FLAGS
,src
,srcLen
,dest
,dstLen
);
195 virtual long getOperationsPerIteration(void){
200 class WinANSIFromUnicodePerfFunction
: public UPerfFunction
{
206 char dest
[MAX_BUF_SIZE
];
209 BOOL lpUsedDefaultChar
;
212 WinANSIFromUnicodePerfFunction(const char* cpName
, WCHAR
* pszIn
,UINT szLen
, UErrorCode
& status
){
216 dstLen
= UPRV_LENGTHOF(dest
);
217 lpUsedDefaultChar
=FALSE
;
218 unsigned short bEnc
[30]={'\0'};
219 const char* tenc
=name
;
220 for(int i
=0;*tenc
!='\0';i
++){
224 LPMULTILANGUAGE2 pMulti
;
228 /* create instance of converter object*/
230 __uuidof(CMultiLanguage
),
233 __uuidof(IMultiLanguage2
),
239 MIMECSETINFO mimeInfo
;
240 mimeInfo
.uiCodePage
= 0;
241 mimeInfo
.uiInternetEncoding
=0;
242 /* get the charset info */
243 pMulti
->GetCharsetInfo((wchar_t *)bEnc
,&mimeInfo
);
244 uiCodePage
= (mimeInfo
.uiInternetEncoding
==0)?mimeInfo
.uiCodePage
:mimeInfo
.uiInternetEncoding
;
246 virtual void call(UErrorCode
* status
){
247 BOOL
* pUsedDefaultChar
=(uiCodePage
==CP_UTF8
)?NULL
:&lpUsedDefaultChar
;
248 int winSize
= WideCharToMultiByte(uiCodePage
,CONVERSION_FLAGS
,src
,srcLen
,dest
,dstLen
,NULL
, pUsedDefaultChar
);
250 virtual long getOperationsPerIteration(void){
254 static inline void getErr(HRESULT err
, UErrorCode
& status
){
259 //printf("Operation %s successful\n",operation);
262 status
= U_INTERNAL_PROGRAM_ERROR
;
265 status
= U_ILLEGAL_CHAR_FOUND
;
268 class WinIMultiLanguageToUnicodePerfFunction
: public UPerfFunction
{
271 LPMULTILANGUAGE2 pMulti
;
272 LPMLANGCONVERTCHARSET pConvToUni
;
275 WCHAR dst
[MAX_BUF_SIZE
];
280 WinIMultiLanguageToUnicodePerfFunction(const char* name
,char* source
, UINT sourceLen
, UErrorCode
& status
){
284 /* create instance of converter object*/
286 __uuidof(CMultiLanguage
),
289 __uuidof(IMultiLanguage2
),
295 MIMECSETINFO mimeInfo
;
296 mimeInfo
.uiCodePage
= 0;
297 mimeInfo
.uiInternetEncoding
=0;
299 unsigned short bEnc
[30]={'\0'};
300 const char* tenc
=name
;
301 for(int i
=0;*tenc
!='\0';i
++){
305 /* get the charset info */
306 pMulti
->GetCharsetInfo((wchar_t *)bEnc
,&mimeInfo
);
307 pMulti
->CreateConvertCharset(mimeInfo
.uiCodePage
, 1200 /*unicode*/, (DWORD
)0,&pConvToUni
);
311 dstLen
= UPRV_LENGTHOF(dst
);
315 virtual void call(UErrorCode
* status
){
316 HRESULT err
= pConvToUni
->DoConversionToUnicode(src
,&srcLen
,dst
, &dstLen
);
319 virtual long getOperationsPerIteration(void){
324 class WinIMultiLanguageFromUnicodePerfFunction
: public UPerfFunction
{
327 LPMULTILANGUAGE2 pMulti
;
328 LPMLANGCONVERTCHARSET pConvFromUni
;
331 char dst
[MAX_BUF_SIZE
];
336 WinIMultiLanguageFromUnicodePerfFunction(const char* name
,WCHAR
* source
, UINT sourceLen
, UErrorCode
& status
){
340 /* create instance of converter object*/
342 __uuidof(CMultiLanguage
),
345 __uuidof(IMultiLanguage2
),
351 MIMECSETINFO mimeInfo
;
352 mimeInfo
.uiCodePage
= 0;
353 mimeInfo
.uiInternetEncoding
=0;
355 unsigned short bEnc
[30]={'\0'};
356 const char* tenc
=name
;
357 for(int i
=0;*tenc
!='\0';i
++){
361 /* get the charset info */
362 pMulti
->GetCharsetInfo((wchar_t *)bEnc
,&mimeInfo
);
363 pMulti
->CreateConvertCharset(1200 /*unicode*/, mimeInfo
.uiCodePage
, (DWORD
)0,&pConvFromUni
);
367 dstLen
= UPRV_LENGTHOF(dst
);
372 virtual void call(UErrorCode
* status
){
373 HRESULT err
= pConvFromUni
->DoConversionFromUnicode(src
,&srcLen
,dst
, &dstLen
);
376 virtual long getOperationsPerIteration(void){
381 class WinIMultiLanguage2ToUnicodePerfFunction
: public UPerfFunction
{
384 LPMULTILANGUAGE2 pMulti
;
387 WCHAR dst
[MAX_BUF_SIZE
];
392 WinIMultiLanguage2ToUnicodePerfFunction(const char* name
,char* source
, UINT sourceLen
, UErrorCode
& status
){
396 /* create instance of converter object*/
398 __uuidof(CMultiLanguage
),
401 __uuidof(IMultiLanguage2
),
407 dstLen
= UPRV_LENGTHOF(dst
);
409 unsigned short bEnc
[30]={'\0'};
410 const char* tenc
=name
;
411 for(int i
=0;*tenc
!='\0';i
++){
415 /* get the charset info */
416 MIMECSETINFO mimeInfo
;
417 mimeInfo
.uiCodePage
= 0;
418 mimeInfo
.uiInternetEncoding
=0;
419 pMulti
->GetCharsetInfo((wchar_t *)bEnc
,&mimeInfo
);
420 dwEnc
= (mimeInfo
.uiInternetEncoding
==0)?mimeInfo
.uiCodePage
:mimeInfo
.uiInternetEncoding
;
423 virtual void call(UErrorCode
* status
){
425 HRESULT err
= pMulti
->ConvertStringToUnicode(&dwMode
,dwEnc
,(char*)src
,&srcLen
,dst
, &dstLen
);
428 virtual long getOperationsPerIteration(void){
433 class WinIMultiLanguage2FromUnicodePerfFunction
: public UPerfFunction
{
436 LPMULTILANGUAGE2 pMulti
;
437 LPMLANGCONVERTCHARSET pConvFromUni
;
440 char dst
[MAX_BUF_SIZE
];
446 WinIMultiLanguage2FromUnicodePerfFunction(const char* name
,WCHAR
* source
, UINT sourceLen
, UErrorCode
& status
){
450 /* create instance of converter object*/
452 __uuidof(CMultiLanguage
),
455 __uuidof(IMultiLanguage2
),
460 unsigned short bEnc
[30]={'\0'};
461 const char* tenc
=name
;
462 for(int i
=0;*tenc
!='\0';i
++){
468 dstLen
= UPRV_LENGTHOF(dst
);
470 /* get the charset info */
471 MIMECSETINFO mimeInfo
;
472 mimeInfo
.uiCodePage
= 0;
473 mimeInfo
.uiInternetEncoding
=0;
475 pMulti
->GetCharsetInfo((wchar_t *)bEnc
,&mimeInfo
);
476 dwEnc
= (mimeInfo
.uiInternetEncoding
==0)?mimeInfo
.uiCodePage
:mimeInfo
.uiInternetEncoding
;
479 virtual void call(UErrorCode
* status
){
481 HRESULT err
= pMulti
->ConvertStringFromUnicode(&dwMode
,dwEnc
,src
,&srcLen
,dst
, &dstLen
);
484 virtual long getOperationsPerIteration(void){
489 class ConverterPerformanceTest
: public UPerfTest
{
493 ConverterPerformanceTest(int32_t argc
, const char* argv
[], UErrorCode
& status
);
494 ~ConverterPerformanceTest();
495 virtual UPerfFunction
* runIndexedTest(int32_t index
, UBool exec
,const char* &name
, char* par
= NULL
);
497 UPerfFunction
* TestICU_CleanOpenAllConverters();
498 UPerfFunction
* TestICU_OpenAllConverters();
500 UPerfFunction
* TestICU_UTF8_ToUnicode();
501 UPerfFunction
* TestICU_UTF8_FromUnicode();
502 UPerfFunction
* TestWinANSI_UTF8_ToUnicode();
503 UPerfFunction
* TestWinANSI_UTF8_FromUnicode();
504 UPerfFunction
* TestWinIML2_UTF8_ToUnicode();
505 UPerfFunction
* TestWinIML2_UTF8_FromUnicode();
507 UPerfFunction
* TestICU_Latin1_ToUnicode();
508 UPerfFunction
* TestICU_Latin1_FromUnicode();
509 UPerfFunction
* TestWinANSI_Latin1_ToUnicode();
510 UPerfFunction
* TestWinANSI_Latin1_FromUnicode();
511 UPerfFunction
* TestWinIML2_Latin1_ToUnicode();
512 UPerfFunction
* TestWinIML2_Latin1_FromUnicode();
514 UPerfFunction
* TestICU_EBCDIC_Arabic_ToUnicode();
515 UPerfFunction
* TestICU_EBCDIC_Arabic_FromUnicode();
516 UPerfFunction
* TestWinANSI_EBCDIC_Arabic_ToUnicode();
517 UPerfFunction
* TestWinANSI_EBCDIC_Arabic_FromUnicode();
518 UPerfFunction
* TestWinIML2_EBCDIC_Arabic_ToUnicode();
519 UPerfFunction
* TestWinIML2_EBCDIC_Arabic_FromUnicode();
521 UPerfFunction
* TestICU_Latin8_ToUnicode();
522 UPerfFunction
* TestICU_Latin8_FromUnicode();
523 UPerfFunction
* TestWinANSI_Latin8_ToUnicode();
524 UPerfFunction
* TestWinANSI_Latin8_FromUnicode();
525 UPerfFunction
* TestWinIML2_Latin8_ToUnicode();
526 UPerfFunction
* TestWinIML2_Latin8_FromUnicode();
529 UPerfFunction
* TestICU_SJIS_ToUnicode();
530 UPerfFunction
* TestICU_SJIS_FromUnicode();
531 UPerfFunction
* TestWinANSI_SJIS_ToUnicode();
532 UPerfFunction
* TestWinANSI_SJIS_FromUnicode();
533 UPerfFunction
* TestWinIML2_SJIS_ToUnicode();
534 UPerfFunction
* TestWinIML2_SJIS_FromUnicode();
536 UPerfFunction
* TestICU_EUCJP_ToUnicode();
537 UPerfFunction
* TestICU_EUCJP_FromUnicode();
538 UPerfFunction
* TestWinANSI_EUCJP_ToUnicode();
539 UPerfFunction
* TestWinANSI_EUCJP_FromUnicode();
540 UPerfFunction
* TestWinIML2_EUCJP_ToUnicode();
541 UPerfFunction
* TestWinIML2_EUCJP_FromUnicode();
543 UPerfFunction
* TestICU_GB2312_ToUnicode();
544 UPerfFunction
* TestICU_GB2312_FromUnicode();
545 UPerfFunction
* TestWinANSI_GB2312_ToUnicode();
546 UPerfFunction
* TestWinANSI_GB2312_FromUnicode();
547 UPerfFunction
* TestWinIML2_GB2312_ToUnicode();
548 UPerfFunction
* TestWinIML2_GB2312_FromUnicode();
551 UPerfFunction
* TestICU_ISO2022KR_ToUnicode();
552 UPerfFunction
* TestICU_ISO2022KR_FromUnicode();
553 UPerfFunction
* TestWinANSI_ISO2022KR_ToUnicode();
554 UPerfFunction
* TestWinANSI_ISO2022KR_FromUnicode();
555 UPerfFunction
* TestWinIML2_ISO2022KR_ToUnicode();
556 UPerfFunction
* TestWinIML2_ISO2022KR_FromUnicode();
558 UPerfFunction
* TestICU_ISO2022JP_ToUnicode();
559 UPerfFunction
* TestICU_ISO2022JP_FromUnicode();
560 UPerfFunction
* TestWinANSI_ISO2022JP_ToUnicode();
561 UPerfFunction
* TestWinANSI_ISO2022JP_FromUnicode();
562 UPerfFunction
* TestWinIML2_ISO2022JP_ToUnicode();
563 UPerfFunction
* TestWinIML2_ISO2022JP_FromUnicode();