Home | History | Annotate | Download | only in charperf
      1 /***********************************************************************
      2  * Copyright (C) 2016 and later: Unicode, Inc. and others.
      3  * License & terms of use: http://www.unicode.org/copyright.html#License
      4  ***********************************************************************
      5  ***********************************************************************
      6  * COPYRIGHT:
      7  * Copyright (C) 2002-2016 IBM, Inc.   All Rights Reserved.
      8  *
      9  ***********************************************************************/
     10 /*****************************************************************************
     11 * File charperf.cpp
     12 *
     13 * Modification History:
     14 * Name                     Description
     15 * Syn Wee Quek             First Version
     16 ******************************************************************************
     17 */
     18 
     19 /**
     20  * This program tests character properties performance.
     21  * APIs tested:
     22  * ICU4C
     23  * Windows
     24  */
     25 
     26 #include "charperf.h"
     27 #include "cmemory.h"
     28 #include "uoptions.h"
     29 
     30 UOption options[] = {
     31     UOPTION_DEF("min", 'n', UOPT_REQUIRES_ARG),
     32         UOPTION_DEF("min", 'x', UOPT_REQUIRES_ARG),
     33 };
     34 int MIN_OPTION_ = 0;
     35 int MAX_OPTION_ = 1;
     36 
     37 int main(int argc, const char *argv[])
     38 {
     39     UErrorCode status = U_ZERO_ERROR;
     40     CharPerformanceTest test(argc, argv, status);
     41     if (U_FAILURE(status)){
     42         return status;
     43     }
     44     if (test.run() == FALSE){
     45         fprintf(stderr, "FAILED: Tests could not be run please check the "
     46             "arguments.\n");
     47         return -1;
     48     }
     49     return 0;
     50 }
     51 
     52 CharPerformanceTest::CharPerformanceTest(int32_t argc, const char *argv[],
     53                                          UErrorCode &status)
     54                                          : UPerfTest(argc, argv, status)
     55 {
     56     if (status== U_ILLEGAL_ARGUMENT_ERROR){
     57         fprintf(stderr,gUsageString, "charperf");
     58         return;
     59     }
     60     if (U_FAILURE(status)){
     61         fprintf(stderr, "FAILED to create UPerfTest object. Error: %s\n",
     62             u_errorName(status));
     63         return;
     64     }
     65 
     66     if (_remainingArgc < 0) {
     67         // that means there are some -names not matched in the super class
     68         // first tag is always skipped in u_parseArgs
     69         int size = - _remainingArgc;
     70         argv += argc - size;
     71         argc = size;
     72         _remainingArgc = u_parseArgs(argc, (char**)argv,
     73             UPRV_LENGTHOF(options), options);
     74     }
     75     MIN_ = 0;
     76     if (sizeof(wchar_t) > 2)  {
     77         // for stdlibs like glibc that supports 32 bits wchar
     78         // we test for the whole unicode character set by default
     79         MAX_ = 0x10ffff;
     80     }
     81     else {
     82         MAX_ = 0xffff;
     83     }
     84     printf("MAX_ size will be 0x%x\n", MAX_);
     85     if (options[MIN_OPTION_].doesOccur) {
     86         MIN_ = atoi(options[MIN_OPTION_].value);
     87     }
     88     if (options[MAX_OPTION_].doesOccur) {
     89         MAX_ = atoi(options[MAX_OPTION_].value);
     90     }
     91 }
     92 
     93 CharPerformanceTest::~CharPerformanceTest()
     94 {
     95 }
     96 
     97 UPerfFunction* CharPerformanceTest::runIndexedTest(int32_t index, UBool exec,
     98                                                    const char *&name,
     99                                                    char* par)
    100 {
    101     switch (index) {
    102         TESTCASE(0, TestIsAlpha);
    103         TESTCASE(1, TestIsUpper);
    104         TESTCASE(2, TestIsLower);
    105         TESTCASE(3, TestIsDigit);
    106         TESTCASE(4, TestIsSpace);
    107         TESTCASE(5, TestIsAlphaNumeric);
    108         TESTCASE(6, TestIsPrint);
    109         TESTCASE(7, TestIsControl);
    110         TESTCASE(8, TestToLower);
    111         TESTCASE(9, TestToUpper);
    112         TESTCASE(10, TestIsWhiteSpace);
    113         TESTCASE(11, TestStdLibIsAlpha);
    114         TESTCASE(12, TestStdLibIsUpper);
    115         TESTCASE(13, TestStdLibIsLower);
    116         TESTCASE(14, TestStdLibIsDigit);
    117         TESTCASE(15, TestStdLibIsSpace);
    118         TESTCASE(16, TestStdLibIsAlphaNumeric);
    119         TESTCASE(17, TestStdLibIsPrint);
    120         TESTCASE(18, TestStdLibIsControl);
    121         TESTCASE(19, TestStdLibToLower);
    122         TESTCASE(20, TestStdLibToUpper);
    123         TESTCASE(21, TestStdLibIsWhiteSpace);
    124         default:
    125             name = "";
    126             return NULL;
    127     }
    128     return NULL;
    129 }
    130 
    131 UPerfFunction* CharPerformanceTest::TestIsAlpha()
    132 {
    133     return new CharPerfFunction(isAlpha, MIN_, MAX_);
    134 }
    135 
    136 UPerfFunction* CharPerformanceTest::TestIsUpper()
    137 {
    138     return new CharPerfFunction(isUpper, MIN_, MAX_);
    139 }
    140 
    141 UPerfFunction* CharPerformanceTest::TestIsLower()
    142 {
    143     return new CharPerfFunction(isLower, MIN_, MAX_);
    144 }
    145 
    146 UPerfFunction* CharPerformanceTest::TestIsDigit()
    147 {
    148     return new CharPerfFunction(isDigit, MIN_, MAX_);
    149 }
    150 
    151 UPerfFunction* CharPerformanceTest::TestIsSpace()
    152 {
    153     return new CharPerfFunction(isSpace, MIN_, MAX_);
    154 }
    155 
    156 UPerfFunction* CharPerformanceTest::TestIsAlphaNumeric()
    157 {
    158     return new CharPerfFunction(isAlphaNumeric, MIN_, MAX_);
    159 }
    160 
    161 /**
    162 * This test may be different since c lib has a type PUNCT and it is printable.
    163 * iswgraph is not used for testing since it is a subset of iswprint with the
    164 * exception of returning true for white spaces. no match found in icu4c.
    165 */
    166 UPerfFunction* CharPerformanceTest::TestIsPrint()
    167 {
    168     return new CharPerfFunction(isPrint, MIN_, MAX_);
    169 }
    170 
    171 UPerfFunction* CharPerformanceTest::TestIsControl()
    172 {
    173     return new CharPerfFunction(isControl, MIN_, MAX_);
    174 }
    175 
    176 UPerfFunction* CharPerformanceTest::TestToLower()
    177 {
    178     return new CharPerfFunction(toLower, MIN_, MAX_);
    179 }
    180 
    181 UPerfFunction* CharPerformanceTest::TestToUpper()
    182 {
    183     return new CharPerfFunction(toUpper, MIN_, MAX_);
    184 }
    185 
    186 UPerfFunction* CharPerformanceTest::TestIsWhiteSpace()
    187 {
    188     return new CharPerfFunction(isWhiteSpace, MIN_, MAX_);
    189 }
    190 
    191 UPerfFunction* CharPerformanceTest::TestStdLibIsAlpha()
    192 {
    193     return new StdLibCharPerfFunction(StdLibIsAlpha, (wchar_t)MIN_,
    194         (wchar_t)MAX_);
    195 }
    196 
    197 UPerfFunction* CharPerformanceTest::TestStdLibIsUpper()
    198 {
    199     return new StdLibCharPerfFunction(StdLibIsUpper, (wchar_t)MIN_,
    200         (wchar_t)MAX_);
    201 }
    202 
    203 UPerfFunction* CharPerformanceTest::TestStdLibIsLower()
    204 {
    205     return new StdLibCharPerfFunction(StdLibIsLower, (wchar_t)MIN_,
    206         (wchar_t)MAX_);
    207 }
    208 
    209 UPerfFunction* CharPerformanceTest::TestStdLibIsDigit()
    210 {
    211     return new StdLibCharPerfFunction(StdLibIsDigit, (wchar_t)MIN_,
    212         (wchar_t)MAX_);
    213 }
    214 
    215 UPerfFunction* CharPerformanceTest::TestStdLibIsSpace()
    216 {
    217     return new StdLibCharPerfFunction(StdLibIsSpace, (wchar_t)MIN_,
    218         (wchar_t)MAX_);
    219 }
    220 
    221 UPerfFunction* CharPerformanceTest::TestStdLibIsAlphaNumeric()
    222 {
    223     return new StdLibCharPerfFunction(StdLibIsAlphaNumeric, (wchar_t)MIN_,
    224         (wchar_t)MAX_);
    225 }
    226 
    227 /**
    228 * This test may be different since c lib has a type PUNCT and it is printable.
    229 * iswgraph is not used for testing since it is a subset of iswprint with the
    230 * exception of returning true for white spaces. no match found in icu4c.
    231 */
    232 UPerfFunction* CharPerformanceTest::TestStdLibIsPrint()
    233 {
    234     return new StdLibCharPerfFunction(StdLibIsPrint, (wchar_t)MIN_,
    235         (wchar_t)MAX_);
    236 }
    237 
    238 UPerfFunction* CharPerformanceTest::TestStdLibIsControl()
    239 {
    240     return new StdLibCharPerfFunction(StdLibIsControl, (wchar_t)MIN_,
    241         (wchar_t)MAX_);
    242 }
    243 
    244 UPerfFunction* CharPerformanceTest::TestStdLibToLower()
    245 {
    246     return new StdLibCharPerfFunction(StdLibToLower, (wchar_t)MIN_,
    247         (wchar_t)MAX_);
    248 }
    249 
    250 UPerfFunction* CharPerformanceTest::TestStdLibToUpper()
    251 {
    252     return new StdLibCharPerfFunction(StdLibToUpper, (wchar_t)MIN_,
    253         (wchar_t)MAX_);
    254 }
    255 
    256 UPerfFunction* CharPerformanceTest::TestStdLibIsWhiteSpace()
    257 {
    258     return new StdLibCharPerfFunction(StdLibIsWhiteSpace, (wchar_t)MIN_,
    259         (wchar_t)MAX_);
    260 }
    261