summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/thread/qatomicint/tst_qatomicint.cpp
blob: bef491d5f0d9d2e720411d676a965ce26f1c04ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2016 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/QtTest>

#include <QAtomicInt>
#include <QCoreApplication>
#include <QElapsedTimer>

#include <limits.h>

class tst_QAtomicInt : public QObject
{
    Q_OBJECT

private slots:
    void warningFree();
    void alignment();

    // QAtomicInt members
    void constructor_data();
    void constructor();
    void copy_constructor_data();
    void copy_constructor();
    void assignment_operator_data();
    void assignment_operator();

    void isReferenceCountingNative();
    void isReferenceCountingWaitFree();
    void ref_data();
    void ref();
    void deref_data();
    void deref();

    void isTestAndSetNative();
    void isTestAndSetWaitFree();
    void testAndSet_data();
    void testAndSet();

    void isFetchAndStoreNative();
    void isFetchAndStoreWaitFree();
    void fetchAndStore_data();
    void fetchAndStore();

    void isFetchAndAddNative();
    void isFetchAndAddWaitFree();
    void fetchAndAdd_data();
    void fetchAndAdd();

    void operators();

    // stress tests
    void testAndSet_loop();
    void fetchAndAdd_loop();
    void fetchAndAdd_threadedLoop();

private:
    static void warningFreeHelper();
};

template <int I>
static inline void assemblyMarker(void *ptr = 0)
{
    puts((char *)ptr + I);
}

template <typename  T, typename Atomic>
static void warningFreeHelperTemplate()
{
    T expectedValue = 0;
    T newValue = 0;
    T valueToAdd = 0;

    // the marker calls are here only to provide a divider for
    // those reading the assembly output
    assemblyMarker<0>();
    Atomic i = Q_BASIC_ATOMIC_INITIALIZER(0);
    printf("%d\n", int(i.loadAcquire()));
    assemblyMarker<1>(&i);

    // the loads sometimes generate no assembly output
    i.loadRelaxed();
    assemblyMarker<11>(&i);
    i.loadAcquire();
    assemblyMarker<12>(&i);

    i.storeRelaxed(newValue);
    assemblyMarker<21>(&i);
    i.storeRelease(newValue);
    assemblyMarker<22>(&i);

    i.ref();
    assemblyMarker<31>(&i);
    i.deref();
    assemblyMarker<32>(&i);

    i.testAndSetRelaxed(expectedValue, newValue);
    assemblyMarker<41>(&i);
    i.testAndSetAcquire(expectedValue, newValue);
    assemblyMarker<42>(&i);
    i.testAndSetRelease(expectedValue, newValue);
    assemblyMarker<43>(&i);
    i.testAndSetOrdered(expectedValue, newValue);
    assemblyMarker<44>(&i);

    i.fetchAndStoreRelaxed(newValue);
    assemblyMarker<51>(&i);
    i.fetchAndStoreAcquire(newValue);
    assemblyMarker<52>(&i);
    i.fetchAndStoreRelease(newValue);
    assemblyMarker<53>(&i);
    i.fetchAndStoreOrdered(newValue);
    assemblyMarker<54>(&i);

    i.fetchAndAddRelaxed(valueToAdd);
    assemblyMarker<61>(&i);
    i.fetchAndAddAcquire(valueToAdd);
    assemblyMarker<62>(&i);
    i.fetchAndAddRelease(valueToAdd);
    assemblyMarker<63>(&i);
    i.fetchAndAddOrdered(valueToAdd);
    assemblyMarker<64>(&i);
}

template <bool> inline void booleanHelper()
{ }

template <typename Atomic>
static void constexprFunctionsHelperTemplate()
{
#ifdef Q_COMPILER_CONSTEXPR
    // this is a compile-time test only
    booleanHelper<Atomic::isReferenceCountingNative()>();
    booleanHelper<Atomic::isReferenceCountingWaitFree()>();
    booleanHelper<Atomic::isTestAndSetNative()>();
    booleanHelper<Atomic::isTestAndSetWaitFree()>();
    booleanHelper<Atomic::isFetchAndStoreNative()>();
    booleanHelper<Atomic::isFetchAndStoreWaitFree()>();
    booleanHelper<Atomic::isFetchAndAddNative()>();
    booleanHelper<Atomic::isFetchAndAddWaitFree()>();
#endif
}

