2 **********************************************************************
3 * Copyright (c) 2002-2010,International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 **********************************************************************
10 #define _DATEFMTPERF_H
13 #include "unicode/stringpiece.h"
14 #include "unicode/unistr.h"
15 #include "unicode/uperf.h"
17 #include "unicode/utypes.h"
18 #include "unicode/datefmt.h"
19 #include "unicode/calendar.h"
20 #include "unicode/uclean.h"
21 #include "unicode/brkiter.h"
22 #include "unicode/numfmt.h"
23 #include "unicode/coll.h"
27 #include "breakdata.h"
28 #include "collationdata.h"
37 // Stubs for Windows API functions when building on UNIXes.
39 #if defined(U_WINDOWS)
44 inline int FoldStringW(DWORD dwMapFlags
, const UChar
* lpSrcStr
,int cchSrc
, UChar
* lpDestStr
,int cchDest
);
47 class BreakItFunction
: public UPerfFunction
55 BreakItFunction(){num
= -1;}
56 BreakItFunction(int a
, bool b
){num
= a
; wordIteration
= b
;}
58 virtual void call(UErrorCode
*status
)
60 BreakIterator
* boundary
;
64 for(int i
= 0; i
< num
; i
++)
66 boundary
= BreakIterator::createWordInstance("en", *status
);
67 boundary
->setText(str
);
69 int32_t start
= boundary
->first();
70 for (int32_t end
= boundary
->next();
71 end
!= BreakIterator::DONE
;
72 start
= end
, end
= boundary
->next())
74 printTextRange( *boundary
, start
, end
);
79 else // character iteration
81 for(int i
= 0; i
< num
; i
++)
83 boundary
= BreakIterator::createCharacterInstance(Locale::getUS(), *status
);
84 boundary
->setText(str
);
86 int32_t start
= boundary
->first();
87 for (int32_t end
= boundary
->next();
88 end
!= BreakIterator::DONE
;
89 start
= end
, end
= boundary
->next())
91 printTextRange( *boundary
, start
, end
);
99 virtual long getOperationsPerIteration()
101 if(wordIteration
) return 125*num
;
105 void printUnicodeString(const UnicodeString
&s
) {
107 s
.extract(0, s
.length(), charBuf
, sizeof(charBuf
)-1, 0);
108 charBuf
[sizeof(charBuf
)-1] = 0;
109 printf("%s", charBuf
);
113 void printTextRange( BreakIterator
& iterator
,
114 int32_t start
, int32_t end
)
116 CharacterIterator
*strIter
= iterator
.getText().clone();
119 //printUnicodeString(UnicodeString(s, start, end-start));
124 // Print the given string to stdout (for debugging purposes)
125 void uprintf(const UnicodeString
&str
) {
127 int32_t len
= str
.length();
128 int32_t bufLen
= len
+ 16;
130 buf
= new char[bufLen
+ 1];
131 actualLen
= str
.extract(0, len
, buf
/*, bufLen*/); // Default codepage conversion
139 class DateFmtFunction
: public UPerfFunction
152 DateFmtFunction(int a
, const char* loc
)
158 virtual void call(UErrorCode
* status
)
161 UErrorCode status2
= U_ZERO_ERROR
;
167 cal
= Calendar::createInstance(status2
);
168 check(status2
, "Calendar::createInstance");
169 zone
= TimeZone::createTimeZone("GMT"); // Create a GMT zone
170 cal
->adoptTimeZone(zone
);
174 fmt
= DateFormat::createDateTimeInstance(
175 DateFormat::kShort
, DateFormat::kFull
, loc
);
178 // (dates are imported from datedata.h)
179 for(int j
= 0; j
< num
; j
++)
180 for(int i
= 0; i
< NUM_DATES
; i
++)
183 cal
->set(years
[i
], months
[i
], days
[i
]);
184 date
= cal
->getTime(status2
);
185 check(status2
, "Calendar::getTime");
187 fmt
->setCalendar(*cal
);
191 fmt
->format(date
, str
, status2
);
194 // Display the formatted date string
205 virtual long getOperationsPerIteration()
207 return NUM_DATES
* num
;
210 // Print the given string to stdout (for debugging purposes)
211 void uprintf(const UnicodeString
&str
) {
213 int32_t len
= str
.length();
214 int32_t bufLen
= len
+ 16;
216 buf
= new char[bufLen
+ 1];
217 actualLen
= str
.extract(0, len
, buf
/*, bufLen*/); // Default codepage conversion
223 // Verify that a UErrorCode is successful; exit(1) if not
224 void check(UErrorCode
& status
, const char* msg
) {
225 if (U_FAILURE(status
)) {
226 printf("ERROR: %s (%s)\n", u_errorName(status
), msg
);
233 class NumFmtFunction
: public UPerfFunction
246 NumFmtFunction(int a
, const char* loc
)
252 virtual void call(UErrorCode
* status2
)
255 UErrorCode status
= U_ZERO_ERROR
;
257 // Create a number formatter for the locale
258 NumberFormat
*fmt
= NumberFormat::createInstance(loc
, status
);
260 // Parse a string. The string uses the digits '0' through '9'
261 // and the decimal separator '.', standard in the US locale
263 for(int i
= 0; i
< num
; i
++)
265 UnicodeString
str("9876543210.123");
267 fmt
->parse(str
, result
, status
);
269 //uprintf(formattableToString(result));
272 // Take the number parsed above, and use the formatter to
274 str
.remove(); // format() will APPEND to this string
275 fmt
->format(result
, str
, status
);
281 delete fmt
; // Release the storage used by the formatter
288 U_LEFT_SQUARE_BRACKET
=0x5b,
290 U_RIGHT_SQUARE_BRACKET
=0x5d,
294 // Create a display string for a formattable
295 UnicodeString
formattableToString(const Formattable
& f
) {
296 switch (f
.getType()) {
297 case Formattable::kDate
:
298 // TODO: Finish implementing this
299 return UNICODE_STRING_SIMPLE("Formattable_DATE_TBD");
300 case Formattable::kDouble
:
303 sprintf(buf
, "%gD", f
.getDouble());
304 return UnicodeString(buf
, "");
306 case Formattable::kLong
:
307 case Formattable::kInt64
:
310 sprintf(buf
, "%ldL", f
.getLong());
311 return UnicodeString(buf
, "");
313 case Formattable::kString
:
314 return UnicodeString((UChar
)U_DQUOTE
).append(f
.getString()).append((UChar
)U_DQUOTE
);
315 case Formattable::kArray
:
318 const Formattable
* array
= f
.getArray(count
);
319 UnicodeString
result((UChar
)U_LEFT_SQUARE_BRACKET
);
320 for (i
=0; i
<count
; ++i
) {
322 (result
+= (UChar
)U_COMMA
) += (UChar
)U_SPACE
;
324 result
+= formattableToString(array
[i
]);
326 result
+= (UChar
)U_RIGHT_SQUARE_BRACKET
;
330 return UNICODE_STRING_SIMPLE("INVALID_Formattable");
334 virtual long getOperationsPerIteration()
339 // Print the given string to stdout using the UTF-8 converter (for debugging purposes only)
340 void uprintf(const UnicodeString
&str
) {
341 char stackBuffer
[100];
344 int32_t bufLen
= str
.extract(0, 0x7fffffff, stackBuffer
, sizeof(stackBuffer
), "UTF-8");
345 if(bufLen
< sizeof(stackBuffer
)) {
348 buf
= new char[bufLen
+ 1];
349 bufLen
= str
.extract(0, 0x7fffffff, buf
, bufLen
+ 1, "UTF-8");
352 if(buf
!= stackBuffer
) {
358 class CollationFunction
: public UPerfFunction
364 UnicodeString
*collation_strings
;
367 * Unescape the strings
370 uint32_t listSize
= sizeof(collation_strings_escaped
)/sizeof(collation_strings_escaped
[0]);
371 collation_strings
= new UnicodeString
[listSize
];
372 for(uint32_t k
=0;k
<listSize
;k
++) {
373 collation_strings
[k
] = collation_strings_escaped
[k
].unescape();
375 UnicodeString
shorty((UChar32
)0x12345);
386 ~CollationFunction() {
387 delete [] collation_strings
;
390 CollationFunction(int a
, const char* loc
)
397 virtual void call(UErrorCode
* status2
)
399 uint32_t listSize
= sizeof(collation_strings_escaped
)/sizeof(collation_strings_escaped
[0]);
400 UErrorCode status
= U_ZERO_ERROR
;
401 Collator
*coll
= Collator::createInstance(Locale(locale
), status
);
403 for(int k
= 0; k
< num
; k
++)
406 for(i
=listSize
-1; i
>=1; i
--) {
408 if(coll
->compare(collation_strings
[j
], collation_strings
[j
+1]) == UCOL_LESS
) {
409 //cout << "Success!" << endl;
417 virtual long getOperationsPerIteration()
423 class DateFormatPerfTest
: public UPerfTest
429 DateFormatPerfTest(int32_t argc
, const char* argv
[], UErrorCode
& status
);
430 ~DateFormatPerfTest();
431 virtual UPerfFunction
* runIndexedTest(int32_t index
, UBool exec
,const char* &name
, char* par
);
433 UPerfFunction
* DateFmt250();
434 UPerfFunction
* DateFmt10000();
435 UPerfFunction
* DateFmt100000();
436 UPerfFunction
* BreakItWord250();
437 UPerfFunction
* BreakItWord10000();
438 UPerfFunction
* BreakItChar250();
439 UPerfFunction
* BreakItChar10000();
440 UPerfFunction
* NumFmt10000();
441 UPerfFunction
* NumFmt100000();
442 UPerfFunction
* Collation10000();
443 UPerfFunction
* Collation100000();
446 #endif // DateFmtPerf