]>
Commit | Line | Data |
---|---|---|
46f4442e A |
1 | /******************************************************************** |
2 | * COPYRIGHT: | |
3 | * Copyright (C) 2008 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 | ||
14 | StringSearchPerformanceTest::StringSearchPerformanceTest(int32_t argc, const char *argv[], UErrorCode &status) | |
15 | :UPerfTest(argc,argv,status){ | |
16 | int32_t start, end; | |
17 | srch = NULL; | |
18 | pttrn = NULL; | |
19 | if(status== U_ILLEGAL_ARGUMENT_ERROR || line_mode){ | |
20 | fprintf(stderr,gUsageString, "strsrchperf"); | |
21 | return; | |
22 | } | |
23 | /* Get the Text */ | |
24 | src = getBuffer(srcLen, status); | |
25 | ||
26 | /* Get a word to find. Do this by selecting a random word with a word breakiterator. */ | |
27 | UBreakIterator* brk = ubrk_open(UBRK_WORD, locale, src, srcLen, &status); | |
28 | if(U_FAILURE(status)){ | |
29 | fprintf(stderr, "FAILED to create pattern for searching. Error: %s\n", u_errorName(status)); | |
30 | return; | |
31 | } | |
32 | start = ubrk_preceding(brk, 1000); | |
33 | end = ubrk_following(brk, start); | |
34 | pttrnLen = end - start; | |
35 | UChar* temp = (UChar*)malloc(sizeof(UChar)*(pttrnLen)); | |
36 | for (int i = 0; i < pttrnLen; i++) { | |
37 | temp[i] = src[start++]; | |
38 | } | |
39 | pttrn = temp; /* store word in pttrn */ | |
40 | ubrk_close(brk); | |
41 | ||
42 | /* Create the StringSearch object to be use in performance test. */ | |
43 | srch = usearch_open(pttrn, pttrnLen, src, srcLen, locale, NULL, &status); | |
44 | if(U_FAILURE(status)){ | |
45 | fprintf(stderr, "FAILED to create UPerfTest object. Error: %s\n", u_errorName(status)); | |
46 | return; | |
47 | } | |
48 | ||
49 | } | |
50 | ||
51 | StringSearchPerformanceTest::~StringSearchPerformanceTest() { | |
52 | if (pttrn != NULL) { | |
53 | free(pttrn); | |
54 | } | |
55 | if (srch != NULL) { | |
56 | usearch_close(srch); | |
57 | } | |
58 | } | |
59 | ||
60 | UPerfFunction* StringSearchPerformanceTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char *par) { | |
61 | switch (index) { | |
62 | TESTCASE(0,Test_ICU_Forward_Search); | |
63 | TESTCASE(1,Test_ICU_Backward_Search); | |
64 | ||
65 | default: | |
66 | name = ""; | |
67 | return NULL; | |
68 | } | |
69 | return NULL; | |
70 | } | |
71 | ||
72 | UPerfFunction* StringSearchPerformanceTest::Test_ICU_Forward_Search(){ | |
73 | StringSearchPerfFunction* func = new StringSearchPerfFunction(ICUForwardSearch, srch, src, srcLen, pttrn, pttrnLen); | |
74 | return func; | |
75 | } | |
76 | ||
77 | UPerfFunction* StringSearchPerformanceTest::Test_ICU_Backward_Search(){ | |
78 | StringSearchPerfFunction* func = new StringSearchPerfFunction(ICUBackwardSearch, srch, src, srcLen, pttrn, pttrnLen); | |
79 | return func; | |
80 | } | |
81 | ||
82 | int main (int argc, const char* argv[]) { | |
83 | UErrorCode status = U_ZERO_ERROR; | |
84 | StringSearchPerformanceTest test(argc, argv, status); | |
85 | if(U_FAILURE(status)){ | |
86 | return status; | |
87 | } | |
88 | if(test.run()==FALSE){ | |
89 | fprintf(stderr,"FAILED: Tests could not be run please check the arguments.\n"); | |
90 | return -1; | |
91 | } | |
92 | return 0; | |
93 | } |