1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (C) 2008-2009 IBM, Inc.   All Rights Reserved.
4 *
5 ********************************************************************/
6/**
7 * This program tests string search performance.
8 * APIs tested:
9 * ICU4C
10 */
11
12#include "strsrchperf.h"
13
14StringSearchPerformanceTest::StringSearchPerformanceTest(int32_t argc, const char *argv[], UErrorCode &status)
15:UPerfTest(argc,argv,status){
16    int32_t start, end;
17
18#ifdef TEST_BOYER_MOORE_SEARCH
19    bms = NULL;
20#else
21    srch = NULL;
22#endif
23
24    pttrn = NULL;
25    if(status== U_ILLEGAL_ARGUMENT_ERROR || line_mode){
26       fprintf(stderr,gUsageString, "strsrchperf");
27       return;
28    }
29    /* Get the Text */
30    src = getBuffer(srcLen, status);
31
32#if 0
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));
37        return;
38    }
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++];
45    }
46    pttrn = temp; /* store word in pttrn */
47    ubrk_close(brk);
48#else
49    /* The first line of the file contains the pattern */
50    start = 0;
51
52    for(end = start; ; end += 1) {
53        UChar ch = src[end];
54
55        if (ch == 0x000A || ch == 0x000D || ch == 0x2028) {
56            break;
57        }
58    }
59
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++];
64    }
65    pttrn = temp; /* store word in pttrn */
66#endif
67
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);
72
73    targetString = new UnicodeString(src, srcLen);
74    bms = new BoyerMooreSearch(data, patternString, targetString, status);
75#else
76    /* Create the StringSearch object to be use in performance test. */
77    srch = usearch_open(pttrn, pttrnLen, src, srcLen, locale, NULL, &status);
78#endif
79
80    if(U_FAILURE(status)){
81        fprintf(stderr, "FAILED to create UPerfTest object. Error: %s\n", u_errorName(status));
82        return;
83    }
84
85}
86
87StringSearchPerformanceTest::~StringSearchPerformanceTest() {
88    CollData *data  = bms->getData();
89    UCollator *coll = data->getCollator();
90
91    delete bms;
92    delete targetString;
93    CollData::close(data);
94    ucol_close(coll);
95
96    if (pttrn != NULL) {
97        free(pttrn);
98    }
99
100#ifndef TEST_BOYER_MOORE_SEARCH
101    if (srch != NULL) {
102        usearch_close(srch);
103    }
104#endif
105}
106
107UPerfFunction* StringSearchPerformanceTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char *par) {
108    switch (index) {
109        TESTCASE(0,Test_ICU_Forward_Search);
110        TESTCASE(1,Test_ICU_Backward_Search);
111
112        default:
113            name = "";
114            return NULL;
115    }
116    return NULL;
117}
118
119UPerfFunction* StringSearchPerformanceTest::Test_ICU_Forward_Search(){
120#ifdef TEST_BOYER_MOORE_SEARCH
121    StringSearchPerfFunction *func = new StringSearchPerfFunction(ICUForwardSearch, bms, src, srcLen, pttrn, pttrnLen);
122#else
123    StringSearchPerfFunction* func = new StringSearchPerfFunction(ICUForwardSearch, srch, src, srcLen, pttrn, pttrnLen);
124#endif
125    return func;
126}
127
128UPerfFunction* StringSearchPerformanceTest::Test_ICU_Backward_Search(){
129#ifdef TEST_BOYER_MOORE_SEARCH
130    StringSearchPerfFunction *func = new StringSearchPerfFunction(ICUBackwardSearch, bms, src, srcLen, pttrn, pttrnLen);
131#else
132    StringSearchPerfFunction* func = new StringSearchPerfFunction(ICUBackwardSearch, srch, src, srcLen, pttrn, pttrnLen);
133#endif
134    return func;
135}
136
137int main (int argc, const char* argv[]) {
138    UErrorCode status = U_ZERO_ERROR;
139    StringSearchPerformanceTest test(argc, argv, status);
140    if(U_FAILURE(status)){
141        return status;
142    }
143    if(test.run()==FALSE){
144        fprintf(stderr,"FAILED: Tests could not be run please check the arguments.\n");
145        return -1;
146    }
147    return 0;
148}
149