1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (C) 2002-2006 International Business Machines Corporation
4 * and others. All Rights Reserved.
5 *
6 ********************************************************************/
7/*****************************************************************************
8* File stringperf.cpp
9*
10* Modification History:
11* Name                     Description
12* Doug Wang                Second version
13* Doug Wang                First Version
14******************************************************************************
15*/
16
17/**
18 * This program tests UnicodeString performance.
19 * APIs tested: UnicodeString
20 * ICU4C
21 * Windows 2000/XP, Linux
22 */
23
24#include "stringperf.h"
25
26
27int main(int argc, const char *argv[])
28{
29    UErrorCode status = U_ZERO_ERROR;
30
31    bCatenatePrealloc=TRUE;
32
33    StringPerformanceTest test(argc, argv, status);
34    if (U_FAILURE(status)){
35        return status;
36    }
37
38    int loops = LOOPS;
39    if (bCatenatePrealloc) {
40        int to_alloc = loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN);
41        catICU = new UnicodeString(to_alloc,'a',0);
42        //catICU = new UnicodeString();
43
44        catStd = new stlstring();
45        catStd -> reserve(loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN));
46        //catStd -> reserve(110000000);
47    } else {
48        catICU = new UnicodeString();
49        catStd = new stlstring();
50    }
51
52    if (test.run() == FALSE){
53        fprintf(stderr, "FAILED: Tests could not be run please check the "
54            "arguments.\n");
55        return -1;
56    }
57
58    delete catICU;
59    delete catStd;
60    return 0;
61}
62
63StringPerformanceTest::StringPerformanceTest(int32_t argc, const char *argv[],
64                                             UErrorCode &status)
65                                             : UPerfTest(argc, argv, status)
66{
67    filelines_=NULL;
68    StrBuffer=NULL;
69    StrBufferLen=0;
70
71    int32_t len =0;
72
73    if (status== U_ILLEGAL_ARGUMENT_ERROR){
74        //fprintf(stderr,gUsageString, "stringperf");
75        return;
76    }
77    if (U_FAILURE(status)){
78        fprintf(stderr, "FAILED to create UPerfTest object. Error: %s\n",
79            u_errorName(status));
80        return;
81    }
82
83
84    if(line_mode){
85        ULine* filelines = getLines(status);
86        if(U_FAILURE(status)){
87            fprintf(stderr, "FAILED to read lines from file and create UPerfTest object. Error: %s\n", u_errorName(status));
88            return;
89        }
90
91        filelines_ = new ULine[numLines];
92        for (int i =0; i < numLines; i++) {
93            len = filelines[i].len;
94            filelines_[i].name  = new UChar[len];
95            filelines_[i].len   = len;
96            memcpy(filelines_[i].name, filelines[i].name, len * U_SIZEOF_UCHAR);
97        }
98
99    }else if(bulk_mode){
100        int32_t srcLen = 0;
101        const UChar* src = getBuffer(srcLen,status);
102        if(U_FAILURE(status)){
103            fprintf(stderr, "FAILED to read buffer from file and create UPerfTest object. Error: %s\n", u_errorName(status));
104            return;
105        }
106
107        StrBuffer = new UChar[srcLen];
108        StrBufferLen = srcLen;
109        memcpy(StrBuffer, src, srcLen * U_SIZEOF_UCHAR);
110
111    }
112}
113
114StringPerformanceTest::~StringPerformanceTest()
115{
116    delete[] filelines_;
117    delete[] StrBuffer;
118}
119
120UPerfFunction* StringPerformanceTest::runIndexedTest(int32_t index, UBool exec,
121                                                   const char *&name,
122                                                   char* par)
123{
124    switch (index) {
125        TESTCASE(0, TestCtor);
126        TESTCASE(1, TestCtor1);
127        TESTCASE(2, TestCtor2);
128        TESTCASE(3, TestCtor3);
129        TESTCASE(4, TestAssign);
130        TESTCASE(5, TestAssign1);
131        TESTCASE(6, TestAssign2);
132        TESTCASE(7, TestGetch);
133        TESTCASE(8, TestCatenate);
134        TESTCASE(9, TestScan);
135        TESTCASE(10, TestScan1);
136        TESTCASE(11, TestScan2);
137
138        TESTCASE(12, TestStdLibCtor);
139        TESTCASE(13, TestStdLibCtor1);
140        TESTCASE(14, TestStdLibCtor2);
141        TESTCASE(15, TestStdLibCtor3);
142        TESTCASE(16, TestStdLibAssign);
143        TESTCASE(17, TestStdLibAssign1);
144        TESTCASE(18, TestStdLibAssign2);
145        TESTCASE(19, TestStdLibGetch);
146        TESTCASE(20, TestStdLibCatenate);
147        TESTCASE(21, TestStdLibScan);
148        TESTCASE(22, TestStdLibScan1);
149        TESTCASE(23, TestStdLibScan2);
150
151        default:
152            name = "";
153            return NULL;
154    }
155    return NULL;
156}
157
158UPerfFunction* StringPerformanceTest::TestCtor()
159{
160    if (line_mode) {
161        return new StringPerfFunction(ctor, filelines_, numLines, uselen);
162    } else {
163        return new StringPerfFunction(ctor, StrBuffer, StrBufferLen, uselen);
164    }
165}
166
167UPerfFunction* StringPerformanceTest::TestCtor1()
168{
169    if (line_mode) {
170        return new StringPerfFunction(ctor1, filelines_, numLines, uselen);
171    } else {
172        return new StringPerfFunction(ctor1, StrBuffer, StrBufferLen, uselen);
173    }
174}
175
176UPerfFunction* StringPerformanceTest::TestCtor2()
177{
178    if (line_mode) {
179        return new StringPerfFunction(ctor2, filelines_, numLines, uselen);
180    } else {
181        return new StringPerfFunction(ctor2, StrBuffer, StrBufferLen, uselen);
182    }
183}
184
185UPerfFunction* StringPerformanceTest::TestCtor3()
186{
187    if (line_mode) {
188        return new StringPerfFunction(ctor3, filelines_, numLines, uselen);
189    } else {
190        return new StringPerfFunction(ctor3, StrBuffer, StrBufferLen, uselen);
191    }
192}
193
194UPerfFunction* StringPerformanceTest::TestAssign()
195{
196    if (line_mode) {
197        return new StringPerfFunction(assign, filelines_, numLines, uselen);
198    } else {
199        return new StringPerfFunction(assign, StrBuffer, StrBufferLen, uselen);
200    }
201}
202
203UPerfFunction* StringPerformanceTest::TestAssign1()
204{
205    if (line_mode) {
206        return new StringPerfFunction(assign1, filelines_, numLines, uselen);
207    } else {
208        return new StringPerfFunction(assign1, StrBuffer, StrBufferLen, uselen);
209    }
210}
211
212UPerfFunction* StringPerformanceTest::TestAssign2()
213{
214    if (line_mode) {
215        return new StringPerfFunction(assign2, filelines_, numLines, uselen);
216    } else {
217        return new StringPerfFunction(assign2, StrBuffer, StrBufferLen, uselen);
218    }
219}
220
221
222UPerfFunction* StringPerformanceTest::TestGetch()
223{
224    if (line_mode) {
225        return new StringPerfFunction(getch, filelines_, numLines, uselen);
226    } else {
227        return new StringPerfFunction(getch, StrBuffer, StrBufferLen, uselen);
228    }
229}
230
231UPerfFunction* StringPerformanceTest::TestCatenate()
232{
233    if (line_mode) {
234        return new StringPerfFunction(catenate, filelines_, numLines, uselen);
235    } else {
236        //return new StringPerfFunction(catenate, buffer, bufferLen, uselen);
237        return new StringPerfFunction(catenate, StrBuffer, StrBufferLen, uselen);
238    }
239}
240
241UPerfFunction* StringPerformanceTest::TestScan()
242{
243    if (line_mode) {
244        return new StringPerfFunction(scan, filelines_, numLines, uselen);
245    } else {
246        return new StringPerfFunction(scan, StrBuffer, StrBufferLen, uselen);
247    }
248}
249
250UPerfFunction* StringPerformanceTest::TestScan1()
251{
252    if (line_mode) {
253        return new StringPerfFunction(scan1, filelines_, numLines, uselen);
254    } else {
255        return new StringPerfFunction(scan1, StrBuffer, StrBufferLen, uselen);
256    }
257}
258
259UPerfFunction* StringPerformanceTest::TestScan2()
260{
261    if (line_mode) {
262        return new StringPerfFunction(scan2, filelines_, numLines, uselen);
263    } else {
264        return new StringPerfFunction(scan2, StrBuffer, StrBufferLen, uselen);
265    }
266}
267
268UPerfFunction* StringPerformanceTest::TestStdLibCtor()
269{
270    if (line_mode) {
271        return new StringPerfFunction(StdLibCtor, filelines_, numLines, uselen);
272    } else {
273        return new StringPerfFunction(StdLibCtor, StrBuffer, StrBufferLen, uselen);
274    }
275}
276
277UPerfFunction* StringPerformanceTest::TestStdLibCtor1()
278{
279    if (line_mode) {
280        return new StringPerfFunction(StdLibCtor1, filelines_, numLines, uselen);
281    } else {
282        return new StringPerfFunction(StdLibCtor1, StrBuffer, StrBufferLen, uselen);
283    }
284}
285
286UPerfFunction* StringPerformanceTest::TestStdLibCtor2()
287{
288    if (line_mode) {
289        return new StringPerfFunction(StdLibCtor2, filelines_, numLines, uselen);
290    } else {
291        return new StringPerfFunction(StdLibCtor2, StrBuffer, StrBufferLen, uselen);
292    }
293}
294
295UPerfFunction* StringPerformanceTest::TestStdLibCtor3()
296{
297    if (line_mode) {
298        return new StringPerfFunction(StdLibCtor3, filelines_, numLines, uselen);
299    } else {
300        return new StringPerfFunction(StdLibCtor3, StrBuffer, StrBufferLen, uselen);
301    }
302}
303
304UPerfFunction* StringPerformanceTest::TestStdLibAssign()
305{
306    if (line_mode) {
307        return new StringPerfFunction(StdLibAssign, filelines_, numLines, uselen);
308    } else {
309        return new StringPerfFunction(StdLibAssign, StrBuffer, StrBufferLen, uselen);
310    }
311}
312
313UPerfFunction* StringPerformanceTest::TestStdLibAssign1()
314{
315    if (line_mode) {
316        return new StringPerfFunction(StdLibAssign1, filelines_, numLines, uselen);
317    } else {
318        return new StringPerfFunction(StdLibAssign1, StrBuffer, StrBufferLen, uselen);
319    }
320}
321
322UPerfFunction* StringPerformanceTest::TestStdLibAssign2()
323{
324    if (line_mode) {
325        return new StringPerfFunction(StdLibAssign2, filelines_, numLines, uselen);
326    } else {
327        return new StringPerfFunction(StdLibAssign2, StrBuffer, StrBufferLen, uselen);
328    }
329}
330
331UPerfFunction* StringPerformanceTest::TestStdLibGetch()
332{
333    if (line_mode) {
334        return new StringPerfFunction(StdLibGetch, filelines_, numLines, uselen);
335    } else {
336        return new StringPerfFunction(StdLibGetch, StrBuffer, StrBufferLen, uselen);
337    }
338}
339
340UPerfFunction* StringPerformanceTest::TestStdLibCatenate()
341{
342    if (line_mode) {
343        return new StringPerfFunction(StdLibCatenate, filelines_, numLines, uselen);
344    } else {
345        //return new StringPerfFunction(StdLibCatenate, buffer, bufferLen, uselen);
346        return new StringPerfFunction(StdLibCatenate, StrBuffer, StrBufferLen, uselen);
347    }
348}
349
350UPerfFunction* StringPerformanceTest::TestStdLibScan()
351{
352    if (line_mode) {
353        return new StringPerfFunction(StdLibScan, filelines_, numLines, uselen);
354    } else {
355        return new StringPerfFunction(StdLibScan, StrBuffer, StrBufferLen, uselen);
356    }
357}
358
359UPerfFunction* StringPerformanceTest::TestStdLibScan1()
360{
361    if (line_mode) {
362        return new StringPerfFunction(StdLibScan1, filelines_, numLines, uselen);
363    } else {
364        return new StringPerfFunction(StdLibScan1, StrBuffer, StrBufferLen, uselen);
365    }
366}
367
368UPerfFunction* StringPerformanceTest::TestStdLibScan2()
369{
370    if (line_mode) {
371        return new StringPerfFunction(StdLibScan2, filelines_, numLines, uselen);
372    } else {
373        return new StringPerfFunction(StdLibScan2, StrBuffer, StrBufferLen, uselen);
374    }
375}
376
377
378