1 /************************************************************************
2 * Copyright (C) 2016 and later: Unicode, Inc. and others.
3 * License & terms of use: http://www.unicode.org/copyright.html#License
5 *************************************************************************
6 ********************************************************************
8 * Copyright (C) 2008-2012 IBM, Inc. All Rights Reserved.
10 ********************************************************************/
12 * This program tests string search performance.
17 #include "strsrchperf.h"
19 StringSearchPerformanceTest::StringSearchPerformanceTest(int32_t argc, const char *argv[], UErrorCode &status)
20 :UPerfTest(argc,argv,status){
24 if(status== U_ILLEGAL_ARGUMENT_ERROR || line_mode){
25 fprintf(stderr,gUsageString, "strsrchperf");
29 src = getBuffer(srcLen, status);
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));
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++];
45 pttrn = temp; /* store word in pttrn */
48 /* The first line of the file contains the pattern */
51 for(end = start; ; end += 1) {
54 if (ch == 0x000A || ch == 0x000D || ch == 0x2028) {
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++];
64 pttrn = temp; /* store word in pttrn */
67 /* Create the StringSearch object to be use in performance test. */
68 srch = usearch_open(pttrn, pttrnLen, src, srcLen, locale, NULL, &status);
70 if(U_FAILURE(status)){
71 fprintf(stderr, "FAILED to create UPerfTest object. Error: %s\n", u_errorName(status));
77 StringSearchPerformanceTest::~StringSearchPerformanceTest() {
86 UPerfFunction* StringSearchPerformanceTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char *par) {
88 TESTCASE(0,Test_ICU_Forward_Search);
89 TESTCASE(1,Test_ICU_Backward_Search);
98 UPerfFunction* StringSearchPerformanceTest::Test_ICU_Forward_Search(){
99 StringSearchPerfFunction* func = new StringSearchPerfFunction(ICUForwardSearch, srch, src, srcLen, pttrn, pttrnLen);
103 UPerfFunction* StringSearchPerformanceTest::Test_ICU_Backward_Search(){
104 StringSearchPerfFunction* func = new StringSearchPerfFunction(ICUBackwardSearch, srch, src, srcLen, pttrn, pttrnLen);
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)){
114 if(test.run()==FALSE){
115 fprintf(stderr,"FAILED: Tests could not be run please check the arguments.\n");