1 /***********************************************************************
2 * Copyright (C) 2016 and later: Unicode, Inc. and others.
3 * License & terms of use: http://www.unicode.org/copyright.html#License
5 ***********************************************************************
6 ***********************************************************************
8 * Copyright (C) 2002-2006 International Business Machines Corporation
9 * and others. All Rights Reserved.
11 ***********************************************************************/
12 /*****************************************************************************
15 * Modification History:
17 * Doug Wang Second version
18 * Doug Wang First Version
19 ******************************************************************************
23 * This program tests UnicodeString performance.
24 * APIs tested: UnicodeString
26 * Windows 2000/XP, Linux
29 #include "stringperf.h"
32 int main(int argc, const char *argv[])
34 UErrorCode status = U_ZERO_ERROR;
36 bCatenatePrealloc=TRUE;
38 StringPerformanceTest test(argc, argv, status);
39 if (U_FAILURE(status)){
44 if (bCatenatePrealloc) {
45 int to_alloc = loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN);
46 catICU = new UnicodeString(to_alloc,'a',0);
47 //catICU = new UnicodeString();
49 catStd = new stlstring();
50 catStd -> reserve(loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN));
51 //catStd -> reserve(110000000);
53 catICU = new UnicodeString();
54 catStd = new stlstring();
57 if (test.run() == FALSE){
58 fprintf(stderr, "FAILED: Tests could not be run please check the "
68 StringPerformanceTest::StringPerformanceTest(int32_t argc, const char *argv[],
70 : UPerfTest(argc, argv, status)
78 if (status== U_ILLEGAL_ARGUMENT_ERROR){
79 //fprintf(stderr,gUsageString, "stringperf");
82 if (U_FAILURE(status)){
83 fprintf(stderr, "FAILED to create UPerfTest object. Error: %s\n",
90 ULine* filelines = getLines(status);
91 if(U_FAILURE(status)){
92 fprintf(stderr, "FAILED to read lines from file and create UPerfTest object. Error: %s\n", u_errorName(status));
96 filelines_ = new ULine[numLines];
97 for (int i =0; i < numLines; i++) {
98 len = filelines[i].len;
99 filelines_[i].name = new UChar[len];
100 filelines_[i].len = len;
101 memcpy(filelines_[i].name, filelines[i].name, len * U_SIZEOF_UCHAR);
106 const UChar* src = getBuffer(srcLen,status);
107 if(U_FAILURE(status)){
108 fprintf(stderr, "FAILED to read buffer from file and create UPerfTest object. Error: %s\n", u_errorName(status));
112 StrBuffer = new UChar[srcLen];
113 StrBufferLen = srcLen;
114 memcpy(StrBuffer, src, srcLen * U_SIZEOF_UCHAR);
119 StringPerformanceTest::~StringPerformanceTest()
125 UPerfFunction* StringPerformanceTest::runIndexedTest(int32_t index, UBool exec,
130 TESTCASE(0, TestCtor);
131 TESTCASE(1, TestCtor1);
132 TESTCASE(2, TestCtor2);
133 TESTCASE(3, TestCtor3);
134 TESTCASE(4, TestAssign);
135 TESTCASE(5, TestAssign1);
136 TESTCASE(6, TestAssign2);
137 TESTCASE(7, TestGetch);
138 TESTCASE(8, TestCatenate);
139 TESTCASE(9, TestScan);
140 TESTCASE(10, TestScan1);
141 TESTCASE(11, TestScan2);
143 TESTCASE(12, TestStdLibCtor);
144 TESTCASE(13, TestStdLibCtor1);
145 TESTCASE(14, TestStdLibCtor2);
146 TESTCASE(15, TestStdLibCtor3);
147 TESTCASE(16, TestStdLibAssign);
148 TESTCASE(17, TestStdLibAssign1);
149 TESTCASE(18, TestStdLibAssign2);
150 TESTCASE(19, TestStdLibGetch);
151 TESTCASE(20, TestStdLibCatenate);
152 TESTCASE(21, TestStdLibScan);
153 TESTCASE(22, TestStdLibScan1);
154 TESTCASE(23, TestStdLibScan2);
163 UPerfFunction* StringPerformanceTest::TestCtor()
166 return new StringPerfFunction(ctor, filelines_, numLines, uselen);
168 return new StringPerfFunction(ctor, StrBuffer, StrBufferLen, uselen);
172 UPerfFunction* StringPerformanceTest::TestCtor1()
175 return new StringPerfFunction(ctor1, filelines_, numLines, uselen);
177 return new StringPerfFunction(ctor1, StrBuffer, StrBufferLen, uselen);
181 UPerfFunction* StringPerformanceTest::TestCtor2()
184 return new StringPerfFunction(ctor2, filelines_, numLines, uselen);
186 return new StringPerfFunction(ctor2, StrBuffer, StrBufferLen, uselen);
190 UPerfFunction* StringPerformanceTest::TestCtor3()
193 return new StringPerfFunction(ctor3, filelines_, numLines, uselen);
195 return new StringPerfFunction(ctor3, StrBuffer, StrBufferLen, uselen);
199 UPerfFunction* StringPerformanceTest::TestAssign()
202 return new StringPerfFunction(assign, filelines_, numLines, uselen);
204 return new StringPerfFunction(assign, StrBuffer, StrBufferLen, uselen);
208 UPerfFunction* StringPerformanceTest::TestAssign1()
211 return new StringPerfFunction(assign1, filelines_, numLines, uselen);
213 return new StringPerfFunction(assign1, StrBuffer, StrBufferLen, uselen);
217 UPerfFunction* StringPerformanceTest::TestAssign2()
220 return new StringPerfFunction(assign2, filelines_, numLines, uselen);
222 return new StringPerfFunction(assign2, StrBuffer, StrBufferLen, uselen);
227 UPerfFunction* StringPerformanceTest::TestGetch()
230 return new StringPerfFunction(getch, filelines_, numLines, uselen);
232 return new StringPerfFunction(getch, StrBuffer, StrBufferLen, uselen);
236 UPerfFunction* StringPerformanceTest::TestCatenate()
239 return new StringPerfFunction(catenate, filelines_, numLines, uselen);
241 //return new StringPerfFunction(catenate, buffer, bufferLen, uselen);
242 return new StringPerfFunction(catenate, StrBuffer, StrBufferLen, uselen);
246 UPerfFunction* StringPerformanceTest::TestScan()
249 return new StringPerfFunction(scan, filelines_, numLines, uselen);
251 return new StringPerfFunction(scan, StrBuffer, StrBufferLen, uselen);
255 UPerfFunction* StringPerformanceTest::TestScan1()
258 return new StringPerfFunction(scan1, filelines_, numLines, uselen);
260 return new StringPerfFunction(scan1, StrBuffer, StrBufferLen, uselen);
264 UPerfFunction* StringPerformanceTest::TestScan2()
267 return new StringPerfFunction(scan2, filelines_, numLines, uselen);
269 return new StringPerfFunction(scan2, StrBuffer, StrBufferLen, uselen);
273 UPerfFunction* StringPerformanceTest::TestStdLibCtor()
276 return new StringPerfFunction(StdLibCtor, filelines_, numLines, uselen);
278 return new StringPerfFunction(StdLibCtor, StrBuffer, StrBufferLen, uselen);
282 UPerfFunction* StringPerformanceTest::TestStdLibCtor1()
285 return new StringPerfFunction(StdLibCtor1, filelines_, numLines, uselen);
287 return new StringPerfFunction(StdLibCtor1, StrBuffer, StrBufferLen, uselen);
291 UPerfFunction* StringPerformanceTest::TestStdLibCtor2()
294 return new StringPerfFunction(StdLibCtor2, filelines_, numLines, uselen);
296 return new StringPerfFunction(StdLibCtor2, StrBuffer, StrBufferLen, uselen);
300 UPerfFunction* StringPerformanceTest::TestStdLibCtor3()
303 return new StringPerfFunction(StdLibCtor3, filelines_, numLines, uselen);
305 return new StringPerfFunction(StdLibCtor3, StrBuffer, StrBufferLen, uselen);
309 UPerfFunction* StringPerformanceTest::TestStdLibAssign()
312 return new StringPerfFunction(StdLibAssign, filelines_, numLines, uselen);
314 return new StringPerfFunction(StdLibAssign, StrBuffer, StrBufferLen, uselen);
318 UPerfFunction* StringPerformanceTest::TestStdLibAssign1()
321 return new StringPerfFunction(StdLibAssign1, filelines_, numLines, uselen);
323 return new StringPerfFunction(StdLibAssign1, StrBuffer, StrBufferLen, uselen);
327 UPerfFunction* StringPerformanceTest::TestStdLibAssign2()
330 return new StringPerfFunction(StdLibAssign2, filelines_, numLines, uselen);
332 return new StringPerfFunction(StdLibAssign2, StrBuffer, StrBufferLen, uselen);
336 UPerfFunction* StringPerformanceTest::TestStdLibGetch()
339 return new StringPerfFunction(StdLibGetch, filelines_, numLines, uselen);
341 return new StringPerfFunction(StdLibGetch, StrBuffer, StrBufferLen, uselen);
345 UPerfFunction* StringPerformanceTest::TestStdLibCatenate()
348 return new StringPerfFunction(StdLibCatenate, filelines_, numLines, uselen);
350 //return new StringPerfFunction(StdLibCatenate, buffer, bufferLen, uselen);
351 return new StringPerfFunction(StdLibCatenate, StrBuffer, StrBufferLen, uselen);
355 UPerfFunction* StringPerformanceTest::TestStdLibScan()
358 return new StringPerfFunction(StdLibScan, filelines_, numLines, uselen);
360 return new StringPerfFunction(StdLibScan, StrBuffer, StrBufferLen, uselen);
364 UPerfFunction* StringPerformanceTest::TestStdLibScan1()
367 return new StringPerfFunction(StdLibScan1, filelines_, numLines, uselen);
369 return new StringPerfFunction(StdLibScan1, StrBuffer, StrBufferLen, uselen);
373 UPerfFunction* StringPerformanceTest::TestStdLibScan2()
376 return new StringPerfFunction(StdLibScan2, filelines_, numLines, uselen);
378 return new StringPerfFunction(StdLibScan2, StrBuffer, StrBufferLen, uselen);