summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools/qatomicscopedvaluerollback/tst_qatomicscopedvaluerollback.cpp
blob: 81d8242f71af1fb850293cf2db680aabf7d98bcb (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <QtCore/private/qatomicscopedvaluerollback_p.h>

#include <QTest>

class tst_QAtomicScopedValueRollback : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void leavingScope();
    void leavingScopeAfterCommit();
    void rollbackToPreviousCommit();
    void exceptions();
    void earlyExitScope();
private:
    void earlyExitScope_helper(int exitpoint, std::atomic<int> &member);
};

void tst_QAtomicScopedValueRollback::leavingScope()
{
    QAtomicInt i = 0;
    QBasicAtomicInteger<bool> b = false;
    std::atomic<bool> b2 = false;

    //test rollback on going out of scope
    {
        QAtomicScopedValueRollback ri(i);
        QAtomicScopedValueRollback rb(b);
        QAtomicScopedValueRollback rb2(b2, true);
        QCOMPARE(b.loadRelaxed(), false);
        QCOMPARE(b2, true);
        QCOMPARE(i.loadRelaxed(), 0);
        b.storeRelaxed(true);
        i.storeRelaxed(1);
        QCOMPARE(b.loadRelaxed(), true);
        QCOMPARE(i.loadRelaxed(), 1);
    }
    QCOMPARE(b.loadRelaxed(), false);
    QCOMPARE(b2, false);
    QCOMPARE(i.loadRelaxed(), 0);
}

void tst_QAtomicScopedValueRollback::leavingScopeAfterCommit()
{
    std::atomic<int> i = 0;
    QAtomicInteger<bool> b = false;

    //test rollback on going out of scope
    {
        QAtomicScopedValueRollback ri(i);
        QAtomicScopedValueRollback rb(b);
        QCOMPARE(b.loadRelaxed(), false);
        QCOMPARE(i, 0);
        b.storeRelaxed(true);
        i = 1;
        QCOMPARE(b.loadRelaxed(), true);
        QCOMPARE(i, 1);
        ri.commit();
        rb.commit();
    }
    QCOMPARE(b.loadRelaxed(), true);
    QCOMPARE(i, 1);
}

void tst_QAtomicScopedValueRollback::rollbackToPreviousCommit()
{
    QBasicAtomicInt i = 0;
    {
        QAtomicScopedValueRollback ri(i);
        i++;
        ri.commit();
        i++;
    }
    QCOMPARE(i.loadRelaxed(), 1);
    {
        QAtomicScopedValueRollback ri1(i);
        i++;
        ri1.commit();
        i++;
        ri1.commit();
        i++;
    }
    QCOMPARE(i.loadRelaxed(), 3);
}

void tst_QAtomicScopedValueRollback::exceptions()
{
    std::atomic<bool> b = false;
    bool caught = false;
    QT_TRY
    {
        QAtomicScopedValueRollback rb(b);
        b = true;
        QT_THROW(std::bad_alloc()); //if Qt compiled without exceptions this is noop
        rb.commit(); //if Qt compiled without exceptions, true is committed
    }
    QT_CATCH(...)
    {
        caught = true;
    }
    QCOMPARE(b, !caught); //expect false if exception was thrown, true otherwise
}

void tst_QAtomicScopedValueRollback::earlyExitScope()
{
    QAtomicInt ai = 0;
    std::atomic<int> aj = 0;
    while (true) {
        QAtomicScopedValueRollback ri(ai);
        ++ai;
        aj = ai.loadRelaxed();
        if (ai.loadRelaxed() > 8) break;
        ri.commit();
    }
    QCOMPARE(ai.loadRelaxed(), 8);
    QCOMPARE(aj.load(), 9);

    for (int i = 0; i < 5; ++i) {
        aj = 1;
        earlyExitScope_helper(i, aj);
        QCOMPARE(aj.load(), 1 << i);
    }
}

static void operator*=(std::atomic<int> &lhs, int rhs)
{
    int expected = lhs.load();
    while (!lhs.compare_exchange_weak(expected, expected * rhs))
        ;
}

void tst_QAtomicScopedValueRollback::earlyExitScope_helper(int exitpoint, std::atomic<int>& member)
{
    QAtomicScopedValueRollback r(member);
    member *= 2;
    if (exitpoint == 0)
        return;
    r.commit();
    member *= 2;
    if (exitpoint == 1)
        return;
    r.commit();
    member *= 2;
    if (exitpoint == 2)
        return;
    r.commit();
    member *= 2;
    if (exitpoint == 3)
        return;
    r.commit();
}

QTEST_MAIN(tst_QAtomicScopedValueRollback)
#include "tst_qatomicscopedvaluerollback.moc"