]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/perf/convperf/convperf.h
ICU-6.2.8.tar.gz
[apple/icu.git] / icuSources / test / perf / convperf / convperf.h
1 /*
2 **********************************************************************
3 * Copyright (c) 2002-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 **********************************************************************
7 */
8 #ifndef _CONVPERF_H
9 #define _CONVPERF_H
10
11 #include "uperf.h"
12 #include "unicode/ucnv.h"
13 #include "unicode/uclean.h"
14 #include "unicode/ustring.h"
15 #include <mlang.h>
16 #include <objbase.h>
17
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]))
21
22 class ICUToUnicodePerfFunction : public UPerfFunction{
23 private:
24 UConverter* conv;
25 const char* src;
26 int32_t srcLen;
27 UChar* target;
28 UChar* targetLimit;
29
30 public:
31 ICUToUnicodePerfFunction(const char* name, const char* source, int32_t sourceLen, UErrorCode& status){
32 conv = ucnv_open(name,&status);
33 src = source;
34 srcLen = sourceLen;
35 if(U_FAILURE(status)){
36 conv = NULL;
37 return;
38 }
39 target = NULL;
40 targetLimit = NULL;
41 int32_t reqdLen = ucnv_toUChars(conv, target, 0,
42 source, srcLen, &status);
43 if(status==U_BUFFER_OVERFLOW_ERROR) {
44 status=U_ZERO_ERROR;
45 target=(UChar*)uprv_malloc((reqdLen) * U_SIZEOF_UCHAR*2);
46 targetLimit = target + reqdLen;
47 if(target == NULL){
48 status = U_MEMORY_ALLOCATION_ERROR;
49 return;
50 }
51 }
52 }
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);
58 }
59 virtual long getOperationsPerIteration(void){
60 return srcLen;
61 }
62 ~ICUToUnicodePerfFunction(){
63 uprv_free(target);
64 ucnv_close(conv);
65 }
66 };
67 class ICUFromUnicodePerfFunction : public UPerfFunction{
68 private:
69 UConverter* conv;
70 const UChar* src;
71 int32_t srcLen;
72 char* target;
73 char* targetLimit;
74 const char* name;
75
76 public:
77 ICUFromUnicodePerfFunction(const char* name, const UChar* source, int32_t sourceLen, UErrorCode& status){
78 conv = ucnv_open(name,&status);
79 src = source;
80 srcLen = sourceLen;
81 if(U_FAILURE(status)){
82 conv = NULL;
83 return;
84 }
85 target = NULL;
86 targetLimit = NULL;
87 int32_t reqdLen = ucnv_fromUChars(conv, target, 0,
88 source, srcLen, &status);
89 if(status==U_BUFFER_OVERFLOW_ERROR) {
90 status=U_ZERO_ERROR;
91 target=(char*)uprv_malloc((reqdLen*2));
92 targetLimit = target + reqdLen;
93 if(target == NULL){
94 status = U_MEMORY_ALLOCATION_ERROR;
95 return;
96 }
97 }
98 }
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);
104 }
105 virtual long getOperationsPerIteration(void){
106 return srcLen;
107 }
108 ~ICUFromUnicodePerfFunction(){
109 uprv_free(target);
110 ucnv_close(conv);
111 }
112 };
113
114 class ICUOpenAllConvertersFunction : public UPerfFunction{
115 private:
116 UBool cleanup;
117 int32_t availableConverters;
118 const char **convNames;
119 public:
120 ICUOpenAllConvertersFunction(UBool callCleanup, UErrorCode& status){
121 int32_t idx;
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);
127 }
128 }
129 virtual void call(UErrorCode* status){
130 int32_t idx;
131 if (cleanup) {
132 u_cleanup();
133 }
134 for (idx = 0; idx < availableConverters; idx++) {
135 ucnv_close(ucnv_open(convNames[idx], status));
136 }
137 }
138 virtual long getOperationsPerIteration(void){
139 return availableConverters;
140 }
141 ~ICUOpenAllConvertersFunction(){
142 delete []convNames;
143 }
144 };
145
146 class WinANSIToUnicodePerfFunction : public UPerfFunction{
147
148 private:
149 DWORD uiCodePage;
150 char* src;
151 UINT srcLen;
152 WCHAR dest[MAX_BUF_SIZE];
153 UINT dstLen;
154 const char* name;
155 public:
156 WinANSIToUnicodePerfFunction(const char* cpName, char* pszIn,UINT szLen, UErrorCode& status){
157 name = cpName;
158 src = pszIn;
159 srcLen = szLen;
160 dstLen = LENGTHOF(dest);
161 unsigned short bEnc[30]={'\0'};
162 const char* tenc=name;
163 for(int i=0;*tenc!='\0';i++){
164 bEnc[i]=*tenc;
165 tenc++;
166 }
167 LPMULTILANGUAGE2 pMulti;
168
169 CoInitialize(NULL);
170
171 /* create instance of converter object*/
172 CoCreateInstance(
173 __uuidof(CMultiLanguage),
174 NULL,
175 CLSCTX_SERVER,
176 __uuidof(IMultiLanguage2),
177 (void**)&pMulti
178 );
179
180
181
182 MIMECSETINFO mimeInfo;
183
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;
189 }
190 virtual void call(UErrorCode* status){
191 int winSize =MultiByteToWideChar(uiCodePage,CONVERSION_FLAGS,src,srcLen,dest,dstLen);
192 }
193 virtual long getOperationsPerIteration(void){
194 return srcLen;
195 }
196 };
197
198 class WinANSIFromUnicodePerfFunction : public UPerfFunction{
199
200 private:
201 DWORD uiCodePage;
202 WCHAR* src;
203 UINT srcLen;
204 char dest[MAX_BUF_SIZE];
205 UINT dstLen;
206 const char* name;
207 BOOL lpUsedDefaultChar;
208
209 public:
210 WinANSIFromUnicodePerfFunction(const char* cpName, WCHAR* pszIn,UINT szLen, UErrorCode& status){
211 name = cpName;
212 src = pszIn;
213 srcLen = szLen;
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++){
219 bEnc[i]=*tenc;
220 tenc++;
221 }
222 LPMULTILANGUAGE2 pMulti;
223
224 CoInitialize(NULL);
225
226 /* create instance of converter object*/
227 CoCreateInstance(
228 __uuidof(CMultiLanguage),
229 NULL,
230 CLSCTX_SERVER,
231 __uuidof(IMultiLanguage2),
232 (void**)&pMulti
233 );
234
235
236
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;
243 }
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);
247 }
248 virtual long getOperationsPerIteration(void){
249 return srcLen;
250 }
251 };
252 static inline void getErr(HRESULT err, UErrorCode& status){
253
254 switch (err){
255
256 case S_OK:
257 //printf("Operation %s successful\n",operation);
258 break;
259 case S_FALSE:
260 status = U_INTERNAL_PROGRAM_ERROR;
261 break;
262 case E_FAIL:
263 status = U_ILLEGAL_CHAR_FOUND;
264 }
265 }
266 class WinIMultiLanguageToUnicodePerfFunction : public UPerfFunction{
267
268 private:
269 LPMULTILANGUAGE2 pMulti;
270 LPMLANGCONVERTCHARSET pConvToUni;
271 char* src;
272 UINT srcLen;
273 WCHAR dst[MAX_BUF_SIZE];
274 UINT dstLen;
275 const char* cpName;
276
277 public:
278 WinIMultiLanguageToUnicodePerfFunction(const char* name,char* source, UINT sourceLen, UErrorCode& status){
279
280 CoInitialize(NULL);
281
282 /* create instance of converter object*/
283 CoCreateInstance(
284 __uuidof(CMultiLanguage),
285 NULL,
286 CLSCTX_SERVER,
287 __uuidof(IMultiLanguage2),
288 (void**)&pMulti
289 );
290
291
292
293 MIMECSETINFO mimeInfo;
294 mimeInfo.uiCodePage = 0;
295 mimeInfo.uiInternetEncoding =0;
296 HRESULT err=S_OK;
297 unsigned short bEnc[30]={'\0'};
298 const char* tenc=name;
299 for(int i=0;*tenc!='\0';i++){
300 bEnc[i]=*tenc;
301 tenc++;
302 }
303 /* get the charset info */
304 pMulti->GetCharsetInfo(bEnc,&mimeInfo);
305 pMulti->CreateConvertCharset(mimeInfo.uiCodePage, 1200 /*unicode*/, (DWORD)0,&pConvToUni);
306 getErr(err,status);
307 src = source;
308 srcLen = sourceLen;
309 dstLen = LENGTHOF(dst);
310 cpName = name;
311 }
312
313 virtual void call(UErrorCode* status){
314 HRESULT err= pConvToUni->DoConversionToUnicode(src,&srcLen,dst, &dstLen);
315 getErr(err,*status);
316 }
317 virtual long getOperationsPerIteration(void){
318 return srcLen;
319 }
320 };
321
322 class WinIMultiLanguageFromUnicodePerfFunction : public UPerfFunction{
323
324 private:
325 LPMULTILANGUAGE2 pMulti;
326 LPMLANGCONVERTCHARSET pConvFromUni;
327 WCHAR* src;
328 UINT srcLen;
329 char dst[MAX_BUF_SIZE];
330 UINT dstLen;
331 const char* cpName;
332
333 public:
334 WinIMultiLanguageFromUnicodePerfFunction(const char* name,WCHAR* source, UINT sourceLen, UErrorCode& status){
335
336 CoInitialize(NULL);
337
338 /* create instance of converter object*/
339 CoCreateInstance(
340 __uuidof(CMultiLanguage),
341 NULL,
342 CLSCTX_SERVER,
343 __uuidof(IMultiLanguage2),
344 (void**)&pMulti
345 );
346
347
348
349 MIMECSETINFO mimeInfo;
350 mimeInfo.uiCodePage = 0;
351 mimeInfo.uiInternetEncoding =0;
352 HRESULT err=S_OK;
353 unsigned short bEnc[30]={'\0'};
354 const char* tenc=name;
355 for(int i=0;*tenc!='\0';i++){
356 bEnc[i]=*tenc;
357 tenc++;
358 }
359 /* get the charset info */
360 pMulti->GetCharsetInfo(bEnc,&mimeInfo);
361 pMulti->CreateConvertCharset(1200 /*unicode*/, mimeInfo.uiCodePage, (DWORD)0,&pConvFromUni);
362 getErr(err,status);
363 src = source;
364 srcLen = sourceLen;
365 dstLen = LENGTHOF(dst);
366 cpName = name;
367
368 }
369
370 virtual void call(UErrorCode* status){
371 HRESULT err= pConvFromUni->DoConversionFromUnicode(src,&srcLen,dst, &dstLen);
372 getErr(err,*status);
373 }
374 virtual long getOperationsPerIteration(void){
375 return srcLen;
376 }
377 };
378
379 class WinIMultiLanguage2ToUnicodePerfFunction : public UPerfFunction{
380
381 private:
382 LPMULTILANGUAGE2 pMulti;
383 char* src;
384 UINT srcLen;
385 WCHAR dst[MAX_BUF_SIZE];
386 UINT dstLen;
387 const char* cpName;
388 DWORD dwEnc;
389 public:
390 WinIMultiLanguage2ToUnicodePerfFunction(const char* name,char* source, UINT sourceLen, UErrorCode& status){
391
392 CoInitialize(NULL);
393
394 /* create instance of converter object*/
395 CoCreateInstance(
396 __uuidof(CMultiLanguage),
397 NULL,
398 CLSCTX_SERVER,
399 __uuidof(IMultiLanguage2),
400 (void**)&pMulti
401 );
402
403 src = source;
404 srcLen = sourceLen;
405 dstLen = LENGTHOF(dst);
406 cpName = name;
407 unsigned short bEnc[30]={'\0'};
408 const char* tenc=name;
409 for(int i=0;*tenc!='\0';i++){
410 bEnc[i]=*tenc;
411 tenc++;
412 }
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;
419 }
420
421 virtual void call(UErrorCode* status){
422 DWORD dwMode=0;
423 HRESULT err= pMulti->ConvertStringToUnicode(&dwMode,dwEnc,(char*)src,&srcLen,dst, &dstLen);
424 getErr(err,*status);
425 }
426 virtual long getOperationsPerIteration(void){
427 return srcLen;
428 }
429 };
430
431 class WinIMultiLanguage2FromUnicodePerfFunction : public UPerfFunction{
432
433 private:
434 LPMULTILANGUAGE2 pMulti;
435 LPMLANGCONVERTCHARSET pConvFromUni;
436 WCHAR* src;
437 UINT srcLen;
438 char dst[MAX_BUF_SIZE];
439 UINT dstLen;
440 const char* cpName;
441 DWORD dwEnc;
442
443 public:
444 WinIMultiLanguage2FromUnicodePerfFunction(const char* name,WCHAR* source, UINT sourceLen, UErrorCode& status){
445
446 CoInitialize(NULL);
447
448 /* create instance of converter object*/
449 CoCreateInstance(
450 __uuidof(CMultiLanguage),
451 NULL,
452 CLSCTX_SERVER,
453 __uuidof(IMultiLanguage2),
454 (void**)&pMulti
455 );
456
457
458 unsigned short bEnc[30]={'\0'};
459 const char* tenc=name;
460 for(int i=0;*tenc!='\0';i++){
461 bEnc[i]=*tenc;
462 tenc++;
463 }
464 src = source;
465 srcLen = sourceLen;
466 dstLen = LENGTHOF(dst);
467 cpName = name;
468 /* get the charset info */
469 MIMECSETINFO mimeInfo;
470 mimeInfo.uiCodePage = 0;
471 mimeInfo.uiInternetEncoding =0;
472
473 pMulti->GetCharsetInfo(bEnc,&mimeInfo);
474 dwEnc = (mimeInfo.uiInternetEncoding==0)?mimeInfo.uiCodePage:mimeInfo.uiInternetEncoding;
475 }
476
477 virtual void call(UErrorCode* status){
478 DWORD dwMode=0;
479 HRESULT err= pMulti->ConvertStringFromUnicode(&dwMode,dwEnc,src,&srcLen,dst, &dstLen);
480 getErr(err,*status);
481 }
482 virtual long getOperationsPerIteration(void){
483 return srcLen;
484 }
485 };
486
487 class ConverterPerformanceTest : public UPerfTest{
488
489 public:
490
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);
494
495 UPerfFunction* TestICU_CleanOpenAllConverters();
496 UPerfFunction* TestICU_OpenAllConverters();
497
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();
504
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();
511
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();
518
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();
525
526
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();
533
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();
540
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();
547
548
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();
555
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();
562
563 };
564
565 #endif
566