]>
Commit | Line | Data |
---|---|---|
46f4442e A |
1 | /******************************************************************** |
2 | * COPYRIGHT: | |
729e4ab9 | 3 | * Copyright (C) 2008-2009 IBM, Inc. All Rights Reserved. |
46f4442e A |
4 | * |
5 | ********************************************************************/ | |
6 | /** | |
7 | * This program tests string search performance. | |
8 | * APIs tested: | |
9 | * ICU4C | |
10 | */ | |
11 | ||
12 | #include "strsrchperf.h" | |
13 | ||
14 | StringSearchPerformanceTest::StringSearchPerformanceTest(int32_t argc, const char *argv[], UErrorCode &status) | |
15 | :UPerfTest(argc,argv,status){ | |
16 | int32_t start, end; | |
729e4ab9 A |
17 | |
18 | #ifdef TEST_BOYER_MOORE_SEARCH | |
19 | bms = NULL; | |
20 | #else | |
46f4442e | 21 | srch = NULL; |
729e4ab9 A |
22 | #endif |
23 | ||
46f4442e A |
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); | |
729e4ab9 A |
31 | |
32 | #if 0 | |
46f4442e A |
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); | |
729e4ab9 A |
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 | |
46f4442e | 67 | |
729e4ab9 A |
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 | |
46f4442e A |
76 | /* Create the StringSearch object to be use in performance test. */ |
77 | srch = usearch_open(pttrn, pttrnLen, src, srcLen, locale, NULL, &status); | |
729e4ab9 A |
78 | #endif |
79 | ||
46f4442e A |
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 | ||
87 | StringSearchPerformanceTest::~StringSearchPerformanceTest() { | |
729e4ab9 A |
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 | ||
46f4442e A |
96 | if (pttrn != NULL) { |
97 | free(pttrn); | |
98 | } | |
729e4ab9 A |
99 | |
100 | #ifndef TEST_BOYER_MOORE_SEARCH | |
46f4442e A |
101 | if (srch != NULL) { |
102 | usearch_close(srch); | |
103 | } | |
729e4ab9 | 104 | #endif |
46f4442e A |
105 | } |
106 | ||
107 | UPerfFunction* 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 | ||
119 | UPerfFunction* StringSearchPerformanceTest::Test_ICU_Forward_Search(){ | |
729e4ab9 A |
120 | #ifdef TEST_BOYER_MOORE_SEARCH |
121 | StringSearchPerfFunction *func = new StringSearchPerfFunction(ICUForwardSearch, bms, src, srcLen, pttrn, pttrnLen); | |
122 | #else | |
46f4442e | 123 | StringSearchPerfFunction* func = new StringSearchPerfFunction(ICUForwardSearch, srch, src, srcLen, pttrn, pttrnLen); |
729e4ab9 | 124 | #endif |
46f4442e A |
125 | return func; |
126 | } | |
127 | ||
128 | UPerfFunction* StringSearchPerformanceTest::Test_ICU_Backward_Search(){ | |
729e4ab9 A |
129 | #ifdef TEST_BOYER_MOORE_SEARCH |
130 | StringSearchPerfFunction *func = new StringSearchPerfFunction(ICUBackwardSearch, bms, src, srcLen, pttrn, pttrnLen); | |
131 | #else | |
46f4442e | 132 | StringSearchPerfFunction* func = new StringSearchPerfFunction(ICUBackwardSearch, srch, src, srcLen, pttrn, pttrnLen); |
729e4ab9 | 133 | #endif |
46f4442e A |
134 | return func; |
135 | } | |
136 | ||
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)){ | |
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 | } |