void tst_QAtomicInt::warningFreeHelper()
{
    qFatal("This code is bogus, and shouldn't be run. We're looking for compiler warnings only.");
    warningFreeHelperTemplate<int, QBasicAtomicInt>();

    // 32-bit are always supported:
    warningFreeHelperTemplate<int, QBasicAtomicInteger<int> >();
    warningFreeHelperTemplate<unsigned int, QBasicAtomicInteger<unsigned int> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<int> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<unsigned int> >();
# ifdef Q_COMPILER_UNICODE_STRINGS
    warningFreeHelperTemplate<qint16, QBasicAtomicInteger<char32_t> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<char32_t> >();
# endif

    // pointer-sized integers are always supported:
    warningFreeHelperTemplate<int, QBasicAtomicInteger<qptrdiff> >();
    warningFreeHelperTemplate<unsigned int, QBasicAtomicInteger<quintptr> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<qptrdiff> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<quintptr> >();

    // long is always supported because it's either 32-bit or pointer-sized:
    warningFreeHelperTemplate<int, QBasicAtomicInteger<long int> >();
    warningFreeHelperTemplate<unsigned int, QBasicAtomicInteger<unsigned long int> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<long int> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<unsigned long int> >();

#ifdef Q_ATOMIC_INT16_IS_SUPPORTED
    warningFreeHelperTemplate<qint16, QBasicAtomicInteger<qint16> >();
    warningFreeHelperTemplate<quint16, QBasicAtomicInteger<quint16> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<qint16> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<quint16> >();
# ifdef Q_COMPILER_UNICODE_STRINGS
    warningFreeHelperTemplate<qint16, QBasicAtomicInteger<char16_t> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<char16_t> >();
# endif
#endif

#ifdef Q_ATOMIC_INT8_IS_SUPPORTED
    warningFreeHelperTemplate<char, QBasicAtomicInteger<char> >();
    warningFreeHelperTemplate<signed char, QBasicAtomicInteger<signed char> >();
    warningFreeHelperTemplate<unsigned char, QBasicAtomicInteger<unsigned char> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<char> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<signed char> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<unsigned char> >();
#endif

#ifdef Q_ATOMIC_INT64_IS_SUPPORTED
#if !defined(__i386__) || (defined(Q_CC_GNU) && defined(__OPTIMIZE__))
    warningFreeHelperTemplate<qlonglong, QBasicAtomicInteger<qlonglong> >();
    warningFreeHelperTemplate<qulonglong, QBasicAtomicInteger<qulonglong> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<qlonglong> >();
    constexprFunctionsHelperTemplate<QBasicAtomicInteger<qulonglong> >();
#endif
#endif
}

void tst_QAtomicInt::warningFree()
{
    // This is a compile time check for warnings.
    // No need for actual work here.

    void (*foo)() = &warningFreeHelper;
    (void)foo;
}

template <typename T> struct TypeInStruct { T type; };

void tst_QAtomicInt::alignment()
{
#ifdef Q_ALIGNOF
    // this will cause a build error if the alignment isn't the same
    char dummy1[Q_ALIGNOF(QBasicAtomicInt) == Q_ALIGNOF(TypeInStruct<int>) ? 1 : -1];
    char dummy2[Q_ALIGNOF(QAtomicInt) == Q_ALIGNOF(TypeInStruct<int>) ? 1 : -1];
    (void)dummy1; (void)dummy2;

#ifdef Q_ATOMIC_INT32_IS_SUPPORTED
    QCOMPARE(Q_ALIGNOF(QBasicAtomicInteger<int>), Q_ALIGNOF(TypeInStruct<int>));
#endif

#ifdef Q_ATOMIC_INT16_IS_SUPPORTED
    QCOMPARE(Q_ALIGNOF(QBasicAtomicInteger<short>), Q_ALIGNOF(TypeInStruct<short>));
#endif

#ifdef Q_ATOMIC_INT8_IS_SUPPORTED
    QCOMPARE(Q_ALIGNOF(QBasicAtomicInteger<char>), Q_ALIGNOF(TypeInStruct<char>));
#endif

#ifdef Q_ATOMIC_INT64_IS_SUPPORTED
    QCOMPARE(Q_ALIGNOF(QBasicAtomicInteger<qlonglong>), Q_ALIGNOF(TypeInStruct<qlonglong>));
#endif

#endif
}

