Home | History | Annotate | Download | only in normperf
      1 /*
      2 **********************************************************************
      3 * Copyright (c) 2002-2006, International Business Machines
      4 * Corporation and others.  All Rights Reserved.
      5 **********************************************************************
      6 **********************************************************************
      7 */
      8 #ifndef _NORMPERF_H
      9 #define _NORMPERF_H
     10 
     11 #include "unicode/unorm.h"
     12 #include "unicode/ustring.h"
     13 
     14 #include "unicode/uperf.h"
     15 #include <stdlib.h>
     16 
     17 //  Stubs for Windows API functions when building on UNIXes.
     18 //
     19 #if defined(U_WINDOWS)
     20 // do nothing
     21 #else
     22 #define _UNICODE
     23 typedef int DWORD;
     24 inline int FoldStringW(DWORD dwMapFlags, const UChar* lpSrcStr,int cchSrc, UChar* lpDestStr,int cchDest);
     25 #endif
     26 
     27 #define DEST_BUFFER_CAPACITY 6000
     28 typedef int32_t (*NormFn)(const UChar* src,int32_t srcLen, UChar* dest,int32_t dstLen, int32_t options, UErrorCode* status);
     29 typedef int32_t (*QuickCheckFn)(const UChar* src,int32_t srcLen, UNormalizationMode mode, int32_t options, UErrorCode* status);
     30 
     31 class QuickCheckPerfFunction : public UPerfFunction{
     32 private:
     33     ULine* lines;
     34     int32_t numLines;
     35     QuickCheckFn fn;
     36     UNormalizationMode mode;
     37     int32_t retVal;
     38     UBool uselen;
     39     const UChar* src;
     40     int32_t srcLen;
     41     UBool line_mode;
     42     int32_t options;
     43 
     44 public:
     45     virtual void call(UErrorCode* status){
     46         if(line_mode==TRUE){
     47             if(uselen){
     48                 for(int32_t i = 0; i< numLines; i++){
     49                     retVal =  (*fn)(lines[i].name,lines[i].len,mode, options, status);
     50                 }
     51             }else{
     52                 for(int32_t i = 0; i< numLines; i++){
     53                     retVal =  (*fn)(lines[i].name,-1,mode, options, status);
     54                 }
     55             }
     56         }else{
     57             if(uselen){
     58 
     59                 retVal =  (*fn)(src,srcLen,mode, options, status);
     60             }else{
     61                 retVal =  (*fn)(src,-1,mode, options, status);
     62             }
     63         }
     64 
     65     }
     66     virtual long getOperationsPerIteration(){
     67         if(line_mode==TRUE){
     68             int32_t totalChars=0;
     69             for(int32_t i =0; i< numLines; i++){
     70                 totalChars+= lines[i].len;
     71             }
     72             return totalChars;
     73         }else{
     74             return srcLen;
     75         }
     76     }
     77     QuickCheckPerfFunction(QuickCheckFn func, ULine* srcLines,int32_t srcNumLines, UNormalizationMode _mode, int32_t opts, UBool _uselen) : options(opts) {
     78         fn = func;
     79         lines = srcLines;
     80         numLines = srcNumLines;
     81         uselen = _uselen;
     82         mode = _mode;
     83         src = NULL;
     84         srcLen = 0;
     85         line_mode = TRUE;
     86     }
     87     QuickCheckPerfFunction(QuickCheckFn func, const UChar* source,int32_t sourceLen, UNormalizationMode _mode, int32_t opts, UBool _uselen) : options(opts) {
     88         fn = func;
     89         lines = NULL;
     90         numLines = 0;
     91         uselen = _uselen;
     92         mode = _mode;
     93         src = source;
     94         srcLen = sourceLen;
     95         line_mode = FALSE;
     96     }
     97 };
     98 
     99 
    100 class NormPerfFunction : public UPerfFunction{
    101 private:
    102     ULine* lines;
    103     int32_t numLines;
    104     UChar dest[DEST_BUFFER_CAPACITY];
    105     UChar* pDest;
    106     int32_t destLen;
    107     NormFn fn;
    108     int32_t retVal;
    109     UBool uselen;
    110     const UChar* src;
    111     int32_t srcLen;
    112     UBool line_mode;
    113     int32_t options;
    114 
    115 public:
    116     virtual void call(UErrorCode* status){
    117         if(line_mode==TRUE){
    118             if(uselen){
    119                 for(int32_t i = 0; i< numLines; i++){
    120                     retVal =  (*fn)(lines[i].name,lines[i].len,pDest,destLen, options, status);
    121                 }
    122             }else{
    123                 for(int32_t i = 0; i< numLines; i++){
    124                     retVal =  (*fn)(lines[i].name,-1,pDest,destLen, options, status);
    125                 }
    126             }
    127         }else{
    128             if(uselen){
    129                 retVal =  (*fn)(src,srcLen,pDest,destLen, options, status);
    130             }else{
    131                 retVal =  (*fn)(src,-1,pDest,destLen, options, status);
    132             }
    133         }
    134     }
    135     virtual long getOperationsPerIteration(){
    136         if(line_mode ==TRUE){
    137             int32_t totalChars=0;
    138             for(int32_t i =0; i< numLines; i++){
    139                 totalChars+= lines[i].len;
    140             }
    141             return totalChars;
    142         }else{
    143             return srcLen;
    144         }
    145     }
    146     NormPerfFunction(NormFn func, int32_t opts, ULine* srcLines,int32_t srcNumLines,UBool _uselen) : options(opts) {
    147         fn = func;
    148         lines = srcLines;
    149         numLines = srcNumLines;
    150         uselen = _uselen;
    151         destLen = DEST_BUFFER_CAPACITY;
    152         pDest = dest;
    153         src = NULL;
    154         srcLen = 0;
    155         line_mode = TRUE;
    156     }
    157     NormPerfFunction(NormFn func, int32_t opts, const UChar* source,int32_t sourceLen,UBool _uselen) : options(opts) {
    158         fn = func;
    159         lines = NULL;
    160         numLines = 0;
    161         uselen = _uselen;
    162         destLen = sourceLen*3;
    163         pDest = (UChar*) malloc(destLen * U_SIZEOF_UCHAR);
    164         src = source;
    165         srcLen = sourceLen;
    166         line_mode = FALSE;
    167     }
    168     ~NormPerfFunction(){
    169         if(dest != pDest){
    170             free(pDest);
    171         }
    172     }
    173 };
    174 
    175 
    176 
    177 class  NormalizerPerformanceTest : public UPerfTest{
    178 private:
    179     ULine* NFDFileLines;
    180     ULine* NFCFileLines;
    181     UChar* NFDBuffer;
    182     UChar* NFCBuffer;
    183     UChar* origBuffer;
    184     int32_t origBufferLen;
    185     int32_t NFDBufferLen;
    186     int32_t NFCBufferLen;
    187     int32_t options;
    188 
    189     void normalizeInput(ULine* dest,const UChar* src ,int32_t srcLen,UNormalizationMode mode, int32_t options);
    190     UChar* normalizeInput(int32_t& len, const UChar* src ,int32_t srcLen,UNormalizationMode mode, int32_t options);
    191 
    192 public:
    193 
    194     NormalizerPerformanceTest(int32_t argc, const char* argv[], UErrorCode& status);
    195     ~NormalizerPerformanceTest();
    196     virtual UPerfFunction* runIndexedTest(int32_t index, UBool exec,const char* &name, char* par = NULL);
    197     /* NFC performance */
    198     UPerfFunction* TestICU_NFC_NFD_Text();
    199     UPerfFunction* TestICU_NFC_NFC_Text();
    200     UPerfFunction* TestICU_NFC_Orig_Text();
    201 
    202     /* NFD performance */
    203     UPerfFunction* TestICU_NFD_NFD_Text();
    204     UPerfFunction* TestICU_NFD_NFC_Text();
    205     UPerfFunction* TestICU_NFD_Orig_Text();
    206 
    207     /* FCD performance */
    208     UPerfFunction* TestICU_FCD_NFD_Text();
    209     UPerfFunction* TestICU_FCD_NFC_Text();
    210     UPerfFunction* TestICU_FCD_Orig_Text();
    211 
    212     /*Win NFC performance */
    213     UPerfFunction* TestWin_NFC_NFD_Text();
    214     UPerfFunction* TestWin_NFC_NFC_Text();
    215     UPerfFunction* TestWin_NFC_Orig_Text();
    216 
    217     /* Win NFD performance */
    218     UPerfFunction* TestWin_NFD_NFD_Text();
    219     UPerfFunction* TestWin_NFD_NFC_Text();
    220     UPerfFunction* TestWin_NFD_Orig_Text();
    221 
    222     /* Quick check performance */
    223     UPerfFunction* TestQC_NFC_NFD_Text();
    224     UPerfFunction* TestQC_NFC_NFC_Text();
    225     UPerfFunction* TestQC_NFC_Orig_Text();
    226 
    227     UPerfFunction* TestQC_NFD_NFD_Text();
    228     UPerfFunction* TestQC_NFD_NFC_Text();
    229     UPerfFunction* TestQC_NFD_Orig_Text();
    230 
    231     UPerfFunction* TestQC_FCD_NFD_Text();
    232     UPerfFunction* TestQC_FCD_NFC_Text();
    233     UPerfFunction* TestQC_FCD_Orig_Text();
    234 
    235     /* IsNormalized performnace */
    236     UPerfFunction* TestIsNormalized_NFC_NFD_Text();
    237     UPerfFunction* TestIsNormalized_NFC_NFC_Text();
    238     UPerfFunction* TestIsNormalized_NFC_Orig_Text();
    239 
    240     UPerfFunction* TestIsNormalized_NFD_NFD_Text();
    241     UPerfFunction* TestIsNormalized_NFD_NFC_Text();
    242     UPerfFunction* TestIsNormalized_NFD_Orig_Text();
    243 
    244     UPerfFunction* TestIsNormalized_FCD_NFD_Text();
    245     UPerfFunction* TestIsNormalized_FCD_NFC_Text();
    246     UPerfFunction* TestIsNormalized_FCD_Orig_Text();
    247 
    248 };
    249 
    250 //---------------------------------------------------------------------------------------
    251 // Platform / ICU version specific proto-types
    252 //---------------------------------------------------------------------------------------
    253 
    254 
    255 #if (U_ICU_VERSION_MAJOR_NUM > 1 ) || ((U_ICU_VERSION_MAJOR_NUM == 1 )&&(U_ICU_VERSION_MINOR_NUM > 8) && (U_ICU_VERSION_PATCHLEVEL_NUM >=1))
    256 
    257 int32_t ICUNormNFD(const UChar* src, int32_t srcLen,UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    258     return unorm_normalize(src,srcLen,UNORM_NFD, options,dest,dstLen,status);
    259 }
    260 
    261 int32_t ICUNormNFC(const UChar* src, int32_t srcLen,UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    262     return unorm_normalize(src,srcLen,UNORM_NFC, options,dest,dstLen,status);
    263 }
    264 
    265 int32_t ICUNormNFKD(const UChar* src, int32_t srcLen,UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    266     return unorm_normalize(src,srcLen,UNORM_NFKD, options,dest,dstLen,status);
    267 }
    268 int32_t ICUNormNFKC(const UChar* src, int32_t srcLen,UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    269     return unorm_normalize(src,srcLen,UNORM_NFKC, options,dest,dstLen,status);
    270 }
    271 
    272 int32_t ICUNormFCD(const UChar* src, int32_t srcLen,UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    273     return unorm_normalize(src,srcLen,UNORM_FCD, options,dest,dstLen,status);
    274 }
    275 
    276 int32_t ICUQuickCheck(const UChar* src,int32_t srcLen, UNormalizationMode mode, int32_t options, UErrorCode* status){
    277 #if (U_ICU_VERSION_MAJOR_NUM > 2 ) || ((U_ICU_VERSION_MAJOR_NUM == 2 )&&(U_ICU_VERSION_MINOR_NUM >= 6))
    278     return unorm_quickCheckWithOptions(src,srcLen,mode, options, status);
    279 #else
    280     return unorm_quickCheck(src,srcLen,mode,status);
    281 #endif
    282 }
    283 int32_t ICUIsNormalized(const UChar* src,int32_t srcLen, UNormalizationMode mode, int32_t options, UErrorCode* status){
    284     return unorm_isNormalized(src,srcLen,mode,status);
    285 }
    286 
    287 
    288 #else
    289 
    290 int32_t ICUNormNFD(const UChar* src, int32_t srcLen,UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    291     return unorm_normalize(src,srcLen,UCOL_DECOMP_CAN, options,dest,dstLen,status);
    292 }
    293 
    294 int32_t ICUNormNFC(const UChar* src, int32_t srcLen,UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    295     return unorm_normalize(src,srcLen,UCOL_COMPOSE_CAN, options,dest,dstLen,status);
    296 }
    297 
    298 int32_t ICUNormNFKD(const UChar* src, int32_t srcLen,UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    299     return unorm_normalize(src,srcLen,UCOL_DECOMP_COMPAT, options,dest,dstLen,status);
    300 }
    301 int32_t ICUNormNFKC(const UChar* src, int32_t srcLen,UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    302     return unorm_normalize(src,srcLen,UCOL_COMPOSE_COMPAT, options,dest,dstLen,status);
    303 }
    304 
    305 int32_t ICUNormFCD(const UChar* src, int32_t srcLen,UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    306     return unorm_normalize(src,srcLen,UNORM_FCD, options,dest,dstLen,status);
    307 }
    308 
    309 int32_t ICUQuickCheck(const UChar* src,int32_t srcLen, UNormalizationMode mode, int32_t options, UErrorCode* status){
    310     return unorm_quickCheck(src,srcLen,mode,status);
    311 }
    312 
    313 int32_t ICUIsNormalized(const UChar* src,int32_t srcLen, UNormalizationMode mode, int32_t options, UErrorCode* status){
    314     return 0;
    315 }
    316 #endif
    317 
    318 #if defined(U_WINDOWS)
    319 
    320 int32_t WinNormNFD(const UChar* src, int32_t srcLen, UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    321     return FoldStringW(MAP_COMPOSITE,src,srcLen,dest,dstLen);
    322 }
    323 
    324 int32_t WinNormNFC(const UChar* src, int32_t srcLen, UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    325     return FoldStringW(MAP_PRECOMPOSED,src,srcLen,dest,dstLen);
    326 }
    327 
    328 int32_t WinNormNFKD(const UChar* src, int32_t srcLen, UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    329     return FoldStringW(MAP_COMPOSITE+MAP_FOLDCZONE,src,srcLen,dest,dstLen);
    330 }
    331 int32_t WinNormNFKC(const UChar* src, int32_t srcLen, UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    332     return FoldStringW(MAP_FOLDCZONE,src,srcLen,dest,dstLen);
    333 }
    334 #else
    335 int32_t WinNormNFD(const UChar* src, int32_t srcLen, UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    336     return 0 ;
    337 }
    338 
    339 int32_t WinNormNFC(const UChar* src, int32_t srcLen, UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    340     return 0;
    341 }
    342 
    343 int32_t WinNormNFKD(const UChar* src, int32_t srcLen, UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    344     return 0;
    345 }
    346 int32_t WinNormNFKC(const UChar* src, int32_t srcLen, UChar* dest, int32_t dstLen, int32_t options, UErrorCode* status) {
    347     return 0;
    348 }
    349 #endif
    350 
    351 
    352 #endif // NORMPERF_H
    353 
    354