1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (C) 2008-2009 IBM, Inc.   All Rights Reserved.
4 *
5 ********************************************************************/
6#ifndef _STRSRCHPERF_H
7#define _STRSRCHPERF_H
8
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"
14#include <stdlib.h>
15#include <stdio.h>
16
17#define TEST_BOYER_MOORE_SEARCH
18
19#ifdef TEST_BOYER_MOORE_SEARCH
20typedef void (*StrSrchFn) (BoyerMooreSearch * bms, const UChar *src, int32_t srcLen, const UChar *pttrn, int32_t pttrnLen, UErrorCode *status);
21#else
22typedef void (*StrSrchFn)(UStringSearch* srch, const UChar* src,int32_t srcLen, const UChar* pttrn, int32_t pttrnLen, UErrorCode* status);
23#endif
24
25class StringSearchPerfFunction : public UPerfFunction {
26private:
27    StrSrchFn fn;
28    const UChar* src;
29    int32_t srcLen;
30    const UChar* pttrn;
31    int32_t pttrnLen;
32#ifdef TEST_BOYER_MOORE_SEARCH
33    BoyerMooreSearch *bms;
34#else
35    UStringSearch* srch;
36#endif
37
38public:
39    virtual void call(UErrorCode* status) {
40#ifdef TEST_BOYER_MOORE_SEARCH
41        (*fn)(bms, src, srcLen, pttrn, pttrnLen, status);
42#else
43        (*fn)(srch, src, srcLen, pttrn, pttrnLen, status);
44#endif
45    }
46
47    virtual long getOperationsPerIteration() {
48#if 0
49        return (long)(srcLen/pttrnLen);
50#else
51        return (long) srcLen;
52#endif
53    }
54
55#ifdef TEST_BOYER_MOORE_SEARCH
56    StringSearchPerfFunction(StrSrchFn func, BoyerMooreSearch *search, const UChar *source, int32_t sourceLen, const UChar *pattern, int32_t patternLen) {
57        fn       = func;
58        src      = source;
59        srcLen   = sourceLen;
60        pttrn    = pattern;
61        pttrnLen = patternLen;
62        bms      = search;
63    }
64#else
65    StringSearchPerfFunction(StrSrchFn func, UStringSearch* search, const UChar* source,int32_t sourceLen, const UChar* pattern, int32_t patternLen) {
66        fn = func;
67        src = source;
68        srcLen = sourceLen;
69        pttrn = pattern;
70        pttrnLen = patternLen;
71        srch = search;
72    }
73#endif
74};
75
76class StringSearchPerformanceTest : public UPerfTest {
77private:
78    const UChar* src;
79    int32_t srcLen;
80    UChar* pttrn;
81    int32_t pttrnLen;
82#ifdef TEST_BOYER_MOORE_SEARCH
83    UnicodeString *targetString;
84    BoyerMooreSearch *bms;
85#else
86    UStringSearch* srch;
87#endif
88
89public:
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);
93
94    UPerfFunction* Test_ICU_Forward_Search();
95
96    UPerfFunction* Test_ICU_Backward_Search();
97};
98
99
100#ifdef TEST_BOYER_MOORE_SEARCH
101void 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;
103
104    while (bms->search(offset, start, end)) {
105        offset = end;
106    }
107}
108
109void 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;
111
112    /* NOTE: No Boyer-Moore backward search yet... */
113    while (bms->search(offset, start, end)) {
114        offset = end;
115    }
116}
117#else
118void ICUForwardSearch(UStringSearch *srch, const UChar* source, int32_t sourceLen, const UChar* pattern, int32_t patternLen, UErrorCode* status) {
119    int32_t match;
120
121    match = usearch_first(srch, status);
122    while (match != USEARCH_DONE) {
123        match = usearch_next(srch, status);
124    }
125}
126
127void ICUBackwardSearch(UStringSearch *srch, const UChar* source, int32_t sourceLen, const UChar* pattern, int32_t patternLen, UErrorCode* status) {
128    int32_t match;
129
130    match = usearch_last(srch, status);
131    while (match != USEARCH_DONE) {
132        match = usearch_previous(srch, status);
133    }
134}
135#endif
136
137#endif /* _STRSRCHPERF_H */
138