void tst_QAtomicInt::constructor_data()
{
    QTest::addColumn<int>("value");

    QTest::newRow("0") << 31337;
    QTest::newRow("1") << 0;
    QTest::newRow("2") << 1;
    QTest::newRow("3") << -1;
    QTest::newRow("4") << 2;
    QTest::newRow("5") << -2;
    QTest::newRow("6") << 3;
    QTest::newRow("7") << -3;
    QTest::newRow("8") << INT_MAX;
    QTest::newRow("9") << INT_MIN+1;
}

void tst_QAtomicInt::constructor()
{
    QFETCH(int, value);
    QAtomicInt atomic1(value);
    QCOMPARE(atomic1.loadRelaxed(), value);
    QAtomicInt atomic2 = value;
    QCOMPARE(atomic2.loadRelaxed(), value);
}

void tst_QAtomicInt::copy_constructor_data()
{ constructor_data(); }

void tst_QAtomicInt::copy_constructor()
{
    QFETCH(int, value);
    QAtomicInt atomic1(value);
    QCOMPARE(atomic1.loadRelaxed(), value);

    QAtomicInt atomic2(atomic1);
    QCOMPARE(atomic2.loadRelaxed(), value);
    QAtomicInt atomic3 = atomic1;
    QCOMPARE(atomic3.loadRelaxed(), value);
    QAtomicInt atomic4(atomic2);
    QCOMPARE(atomic4.loadRelaxed(), value);
    QAtomicInt atomic5 = atomic2;
    QCOMPARE(atomic5.loadRelaxed(), value);
}

void tst_QAtomicInt::assignment_operator_data()
{
    QTest::addColumn<int>("value");
    QTest::addColumn<int>("newval");

    QTest::newRow("value0") <<  0 <<  1;
    QTest::newRow("value1") <<  1 <<  0;
    QTest::newRow("value2") <<  0 << -1;
    QTest::newRow("value3") << -1 <<  0;
    QTest::newRow("value4") << -1 <<  1;
    QTest::newRow("value5") <<  1 << -1;
}

void tst_QAtomicInt::assignment_operator()
{
    QFETCH(int, value);
    QFETCH(int, newval);

    {
        QAtomicInt atomic1 = value;
        atomic1 = newval;
        QCOMPARE(atomic1.loadRelaxed(), newval);
        atomic1 = value;
        QCOMPARE(atomic1.loadRelaxed(), value);

        QAtomicInt atomic2 = newval;
        atomic1 = atomic2;
        QCOMPARE(atomic1.loadRelaxed(), atomic2.loadRelaxed());
    }
}

void tst_QAtomicInt::isReferenceCountingNative()
{
#if defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE)
    // the runtime test should say the same thing
    QVERIFY(QAtomicInt::isReferenceCountingNative());

#  if (defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE)     \
       || defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE))
#    error "Define only one of Q_ATOMIC_INT_REFERENCE_COUNTING_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE"
#  endif
#elif defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE)
    // could be either, just want to make sure the function is implemented
    QVERIFY(QAtomicInt::isReferenceCountingNative() || !QAtomicInt::isReferenceCountingNative());

#  if (defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE) \
       || defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE))
#    error "Define only one of Q_ATOMIC_INT_REFERENCE_COUNTING_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE"
#  endif
#elif defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE)
    // the runtime test should say the same thing
    QVERIFY(!QAtomicInt::isReferenceCountingNative());

#  if (defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE) \
       || defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE))
