1 /***********************************************************************
2 * © 2016 and later: Unicode, Inc. and others.
3 * License & terms of use: http://www.unicode.org/copyright.html
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
main(int argc,const char * argv[])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
CharPerformanceTest(int32_t argc,const char * argv[],UErrorCode & status)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 if (options[MIN_OPTION_].doesOccur) {
85 MIN_ = atoi(options[MIN_OPTION_].value);
86 }
87 if (options[MAX_OPTION_].doesOccur) {
88 MAX_ = atoi(options[MAX_OPTION_].value);
89 }
90 }
91
~CharPerformanceTest()92 CharPerformanceTest::~CharPerformanceTest()
93 {
94 }
95
runIndexedTest(int32_t index,UBool exec,const char * & name,char * par)96 UPerfFunction* CharPerformanceTest::runIndexedTest(int32_t index, UBool exec,
97 const char *&name,
98 char* par)
99 {
100 switch (index) {
101 TESTCASE(0, TestIsAlpha);
102 TESTCASE(1, TestIsUpper);
103 TESTCASE(2, TestIsLower);
104 TESTCASE(3, TestIsDigit);
105 TESTCASE(4, TestIsSpace);
106 TESTCASE(5, TestIsAlphaNumeric);
107 TESTCASE(6, TestIsPrint);
108 TESTCASE(7, TestIsControl);
109 TESTCASE(8, TestToLower);
110 TESTCASE(9, TestToUpper);
111 TESTCASE(10, TestIsWhiteSpace);
112 TESTCASE(11, TestStdLibIsAlpha);
113 TESTCASE(12, TestStdLibIsUpper);
114 TESTCASE(13, TestStdLibIsLower);
115 TESTCASE(14, TestStdLibIsDigit);
116 TESTCASE(15, TestStdLibIsSpace);
117 TESTCASE(16, TestStdLibIsAlphaNumeric);
118 TESTCASE(17, TestStdLibIsPrint);
119 TESTCASE(18, TestStdLibIsControl);
120 TESTCASE(19, TestStdLibToLower);
121 TESTCASE(20, TestStdLibToUpper);
122 TESTCASE(21, TestStdLibIsWhiteSpace);
123 default:
124 name = "";
125 return NULL;
126 }
127 return NULL;
128 }
129
TestIsAlpha()130 UPerfFunction* CharPerformanceTest::TestIsAlpha()
131 {
132 return new CharPerfFunction(isAlpha, MIN_, MAX_);
133 }
134
TestIsUpper()135 UPerfFunction* CharPerformanceTest::TestIsUpper()
136 {
137 return new CharPerfFunction(isUpper, MIN_, MAX_);
138 }
139
TestIsLower()140 UPerfFunction* CharPerformanceTest::TestIsLower()
141 {
142 return new CharPerfFunction(isLower, MIN_, MAX_);
143 }
144
TestIsDigit()145 UPerfFunction* CharPerformanceTest::TestIsDigit()
146 {
147 return new CharPerfFunction(isDigit, MIN_, MAX_);
148 }
149
TestIsSpace()150 UPerfFunction* CharPerformanceTest::TestIsSpace()
151 {
152 return new CharPerfFunction(isSpace, MIN_, MAX_);
153 }
154
TestIsAlphaNumeric()155 UPerfFunction* CharPerformanceTest::TestIsAlphaNumeric()
156 {
157 return new CharPerfFunction(isAlphaNumeric, MIN_, MAX_);
158 }
159
160 /**
161 * This test may be different since c lib has a type PUNCT and it is printable.
162 * iswgraph is not used for testing since it is a subset of iswprint with the
163 * exception of returning true for white spaces. no match found in icu4c.
164 */
TestIsPrint()165 UPerfFunction* CharPerformanceTest::TestIsPrint()
166 {
167 return new CharPerfFunction(isPrint, MIN_, MAX_);
168 }
169
TestIsControl()170 UPerfFunction* CharPerformanceTest::TestIsControl()
171 {
172 return new CharPerfFunction(isControl, MIN_, MAX_);
173 }
174
TestToLower()175 UPerfFunction* CharPerformanceTest::TestToLower()
176 {
177 return new CharPerfFunction(toLower, MIN_, MAX_);
178 }
179
TestToUpper()180 UPerfFunction* CharPerformanceTest::TestToUpper()
181 {
182 return new CharPerfFunction(toUpper, MIN_, MAX_);
183 }
184
TestIsWhiteSpace()185 UPerfFunction* CharPerformanceTest::TestIsWhiteSpace()
186 {
187 return new CharPerfFunction(isWhiteSpace, MIN_, MAX_);
188 }
189
TestStdLibIsAlpha()190 UPerfFunction* CharPerformanceTest::TestStdLibIsAlpha()
191 {
192 return new StdLibCharPerfFunction(StdLibIsAlpha, (wchar_t)MIN_,
193 (wchar_t)MAX_);
194 }
195
TestStdLibIsUpper()196 UPerfFunction* CharPerformanceTest::TestStdLibIsUpper()
197 {
198 return new StdLibCharPerfFunction(StdLibIsUpper, (wchar_t)MIN_,
199 (wchar_t)MAX_);
200 }
201
TestStdLibIsLower()202 UPerfFunction* CharPerformanceTest::TestStdLibIsLower()
203 {
204 return new StdLibCharPerfFunction(StdLibIsLower, (wchar_t)MIN_,
205 (wchar_t)MAX_);
206 }
207
TestStdLibIsDigit()208 UPerfFunction* CharPerformanceTest::TestStdLibIsDigit()
209 {
210 return new StdLibCharPerfFunction(StdLibIsDigit, (wchar_t)MIN_,
211 (wchar_t)MAX_);
212 }
213
TestStdLibIsSpace()214 UPerfFunction* CharPerformanceTest::TestStdLibIsSpace()
215 {
216 return new StdLibCharPerfFunction(StdLibIsSpace, (wchar_t)MIN_,
217 (wchar_t)MAX_);
218 }
219
TestStdLibIsAlphaNumeric()220 UPerfFunction* CharPerformanceTest::TestStdLibIsAlphaNumeric()
221 {
222 return new StdLibCharPerfFunction(StdLibIsAlphaNumeric, (wchar_t)MIN_,
223 (wchar_t)MAX_);
224 }
225
226 /**
227 * This test may be different since c lib has a type PUNCT and it is printable.
228 * iswgraph is not used for testing since it is a subset of iswprint with the
229 * exception of returning true for white spaces. no match found in icu4c.
230 */
TestStdLibIsPrint()231 UPerfFunction* CharPerformanceTest::TestStdLibIsPrint()
232 {
233 return new StdLibCharPerfFunction(StdLibIsPrint, (wchar_t)MIN_,
234 (wchar_t)MAX_);
235 }
236
TestStdLibIsControl()237 UPerfFunction* CharPerformanceTest::TestStdLibIsControl()
238 {
239 return new StdLibCharPerfFunction(StdLibIsControl, (wchar_t)MIN_,
240 (wchar_t)MAX_);
241 }
242
TestStdLibToLower()243 UPerfFunction* CharPerformanceTest::TestStdLibToLower()
244 {
245 return new StdLibCharPerfFunction(StdLibToLower, (wchar_t)MIN_,
246 (wchar_t)MAX_);
247 }
248
TestStdLibToUpper()249 UPerfFunction* CharPerformanceTest::TestStdLibToUpper()
250 {
251 return new StdLibCharPerfFunction(StdLibToUpper, (wchar_t)MIN_,
252 (wchar_t)MAX_);
253 }
254
TestStdLibIsWhiteSpace()255 UPerfFunction* CharPerformanceTest::TestStdLibIsWhiteSpace()
256 {
257 return new StdLibCharPerfFunction(StdLibIsWhiteSpace, (wchar_t)MIN_,
258 (wchar_t)MAX_);
259 }
260