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