#    error "Define only one of Q_ATOMIC_INT_REFERENCE_COUNTING_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE"
#  endif
#else
#  error "Q_ATOMIC_INT_REFERENCE_COUNTING_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE is not defined"
#endif
}

void tst_QAtomicInt::isReferenceCountingWaitFree()
{
#if defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE)
    // the runtime test should say the same thing
    QVERIFY(QAtomicInt::isReferenceCountingWaitFree());

    // enforce some invariants
    QVERIFY(QAtomicInt::isReferenceCountingNative());
#  if defined(Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE)
#    error "Reference counting cannot be wait-free and unsupported at the same time!"
#  endif
#else
    // the runtime test should say the same thing
    QVERIFY(!QAtomicInt::isReferenceCountingWaitFree());
#endif
}

void tst_QAtomicInt::ref_data()
{
    QTest::addColumn<int>("value");
    QTest::addColumn<int>("result");
    QTest::addColumn<int>("expected");

    QTest::newRow("data0") <<  0 << 1 << 1;
    QTest::newRow("data1") << -1 << 0 << 0;
    QTest::newRow("data2") <<  1 << 1 << 2;
}

void tst_QAtomicInt::ref()
{
    QFETCH(int, value);
    QAtomicInt x = value;
    QTEST(x.ref() ? 1 : 0, "result");
    QTEST(x.loadRelaxed(), "expected");
}

void tst_QAtomicInt::deref_data()
{
    QTest::addColumn<int>("value");
    QTest::addColumn<int>("result");
    QTest::addColumn<int>("expected");

    QTest::newRow("data0") <<  0 << 1 << -1;
    QTest::newRow("data1") <<  1 << 0 <<  0;
    QTest::newRow("data2") <<  2 << 1 <<  1;
}

void tst_QAtomicInt::deref()
{
    QFETCH(int, value);
    QAtomicInt x = value;
    QTEST(x.deref() ? 1 : 0, "result");
    QTEST(x.loadRelaxed(), "expected");
}

void tst_QAtomicInt::isTestAndSetNative()
{
#if defined(Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE)
    // the runtime test should say the same thing
    QVERIFY(QAtomicInt::isTestAndSetNative());

#  if (defined(Q_ATOMIC_INT_TEST_AND_SET_IS_SOMETIMES_NATIVE)     \
       || defined(Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE))
#    error "Define only one of Q_ATOMIC_INT_TEST_AND_SET_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE"
#  endif
#elif defined(Q_ATOMIC_INT_TEST_AND_SET_IS_SOMETIMES_NATIVE)
    // could be either, just want to make sure the function is implemented
    QVERIFY(QAtomicInt::isTestAndSetNative() || !QAtomicInt::isTestAndSetNative());

#  if (defined(Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE) \
       || defined(Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE))
#    error "Define only one of Q_ATOMIC_INT_TEST_AND_SET_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE"
#  endif
#elif defined(Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE)
    // the runtime test should say the same thing
    QVERIFY(!QAtomicInt::isTestAndSetNative());

#  if (defined(Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE) \
       || defined(Q_ATOMIC_INT_TEST_AND_SET_IS_SOMETIMES_NATIVE))
#    error "Define only one of Q_ATOMIC_INT_TEST_AND_SET_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE"
#  endif
#else
#  error "Q_ATOMIC_INT_TEST_AND_SET_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE is not defined"
#endif
}

void tst_QAtomicInt::isTestAndSetWaitFree()
{
#if defined(Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE)
    // the runtime test should say the same thing
    QVERIFY(QAtomicInt::isTestAndSetWaitFree());

    // enforce some invariants
    QVERIFY(QAtomicInt::isTestAndSetNative());
#  if defined(Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE)
#    error "Reference counting cannot be wait-free and unsupported at the same time!"
#  endif
#else
    // the runtime test should say the same thing
    QVERIFY(!QAtomicInt::isTestAndSetWaitFree());
#endif
}

