1 /********************************************************************
3 * Copyright (C) 2008-2012 IBM, Inc. All Rights Reserved.
5 ********************************************************************/
7 * This program tests string search performance.
12 #include "strsrchperf.h"
14 StringSearchPerformanceTest::StringSearchPerformanceTest(int32_t argc
, const char *argv
[], UErrorCode
&status
)
15 :UPerfTest(argc
,argv
,status
){
19 if(status
== U_ILLEGAL_ARGUMENT_ERROR
|| line_mode
){
20 fprintf(stderr
,gUsageString
, "strsrchperf");
24 src
= getBuffer(srcLen
, status
);
27 /* Get a word to find. Do this by selecting a random word with a word breakiterator. */
28 UBreakIterator
* brk
= ubrk_open(UBRK_WORD
, locale
, src
, srcLen
, &status
);
29 if(U_FAILURE(status
)){
30 fprintf(stderr
, "FAILED to create pattern for searching. Error: %s\n", u_errorName(status
));
33 start
= ubrk_preceding(brk
, 1000);
34 end
= ubrk_following(brk
, start
);
35 pttrnLen
= end
- start
;
36 UChar
* temp
= (UChar
*)malloc(sizeof(UChar
)*(pttrnLen
));
37 for (int i
= 0; i
< pttrnLen
; i
++) {
38 temp
[i
] = src
[start
++];
40 pttrn
= temp
; /* store word in pttrn */
43 /* The first line of the file contains the pattern */
46 for(end
= start
; ; end
+= 1) {
49 if (ch
== 0x000A || ch
== 0x000D || ch
== 0x2028) {
54 pttrnLen
= end
- start
;
55 UChar
* temp
= (UChar
*)malloc(sizeof(UChar
)*(pttrnLen
));
56 for (int i
= 0; i
< pttrnLen
; i
++) {
57 temp
[i
] = src
[start
++];
59 pttrn
= temp
; /* store word in pttrn */
62 /* Create the StringSearch object to be use in performance test. */
63 srch
= usearch_open(pttrn
, pttrnLen
, src
, srcLen
, locale
, NULL
, &status
);
65 if(U_FAILURE(status
)){
66 fprintf(stderr
, "FAILED to create UPerfTest object. Error: %s\n", u_errorName(status
));
72 StringSearchPerformanceTest::~StringSearchPerformanceTest() {
81 UPerfFunction
* StringSearchPerformanceTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
) {
83 TESTCASE(0,Test_ICU_Forward_Search
);
84 TESTCASE(1,Test_ICU_Backward_Search
);
93 UPerfFunction
* StringSearchPerformanceTest::Test_ICU_Forward_Search(){
94 StringSearchPerfFunction
* func
= new StringSearchPerfFunction(ICUForwardSearch
, srch
, src
, srcLen
, pttrn
, pttrnLen
);
98 UPerfFunction
* StringSearchPerformanceTest::Test_ICU_Backward_Search(){
99 StringSearchPerfFunction
* func
= new StringSearchPerfFunction(ICUBackwardSearch
, srch
, src
, srcLen
, pttrn
, pttrnLen
);
103 int main (int argc
, const char* argv
[]) {
104 UErrorCode status
= U_ZERO_ERROR
;
105 StringSearchPerformanceTest
test(argc
, argv
, status
);
106 if(U_FAILURE(status
)){
109 if(test
.run()==FALSE
){
110 fprintf(stderr
,"FAILED: Tests could not be run please check the arguments.\n");