2 **********************************************************************
3 * Copyright (C) 2002-2005, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * file name: utfperf.cpp
8 * tab size: 8 (not used)
11 * created on: 2005Nov17
12 * created by: Raymond Yang
14 * Ported from utfper.c created by Markus W. Scherer
15 * Performance test program for Unicode converters
19 #include "unicode/uperf.h"
22 /* definitions and text buffers */
24 #define INPUT_CAPACITY (1024*1024)
25 #define INTERMEDIATE_CAPACITY 4096
26 #define INTERMEDIATE_SMALL_CAPACITY 20
27 #define OUTPUT_CAPACITY INPUT_CAPACITY
29 static UChar input
[INPUT_CAPACITY
];
30 static UChar output
[OUTPUT_CAPACITY
];
31 static char intermediate
[INTERMEDIATE_CAPACITY
];
33 static int32_t inputLength
, encodedLength
, outputLength
, countInputCodePoints
;
36 class Command
: public UPerfFunction
{
38 Command(const char * name
, int32_t buf_cap
):name(name
),buf_cap(buf_cap
){
39 errorCode
=U_ZERO_ERROR
;
40 cnv
=ucnv_open(name
, &errorCode
);
43 static UPerfFunction
* get(const char * name
, int32_t buf_cap
){
44 Command
* t
= new Command(name
, buf_cap
);
45 if (U_SUCCESS(t
->errorCode
)){
48 //fprintf(stderr, "error opening converter for \"%s\" - %s\n", name, u_errorName(errorCode));
54 if(U_SUCCESS(errorCode
)) {
58 virtual void call(UErrorCode
* pErrorCode
){
59 const UChar
*pIn
, *pInLimit
;
60 UChar
*pOut
, *pOutLimit
;
61 char *pInter
, *pInterLimit
;
68 pInLimit
=input
+inputLength
;
71 pOutLimit
=output
+OUTPUT_CAPACITY
;
73 pInterLimit
=intermediate
+buf_cap
;
75 encodedLength
=outputLength
=0;
78 while(pIn
<pInLimit
|| !flush
) {
79 /* convert a block of [pIn..pInLimit[ to the encoding in intermediate[] */
81 flush
=(UBool
)(pIn
==pInLimit
);
82 ucnv_fromUnicode(cnv
, &pInter
, pInterLimit
, &pIn
, pInLimit
, NULL
, flush
, pErrorCode
);
83 encodedLength
+=(int32_t)(pInter
-intermediate
);
85 if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
86 /* in case flush was TRUE make sure that we convert once more to really flush */
88 *pErrorCode
=U_ZERO_ERROR
;
89 } else if(U_FAILURE(*pErrorCode
)) {
93 /* convert the block [intermediate..pInter[ back to UTF-16 */
95 ucnv_toUnicode(cnv
, &pOut
, pOutLimit
,&p
, pInter
,NULL
, flush
,pErrorCode
);
96 if(U_FAILURE(*pErrorCode
)) {
99 /* intermediate must have been consumed (p==pInter) because of the converter semantics */
102 outputLength
=pOut
-output
;
103 if(inputLength
!=outputLength
) {
104 fprintf(stderr
, "error: roundtrip failed, inputLength %d!=outputLength %d\n", inputLength
, outputLength
);
105 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
108 virtual long getOperationsPerIteration(){
109 return countInputCodePoints
;
113 UErrorCode errorCode
;
117 class UtfPerformanceTest
: public UPerfTest
{
119 UtfPerformanceTest(int32_t argc
, const char *argv
[], UErrorCode
&status
) :UPerfTest(argc
,argv
,status
){
120 getBuffer(inputLength
, status
);
121 u_strncpy(input
, buffer
, inputLength
);
122 countInputCodePoints
= u_countChar32(input
, inputLength
);
125 virtual UPerfFunction
* runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
= NULL
){
127 case 0: name
= "UTF_8"; if (exec
) return Command::get("UTF-8", INTERMEDIATE_CAPACITY
); break;
128 case 1: name
= "UTF_8_SB"; if (exec
) return Command::get("UTF-8",INTERMEDIATE_SMALL_CAPACITY
); break;
129 case 2: name
= "SCSU"; if (exec
) return Command::get("SCSU", INTERMEDIATE_CAPACITY
); break;
130 case 3: name
= "SCSU_SB"; if (exec
) return Command::get("SCSU", INTERMEDIATE_SMALL_CAPACITY
); break;
131 case 4: name
= "BOCU_1"; if (exec
) return Command::get("BOCU-1", INTERMEDIATE_CAPACITY
); break;
132 case 5: name
= "BOCU_1_SB"; if (exec
) return Command::get("BOCU-1",INTERMEDIATE_SMALL_CAPACITY
); break;
133 default: name
= ""; break;
140 int main(int argc
, const char *argv
[])
142 UErrorCode status
= U_ZERO_ERROR
;
143 UtfPerformanceTest
test(argc
, argv
, status
);
145 if (U_FAILURE(status
)){
146 printf("The error is %s\n", u_errorName(status
));
150 if (test
.run() == FALSE
){
151 fprintf(stderr
, "FAILED: Tests could not be run please check the "