void tst_QAtomicInt::testAndSet_data()
{
    QTest::addColumn<int>("value");
    QTest::addColumn<int>("expected");
    QTest::addColumn<int>("newval");
    QTest::addColumn<bool>("result");

    // these should succeed
    QTest::newRow("success0") <<         0 <<         0 <<         0 << true;
    QTest::newRow("success1") <<         0 <<         0 <<         1 << true;
    QTest::newRow("success2") <<         0 <<         0 <<        -1 << true;
    QTest::newRow("success3") <<         1 <<         1 <<         0 << true;
    QTest::newRow("success4") <<         1 <<         1 <<         1 << true;
    QTest::newRow("success5") <<         1 <<         1 <<        -1 << true;
    QTest::newRow("success6") <<        -1 <<        -1 <<         0 << true;
    QTest::newRow("success7") <<        -1 <<        -1 <<         1 << true;
    QTest::newRow("success8") <<        -1 <<        -1 <<        -1 << true;
    QTest::newRow("success9") << INT_MIN+1 << INT_MIN+1 << INT_MIN+1 << true;
    QTest::newRow("successA") << INT_MIN+1 << INT_MIN+1 <<         1 << true;
    QTest::newRow("successB") << INT_MIN+1 << INT_MIN+1 <<        -1 << true;
    QTest::newRow("successC") << INT_MAX   << INT_MAX   << INT_MAX   << true;
    QTest::newRow("successD") << INT_MAX   << INT_MAX   <<         1 << true;
    QTest::newRow("successE") << INT_MAX   << INT_MAX   <<        -1 << true;

    // these should fail
    QTest::newRow("failure0") <<       0   <<       1   <<        ~0 << false;
    QTest::newRow("failure1") <<       0   <<      -1   <<        ~0 << false;
    QTest::newRow("failure2") <<       1   <<       0   <<        ~0 << false;
    QTest::newRow("failure3") <<      -1   <<       0   <<        ~0 << false;
    QTest::newRow("failure4") <<       1   <<      -1   <<        ~0 << false;
    QTest::newRow("failure5") <<      -1   <<       1   <<        ~0 << false;
    QTest::newRow("failure6") << INT_MIN+1 << INT_MAX   <<        ~0 << false;
    QTest::newRow("failure7") << INT_MAX   << INT_MIN+1 <<        ~0 << false;
}

void tst_QAtomicInt::testAndSet()
{
    QFETCH(int, value);
    QFETCH(int, expected);
    QFETCH(int, newval);

    {
        QAtomicInt atomic = value;
        QTEST(atomic.testAndSetRelaxed(expected, newval), "result");
    }

    {
        QAtomicInt atomic = value;
        QTEST(atomic.testAndSetAcquire(expected, newval), "result");
    }

    {
        QAtomicInt atomic = value;
        QTEST(atomic.testAndSetRelease(expected, newval), "result");
    }

    {
        QAtomicInt atomic = value;
        QTEST(atomic.testAndSetOrdered(expected, newval), "result");
    }

#ifdef Q_ATOMIC_INT32_IS_SUPPORTED
    QFETCH(bool, result);
    // the new implementation has the version that loads the current value

    {
        QAtomicInt atomic = value;
        int currentval = 0xdeadbeef;
        QCOMPARE(atomic.testAndSetRelaxed(expected, newval, currentval), result);
        if (!result)
            QCOMPARE(currentval, value);
    }

    {
        QAtomicInt atomic = value;
        int currentval = 0xdeadbeef;
        QCOMPARE(atomic.testAndSetAcquire(expected, newval, currentval), result);
        if (!result)
            QCOMPARE(currentval, value);
    }

    {
        QAtomicInt atomic = value;
        int currentval = 0xdeadbeef;
        QCOMPARE(atomic.testAndSetRelease(expected, newval, currentval), result);
        if (!result)
            QCOMPARE(currentval, value);
    }

    {
        QAtomicInt atomic = value;
        int currentval = 0xdeadbeef;
        QCOMPARE(atomic.testAndSetOrdered(expected, newval, currentval), result);
        if (!result)
            QCOMPARE(currentval, value);
    }
#endif
}

void tst_QAtomicInt::isFetchAndStoreNative()
{
#if defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE)
    // the runtime test should say the same thing
    QVERIFY(QAtomicInt::isFetchAndStoreNative());

#  if (defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_SOMETIMES_NATIVE)     \
       || defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE))
