1 /************************************************************************
2 * © 2016 and later: Unicode, Inc. and others.
3 * License & terms of use: http://www.unicode.org/copyright.html#License
4 *
5 *************************************************************************
6 ********************************************************************
7 * COPYRIGHT:
8 * Copyright (C) 2008-2012 IBM, Inc. All Rights Reserved.
9 *
10 ********************************************************************/
11 /**
12 * This program tests string search performance.
13 * APIs tested:
14 * ICU4C
15 */
16
17 #include "strsrchperf.h"
18
StringSearchPerformanceTest(int32_t argc,const char * argv[],UErrorCode & status)19 StringSearchPerformanceTest::StringSearchPerformanceTest(int32_t argc, const char *argv[], UErrorCode &status)
20 :UPerfTest(argc,argv,status){
21 int32_t start, end;
22 srch = NULL;
23 pttrn = NULL;
24 if(status== U_ILLEGAL_ARGUMENT_ERROR || line_mode){
25 fprintf(stderr,gUsageString, "strsrchperf");
26 return;
27 }
28 /* Get the Text */
29 src = getBuffer(srcLen, status);
30
31 #if 0
32 /* Get a word to find. Do this by selecting a random word with a word breakiterator. */
33 UBreakIterator* brk = ubrk_open(UBRK_WORD, locale, src, srcLen, &status);
34 if(U_FAILURE(status)){
35 fprintf(stderr, "FAILED to create pattern for searching. Error: %s\n", u_errorName(status));
36 return;
37 }
38 start = ubrk_preceding(brk, 1000);
39 end = ubrk_following(brk, start);
40 pttrnLen = end - start;
41 UChar* temp = (UChar*)malloc(sizeof(UChar)*(pttrnLen));
42 for (int i = 0; i < pttrnLen; i++) {
43 temp[i] = src[start++];
44 }
45 pttrn = temp; /* store word in pttrn */
46 ubrk_close(brk);
47 #else
48 /* The first line of the file contains the pattern */
49 start = 0;
50
51 for(end = start; ; end += 1) {
52 UChar ch = src[end];
53
54 if (ch == 0x000A || ch == 0x000D || ch == 0x2028) {
55 break;
56 }
57 }
58
59 pttrnLen = end - start;
60 UChar* temp = (UChar*)malloc(sizeof(UChar)*(pttrnLen));
61 for (int i = 0; i < pttrnLen; i++) {
62 temp[i] = src[start++];
63 }
64 pttrn = temp; /* store word in pttrn */
65 #endif
66
67 /* Create the StringSearch object to be use in performance test. */
68 srch = usearch_open(pttrn, pttrnLen, src, srcLen, locale, NULL, &status);
69
70 if(U_FAILURE(status)){
71 fprintf(stderr, "FAILED to create UPerfTest object. Error: %s\n", u_errorName(status));
72 return;
73 }
74
75 }
76
~StringSearchPerformanceTest()77 StringSearchPerformanceTest::~StringSearchPerformanceTest() {
78 if (pttrn != NULL) {
79 free(pttrn);
80 }
81 if (srch != NULL) {
82 usearch_close(srch);
83 }
84 }
85
runIndexedTest(int32_t index,UBool exec,const char * & name,char * par)86 UPerfFunction* StringSearchPerformanceTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char *par) {
87 switch (index) {
88 TESTCASE(0,Test_ICU_Forward_Search);
89 TESTCASE(1,Test_ICU_Backward_Search);
90
91 default:
92 name = "";
93 return NULL;
94 }
95 return NULL;
96 }
97
Test_ICU_Forward_Search()98 UPerfFunction* StringSearchPerformanceTest::Test_ICU_Forward_Search(){
99 StringSearchPerfFunction* func = new StringSearchPerfFunction(ICUForwardSearch, srch, src, srcLen, pttrn, pttrnLen);
100 return func;
101 }
102
Test_ICU_Backward_Search()103 UPerfFunction* StringSearchPerformanceTest::Test_ICU_Backward_Search(){
104 StringSearchPerfFunction* func = new StringSearchPerfFunction(ICUBackwardSearch, srch, src, srcLen, pttrn, pttrnLen);
105 return func;
106 }
107
main(int argc,const char * argv[])108 int main (int argc, const char* argv[]) {
109 UErrorCode status = U_ZERO_ERROR;
110 StringSearchPerformanceTest test(argc, argv, status);
111 if(U_FAILURE(status)){
112 return status;
113 }
114 if(test.run()==FALSE){
115 fprintf(stderr,"FAILED: Tests could not be run please check the arguments.\n");
116 return -1;
117 }
118 return 0;
119 }
120