Home | History | Annotate | Download | only in strsrchperf
      1 /********************************************************************
      2  * COPYRIGHT:
      3  * Copyright (C) 2008-2012 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 #if 0
     27     /* Get a word to find. Do this by selecting a random word with a word breakiterator. */
     28     UBreakIterator* brk = ubrk_open(UBRK_WORD, locale, src, srcLen, &status);
     29     if(U_FAILURE(status)){
     30         fprintf(stderr, "FAILED to create pattern for searching. Error: %s\n", u_errorName(status));
     31         return;
     32     }
     33     start = ubrk_preceding(brk, 1000);
     34     end = ubrk_following(brk, start);
     35     pttrnLen = end - start;
     36     UChar* temp = (UChar*)malloc(sizeof(UChar)*(pttrnLen));
     37     for (int i = 0; i < pttrnLen; i++) {
     38         temp[i] = src[start++];
     39     }
     40     pttrn = temp; /* store word in pttrn */
     41     ubrk_close(brk);
     42 #else
     43     /* The first line of the file contains the pattern */
     44     start = 0;
     45 
     46     for(end = start; ; end += 1) {
     47         UChar ch = src[end];
     48 
     49         if (ch == 0x000A || ch == 0x000D || ch == 0x2028) {
     50             break;
     51         }
     52     }
     53 
     54     pttrnLen = end - start;
     55     UChar* temp = (UChar*)malloc(sizeof(UChar)*(pttrnLen));
     56     for (int i = 0; i < pttrnLen; i++) {
     57         temp[i] = src[start++];
     58     }
     59     pttrn = temp; /* store word in pttrn */
     60 #endif
     61 
     62     /* Create the StringSearch object to be use in performance test. */
     63     srch = usearch_open(pttrn, pttrnLen, src, srcLen, locale, NULL, &status);
     64 
     65     if(U_FAILURE(status)){
     66         fprintf(stderr, "FAILED to create UPerfTest object. Error: %s\n", u_errorName(status));
     67         return;
     68     }
     69 
     70 }
     71 
     72 StringSearchPerformanceTest::~StringSearchPerformanceTest() {
     73     if (pttrn != NULL) {
     74         free(pttrn);
     75     }
     76     if (srch != NULL) {
     77         usearch_close(srch);
     78     }
     79 }
     80 
     81 UPerfFunction* StringSearchPerformanceTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char *par) {
     82     switch (index) {
     83         TESTCASE(0,Test_ICU_Forward_Search);
     84         TESTCASE(1,Test_ICU_Backward_Search);
     85 
     86         default:
     87             name = "";
     88             return NULL;
     89     }
     90     return NULL;
     91 }
     92 
     93 UPerfFunction* StringSearchPerformanceTest::Test_ICU_Forward_Search(){
     94     StringSearchPerfFunction* func = new StringSearchPerfFunction(ICUForwardSearch, srch, src, srcLen, pttrn, pttrnLen);
     95     return func;
     96 }
     97 
     98 UPerfFunction* StringSearchPerformanceTest::Test_ICU_Backward_Search(){
     99     StringSearchPerfFunction* func = new StringSearchPerfFunction(ICUBackwardSearch, srch, src, srcLen, pttrn, pttrnLen);
    100     return func;
    101 }
    102 
    103 int main (int argc, const char* argv[]) {
    104     UErrorCode status = U_ZERO_ERROR;
    105     StringSearchPerformanceTest test(argc, argv, status);
    106     if(U_FAILURE(status)){
    107         return status;
    108     }
    109     if(test.run()==FALSE){
    110         fprintf(stderr,"FAILED: Tests could not be run please check the arguments.\n");
    111         return -1;
    112     }
    113     return 0;
    114 }
    115