#    error "Define only one of Q_ATOMIC_INT_FETCH_AND_STORE_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE"
#  endif
#elif defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_SOMETIMES_NATIVE)
    // could be either, just want to make sure the function is implemented
    QVERIFY(QAtomicInt::isFetchAndStoreNative() || !QAtomicInt::isFetchAndStoreNative());

#  if (defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE) \
       || defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE))
#    error "Define only one of Q_ATOMIC_INT_FETCH_AND_STORE_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE"
#  endif
#elif defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE)
    // the runtime test should say the same thing
    QVERIFY(!QAtomicInt::isFetchAndStoreNative());

#  if (defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE) \
       || defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_SOMETIMES_NATIVE))
#    error "Define only one of Q_ATOMIC_INT_FETCH_AND_STORE_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE"
#  endif
#else
#  error "Q_ATOMIC_INT_FETCH_AND_STORE_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE is not defined"
#endif
}

void tst_QAtomicInt::isFetchAndStoreWaitFree()
{
#if defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE)
    // the runtime test should say the same thing
    QVERIFY(QAtomicInt::isFetchAndStoreWaitFree());

    // enforce some invariants
    QVERIFY(QAtomicInt::isFetchAndStoreNative());
#  if defined(Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE)
#    error "Reference counting cannot be wait-free and unsupported at the same time!"
#  endif
#else
    // the runtime test should say the same thing
    QVERIFY(!QAtomicInt::isFetchAndStoreWaitFree());
#endif
}

void tst_QAtomicInt::fetchAndStore_data()
{
    QTest::addColumn<int>("value");
    QTest::addColumn<int>("newval");

    QTest::newRow("data0") << 0 << 1;
    QTest::newRow("data1") << 1 << 2;
    QTest::newRow("data2") << 3 << 8;
}

void tst_QAtomicInt::fetchAndStore()
{
    QFETCH(int, value);
    QFETCH(int, newval);

    {
        QAtomicInt atomic = value;
        QCOMPARE(atomic.fetchAndStoreRelaxed(newval), value);
        QCOMPARE(atomic.loadRelaxed(), newval);
    }

    {
        QAtomicInt atomic = value;
        QCOMPARE(atomic.fetchAndStoreAcquire(newval), value);
        QCOMPARE(atomic.loadRelaxed(), newval);
    }

    {
        QAtomicInt atomic = value;
        QCOMPARE(atomic.fetchAndStoreRelease(newval), value);
        QCOMPARE(atomic.loadRelaxed(), newval);
    }

    {
        QAtomicInt atomic = value;
        QCOMPARE(atomic.fetchAndStoreOrdered(newval), value);
        QCOMPARE(atomic.loadRelaxed(), newval);
    }
}

void tst_QAtomicInt::isFetchAndAddNative()
{
#if defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE)
    // the runtime test should say the same thing
    QVERIFY(QAtomicInt::isFetchAndAddNative());

#  if (defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_SOMETIMES_NATIVE)     \
       || defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE))
#    error "Define only one of Q_ATOMIC_INT_FETCH_AND_ADD_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE"
#  endif
#elif defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_SOMETIMES_NATIVE)
    // could be either, just want to make sure the function is implemented
    QVERIFY(QAtomicInt::isFetchAndAddNative() || !QAtomicInt::isFetchAndAddNative());

#  if (defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE) \
       || defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE))
#    error "Define only one of Q_ATOMIC_INT_FETCH_AND_ADD_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE"
#  endif
#elif defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE)
    // the runtime test should say the same thing
    QVERIFY(!QAtomicInt::isFetchAndAddNative());

#  if (defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE) \
       || defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_SOMETIMES_NATIVE))
#    error "Define only one of Q_ATOMIC_INT_FETCH_AND_ADD_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE"
#  endif
#else
#  error "Q_ATOMIC_INT_FETCH_AND_ADD_IS_{ALWAYS,SOMTIMES,NOT}_NATIVE is not defined"
#endif
}

