1 /********************************************************************
3 * Copyright (C) 2008-2009 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
){
18 #ifdef TEST_BOYER_MOORE_SEARCH
25 if(status
== U_ILLEGAL_ARGUMENT_ERROR
|| line_mode
){
26 fprintf(stderr
,gUsageString
, "strsrchperf");
30 src
= getBuffer(srcLen
, status
);
33 /* Get a word to find. Do this by selecting a random word with a word breakiterator. */
34 UBreakIterator
* brk
= ubrk_open(UBRK_WORD
, locale
, src
, srcLen
, &status
);
35 if(U_FAILURE(status
)){
36 fprintf(stderr
, "FAILED to create pattern for searching. Error: %s\n", u_errorName(status
));
39 start
= ubrk_preceding(brk
, 1000);
40 end
= ubrk_following(brk
, start
);
41 pttrnLen
= end
- start
;
42 UChar
* temp
= (UChar
*)malloc(sizeof(UChar
)*(pttrnLen
));
43 for (int i
= 0; i
< pttrnLen
; i
++) {
44 temp
[i
] = src
[start
++];
46 pttrn
= temp
; /* store word in pttrn */
49 /* The first line of the file contains the pattern */
52 for(end
= start
; ; end
+= 1) {
55 if (ch
== 0x000A || ch
== 0x000D || ch
== 0x2028) {
60 pttrnLen
= end
- start
;
61 UChar
* temp
= (UChar
*)malloc(sizeof(UChar
)*(pttrnLen
));
62 for (int i
= 0; i
< pttrnLen
; i
++) {
63 temp
[i
] = src
[start
++];
65 pttrn
= temp
; /* store word in pttrn */
68 #ifdef TEST_BOYER_MOORE_SEARCH
69 UnicodeString
patternString(pttrn
, pttrnLen
);
70 UCollator
*coll
= ucol_open(locale
, &status
);
71 CollData
*data
= CollData::open(coll
, status
);
73 targetString
= new UnicodeString(src
, srcLen
);
74 bms
= new BoyerMooreSearch(data
, patternString
, targetString
, status
);
76 /* Create the StringSearch object to be use in performance test. */
77 srch
= usearch_open(pttrn
, pttrnLen
, src
, srcLen
, locale
, NULL
, &status
);
80 if(U_FAILURE(status
)){
81 fprintf(stderr
, "FAILED to create UPerfTest object. Error: %s\n", u_errorName(status
));
87 StringSearchPerformanceTest::~StringSearchPerformanceTest() {
88 CollData
*data
= bms
->getData();
89 UCollator
*coll
= data
->getCollator();
93 CollData::close(data
);
100 #ifndef TEST_BOYER_MOORE_SEARCH
107 UPerfFunction
* StringSearchPerformanceTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
) {
109 TESTCASE(0,Test_ICU_Forward_Search
);
110 TESTCASE(1,Test_ICU_Backward_Search
);
119 UPerfFunction
* StringSearchPerformanceTest::Test_ICU_Forward_Search(){
120 #ifdef TEST_BOYER_MOORE_SEARCH
121 StringSearchPerfFunction
*func
= new StringSearchPerfFunction(ICUForwardSearch
, bms
, src
, srcLen
, pttrn
, pttrnLen
);
123 StringSearchPerfFunction
* func
= new StringSearchPerfFunction(ICUForwardSearch
, srch
, src
, srcLen
, pttrn
, pttrnLen
);
128 UPerfFunction
* StringSearchPerformanceTest::Test_ICU_Backward_Search(){
129 #ifdef TEST_BOYER_MOORE_SEARCH
130 StringSearchPerfFunction
*func
= new StringSearchPerfFunction(ICUBackwardSearch
, bms
, src
, srcLen
, pttrn
, pttrnLen
);
132 StringSearchPerfFunction
* func
= new StringSearchPerfFunction(ICUBackwardSearch
, srch
, src
, srcLen
, pttrn
, pttrnLen
);
137 int main (int argc
, const char* argv
[]) {
138 UErrorCode status
= U_ZERO_ERROR
;
139 StringSearchPerformanceTest
test(argc
, argv
, status
);
140 if(U_FAILURE(status
)){
143 if(test
.run()==FALSE
){
144 fprintf(stderr
,"FAILED: Tests could not be run please check the arguments.\n");