2008-04-17 17:46:37 +00:00
|
|
|
/********************************************************************
|
|
|
|
* COPYRIGHT:
|
2009-01-22 00:24:48 +00:00
|
|
|
* Copyright (C) 2008-2009 IBM, Inc. All Rights Reserved.
|
2008-04-17 17:46:37 +00:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
/**
|
|
|
|
* This program tests string search performance.
|
|
|
|
* APIs tested:
|
|
|
|
* ICU4C
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "strsrchperf.h"
|
|
|
|
|
2008-04-18 19:14:38 +00:00
|
|
|
StringSearchPerformanceTest::StringSearchPerformanceTest(int32_t argc, const char *argv[], UErrorCode &status)
|
|
|
|
:UPerfTest(argc,argv,status){
|
|
|
|
int32_t start, end;
|
2009-01-22 00:24:48 +00:00
|
|
|
|
|
|
|
#ifdef TEST_BOYER_MOORE_SEARCH
|
|
|
|
bms = NULL;
|
|
|
|
#else
|
2008-04-18 19:19:51 +00:00
|
|
|
srch = NULL;
|
2009-01-22 00:24:48 +00:00
|
|
|
#endif
|
|
|
|
|
2008-04-18 19:19:51 +00:00
|
|
|
pttrn = NULL;
|
2008-04-18 19:14:38 +00:00
|
|
|
if(status== U_ILLEGAL_ARGUMENT_ERROR || line_mode){
|
|
|
|
fprintf(stderr,gUsageString, "strsrchperf");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Get the Text */
|
|
|
|
src = getBuffer(srcLen, status);
|
2009-01-22 00:24:48 +00:00
|
|
|
|
|
|
|
#if 0
|
2008-04-18 19:14:38 +00:00
|
|
|
/* Get a word to find. Do this by selecting a random word with a word breakiterator. */
|
|
|
|
UBreakIterator* brk = ubrk_open(UBRK_WORD, locale, src, srcLen, &status);
|
|
|
|
if(U_FAILURE(status)){
|
|
|
|
fprintf(stderr, "FAILED to create pattern for searching. Error: %s\n", u_errorName(status));
|
|
|
|
return;
|
|
|
|
}
|
2008-04-25 15:27:13 +00:00
|
|
|
start = ubrk_preceding(brk, 1000);
|
2008-04-18 19:14:38 +00:00
|
|
|
end = ubrk_following(brk, start);
|
|
|
|
pttrnLen = end - start;
|
|
|
|
UChar* temp = (UChar*)malloc(sizeof(UChar)*(pttrnLen));
|
|
|
|
for (int i = 0; i < pttrnLen; i++) {
|
|
|
|
temp[i] = src[start++];
|
|
|
|
}
|
|
|
|
pttrn = temp; /* store word in pttrn */
|
|
|
|
ubrk_close(brk);
|
2009-01-22 00:24:48 +00:00
|
|
|
#else
|
|
|
|
/* The first line of the file contains the pattern */
|
|
|
|
start = 0;
|
|
|
|
|
|
|
|
for(end = start; ; end += 1) {
|
|
|
|
UChar ch = src[end];
|
|
|
|
|
|
|
|
if (ch == 0x000A || ch == 0x000D || ch == 0x2028) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pttrnLen = end - start;
|
|
|
|
UChar* temp = (UChar*)malloc(sizeof(UChar)*(pttrnLen));
|
|
|
|
for (int i = 0; i < pttrnLen; i++) {
|
|
|
|
temp[i] = src[start++];
|
|
|
|
}
|
|
|
|
pttrn = temp; /* store word in pttrn */
|
|
|
|
#endif
|
2008-04-18 19:14:38 +00:00
|
|
|
|
2009-01-22 00:24:48 +00:00
|
|
|
#ifdef TEST_BOYER_MOORE_SEARCH
|
|
|
|
UnicodeString patternString(pttrn, pttrnLen);
|
|
|
|
UCollator *coll = ucol_open(locale, &status);
|
|
|
|
CollData *data = CollData::open(coll, status);
|
|
|
|
|
|
|
|
targetString = new UnicodeString(src, srcLen);
|
|
|
|
bms = new BoyerMooreSearch(data, patternString, targetString, status);
|
|
|
|
#else
|
2008-04-18 19:14:38 +00:00
|
|
|
/* Create the StringSearch object to be use in performance test. */
|
2008-04-25 15:27:13 +00:00
|
|
|
srch = usearch_open(pttrn, pttrnLen, src, srcLen, locale, NULL, &status);
|
2009-01-22 00:24:48 +00:00
|
|
|
#endif
|
|
|
|
|
2008-04-18 19:14:38 +00:00
|
|
|
if(U_FAILURE(status)){
|
|
|
|
fprintf(stderr, "FAILED to create UPerfTest object. Error: %s\n", u_errorName(status));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
StringSearchPerformanceTest::~StringSearchPerformanceTest() {
|
2009-01-22 00:24:48 +00:00
|
|
|
CollData *data = bms->getData();
|
|
|
|
UCollator *coll = data->getCollator();
|
|
|
|
|
|
|
|
delete bms;
|
|
|
|
delete targetString;
|
|
|
|
CollData::close(data);
|
|
|
|
ucol_close(coll);
|
|
|
|
|
2008-04-18 19:19:51 +00:00
|
|
|
if (pttrn != NULL) {
|
|
|
|
free(pttrn);
|
|
|
|
}
|
2009-01-22 00:24:48 +00:00
|
|
|
|
|
|
|
#ifndef TEST_BOYER_MOORE_SEARCH
|
2008-04-18 19:19:51 +00:00
|
|
|
if (srch != NULL) {
|
|
|
|
usearch_close(srch);
|
|
|
|
}
|
2009-01-22 00:24:48 +00:00
|
|
|
#endif
|
2008-04-18 19:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UPerfFunction* StringSearchPerformanceTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char *par) {
|
|
|
|
switch (index) {
|
|
|
|
TESTCASE(0,Test_ICU_Forward_Search);
|
|
|
|
TESTCASE(1,Test_ICU_Backward_Search);
|
|
|
|
|
|
|
|
default:
|
|
|
|
name = "";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
UPerfFunction* StringSearchPerformanceTest::Test_ICU_Forward_Search(){
|
2009-01-22 00:24:48 +00:00
|
|
|
#ifdef TEST_BOYER_MOORE_SEARCH
|
|
|
|
StringSearchPerfFunction *func = new StringSearchPerfFunction(ICUForwardSearch, bms, src, srcLen, pttrn, pttrnLen);
|
|
|
|
#else
|
2008-04-18 19:14:38 +00:00
|
|
|
StringSearchPerfFunction* func = new StringSearchPerfFunction(ICUForwardSearch, srch, src, srcLen, pttrn, pttrnLen);
|
2009-01-22 00:24:48 +00:00
|
|
|
#endif
|
2008-04-18 19:14:38 +00:00
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
UPerfFunction* StringSearchPerformanceTest::Test_ICU_Backward_Search(){
|
2009-01-22 00:24:48 +00:00
|
|
|
#ifdef TEST_BOYER_MOORE_SEARCH
|
|
|
|
StringSearchPerfFunction *func = new StringSearchPerfFunction(ICUBackwardSearch, bms, src, srcLen, pttrn, pttrnLen);
|
|
|
|
#else
|
2008-04-18 19:14:38 +00:00
|
|
|
StringSearchPerfFunction* func = new StringSearchPerfFunction(ICUBackwardSearch, srch, src, srcLen, pttrn, pttrnLen);
|
2009-01-22 00:24:48 +00:00
|
|
|
#endif
|
2008-04-18 19:14:38 +00:00
|
|
|
return func;
|
|
|
|
}
|
2008-04-17 17:46:37 +00:00
|
|
|
|
2008-04-18 19:14:38 +00:00
|
|
|
int main (int argc, const char* argv[]) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
StringSearchPerformanceTest test(argc, argv, status);
|
|
|
|
if(U_FAILURE(status)){
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
if(test.run()==FALSE){
|
|
|
|
fprintf(stderr,"FAILED: Tests could not be run please check the arguments.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2008-04-17 17:46:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|