void tst_QAtomicInt::isFetchAndAddWaitFree()
{
#if defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE)
    // the runtime test should say the same thing
    QVERIFY(QAtomicInt::isFetchAndAddWaitFree());

    // enforce some invariants
    QVERIFY(QAtomicInt::isFetchAndAddNative());
#  if defined(Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE)
#    error "Reference counting cannot be wait-free and unsupported at the same time!"
#  endif
#else
    // the runtime test should say the same thing
    QVERIFY(!QAtomicInt::isFetchAndAddWaitFree());
#endif
}

void tst_QAtomicInt::fetchAndAdd_data()
{
    QTest::addColumn<int>("value1");
    QTest::addColumn<int>("value2");

    QTest::newRow("0+1") << 0 << 1;
    QTest::newRow("1+0") << 1 << 0;
    QTest::newRow("1+2") << 1 << 2;
    QTest::newRow("2+1") << 2 << 1;
    QTest::newRow("10+21") << 10 << 21;
    QTest::newRow("31+40") << 31 << 40;
    QTest::newRow("51+62") << 51 << 62;
    QTest::newRow("72+81") << 72 << 81;
    QTest::newRow("810+721") << 810 << 721;
    QTest::newRow("631+540") << 631 << 540;
    QTest::newRow("451+362") << 451 << 362;
    QTest::newRow("272+181") << 272 << 181;
    QTest::newRow("1810+8721") << 1810 << 8721;
    QTest::newRow("3631+6540") << 3631 << 6540;
    QTest::newRow("5451+4362") << 5451 << 4362;
    QTest::newRow("7272+2181") << 7272 << 2181;

    QTest::newRow("0+-1") << 0 << -1;
    QTest::newRow("1+0") << 1 << 0;
    QTest::newRow("1+-2") << 1 << -2;
    QTest::newRow("2+-1") << 2 << -1;
    QTest::newRow("10+-21") << 10 << -21;
    QTest::newRow("31+-40") << 31 << -40;
    QTest::newRow("51+-62") << 51 << -62;
    QTest::newRow("72+-81") << 72 << -81;
    QTest::newRow("810+-721") << 810 << -721;
    QTest::newRow("631+-540") << 631 << -540;
    QTest::newRow("451+-362") << 451 << -362;
    QTest::newRow("272+-181") << 272 << -181;
    QTest::newRow("1810+-8721") << 1810 << -8721;
    QTest::newRow("3631+-6540") << 3631 << -6540;
    QTest::newRow("5451+-4362") << 5451 << -4362;
    QTest::newRow("7272+-2181") << 7272 << -2181;

    QTest::newRow("0+1") << 0 << 1;
    QTest::newRow("-1+0") << -1 << 0;
    QTest::newRow("-1+2") << -1 << 2;
    QTest::newRow("-2+1") << -2 << 1;
    QTest::newRow("-10+21") << -10 << 21;
    QTest::newRow("-31+40") << -31 << 40;
    QTest::newRow("-51+62") << -51 << 62;
    QTest::newRow("-72+81") << -72 << 81;
    QTest::newRow("-810+721") << -810 << 721;
    QTest::newRow("-631+540") << -631 << 540;
    QTest::newRow("-451+362") << -451 << 362;
    QTest::newRow("-272+181") << -272 << 181;
    QTest::newRow("-1810+8721") << -1810 << 8721;
    QTest::newRow("-3631+6540") << -3631 << 6540;
    QTest::newRow("-5451+4362") << -5451 << 4362;
    QTest::newRow("-7272+2181") << -7272 << 2181;
}

void tst_QAtomicInt::fetchAndAdd()
{
    QFETCH(int, value1);
    QFETCH(int, value2);
    int result;

    {
        QAtomicInt atomic = value1;
        result = atomic.fetchAndAddRelaxed(value2);
        QCOMPARE(result, value1);
        QCOMPARE(atomic.loadRelaxed(), value1 + value2);
    }

    {
        QAtomicInt atomic = value1;
        result = atomic.fetchAndAddAcquire(value2);
        QCOMPARE(result, value1);
        QCOMPARE(atomic.loadRelaxed(), value1 + value2);
    }

    {
        QAtomicInt atomic = value1;
        result = atomic.fetchAndAddRelease(value2);
        QCOMPARE(result, value1);
        QCOMPARE(atomic.loadRelaxed(), value1 + value2);
    }

    {
        QAtomicInt atomic = value1;
        result = atomic.fetchAndAddOrdered(value2);
        QCOMPARE(result, value1);
        QCOMPARE(atomic.loadRelaxed(), value1 + value2);
    }
}

