summaryrefslogtreecommitdiff
path: root/Build/source/libs/icu/icu-4.6/test/perf/strsrchperf/strsrchperf.h
blob: 6f2281c5855e74f3c1031294f6d3579e629cab79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/********************************************************************
 * COPYRIGHT:
 * Copyright (C) 2008-2009 IBM, Inc.   All Rights Reserved.
 *
 ********************************************************************/
#ifndef _STRSRCHPERF_H
#define _STRSRCHPERF_H

#include "unicode/ubrk.h"
#include "unicode/usearch.h"
#include "unicode/colldata.h"
#include "unicode/bmsearch.h"
#include "unicode/uperf.h"
#include <stdlib.h>
#include <stdio.h>

#define TEST_BOYER_MOORE_SEARCH

#ifdef TEST_BOYER_MOORE_SEARCH
typedef void (*StrSrchFn) (BoyerMooreSearch * bms, const UChar *src, int32_t srcLen, const UChar *pttrn, int32_t pttrnLen, UErrorCode *status);
#else
typedef void (*StrSrchFn)(UStringSearch* srch, const UChar* src,int32_t srcLen, const UChar* pttrn, int32_t pttrnLen, UErrorCode* status);
#endif

class StringSearchPerfFunction : public UPerfFunction {
private:
    StrSrchFn fn;
    const UChar* src;
    int32_t srcLen;
    const UChar* pttrn;
    int32_t pttrnLen;
#ifdef TEST_BOYER_MOORE_SEARCH
    BoyerMooreSearch *bms;
#else
    UStringSearch* srch;
#endif
    
public:
    virtual void call(UErrorCode* status) {
#ifdef TEST_BOYER_MOORE_SEARCH
        (*fn)(bms, src, srcLen, pttrn, pttrnLen, status);
#else
        (*fn)(srch, src, srcLen, pttrn, pttrnLen, status);
#endif
    }
    
    virtual long getOperationsPerIteration() {
#if 0
        return (long)(srcLen/pttrnLen);
#else
        return (long) srcLen;
#endif
    }
    
#ifdef TEST_BOYER_MOORE_SEARCH
    StringSearchPerfFunction(StrSrchFn func, BoyerMooreSearch *search, const UChar *source, int32_t sourceLen, const UChar *pattern, int32_t patternLen) {
        fn       = func;
        src      = source;
        srcLen   = sourceLen;
        pttrn    = pattern;
        pttrnLen = patternLen;
        bms      = search;
    }
#else
    StringSearchPerfFunction(StrSrchFn func, UStringSearch* search, const UChar* source,int32_t sourceLen, const UChar* pattern, int32_t patternLen) {
        fn = func;
        src = source;
        srcLen = sourceLen;
        pttrn = pattern;
        pttrnLen = patternLen;
        srch = search;
    }
#endif
};

class StringSearchPerformanceTest : public UPerfTest {
private:
    const UChar* src;
    int32_t srcLen;
    UChar* pttrn;
    int32_t pttrnLen;
#ifdef TEST_BOYER_MOORE_SEARCH
    UnicodeString *targetString;
    BoyerMooreSearch *bms;
#else
    UStringSearch* srch;
#endif
    
public:
    StringSearchPerformanceTest(int32_t argc, const char *argv[], UErrorCode &status);
    ~StringSearchPerformanceTest();
    virtual UPerfFunction* runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = NULL);
    
    UPerfFunction* Test_ICU_Forward_Search();

    UPerfFunction* Test_ICU_Backward_Search();
};


#ifdef TEST_BOYER_MOORE_SEARCH
void ICUForwardSearch(BoyerMooreSearch *bms, const UChar *source, int32_t sourceLen, const UChar *pattern, int32_t patternLen, UErrorCode * /*status*/) { 
    int32_t offset = 0, start = -1, end = -1;

    while (bms->search(offset, start, end)) {
        offset = end;
    }
}

void ICUBackwardSearch(BoyerMooreSearch *bms, const UChar *source, int32_t sourceLen, const UChar *pattern, int32_t patternLen, UErrorCode * /*status*/) { 
    int32_t offset = 0, start = -1, end = -1;

    /* NOTE: No Boyer-Moore backward search yet... */
    while (bms->search(offset, start, end)) {
        offset = end;
    }
}
#else
void ICUForwardSearch(UStringSearch *srch, const UChar* source, int32_t sourceLen, const UChar* pattern, int32_t patternLen, UErrorCode* status) {
    int32_t match;
    
    match = usearch_first(srch, status);
    while (match != USEARCH_DONE) {
        match = usearch_next(srch, status);
    }
}

void ICUBackwardSearch(UStringSearch *srch, const UChar* source, int32_t sourceLen, const UChar* pattern, int32_t patternLen, UErrorCode* status) {
    int32_t match;
    
    match = usearch_last(srch, status);
    while (match != USEARCH_DONE) {
        match = usearch_previous(srch, status);
    }
}
#endif

#endif /* _STRSRCHPERF_H */