]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/perf/utfperf/utfperf.cpp
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / test / perf / utfperf / utfperf.cpp
1 /*
2 **********************************************************************
3 * Copyright (C) 2002-2005, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * file name: utfperf.cpp
7 * encoding: US-ASCII
8 * tab size: 8 (not used)
9 * indentation:4
10 *
11 * created on: 2005Nov17
12 * created by: Raymond Yang
13 *
14 * Ported from utfper.c created by Markus W. Scherer
15 * Performance test program for Unicode converters
16 */
17
18 #include <stdio.h>
19 #include "unicode/uperf.h"
20
21
22 /* definitions and text buffers */
23
24 #define INPUT_CAPACITY (1024*1024)
25 #define INTERMEDIATE_CAPACITY 4096
26 #define INTERMEDIATE_SMALL_CAPACITY 20
27 #define OUTPUT_CAPACITY INPUT_CAPACITY
28
29 static UChar input[INPUT_CAPACITY];
30 static UChar output[OUTPUT_CAPACITY];
31 static char intermediate[INTERMEDIATE_CAPACITY];
32
33 static int32_t inputLength, encodedLength, outputLength, countInputCodePoints;
34
35
36 class Command : public UPerfFunction {
37 private:
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);
41 }
42 public:
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)){
46 return t;
47 } else {
48 //fprintf(stderr, "error opening converter for \"%s\" - %s\n", name, u_errorName(errorCode));
49 delete t;
50 return NULL;
51 }
52 }
53 virtual ~Command(){
54 if(U_SUCCESS(errorCode)) {
55 ucnv_close(cnv);
56 }
57 }
58 virtual void call(UErrorCode* pErrorCode){
59 const UChar *pIn, *pInLimit;
60 UChar *pOut, *pOutLimit;
61 char *pInter, *pInterLimit;
62 const char *p;
63 UBool flush;
64
65 ucnv_reset(cnv);
66
67 pIn=input;
68 pInLimit=input+inputLength;
69
70 pOut=output;
71 pOutLimit=output+OUTPUT_CAPACITY;
72
73 pInterLimit=intermediate+buf_cap;
74
75 encodedLength=outputLength=0;
76 flush=FALSE;
77
78 while(pIn<pInLimit || !flush) {
79 /* convert a block of [pIn..pInLimit[ to the encoding in intermediate[] */
80 pInter=intermediate;
81 flush=(UBool)(pIn==pInLimit);
82 ucnv_fromUnicode(cnv, &pInter, pInterLimit, &pIn, pInLimit, NULL, flush, pErrorCode);
83 encodedLength+=(int32_t)(pInter-intermediate);
84
85 if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
86 /* in case flush was TRUE make sure that we convert once more to really flush */
87 flush=FALSE;
88 *pErrorCode=U_ZERO_ERROR;
89 } else if(U_FAILURE(*pErrorCode)) {
90 return;
91 }
92
93 /* convert the block [intermediate..pInter[ back to UTF-16 */
94 p=intermediate;
95 ucnv_toUnicode(cnv, &pOut, pOutLimit,&p, pInter,NULL, flush,pErrorCode);
96 if(U_FAILURE(*pErrorCode)) {
97 return;
98 }
99 /* intermediate must have been consumed (p==pInter) because of the converter semantics */
100 }
101
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;
106 }
107 }
108 virtual long getOperationsPerIteration(){
109 return countInputCodePoints;
110 }
111 const char * name;
112 int32_t buf_cap;
113 UErrorCode errorCode;
114 UConverter *cnv;
115 };
116
117 class UtfPerformanceTest : public UPerfTest{
118 public:
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);
123 }
124
125 virtual UPerfFunction* runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ){
126 switch (index) {
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;
134 }
135 return NULL;
136 }
137 };
138
139
140 int main(int argc, const char *argv[])
141 {
142 UErrorCode status = U_ZERO_ERROR;
143 UtfPerformanceTest test(argc, argv, status);
144
145 if (U_FAILURE(status)){
146 printf("The error is %s\n", u_errorName(status));
147 return status;
148 }
149
150 if (test.run() == FALSE){
151 fprintf(stderr, "FAILED: Tests could not be run please check the "
152 "arguments.\n");
153 return -1;
154 }
155 return 0;
156 }