void tst_QAtomicInt::operators()
{
    {
        // Test that QBasicAtomicInt also has operator= and cast operators
        // We've been using them for QAtomicInt elsewhere
        QBasicAtomicInt atomic = Q_BASIC_ATOMIC_INITIALIZER(0);
        atomic = 1;
        QCOMPARE(int(atomic), 1);
    }

    QAtomicInt atomic = 0;
    int x = ++atomic;
    QCOMPARE(int(atomic), x);
    QCOMPARE(int(atomic), 1);

    x = atomic++;
    QCOMPARE(int(atomic), x + 1);
    QCOMPARE(int(atomic), 2);

    x = atomic--;
    QCOMPARE(int(atomic), x - 1);
    QCOMPARE(int(atomic), 1);

    x = --atomic;
    QCOMPARE(int(atomic), x);
    QCOMPARE(int(atomic), 0);

    x = (atomic += 1);
    QCOMPARE(int(atomic), x);
    QCOMPARE(int(atomic), 1);

    x = (atomic -= 1);
    QCOMPARE(int(atomic), x);
    QCOMPARE(int(atomic), 0);

    x = (atomic |= 0xf);
    QCOMPARE(int(atomic), x);
    QCOMPARE(int(atomic), 0xf);

    x = (atomic &= 0x17);
    QCOMPARE(int(atomic), x);
    QCOMPARE(int(atomic), 7);

    x = (atomic ^= 0x14);
    QCOMPARE(int(atomic), x);
    QCOMPARE(int(atomic), 0x13);

    x = (atomic ^= atomic);
    QCOMPARE(int(atomic), x);
    QCOMPARE(int(atomic), 0);
}

void tst_QAtomicInt::testAndSet_loop()
{
    QElapsedTimer stopWatch;
    stopWatch.start();

    int iterations = 10000000;

    QAtomicInt val=0;
    for (int i = 0; i < iterations; ++i) {
        int v = val.loadRelaxed();
        QVERIFY(val.testAndSetRelaxed(v, v+1));
        if ((i % 1000) == 999) {
            if (stopWatch.elapsed() > 60 * 1000) {
                // This test shouldn't run for more than two minutes.
                qDebug("Interrupted test after %d iterations (%.2f iterations/sec)",
                       i, (i * 1000.0) / double(stopWatch.elapsed()));
                break;
            }
        }
    }
}

void tst_QAtomicInt::fetchAndAdd_loop()
{
    int iterations = 10000000;
#if defined (Q_OS_HPUX)
    iterations = 1000000;
#endif

    QAtomicInt val=0;
    for (int i = 0; i < iterations; ++i) {
        const int prev = val.fetchAndAddRelaxed(1);
        QCOMPARE(prev, val.loadRelaxed() -1);
    }
}

class FetchAndAddThread : public QThread
{
public:
    void run()
    {

        for (int i = 0; i < iterations; ++i)
            val->fetchAndAddAcquire(1);

        for (int i = 0; i < iterations; ++i)
            val->fetchAndAddAcquire(-1);

    }
QAtomicInt *val;
int iterations;
};


void tst_QAtomicInt::fetchAndAdd_threadedLoop()
{
    QAtomicInt val;
    FetchAndAddThread t1;
    t1.val = &val;
    t1.iterations = 1000000;

    FetchAndAddThread t2;
    t2.val = &val;
    t2.iterations = 2000000;

    t1.start();
    t2.start();
    t1.wait();
    t2.wait();

    QCOMPARE(val.loadRelaxed(), 0);
}

QTEST_MAIN(tst_QAtomicInt)
#include "tst_qatomicint.moc"