1 /*
2 **********************************************************************
3 * Copyright (c) 2002-2011, International Business Machines
4 * Corporation and others.  All Rights Reserved.
5 **********************************************************************
6 **********************************************************************
7 */
8 #ifndef _UBRKPERF_H
9 #define _UBRKPERF_H
10 
11 #include "unicode/uperf.h"
12 
13 #include <unicode/brkiter.h>
14 
15 class ICUBreakFunction : public UPerfFunction {
16 protected:
17   BreakIterator *m_brkIt_;
18   const UChar *m_file_;
19   int32_t m_fileLen_;
20   int32_t m_noBreaks_;
21   UErrorCode m_status_;
22 public:
ICUBreakFunction(const char * locale,const char * mode,const UChar * file,int32_t file_len)23   ICUBreakFunction(const char *locale, const char *mode, const UChar *file, int32_t file_len) :
24       m_brkIt_(NULL),
25       m_file_(file),
26       m_fileLen_(file_len),
27       m_noBreaks_(-1),
28       m_status_(U_ZERO_ERROR)
29   {
30     switch(mode[0]) {
31     case 'c' :
32       m_brkIt_ = BreakIterator::createCharacterInstance(locale, m_status_);
33       break;
34     case 'w' :
35       m_brkIt_ = BreakIterator::createWordInstance(locale, m_status_);
36       break;
37     case 'l' :
38       m_brkIt_ = BreakIterator::createLineInstance(locale, m_status_);
39       break;
40     case 's' :
41       m_brkIt_ = BreakIterator::createSentenceInstance(locale, m_status_);
42       break;
43     default:
44       // should not happen as we already check for this in the caller
45       m_status_ = U_ILLEGAL_ARGUMENT_ERROR;
46       break;
47     }
48   }
49 
~ICUBreakFunction()50   ~ICUBreakFunction() {  delete m_brkIt_; }
51   virtual void call(UErrorCode *status) = 0;
getOperationsPerIteration()52   virtual long getOperationsPerIteration() { return m_fileLen_; }
getEventsPerIteration()53   virtual long getEventsPerIteration() { return m_noBreaks_; }
getStatus()54   virtual UErrorCode getStatus() { return m_status_; }
55 };
56 
57 class ICUIsBound : public ICUBreakFunction {
58 public:
ICUIsBound(const char * locale,const char * mode,const UChar * file,int32_t file_len)59   ICUIsBound(const char *locale, const char *mode, const UChar *file, int32_t file_len) :
60       ICUBreakFunction(locale, mode, file, file_len)
61   {
62     m_noBreaks_ = 0;
63     m_brkIt_->setText(UnicodeString(m_file_, m_fileLen_));
64     m_brkIt_->first();
65     int32_t j = 0;
66     for(j = 0; j < m_fileLen_; j++) {
67       if(m_brkIt_->isBoundary(j)) {
68         m_noBreaks_++;
69       }
70     }
71   }
call(UErrorCode * status)72   virtual void call(UErrorCode *status)
73   {
74     m_noBreaks_ = 0;
75     int32_t j = 0;
76     for(j = 0; j < m_fileLen_; j++) {
77       if(m_brkIt_->isBoundary(j)) {
78         m_noBreaks_++;
79       }
80     }
81   }
82 };
83 
84 class ICUForward : public ICUBreakFunction {
85 public:
ICUForward(const char * locale,const char * mode,const UChar * file,int32_t file_len)86   ICUForward(const char *locale, const char *mode, const UChar *file, int32_t file_len) :
87       ICUBreakFunction(locale, mode, file, file_len)
88   {
89     m_noBreaks_ = 0;
90     m_brkIt_->setText(UnicodeString(m_file_, m_fileLen_));
91     m_brkIt_->first();
92     while(m_brkIt_->next() != BreakIterator::DONE) {
93       m_noBreaks_++;
94     }
95   }
call(UErrorCode * status)96   virtual void call(UErrorCode *status)
97   {
98     m_noBreaks_ = 0;
99     m_brkIt_->first();
100     while(m_brkIt_->next() != BreakIterator::DONE) {
101       m_noBreaks_++;
102     }
103   }
104 };
105 
106 class DarwinBreakFunction : public UPerfFunction {
107 public:
call(UErrorCode * status)108   virtual void call(UErrorCode *status) {};
109 };
110 
111 class BreakIteratorPerformanceTest : public UPerfTest {
112 private:
113   const char* m_mode_;
114   const UChar* m_file_;
115   int32_t m_fileLen_;
116 
117 public:
118   BreakIteratorPerformanceTest(int32_t argc, const char* argv[], UErrorCode& status);
119   ~BreakIteratorPerformanceTest();
120 
121   virtual UPerfFunction* runIndexedTest(int32_t index, UBool exec,
122     const char* &name, char* par = NULL);
123 
124   UPerfFunction* TestICUForward();
125   UPerfFunction* TestICUIsBound();
126 
127   UPerfFunction* TestDarwinForward();
128   UPerfFunction* TestDarwinIsBound();
129 
130 };
131 
132 #endif // UBRKPERF_H
133