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-2006, International Business Machines
8 * Corporation and others.  All Rights Reserved.
9 **********************************************************************
10 **********************************************************************
11 */
12 #ifndef _CHARPERF_H
13 #define _CHARPERF_H
14 
15 #include "unicode/uchar.h"
16 
17 #include "unicode/uperf.h"
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <wchar.h>
21 #include <wctype.h>
22 
23 typedef void (*CharPerfFn)(UChar32 ch);
24 typedef void (*StdLibCharPerfFn)(wchar_t ch);
25 
26 class CharPerfFunction : public UPerfFunction
27 {
28 public:
call(UErrorCode * status)29     virtual void call(UErrorCode* status)
30     {
31         for (UChar32 i = MIN_; i < MAX_; i ++) {
32             (*m_fn_)(i);
33         }
34     }
35 
getOperationsPerIteration()36     virtual long getOperationsPerIteration()
37     {
38         return MAX_ - MIN_;
39     }
CharPerfFunction(CharPerfFn func,UChar32 min,UChar32 max)40     CharPerfFunction(CharPerfFn func, UChar32 min, UChar32 max)
41     {
42         m_fn_ = func;
43         MIN_ = min;
44         MAX_ = max;
45     }
46 
47 private:
48     CharPerfFn m_fn_;
49     UChar32 MIN_;
50     UChar32 MAX_;
51 };
52 
53 class StdLibCharPerfFunction : public UPerfFunction
54 {
55 public:
call(UErrorCode * status)56     virtual void call(UErrorCode* status)
57     {
58         // note wchar_t is unsigned, it will revert to 0 once it reaches
59         // 65535
60         for (wchar_t i = MIN_; i < MAX_; i ++) {
61             (*m_fn_)(i);
62         }
63     }
64 
getOperationsPerIteration()65     virtual long getOperationsPerIteration()
66     {
67         return MAX_ - MIN_;
68     }
69 
StdLibCharPerfFunction(StdLibCharPerfFn func,wchar_t min,wchar_t max)70     StdLibCharPerfFunction(StdLibCharPerfFn func, wchar_t min, wchar_t max)
71     {
72         m_fn_ = func;
73         MIN_ = min;
74         MAX_ = max;
75     }
76 
~StdLibCharPerfFunction()77     ~StdLibCharPerfFunction()
78     {
79     }
80 
81 private:
82     StdLibCharPerfFn m_fn_;
83     wchar_t MIN_;
84     wchar_t MAX_;
85 };
86 
87 class CharPerformanceTest : public UPerfTest
88 {
89 public:
90     CharPerformanceTest(int32_t argc, const char *argv[], UErrorCode &status);
91     ~CharPerformanceTest();
92     virtual UPerfFunction* runIndexedTest(int32_t index, UBool exec,
93         const char *&name,
94         char *par = NULL);
95     UPerfFunction* TestIsAlpha();
96     UPerfFunction* TestIsUpper();
97     UPerfFunction* TestIsLower();
98     UPerfFunction* TestIsDigit();
99     UPerfFunction* TestIsSpace();
100     UPerfFunction* TestIsAlphaNumeric();
101     UPerfFunction* TestIsPrint();
102     UPerfFunction* TestIsControl();
103     UPerfFunction* TestToLower();
104     UPerfFunction* TestToUpper();
105     UPerfFunction* TestIsWhiteSpace();
106     UPerfFunction* TestStdLibIsAlpha();
107     UPerfFunction* TestStdLibIsUpper();
108     UPerfFunction* TestStdLibIsLower();
109     UPerfFunction* TestStdLibIsDigit();
110     UPerfFunction* TestStdLibIsSpace();
111     UPerfFunction* TestStdLibIsAlphaNumeric();
112     UPerfFunction* TestStdLibIsPrint();
113     UPerfFunction* TestStdLibIsControl();
114     UPerfFunction* TestStdLibToLower();
115     UPerfFunction* TestStdLibToUpper();
116     UPerfFunction* TestStdLibIsWhiteSpace();
117 
118 private:
119     UChar32 MIN_;
120     UChar32 MAX_;
121 };
122 
isAlpha(UChar32 ch)123 inline void isAlpha(UChar32 ch)
124 {
125     u_isalpha(ch);
126 }
127 
isUpper(UChar32 ch)128 inline void isUpper(UChar32 ch)
129 {
130     u_isupper(ch);
131 }
132 
isLower(UChar32 ch)133 inline void isLower(UChar32 ch)
134 {
135     u_islower(ch);
136 }
137 
isDigit(UChar32 ch)138 inline void isDigit(UChar32 ch)
139 {
140     u_isdigit(ch);
141 }
142 
isSpace(UChar32 ch)143 inline void isSpace(UChar32 ch)
144 {
145     u_isspace(ch);
146 }
147 
isAlphaNumeric(UChar32 ch)148 inline void isAlphaNumeric(UChar32 ch)
149 {
150     u_isalnum(ch);
151 }
152 
153 /**
154 * This test may be different since c lib has a type PUNCT and it is printable.
155 * iswgraph is not used for testing since it is a subset of iswprint with the
156 * exception of returning true for white spaces. no match found in icu4c.
157 */
isPrint(UChar32 ch)158 inline void isPrint(UChar32 ch)
159 {
160     u_isprint(ch);
161 }
162 
isControl(UChar32 ch)163 inline void isControl(UChar32 ch)
164 {
165     u_iscntrl(ch);
166 }
167 
toLower(UChar32 ch)168 inline void toLower(UChar32 ch)
169 {
170     u_tolower(ch);
171 }
172 
toUpper(UChar32 ch)173 inline void toUpper(UChar32 ch)
174 {
175     u_toupper(ch);
176 }
177 
isWhiteSpace(UChar32 ch)178 inline void isWhiteSpace(UChar32 ch)
179 {
180     u_isWhitespace(ch);
181 }
182 
StdLibIsAlpha(wchar_t ch)183 inline void StdLibIsAlpha(wchar_t ch)
184 {
185     iswalpha(ch);
186 }
187 
StdLibIsUpper(wchar_t ch)188 inline void StdLibIsUpper(wchar_t ch)
189 {
190     iswupper(ch);
191 }
192 
StdLibIsLower(wchar_t ch)193 inline void StdLibIsLower(wchar_t ch)
194 {
195     iswlower(ch);
196 }
197 
StdLibIsDigit(wchar_t ch)198 inline void StdLibIsDigit(wchar_t ch)
199 {
200     iswdigit(ch);
201 }
202 
StdLibIsSpace(wchar_t ch)203 inline void StdLibIsSpace(wchar_t ch)
204 {
205     iswspace(ch);
206 }
207 
StdLibIsAlphaNumeric(wchar_t ch)208 inline void StdLibIsAlphaNumeric(wchar_t ch)
209 {
210     iswalnum(ch);
211 }
212 
213 /**
214 * This test may be different since c lib has a type PUNCT and it is printable.
215 * iswgraph is not used for testing since it is a subset of iswprint with the
216 * exception of returning true for white spaces. no match found in icu4c.
217 */
StdLibIsPrint(wchar_t ch)218 inline void StdLibIsPrint(wchar_t ch)
219 {
220     iswprint(ch);
221 }
222 
StdLibIsControl(wchar_t ch)223 inline void StdLibIsControl(wchar_t ch)
224 {
225     iswcntrl(ch);
226 }
227 
StdLibToLower(wchar_t ch)228 inline void StdLibToLower(wchar_t ch)
229 {
230     towlower(ch);
231 }
232 
StdLibToUpper(wchar_t ch)233 inline void StdLibToUpper(wchar_t ch)
234 {
235     towupper(ch);
236 }
237 
StdLibIsWhiteSpace(wchar_t ch)238 inline void StdLibIsWhiteSpace(wchar_t ch)
239 {
240     iswspace(ch);
241 }
242 
243 #endif // CHARPERF_H
244