1 /********************************************************************
3 * Copyright (C) 2008-2009 IBM, Inc. All Rights Reserved.
5 ********************************************************************/
9 #include "unicode/ubrk.h"
10 #include "unicode/usearch.h"
11 #include "unicode/colldata.h"
12 #include "unicode/bmsearch.h"
13 #include "unicode/uperf.h"
17 #define TEST_BOYER_MOORE_SEARCH
19 #ifdef TEST_BOYER_MOORE_SEARCH
20 typedef void (*StrSrchFn
) (BoyerMooreSearch
* bms
, const UChar
*src
, int32_t srcLen
, const UChar
*pttrn
, int32_t pttrnLen
, UErrorCode
*status
);
22 typedef void (*StrSrchFn
)(UStringSearch
* srch
, const UChar
* src
,int32_t srcLen
, const UChar
* pttrn
, int32_t pttrnLen
, UErrorCode
* status
);
25 class StringSearchPerfFunction
: public UPerfFunction
{
32 #ifdef TEST_BOYER_MOORE_SEARCH
33 BoyerMooreSearch
*bms
;
39 virtual void call(UErrorCode
* status
) {
40 #ifdef TEST_BOYER_MOORE_SEARCH
41 (*fn
)(bms
, src
, srcLen
, pttrn
, pttrnLen
, status
);
43 (*fn
)(srch
, src
, srcLen
, pttrn
, pttrnLen
, status
);
47 virtual long getOperationsPerIteration() {
49 return (long)(srcLen
/pttrnLen
);
55 #ifdef TEST_BOYER_MOORE_SEARCH
56 StringSearchPerfFunction(StrSrchFn func
, BoyerMooreSearch
*search
, const UChar
*source
, int32_t sourceLen
, const UChar
*pattern
, int32_t patternLen
) {
61 pttrnLen
= patternLen
;
65 StringSearchPerfFunction(StrSrchFn func
, UStringSearch
* search
, const UChar
* source
,int32_t sourceLen
, const UChar
* pattern
, int32_t patternLen
) {
70 pttrnLen
= patternLen
;
76 class StringSearchPerformanceTest
: public UPerfTest
{
82 #ifdef TEST_BOYER_MOORE_SEARCH
83 UnicodeString
*targetString
;
84 BoyerMooreSearch
*bms
;
90 StringSearchPerformanceTest(int32_t argc
, const char *argv
[], UErrorCode
&status
);
91 ~StringSearchPerformanceTest();
92 virtual UPerfFunction
* runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= NULL
);
94 UPerfFunction
* Test_ICU_Forward_Search();
96 UPerfFunction
* Test_ICU_Backward_Search();
100 #ifdef TEST_BOYER_MOORE_SEARCH
101 void ICUForwardSearch(BoyerMooreSearch
*bms
, const UChar
*source
, int32_t sourceLen
, const UChar
*pattern
, int32_t patternLen
, UErrorCode
* /*status*/) {
102 int32_t offset
= 0, start
= -1, end
= -1;
104 while (bms
->search(offset
, start
, end
)) {
109 void ICUBackwardSearch(BoyerMooreSearch
*bms
, const UChar
*source
, int32_t sourceLen
, const UChar
*pattern
, int32_t patternLen
, UErrorCode
* /*status*/) {
110 int32_t offset
= 0, start
= -1, end
= -1;
112 /* NOTE: No Boyer-Moore backward search yet... */
113 while (bms
->search(offset
, start
, end
)) {
118 void ICUForwardSearch(UStringSearch
*srch
, const UChar
* source
, int32_t sourceLen
, const UChar
* pattern
, int32_t patternLen
, UErrorCode
* status
) {
121 match
= usearch_first(srch
, status
);
122 while (match
!= USEARCH_DONE
) {
123 match
= usearch_next(srch
, status
);
127 void ICUBackwardSearch(UStringSearch
*srch
, const UChar
* source
, int32_t sourceLen
, const UChar
* pattern
, int32_t patternLen
, UErrorCode
* status
) {
130 match
= usearch_last(srch
, status
);
131 while (match
!= USEARCH_DONE
) {
132 match
= usearch_previous(srch
, status
);
137 #endif /* _STRSRCHPERF_H */