author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
// Select one of the scenarios below |
|
43 |
#define SCENARIO 1 |
|
44 |
||
45 |
#if SCENARIO == 1 |
|
46 |
// this is the "no harm done" version. Only operator% is active, |
|
47 |
// with NO_CAST * defined |
|
48 |
#define P % |
|
49 |
#undef QT_USE_FAST_OPERATOR_PLUS |
|
50 |
#undef QT_USE_FAST_CONCATENATION |
|
51 |
#define QT_NO_CAST_FROM_ASCII |
|
52 |
#define QT_NO_CAST_TO_ASCII |
|
53 |
#endif |
|
54 |
||
55 |
||
56 |
#if SCENARIO == 2 |
|
57 |
// this is the "full" version. Operator+ is replaced by a QStringBuilder |
|
58 |
// based version |
|
59 |
// with NO_CAST * defined |
|
60 |
#define P + |
|
61 |
#define QT_USE_FAST_OPERATOR_PLUS |
|
62 |
#define QT_USE_FAST_CONCATENATION |
|
63 |
#define QT_NO_CAST_FROM_ASCII |
|
64 |
#define QT_NO_CAST_TO_ASCII |
|
65 |
#endif |
|
66 |
||
67 |
#if SCENARIO == 3 |
|
68 |
// this is the "no harm done" version. Only operator% is active, |
|
69 |
// with NO_CAST * _not_ defined |
|
70 |
#define P % |
|
71 |
#undef QT_USE_FAST_OPERATOR_PLUS |
|
72 |
#undef QT_USE_FAST_CONCATENATION |
|
73 |
#undef QT_NO_CAST_FROM_ASCII |
|
74 |
#undef QT_NO_CAST_TO_ASCII |
|
75 |
#endif |
|
76 |
||
77 |
#if SCENARIO == 4 |
|
78 |
// this is the "full" version. Operator+ is replaced by a QStringBuilder |
|
79 |
// based version |
|
80 |
// with NO_CAST * _not_ defined |
|
81 |
#define P + |
|
82 |
#define QT_USE_FAST_OPERATOR_PLUS |
|
83 |
#define QT_USE_FAST_CONCATENATION |
|
84 |
#undef QT_NO_CAST_FROM_ASCII |
|
85 |
#undef QT_NO_CAST_TO_ASCII |
|
86 |
#endif |
|
87 |
||
88 |
||
89 |
#include <qbytearray.h> |
|
90 |
#include <qdebug.h> |
|
91 |
#include <qstring.h> |
|
92 |
#include <qstringbuilder.h> |
|
93 |
||
94 |
#include <qtest.h> |
|
95 |
||
96 |
#include <string> |
|
97 |
||
98 |
#define COMPARE(a, b) QCOMPARE(a, b) |
|
99 |
//#define COMPARE(a, b) |
|
100 |
||
101 |
#define SEP(s) qDebug() << "\n\n-------- " s " ---------"; |
|
102 |
||
103 |
#define LITERAL "some string literal" |
|
104 |
||
105 |
class tst_qstringbuilder : public QObject |
|
106 |
{ |
|
107 |
Q_OBJECT |
|
108 |
||
109 |
public: |
|
110 |
tst_qstringbuilder() |
|
111 |
: l1literal(LITERAL), |
|
112 |
l1string(LITERAL), |
|
113 |
ba(LITERAL), |
|
114 |
string(l1string), |
|
115 |
stdstring(LITERAL), |
|
116 |
stringref(&string, 2, 10), |
|
117 |
achar('c'), |
|
118 |
r2(QLatin1String(LITERAL LITERAL)), |
|
119 |
r3(QLatin1String(LITERAL LITERAL LITERAL)), |
|
120 |
r4(QLatin1String(LITERAL LITERAL LITERAL LITERAL)), |
|
121 |
r5(QLatin1String(LITERAL LITERAL LITERAL LITERAL LITERAL)) |
|
122 |
{} |
|
123 |
||
124 |
||
125 |
public: |
|
126 |
enum { N = 10000 }; |
|
127 |
||
128 |
int run_traditional() |
|
129 |
{ |
|
130 |
int s = 0; |
|
131 |
for (int i = 0; i < N; ++i) { |
|
132 |
#if 0 |
|
133 |
s += QString(l1string + l1string).size(); |
|
134 |
s += QString(l1string + l1string + l1string).size(); |
|
135 |
s += QString(l1string + l1string + l1string + l1string).size(); |
|
136 |
s += QString(l1string + l1string + l1string + l1string + l1string).size(); |
|
137 |
#endif |
|
138 |
s += QString(achar + l1string + achar).size(); |
|
139 |
} |
|
140 |
return s; |
|
141 |
} |
|
142 |
||
143 |
int run_builder() |
|
144 |
{ |
|
145 |
int s = 0; |
|
146 |
for (int i = 0; i < N; ++i) { |
|
147 |
#if 0 |
|
148 |
s += QString(l1literal P l1literal).size(); |
|
149 |
s += QString(l1literal P l1literal P l1literal).size(); |
|
150 |
s += QString(l1literal P l1literal P l1literal P l1literal).size(); |
|
151 |
s += QString(l1literal P l1literal P l1literal P l1literal P l1literal).size(); |
|
152 |
#endif |
|
153 |
s += QString(achar % l1literal % achar).size(); |
|
154 |
} |
|
155 |
return s; |
|
156 |
} |
|
157 |
||
158 |
private slots: |
|
159 |
||
160 |
void separator_0() { |
|
161 |
qDebug() << "\nIn each block the QStringBuilder based result appear first " |
|
162 |
"(with a 'b_' prefix), QStringBased second ('q_' prefix), std::string " |
|
163 |
"last ('s_' prefix)\n"; |
|
164 |
} |
|
165 |
||
166 |
void separator_1() { SEP("literal + literal (builder first)"); } |
|
167 |
||
168 |
void b_2_l1literal() { |
|
169 |
QBENCHMARK { r = l1literal P l1literal; } |
|
170 |
COMPARE(r, r2); |
|
171 |
} |
|
172 |
#ifndef QT_NO_CAST_FROM_ASCII |
|
173 |
void b_l1literal_LITERAL() { |
|
174 |
QBENCHMARK { r = l1literal P LITERAL; } |
|
175 |
COMPARE(r, r2); |
|
176 |
} |
|
177 |
#endif |
|
178 |
void q_2_l1string() { |
|
179 |
QBENCHMARK { r = l1string + l1string; } |
|
180 |
COMPARE(r, r2); |
|
181 |
} |
|
182 |
||
183 |
||
184 |
void separator_2() { SEP("2 strings"); } |
|
185 |
||
186 |
void b_2_string() { |
|
187 |
QBENCHMARK { r = string P string; } |
|
188 |
COMPARE(r, r2); |
|
189 |
} |
|
190 |
void q_2_string() { |
|
191 |
QBENCHMARK { r = string + string; } |
|
192 |
COMPARE(r, r2); |
|
193 |
} |
|
194 |
void s_2_string() { |
|
195 |
QBENCHMARK { stdr = stdstring + stdstring; } |
|
196 |
COMPARE(stdr, stdstring + stdstring); |
|
197 |
} |
|
198 |
||
199 |
||
200 |
void separator_2c() { SEP("2 string refs"); } |
|
201 |
||
202 |
void b_2_stringref() { |
|
203 |
QBENCHMARK { r = stringref % stringref; } |
|
204 |
COMPARE(r, QString(stringref.toString() + stringref.toString())); |
|
205 |
} |
|
206 |
void q_2_stringref() { |
|
207 |
QBENCHMARK { r = stringref.toString() + stringref.toString(); } |
|
208 |
COMPARE(r, QString(stringref % stringref)); |
|
209 |
} |
|
210 |
||
211 |
||
212 |
void separator_2b() { SEP("3 strings"); } |
|
213 |
||
214 |
void b_3_string() { |
|
215 |
QBENCHMARK { r = string P string P string; } |
|
216 |
COMPARE(r, r3); |
|
217 |
} |
|
218 |
void q_3_string() { |
|
219 |
QBENCHMARK { r = string + string + string; } |
|
220 |
COMPARE(r, r3); |
|
221 |
} |
|
222 |
void s_3_string() { |
|
223 |
QBENCHMARK { stdr = stdstring + stdstring + stdstring; } |
|
224 |
COMPARE(stdr, stdstring + stdstring + stdstring); |
|
225 |
} |
|
226 |
||
227 |
void separator_2e() { SEP("4 strings"); } |
|
228 |
||
229 |
void b_4_string() { |
|
230 |
QBENCHMARK { r = string P string P string P string; } |
|
231 |
COMPARE(r, r4); |
|
232 |
} |
|
233 |
void q_4_string() { |
|
234 |
QBENCHMARK { r = string + string + string + string; } |
|
235 |
COMPARE(r, r4); |
|
236 |
} |
|
237 |
void s_4_string() { |
|
238 |
QBENCHMARK { stdr = stdstring + stdstring + stdstring + stdstring; } |
|
239 |
COMPARE(stdr, stdstring + stdstring + stdstring + stdstring); |
|
240 |
} |
|
241 |
||
242 |
||
243 |
||
244 |
void separator_2a() { SEP("string + literal (builder first)"); } |
|
245 |
||
246 |
void b_string_l1literal() { |
|
247 |
QBENCHMARK { r = string % l1literal; } |
|
248 |
COMPARE(r, r2); |
|
249 |
} |
|
250 |
#ifndef QT_NO_CAST_FROM_ASCII |
|
251 |
void b_string_LITERAL() { |
|
252 |
QBENCHMARK { r = string P LITERAL; } |
|
253 |
COMPARE(r, r2); |
|
254 |
} |
|
255 |
void b_LITERAL_string() { |
|
256 |
QBENCHMARK { r = LITERAL P string; } |
|
257 |
COMPARE(r, r2); |
|
258 |
} |
|
259 |
#endif |
|
260 |
void b_string_l1string() { |
|
261 |
QBENCHMARK { r = string P l1string; } |
|
262 |
COMPARE(r, r2); |
|
263 |
} |
|
264 |
void q_string_l1literal() { |
|
265 |
QBENCHMARK { r = string + l1string; } |
|
266 |
COMPARE(r, r2); |
|
267 |
} |
|
268 |
void q_string_l1string() { |
|
269 |
QBENCHMARK { r = string + l1string; } |
|
270 |
COMPARE(r, r2); |
|
271 |
} |
|
272 |
void s_LITERAL_string() { |
|
273 |
QBENCHMARK { stdr = LITERAL + stdstring; } |
|
274 |
COMPARE(stdr, stdstring + stdstring); |
|
275 |
} |
|
276 |
||
277 |
||
278 |
void separator_3() { SEP("3 literals"); } |
|
279 |
||
280 |
void b_3_l1literal() { |
|
281 |
QBENCHMARK { r = l1literal P l1literal P l1literal; } |
|
282 |
COMPARE(r, r3); |
|
283 |
} |
|
284 |
void q_3_l1string() { |
|
285 |
QBENCHMARK { r = l1string + l1string + l1string; } |
|
286 |
COMPARE(r, r3); |
|
287 |
} |
|
288 |
void s_3_l1string() { |
|
289 |
QBENCHMARK { stdr = stdstring + LITERAL + LITERAL; } |
|
290 |
COMPARE(stdr, stdstring + stdstring + stdstring); |
|
291 |
} |
|
292 |
||
293 |
||
294 |
void separator_4() { SEP("4 literals"); } |
|
295 |
||
296 |
void b_4_l1literal() { |
|
297 |
QBENCHMARK { r = l1literal P l1literal P l1literal P l1literal; } |
|
298 |
COMPARE(r, r4); |
|
299 |
} |
|
300 |
void q_4_l1string() { |
|
301 |
QBENCHMARK { r = l1string + l1string + l1string + l1string; } |
|
302 |
COMPARE(r, r4); |
|
303 |
} |
|
304 |
||
305 |
||
306 |
void separator_5() { SEP("5 literals"); } |
|
307 |
||
308 |
void b_5_l1literal() { |
|
309 |
QBENCHMARK { r = l1literal P l1literal P l1literal P l1literal P l1literal; } |
|
310 |
COMPARE(r, r5); |
|
311 |
} |
|
312 |
||
313 |
void q_5_l1string() { |
|
314 |
QBENCHMARK { r = l1string + l1string + l1string + l1string + l1string; } |
|
315 |
COMPARE(r, r5); |
|
316 |
} |
|
317 |
||
318 |
||
319 |
void separator_6() { SEP("4 chars"); } |
|
320 |
||
321 |
void b_string_4_char() { |
|
322 |
QBENCHMARK { r = string + achar + achar + achar + achar; } |
|
323 |
COMPARE(r, QString(string P achar P achar P achar P achar)); |
|
324 |
} |
|
325 |
||
326 |
void q_string_4_char() { |
|
327 |
QBENCHMARK { r = string + achar + achar + achar + achar; } |
|
328 |
COMPARE(r, QString(string P achar P achar P achar P achar)); |
|
329 |
} |
|
330 |
||
331 |
void s_string_4_char() { |
|
332 |
QBENCHMARK { stdr = stdstring + 'c' + 'c' + 'c' + 'c'; } |
|
333 |
COMPARE(stdr, stdstring + 'c' + 'c' + 'c' + 'c'); |
|
334 |
} |
|
335 |
||
336 |
||
337 |
void separator_7() { SEP("char + string + char"); } |
|
338 |
||
339 |
void b_char_string_char() { |
|
340 |
QBENCHMARK { r = achar + string + achar; } |
|
341 |
COMPARE(r, QString(achar P string P achar)); |
|
342 |
} |
|
343 |
||
344 |
void q_char_string_char() { |
|
345 |
QBENCHMARK { r = achar + string + achar; } |
|
346 |
COMPARE(r, QString(achar P string P achar)); |
|
347 |
} |
|
348 |
||
349 |
void s_char_string_char() { |
|
350 |
QBENCHMARK { stdr = 'c' + stdstring + 'c'; } |
|
351 |
COMPARE(stdr, 'c' + stdstring + 'c'); |
|
352 |
} |
|
353 |
||
354 |
||
355 |
void separator_8() { SEP("string.arg"); } |
|
356 |
||
357 |
void b_string_arg() { |
|
358 |
const QString pattern = l1string + QString::fromLatin1("%1") + l1string; |
|
359 |
QBENCHMARK { r = l1literal P string P l1literal; } |
|
360 |
COMPARE(r, r3); |
|
361 |
} |
|
362 |
||
363 |
void q_string_arg() { |
|
364 |
const QString pattern = l1string + QLatin1String("%1") + l1string; |
|
365 |
QBENCHMARK { r = pattern.arg(string); } |
|
366 |
COMPARE(r, r3); |
|
367 |
} |
|
368 |
||
369 |
void q_bytearray_arg() { |
|
370 |
QByteArray result; |
|
371 |
QBENCHMARK { result = ba + ba + ba; } |
|
372 |
} |
|
373 |
||
374 |
||
375 |
void separator_9() { SEP("QString::reserve()"); } |
|
376 |
||
377 |
void b_reserve() { |
|
378 |
QBENCHMARK { |
|
379 |
r.clear(); |
|
380 |
r = string P string P string P string; |
|
381 |
} |
|
382 |
COMPARE(r, r4); |
|
383 |
} |
|
384 |
void b_reserve_lit() { |
|
385 |
QBENCHMARK { |
|
386 |
r.clear(); |
|
387 |
r = string P l1literal P string P string; |
|
388 |
} |
|
389 |
COMPARE(r, r4); |
|
390 |
} |
|
391 |
void s_reserve() { |
|
392 |
QBENCHMARK { |
|
393 |
r.clear(); |
|
394 |
r.reserve(string.size() + string.size() + string.size() + string.size()); |
|
395 |
r += string; |
|
396 |
r += string; |
|
397 |
r += string; |
|
398 |
r += string; |
|
399 |
} |
|
400 |
COMPARE(r, r4); |
|
401 |
} |
|
402 |
void s_reserve_lit() { |
|
403 |
QBENCHMARK { |
|
404 |
r.clear(); |
|
405 |
//r.reserve(string.size() + qstrlen(l1string.latin1()) |
|
406 |
// + string.size() + string.size()); |
|
407 |
r.reserve(1024); |
|
408 |
r += string; |
|
409 |
r += l1string; |
|
410 |
r += string; |
|
411 |
r += string; |
|
412 |
} |
|
413 |
COMPARE(r, r4); |
|
414 |
} |
|
415 |
||
416 |
private: |
|
417 |
const QLatin1Literal l1literal; |
|
418 |
const QLatin1String l1string; |
|
419 |
const QByteArray ba; |
|
420 |
const QString string; |
|
421 |
const std::string stdstring; |
|
422 |
const QStringRef stringref; |
|
423 |
const QLatin1Char achar; |
|
424 |
const QString r2, r3, r4, r5; |
|
425 |
||
426 |
// short cuts for results |
|
427 |
QString r; |
|
428 |
std::string stdr; |
|
429 |
}; |
|
430 |
||
431 |
||
432 |
//void operator%(QString, int) {} |
|
433 |
||
434 |
int main(int argc, char *argv[]) |
|
435 |
{ |
|
436 |
//qDebug() << (QString("xx") * QLatin1String("y")).toString(); |
|
437 |
//42 % 3; // Sanity test, should always work. |
|
438 |
//QString("x") % 2; // Sanity test, should only compile when the |
|
439 |
// operator%(QString, int) is visible. |
|
440 |
||
441 |
if (argc == 2 && (QLatin1String(argv[1]) == QLatin1String("--run-builder") |
|
442 |
|| QLatin1String(argv[1]) == QLatin1String("-b"))) { |
|
443 |
tst_qstringbuilder test; |
|
444 |
return test.run_builder(); |
|
445 |
} |
|
446 |
||
447 |
if (argc == 2 && (QLatin1String(argv[1]) == QLatin1String("--run-traditional") |
|
448 |
|| QLatin1String(argv[1]) == QLatin1String("-t"))) { |
|
449 |
tst_qstringbuilder test; |
|
450 |
return test.run_traditional(); |
|
451 |
} |
|
452 |
||
453 |
if (argc == 1) { |
|
454 |
QCoreApplication app(argc, argv); |
|
455 |
QStringList args = app.arguments(); |
|
456 |
tst_qstringbuilder test; |
|
457 |
return QTest::qExec(&test, argc, argv); |
|
458 |
} |
|
459 |
||
460 |
qDebug() << "Usage: " << argv[0] << " [--run-builder|-r|--run-traditional|-t]"; |
|
461 |
} |
|
462 |
||
463 |
||
464 |
#include "main.moc" |