author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 08 Apr 2010 14:19:33 +0300 | |
branch | RCL_3 |
changeset 7 | 3f74d0d4af4c |
parent 4 | 3b1da2848fc7 |
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 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 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
||
45 |
#include <QAtomicInt> |
|
46 |
#include <QCoreApplication> |
|
47 |
||
48 |
#include <limits.h> |
|
49 |
||
50 |
//TESTED_CLASS= |
|
51 |
//TESTED_FILES= |
|
52 |
||
53 |
class tst_QAtomicInt : public QObject |
|
54 |
{ |
|
55 |
Q_OBJECT |
|
56 |
||
57 |
public: |
|
58 |
tst_QAtomicInt(); |
|
59 |
~tst_QAtomicInt(); |
|
60 |
||
61 |
private slots: |
|
62 |
void warningFree(); |
|
63 |
||
64 |
// QAtomicInt members |
|
65 |
void constructor_data(); |
|
66 |
void constructor(); |
|
67 |
void copy_constructor_data(); |
|
68 |
void copy_constructor(); |
|
69 |
void equality_operator_data(); |
|
70 |
void equality_operator(); |
|
71 |
void inequality_operator_data(); |
|
72 |
void inequality_operator(); |
|
73 |
void not_operator_data(); |
|
74 |
void not_operator(); |
|
75 |
void cast_operator_data(); |
|
76 |
void cast_operator(); |
|
77 |
void assignment_operator_data(); |
|
78 |
void assignment_operator(); |
|
79 |
||
80 |
void isReferenceCountingNative(); |
|
81 |
void isReferenceCountingWaitFree(); |
|
82 |
void ref_data(); |
|
83 |
void ref(); |
|
84 |
void deref_data(); |
|
85 |
void deref(); |
|
86 |
||
87 |
void isTestAndSetNative(); |
|
88 |
void isTestAndSetWaitFree(); |
|
89 |
void testAndSet_data(); |
|
90 |
void testAndSet(); |
|
91 |
||
92 |
void isFetchAndStoreNative(); |
|
93 |
void isFetchAndStoreWaitFree(); |
|
94 |
void fetchAndStore_data(); |
|
95 |
void fetchAndStore(); |
|
96 |
||
97 |
void isFetchAndAddNative(); |
|
98 |
void isFetchAndAddWaitFree(); |
|
99 |
void fetchAndAdd_data(); |
|
100 |
void fetchAndAdd(); |
|
101 |
||
102 |
// stress tests |
|
103 |
void testAndSet_loop(); |
|
104 |
void fetchAndAdd_loop(); |
|
105 |
void fetchAndAdd_threadedLoop(); |
|
106 |
||
107 |
private: |
|
108 |
static void warningFreeHelper(); |
|
109 |
}; |
|
110 |
||
111 |
tst_QAtomicInt::tst_QAtomicInt() |
|
112 |
{ } |
|
113 |
||
114 |
tst_QAtomicInt::~tst_QAtomicInt() |
|
115 |
{ } |
|
116 |
||
117 |
void tst_QAtomicInt::warningFreeHelper() |
|
118 |
{ |
|
119 |
Q_ASSERT(false); |
|
120 |
// The code below is bogus, and shouldn't be run. We're looking for warnings, only. |
|
121 |
||
122 |
QBasicAtomicInt i = Q_BASIC_ATOMIC_INITIALIZER(0); |
|
123 |
||
124 |
int expectedValue = 0; |
|
125 |
int newValue = 0; |
|
126 |
int valueToAdd = 0; |
|
127 |
||
128 |
i.ref(); |
|
129 |
i.deref(); |
|
130 |
||
131 |
i.testAndSetRelaxed(expectedValue, newValue); |
|
132 |
i.testAndSetAcquire(expectedValue, newValue); |
|
133 |
i.testAndSetRelease(expectedValue, newValue); |
|
134 |
i.testAndSetOrdered(expectedValue, newValue); |
|
135 |
||
136 |
i.fetchAndStoreRelaxed(newValue); |
|
137 |
i.fetchAndStoreAcquire(newValue); |
|
138 |
i.fetchAndStoreRelease(newValue); |
|
139 |
i.fetchAndStoreOrdered(newValue); |
|
140 |
||
141 |
i.fetchAndAddRelaxed(valueToAdd); |
|
142 |
i.fetchAndAddAcquire(valueToAdd); |
|
143 |
i.fetchAndAddRelease(valueToAdd); |
|
144 |
i.fetchAndAddOrdered(valueToAdd); |
|
145 |
} |
|
146 |
||
147 |
void tst_QAtomicInt::warningFree() |
|
148 |
{ |
|
149 |
// This is a compile time check for warnings. |
|
150 |
// No need for actual work here. |
|
151 |
||
152 |
void (*foo)() = &warningFreeHelper; |
|
153 |
(void)foo; |
|
154 |
} |
|
155 |
||
156 |
void tst_QAtomicInt::constructor_data() |
|
157 |
{ |
|
158 |
QTest::addColumn<int>("value"); |
|
159 |
||
160 |
QTest::newRow("0") << 31337; |
|
161 |
QTest::newRow("1") << 0; |
|
162 |
QTest::newRow("2") << 1; |
|
163 |
QTest::newRow("3") << -1; |
|
164 |
QTest::newRow("4") << 2; |
|
165 |
QTest::newRow("5") << -2; |
|
166 |
QTest::newRow("6") << 3; |
|
167 |
QTest::newRow("7") << -3; |
|
168 |
QTest::newRow("8") << INT_MAX; |
|
169 |
QTest::newRow("9") << INT_MIN+1; |
|
170 |
} |
|
171 |
||
172 |
void tst_QAtomicInt::constructor() |
|
173 |
{ |
|
174 |
QFETCH(int, value); |
|
175 |
QAtomicInt atomic1(value); |
|
176 |
QCOMPARE(int(atomic1), value); |
|
177 |
QAtomicInt atomic2 = value; |
|
178 |
QCOMPARE(int(atomic2), value); |
|
179 |
} |
|
180 |
||
181 |
void tst_QAtomicInt::copy_constructor_data() |
|
182 |
{ constructor_data(); } |
|
183 |
||
184 |
void tst_QAtomicInt::copy_constructor() |
|
185 |
{ |
|
186 |
QFETCH(int, value); |
|
187 |
QAtomicInt atomic1(value); |
|
188 |
QCOMPARE(int(atomic1), value); |
|
189 |
||
190 |
QAtomicInt atomic2(atomic1); |
|
191 |
QCOMPARE(int(atomic2), value); |
|
192 |
QAtomicInt atomic3 = atomic1; |
|
193 |
QCOMPARE(int(atomic3), value); |
|
194 |
QAtomicInt atomic4(atomic2); |
|
195 |
QCOMPARE(int(atomic4), value); |
|
196 |
QAtomicInt atomic5 = atomic2; |
|
197 |
QCOMPARE(int(atomic5), value); |
|
198 |
} |
|
199 |
||
200 |
void tst_QAtomicInt::equality_operator_data() |
|
201 |
{ |
|
202 |
QTest::addColumn<int>("value1"); |
|
203 |
QTest::addColumn<int>("value2"); |
|
204 |
QTest::addColumn<int>("result"); |
|
205 |
||
206 |
QTest::newRow("success0") << 1 << 1 << 1; |
|
207 |
QTest::newRow("success1") << -1 << -1 << 1; |
|
208 |
QTest::newRow("failure0") << 0 << 1 << 0; |
|
209 |
QTest::newRow("failure1") << 1 << 0 << 0; |
|
210 |
QTest::newRow("failure2") << 0 << -1 << 0; |
|
211 |
QTest::newRow("failure3") << -1 << 0 << 0; |
|
212 |
} |
|
213 |
||
214 |
void tst_QAtomicInt::equality_operator() |
|
215 |
{ |
|
216 |
QFETCH(int, value1); |
|
217 |
QFETCH(int, value2); |
|
218 |
QAtomicInt x = value1; |
|
219 |
QTEST(x == value2 ? 1 : 0, "result"); |
|
220 |
} |
|
221 |
||
222 |
void tst_QAtomicInt::inequality_operator_data() |
|
223 |
{ |
|
224 |
QTest::addColumn<int>("value1"); |
|
225 |
QTest::addColumn<int>("value2"); |
|
226 |
QTest::addColumn<int>("result"); |
|
227 |
||
228 |
QTest::newRow("failure0") << 1 << 1 << 0; |
|
229 |
QTest::newRow("failure1") << -1 << -1 << 0; |
|
230 |
QTest::newRow("success0") << 0 << 1 << 1; |
|
231 |
QTest::newRow("success1") << 1 << 0 << 1; |
|
232 |
QTest::newRow("success2") << 0 << -1 << 1; |
|
233 |
QTest::newRow("success3") << -1 << 0 << 1; |
|
234 |
} |
|
235 |
||
236 |
void tst_QAtomicInt::inequality_operator() |
|
237 |
{ |
|
238 |
QFETCH(int, value1); |
|
239 |
QFETCH(int, value2); |
|
240 |
QAtomicInt x = value1; |
|
241 |
QTEST(x != value2 ? 1 : 0, "result"); |
|
242 |
} |
|
243 |
||
244 |
void tst_QAtomicInt::not_operator_data() |
|
245 |
{ constructor_data(); } |
|
246 |
||
247 |
void tst_QAtomicInt::not_operator() |
|
248 |
{ |
|
249 |
QFETCH(int, value); |
|
250 |
QAtomicInt atomic = value; |
|
251 |
QCOMPARE(!atomic, !value); |
|
252 |
} |
|
253 |
||
254 |
void tst_QAtomicInt::cast_operator_data() |
|
255 |
{ constructor_data(); } |
|
256 |
||
257 |
void tst_QAtomicInt::cast_operator() |
|
258 |
{ |
|
259 |
QFETCH(int, value); |
|
260 |
QAtomicInt atomic = value; |
|
261 |
int copy = atomic; |
|
262 |
QCOMPARE(copy, value); |
|
263 |
} |
|
264 |
||
265 |
void tst_QAtomicInt::assignment_operator_data() |
|
266 |
{ |
|
267 |
QTest::addColumn<int>("value"); |
|
268 |
QTest::addColumn<int>("newval"); |
|
269 |
||
270 |
QTest::newRow("value0") << 0 << 1; |
|
271 |
QTest::newRow("value1") << 1 << 0; |
|
272 |
QTest::newRow("value2") << 0 << -1; |
|
273 |
QTest::newRow("value3") << -1 << 0; |
|
274 |
QTest::newRow("value4") << -1 << 1; |
|
275 |
QTest::newRow("value5") << 1 << -1; |
|
276 |
} |
|
277 |
||
278 |
void tst_QAtomicInt::assignment_operator() |
|
279 |
{ |
|
280 |
QFETCH(int, value); |
|
281 |
QFETCH(int, newval); |
|
282 |
||
283 |
{ |
|
284 |
QAtomicInt atomic1 = value; |
|
285 |
atomic1 = newval; |
|
286 |
QCOMPARE(int(atomic1), newval); |
|
287 |
atomic1 = value; |
|
288 |
QCOMPARE(int(atomic1), value); |
|
289 |
QAtomicInt atomic2 = newval; |
|
290 |
atomic1 = atomic2; |
|
291 |
QCOMPARE(atomic1, atomic2); |
|
292 |
} |
|
293 |
} |
|
294 |
||
295 |
void tst_QAtomicInt::isReferenceCountingNative() |
|
296 |
{ |
|
297 |
#if defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE) |
|
298 |
// the runtime test should say the same thing |
|
299 |
QVERIFY(QAtomicInt::isReferenceCountingNative()); |
|
300 |
||
301 |
# if (defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE) \ |
|
302 |
|| defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE)) |
|
303 |
# error "Define only one of Q_ATOMIC_INT_REFERENCE_COUNTING_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE" |
|
304 |
# endif |
|
305 |
#elif defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE) |
|
306 |
// could be either, just want to make sure the function is implemented |
|
307 |
QVERIFY(QAtomicInt::isReferenceCountingNative() || !QAtomicInt::isReferenceCountingNative()); |
|
308 |
||
309 |
# if (defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE) \ |
|
310 |
|| defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE)) |
|
311 |
# error "Define only one of Q_ATOMIC_INT_REFERENCE_COUNTING_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE" |
|
312 |
# endif |
|
313 |
#elif defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE) |
|
314 |
// the runtime test should say the same thing |
|
315 |
QVERIFY(!QAtomicInt::isReferenceCountingNative()); |
|
316 |
||
317 |
# if (defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE) \ |
|
318 |
|| defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE)) |
|
319 |
# error "Define only one of Q_ATOMIC_INT_REFERENCE_COUNTING_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE" |
|
320 |
# endif |
|
321 |
#else |
|
322 |
# error "Q_ATOMIC_INT_REFERENCE_COUNTING_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE is not defined" |
|
323 |
#endif |
|
324 |
} |
|
325 |
||
326 |
void tst_QAtomicInt::isReferenceCountingWaitFree() |
|
327 |
{ |
|
328 |
#if defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE) |
|
329 |
// the runtime test should say the same thing |
|
330 |
QVERIFY(QAtomicInt::isReferenceCountingWaitFree()); |
|
331 |
||
332 |
// enforce some invariants |
|
333 |
QVERIFY(QAtomicInt::isReferenceCountingNative()); |
|
334 |
# if defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE) |
|
335 |
# error "Reference counting cannot be wait-free and unsupported at the same time!" |
|
336 |
# endif |
|
337 |
#else |
|
338 |
// the runtime test should say the same thing |
|
339 |
QVERIFY(!QAtomicInt::isReferenceCountingWaitFree()); |
|
340 |
#endif |
|
341 |
} |
|
342 |
||
343 |
void tst_QAtomicInt::ref_data() |
|
344 |
{ |
|
345 |
QTest::addColumn<int>("value"); |
|
346 |
QTest::addColumn<int>("result"); |
|
347 |
QTest::addColumn<int>("expected"); |
|
348 |
||
349 |
QTest::newRow("data0") << 0 << 1 << 1; |
|
350 |
QTest::newRow("data1") << -1 << 0 << 0; |
|
351 |
QTest::newRow("data2") << 1 << 1 << 2; |
|
352 |
} |
|
353 |
||
354 |
void tst_QAtomicInt::ref() |
|
355 |
{ |
|
356 |
QFETCH(int, value); |
|
357 |
QAtomicInt x = value; |
|
358 |
QTEST(x.ref() ? 1 : 0, "result"); |
|
359 |
QTEST(int(x), "expected"); |
|
360 |
} |
|
361 |
||
362 |
void tst_QAtomicInt::deref_data() |
|
363 |
{ |
|
364 |
QTest::addColumn<int>("value"); |
|
365 |
QTest::addColumn<int>("result"); |
|
366 |
QTest::addColumn<int>("expected"); |
|
367 |
||
368 |
QTest::newRow("data0") << 0 << 1 << -1; |
|
369 |
QTest::newRow("data1") << 1 << 0 << 0; |
|
370 |
QTest::newRow("data2") << 2 << 1 << 1; |
|
371 |
} |
|
372 |
||
373 |
void tst_QAtomicInt::deref() |
|
374 |
{ |
|
375 |
QFETCH(int, value); |
|
376 |
QAtomicInt x = value; |
|
377 |
QTEST(x.deref() ? 1 : 0, "result"); |
|
378 |
QTEST(int(x), "expected"); |
|
379 |
} |
|
380 |
||
381 |
void tst_QAtomicInt::isTestAndSetNative() |
|
382 |
{ |
|
383 |
#if defined(Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE) |
|
384 |
// the runtime test should say the same thing |
|
385 |
QVERIFY(QAtomicInt::isTestAndSetNative()); |
|
386 |
||
387 |
# if (defined(Q_ATOMIC_INT_TEST_AND_SET_IS_SOMETIMES_NATIVE) \ |
|
388 |
|| defined(Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE)) |
|
389 |
# error "Define only one of Q_ATOMIC_INT_TEST_AND_SET_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE" |
|
390 |
# endif |
|
391 |
#elif defined(Q_ATOMIC_INT_TEST_AND_SET_IS_SOMETIMES_NATIVE) |
|
392 |
// could be either, just want to make sure the function is implemented |
|
393 |
QVERIFY(QAtomicInt::isTestAndSetNative() || !QAtomicInt::isTestAndSetNative()); |
|
394 |
||
395 |
# if (defined(Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE) \ |
|
396 |
|| defined(Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE)) |
|
397 |
# error "Define only one of Q_ATOMIC_INT_TEST_AND_SET_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE" |
|
398 |
# endif |
|
399 |
#elif defined(Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE) |
|
400 |
// the runtime test should say the same thing |
|
401 |
QVERIFY(!QAtomicInt::isTestAndSetNative()); |
|
402 |
||
403 |
# if (defined(Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE) \ |
|
404 |
|| defined(Q_ATOMIC_INT_TEST_AND_SET_IS_SOMETIMES_NATIVE)) |
|
405 |
# error "Define only one of Q_ATOMIC_INT_TEST_AND_SET_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE" |
|
406 |
# endif |
|
407 |
#else |
|
408 |
# error "Q_ATOMIC_INT_TEST_AND_SET_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE is not defined" |
|
409 |
#endif |
|
410 |
} |
|
411 |
||
412 |
void tst_QAtomicInt::isTestAndSetWaitFree() |
|
413 |
{ |
|
414 |
#if defined(Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE) |
|
415 |
// the runtime test should say the same thing |
|
416 |
QVERIFY(QAtomicInt::isTestAndSetWaitFree()); |
|
417 |
||
418 |
// enforce some invariants |
|
419 |
QVERIFY(QAtomicInt::isTestAndSetNative()); |
|
420 |
# if defined(Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE) |
|
421 |
# error "Reference counting cannot be wait-free and unsupported at the same time!" |
|
422 |
# endif |
|
423 |
#else |
|
424 |
// the runtime test should say the same thing |
|
425 |
QVERIFY(!QAtomicInt::isTestAndSetWaitFree()); |
|
426 |
#endif |
|
427 |
} |
|
428 |
||
429 |
void tst_QAtomicInt::testAndSet_data() |
|
430 |
{ |
|
431 |
QTest::addColumn<int>("value"); |
|
432 |
QTest::addColumn<int>("expected"); |
|
433 |
QTest::addColumn<int>("newval"); |
|
434 |
QTest::addColumn<int>("result"); |
|
435 |
||
436 |
// these should succeed |
|
437 |
QTest::newRow("success0") << 0 << 0 << 0 << 1; |
|
438 |
QTest::newRow("success1") << 0 << 0 << 1 << 1; |
|
439 |
QTest::newRow("success2") << 0 << 0 << -1 << 1; |
|
440 |
QTest::newRow("success3") << 1 << 1 << 0 << 1; |
|
441 |
QTest::newRow("success4") << 1 << 1 << 1 << 1; |
|
442 |
QTest::newRow("success5") << 1 << 1 << -1 << 1; |
|
443 |
QTest::newRow("success6") << -1 << -1 << 0 << 1; |
|
444 |
QTest::newRow("success7") << -1 << -1 << 1 << 1; |
|
445 |
QTest::newRow("success8") << -1 << -1 << -1 << 1; |
|
446 |
QTest::newRow("success9") << INT_MIN+1 << INT_MIN+1 << INT_MIN+1 << 1; |
|
447 |
QTest::newRow("successA") << INT_MIN+1 << INT_MIN+1 << 1 << 1; |
|
448 |
QTest::newRow("successB") << INT_MIN+1 << INT_MIN+1 << -1 << 1; |
|
449 |
QTest::newRow("successC") << INT_MAX << INT_MAX << INT_MAX << 1; |
|
450 |
QTest::newRow("successD") << INT_MAX << INT_MAX << 1 << 1; |
|
451 |
QTest::newRow("successE") << INT_MAX << INT_MAX << -1 << 1; |
|
452 |
||
453 |
// these should fail |
|
454 |
QTest::newRow("failure0") << 0 << 1 << ~0 << 0; |
|
455 |
QTest::newRow("failure1") << 0 << -1 << ~0 << 0; |
|
456 |
QTest::newRow("failure2") << 1 << 0 << ~0 << 0; |
|
457 |
QTest::newRow("failure3") << -1 << 0 << ~0 << 0; |
|
458 |
QTest::newRow("failure4") << 1 << -1 << ~0 << 0; |
|
459 |
QTest::newRow("failure5") << -1 << 1 << ~0 << 0; |
|
460 |
QTest::newRow("failure6") << INT_MIN+1 << INT_MAX << ~0 << 0; |
|
461 |
QTest::newRow("failure7") << INT_MAX << INT_MIN+1 << ~0 << 0; |
|
462 |
} |
|
463 |
||
464 |
void tst_QAtomicInt::testAndSet() |
|
465 |
{ |
|
466 |
QFETCH(int, value); |
|
467 |
QFETCH(int, expected); |
|
468 |
QFETCH(int, newval); |
|
469 |
||
470 |
{ |
|
471 |
QAtomicInt atomic = value; |
|
472 |
QTEST(atomic.testAndSetRelaxed(expected, newval) ? 1 : 0, "result"); |
|
473 |
} |
|
474 |
||
475 |
{ |
|
476 |
QAtomicInt atomic = value; |
|
477 |
QTEST(atomic.testAndSetAcquire(expected, newval) ? 1 : 0, "result"); |
|
478 |
} |
|
479 |
||
480 |
{ |
|
481 |
QAtomicInt atomic = value; |
|
482 |
QTEST(atomic.testAndSetRelease(expected, newval) ? 1 : 0, "result"); |
|
483 |
} |
|
484 |
||
485 |
{ |
|
486 |
QAtomicInt atomic = value; |
|
487 |
QTEST(atomic.testAndSetOrdered(expected, newval) ? 1 : 0, "result"); |
|
488 |
} |
|
489 |
} |
|
490 |
||
491 |
void tst_QAtomicInt::isFetchAndStoreNative() |
|
492 |
{ |
|
493 |
#if defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE) |
|
494 |
// the runtime test should say the same thing |
|
495 |
QVERIFY(QAtomicInt::isFetchAndStoreNative()); |
|
496 |
||
497 |
# if (defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_SOMETIMES_NATIVE) \ |
|
498 |
|| defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE)) |
|
499 |
# error "Define only one of Q_ATOMIC_INT_FETCH_AND_STORE_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE" |
|
500 |
# endif |
|
501 |
#elif defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_SOMETIMES_NATIVE) |
|
502 |
// could be either, just want to make sure the function is implemented |
|
503 |
QVERIFY(QAtomicInt::isFetchAndStoreNative() || !QAtomicInt::isFetchAndStoreNative()); |
|
504 |
||
505 |
# if (defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE) \ |
|
506 |
|| defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE)) |
|
507 |
# error "Define only one of Q_ATOMIC_INT_FETCH_AND_STORE_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE" |
|
508 |
# endif |
|
509 |
#elif defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE) |
|
510 |
// the runtime test should say the same thing |
|
511 |
QVERIFY(!QAtomicInt::isFetchAndStoreNative()); |
|
512 |
||
513 |
# if (defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE) \ |
|
514 |
|| defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_SOMETIMES_NATIVE)) |
|
515 |
# error "Define only one of Q_ATOMIC_INT_FETCH_AND_STORE_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE" |
|
516 |
# endif |
|
517 |
#else |
|
518 |
# error "Q_ATOMIC_INT_FETCH_AND_STORE_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE is not defined" |
|
519 |
#endif |
|
520 |
} |
|
521 |
||
522 |
void tst_QAtomicInt::isFetchAndStoreWaitFree() |
|
523 |
{ |
|
524 |
#if defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE) |
|
525 |
// the runtime test should say the same thing |
|
526 |
QVERIFY(QAtomicInt::isFetchAndStoreWaitFree()); |
|
527 |
||
528 |
// enforce some invariants |
|
529 |
QVERIFY(QAtomicInt::isFetchAndStoreNative()); |
|
530 |
# if defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE) |
|
531 |
# error "Reference counting cannot be wait-free and unsupported at the same time!" |
|
532 |
# endif |
|
533 |
#else |
|
534 |
// the runtime test should say the same thing |
|
535 |
QVERIFY(!QAtomicInt::isFetchAndStoreWaitFree()); |
|
536 |
#endif |
|
537 |
} |
|
538 |
||
539 |
void tst_QAtomicInt::fetchAndStore_data() |
|
540 |
{ |
|
541 |
QTest::addColumn<int>("value"); |
|
542 |
QTest::addColumn<int>("newval"); |
|
543 |
||
544 |
QTest::newRow("data0") << 0 << 1; |
|
545 |
QTest::newRow("data1") << 1 << 2; |
|
546 |
QTest::newRow("data2") << 3 << 8; |
|
547 |
} |
|
548 |
||
549 |
void tst_QAtomicInt::fetchAndStore() |
|
550 |
{ |
|
551 |
QFETCH(int, value); |
|
552 |
QFETCH(int, newval); |
|
553 |
||
554 |
{ |
|
555 |
QAtomicInt atomic = value; |
|
556 |
QCOMPARE(atomic.fetchAndStoreRelaxed(newval), value); |
|
557 |
QCOMPARE(int(atomic), newval); |
|
558 |
} |
|
559 |
||
560 |
{ |
|
561 |
QAtomicInt atomic = value; |
|
562 |
QCOMPARE(atomic.fetchAndStoreAcquire(newval), value); |
|
563 |
QCOMPARE(int(atomic), newval); |
|
564 |
} |
|
565 |
||
566 |
{ |
|
567 |
QAtomicInt atomic = value; |
|
568 |
QCOMPARE(atomic.fetchAndStoreRelease(newval), value); |
|
569 |
QCOMPARE(int(atomic), newval); |
|
570 |
} |
|
571 |
||
572 |
{ |
|
573 |
QAtomicInt atomic = value; |
|
574 |
QCOMPARE(atomic.fetchAndStoreOrdered(newval), value); |
|
575 |
QCOMPARE(int(atomic), newval); |
|
576 |
} |
|
577 |
} |
|
578 |
||
579 |
void tst_QAtomicInt::isFetchAndAddNative() |
|
580 |
{ |
|
581 |
#if defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE) |
|
582 |
// the runtime test should say the same thing |
|
583 |
QVERIFY(QAtomicInt::isFetchAndAddNative()); |
|
584 |
||
585 |
# if (defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_SOMETIMES_NATIVE) \ |
|
586 |
|| defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE)) |
|
587 |
# error "Define only one of Q_ATOMIC_INT_FETCH_AND_ADD_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE" |
|
588 |
# endif |
|
589 |
#elif defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_SOMETIMES_NATIVE) |
|
590 |
// could be either, just want to make sure the function is implemented |
|
591 |
QVERIFY(QAtomicInt::isFetchAndAddNative() || !QAtomicInt::isFetchAndAddNative()); |
|
592 |
||
593 |
# if (defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE) \ |
|
594 |
|| defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE)) |
|
595 |
# error "Define only one of Q_ATOMIC_INT_FETCH_AND_ADD_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE" |
|
596 |
# endif |
|
597 |
#elif defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE) |
|
598 |
// the runtime test should say the same thing |
|
599 |
QVERIFY(!QAtomicInt::isFetchAndAddNative()); |
|
600 |
||
601 |
# if (defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE) \ |
|
602 |
|| defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_SOMETIMES_NATIVE)) |
|
603 |
# error "Define only one of Q_ATOMIC_INT_FETCH_AND_ADD_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE" |
|
604 |
# endif |
|
605 |
#else |
|
606 |
# error "Q_ATOMIC_INT_FETCH_AND_ADD_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE is not defined" |
|
607 |
#endif |
|
608 |
} |
|
609 |
||
610 |
void tst_QAtomicInt::isFetchAndAddWaitFree() |
|
611 |
{ |
|
612 |
#if defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE) |
|
613 |
// the runtime test should say the same thing |
|
614 |
QVERIFY(QAtomicInt::isFetchAndAddWaitFree()); |
|
615 |
||
616 |
// enforce some invariants |
|
617 |
QVERIFY(QAtomicInt::isFetchAndAddNative()); |
|
618 |
# if defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE) |
|
619 |
# error "Reference counting cannot be wait-free and unsupported at the same time!" |
|
620 |
# endif |
|
621 |
#else |
|
622 |
// the runtime test should say the same thing |
|
623 |
QVERIFY(!QAtomicInt::isFetchAndAddWaitFree()); |
|
624 |
#endif |
|
625 |
} |
|
626 |
||
627 |
void tst_QAtomicInt::fetchAndAdd_data() |
|
628 |
{ |
|
629 |
QTest::addColumn<int>("value1"); |
|
630 |
QTest::addColumn<int>("value2"); |
|
631 |
||
632 |
QTest::newRow("0+1") << 0 << 1; |
|
633 |
QTest::newRow("1+0") << 1 << 0; |
|
634 |
QTest::newRow("1+2") << 1 << 2; |
|
635 |
QTest::newRow("2+1") << 2 << 1; |
|
636 |
QTest::newRow("10+21") << 10 << 21; |
|
637 |
QTest::newRow("31+40") << 31 << 40; |
|
638 |
QTest::newRow("51+62") << 51 << 62; |
|
639 |
QTest::newRow("72+81") << 72 << 81; |
|
640 |
QTest::newRow("810+721") << 810 << 721; |
|
641 |
QTest::newRow("631+540") << 631 << 540; |
|
642 |
QTest::newRow("451+362") << 451 << 362; |
|
643 |
QTest::newRow("272+181") << 272 << 181; |
|
644 |
QTest::newRow("1810+8721") << 1810 << 8721; |
|
645 |
QTest::newRow("3631+6540") << 3631 << 6540; |
|
646 |
QTest::newRow("5451+4362") << 5451 << 4362; |
|
647 |
QTest::newRow("7272+2181") << 7272 << 2181; |
|
648 |
||
649 |
QTest::newRow("0+-1") << 0 << -1; |
|
650 |
QTest::newRow("1+0") << 1 << 0; |
|
651 |
QTest::newRow("1+-2") << 1 << -2; |
|
652 |
QTest::newRow("2+-1") << 2 << -1; |
|
653 |
QTest::newRow("10+-21") << 10 << -21; |
|
654 |
QTest::newRow("31+-40") << 31 << -40; |
|
655 |
QTest::newRow("51+-62") << 51 << -62; |
|
656 |
QTest::newRow("72+-81") << 72 << -81; |
|
657 |
QTest::newRow("810+-721") << 810 << -721; |
|
658 |
QTest::newRow("631+-540") << 631 << -540; |
|
659 |
QTest::newRow("451+-362") << 451 << -362; |
|
660 |
QTest::newRow("272+-181") << 272 << -181; |
|
661 |
QTest::newRow("1810+-8721") << 1810 << -8721; |
|
662 |
QTest::newRow("3631+-6540") << 3631 << -6540; |
|
663 |
QTest::newRow("5451+-4362") << 5451 << -4362; |
|
664 |
QTest::newRow("7272+-2181") << 7272 << -2181; |
|
665 |
||
666 |
QTest::newRow("0+1") << 0 << 1; |
|
667 |
QTest::newRow("-1+0") << -1 << 0; |
|
668 |
QTest::newRow("-1+2") << -1 << 2; |
|
669 |
QTest::newRow("-2+1") << -2 << 1; |
|
670 |
QTest::newRow("-10+21") << -10 << 21; |
|
671 |
QTest::newRow("-31+40") << -31 << 40; |
|
672 |
QTest::newRow("-51+62") << -51 << 62; |
|
673 |
QTest::newRow("-72+81") << -72 << 81; |
|
674 |
QTest::newRow("-810+721") << -810 << 721; |
|
675 |
QTest::newRow("-631+540") << -631 << 540; |
|
676 |
QTest::newRow("-451+362") << -451 << 362; |
|
677 |
QTest::newRow("-272+181") << -272 << 181; |
|
678 |
QTest::newRow("-1810+8721") << -1810 << 8721; |
|
679 |
QTest::newRow("-3631+6540") << -3631 << 6540; |
|
680 |
QTest::newRow("-5451+4362") << -5451 << 4362; |
|
681 |
QTest::newRow("-7272+2181") << -7272 << 2181; |
|
682 |
} |
|
683 |
||
684 |
void tst_QAtomicInt::fetchAndAdd() |
|
685 |
{ |
|
686 |
QFETCH(int, value1); |
|
687 |
QFETCH(int, value2); |
|
688 |
int result; |
|
689 |
||
690 |
{ |
|
691 |
QAtomicInt atomic = value1; |
|
692 |
result = atomic.fetchAndAddRelaxed(value2); |
|
693 |
QCOMPARE(result, value1); |
|
694 |
QCOMPARE(int(atomic), value1 + value2); |
|
695 |
} |
|
696 |
||
697 |
{ |
|
698 |
QAtomicInt atomic = value1; |
|
699 |
result = atomic.fetchAndAddAcquire(value2); |
|
700 |
QCOMPARE(result, value1); |
|
701 |
QCOMPARE(int(atomic), value1 + value2); |
|
702 |
} |
|
703 |
||
704 |
{ |
|
705 |
QAtomicInt atomic = value1; |
|
706 |
result = atomic.fetchAndAddRelease(value2); |
|
707 |
QCOMPARE(result, value1); |
|
708 |
QCOMPARE(int(atomic), value1 + value2); |
|
709 |
} |
|
710 |
||
711 |
{ |
|
712 |
QAtomicInt atomic = value1; |
|
713 |
result = atomic.fetchAndAddOrdered(value2); |
|
714 |
QCOMPARE(result, value1); |
|
715 |
QCOMPARE(int(atomic), value1 + value2); |
|
716 |
} |
|
717 |
} |
|
718 |
||
719 |
void tst_QAtomicInt::testAndSet_loop() |
|
720 |
{ |
|
721 |
QTime stopWatch; |
|
722 |
stopWatch.start(); |
|
723 |
||
724 |
int iterations = 10000000; |
|
725 |
||
726 |
QAtomicInt val=0; |
|
727 |
for (int i = 0; i < iterations; ++i) { |
|
728 |
QVERIFY(val.testAndSetRelaxed(val, val+1)); |
|
729 |
if ((i % 1000) == 999) { |
|
730 |
if (stopWatch.elapsed() > 60 * 1000) { |
|
731 |
// This test shouldn't run for more than two minutes. |
|
732 |
qDebug("Interrupted test after %d iterations (%.2f iterations/sec)", |
|
733 |
i, (i * 1000.0) / double(stopWatch.elapsed())); |
|
734 |
break; |
|
735 |
} |
|
736 |
} |
|
737 |
} |
|
738 |
} |
|
739 |
||
740 |
void tst_QAtomicInt::fetchAndAdd_loop() |
|
741 |
{ |
|
742 |
int iterations = 10000000; |
|
743 |
#if defined (Q_OS_HPUX) |
|
744 |
iterations = 1000000; |
|
745 |
#endif |
|
746 |
||
747 |
QAtomicInt val=0; |
|
748 |
for (int i = 0; i < iterations; ++i) { |
|
749 |
const int prev = val.fetchAndAddRelaxed(1); |
|
750 |
QCOMPARE(prev, int(val) -1); |
|
751 |
} |
|
752 |
} |
|
753 |
||
754 |
class FetchAndAddThread : public QThread |
|
755 |
{ |
|
756 |
public: |
|
757 |
void run() |
|
758 |
{ |
|
759 |
||
760 |
for (int i = 0; i < iterations; ++i) |
|
761 |
val->fetchAndAddAcquire(1); |
|
762 |
||
763 |
for (int i = 0; i < iterations; ++i) |
|
764 |
val->fetchAndAddAcquire(-1); |
|
765 |
||
766 |
} |
|
767 |
QAtomicInt *val; |
|
768 |
int iterations; |
|
769 |
}; |
|
770 |
||
771 |
||
772 |
void tst_QAtomicInt::fetchAndAdd_threadedLoop() |
|
773 |
{ |
|
774 |
QAtomicInt val; |
|
775 |
FetchAndAddThread t1; |
|
776 |
t1.val = &val; |
|
777 |
t1.iterations = 1000000; |
|
778 |
||
779 |
FetchAndAddThread t2; |
|
780 |
t2.val = &val; |
|
781 |
t2.iterations = 2000000; |
|
782 |
||
783 |
t1.start(); |
|
784 |
t2.start(); |
|
785 |
t1.wait(); |
|
786 |
t2.wait(); |
|
787 |
||
788 |
QCOMPARE(int(val), 0); |
|
789 |
} |
|
790 |
||
791 |
QTEST_MAIN(tst_QAtomicInt) |
|
792 |
#include "tst_qatomicint.moc" |