Debian Codegraph @

tests/auto/corelib/tools/qscopedvaluerollback/tst_qscopedvaluerollback.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QTest>
5 #include <QtCore/QScopedValueRollback>
6
7 /*!
8 \class tst_QScopedValueRollback
9 \internal
10 \since 4.8
11 \brief Tests class QScopedValueRollback.
12
13 */
14 class tst_QScopedValueRollback : public QObject
15 {
16 Q_OBJECT
17
18 private Q_SLOTS:
19 void leavingScope();
20 void leavingScopeAfterCommit();
21 void rollbackToPreviousCommit();
22 void exceptions();
23 void earlyExitScope();
24 void moveOnly();
25 private:
26 void earlyExitScope_helper(int exitpoint, int &member);
27 };
28
29 void tst_QScopedValueRollback::leavingScope()
30 {
31 int i = 0;
32 bool b = false;
33 bool b2 = false;
34 QString s("This is useful");
35
36 //test rollback on going out of scope
37 {
38 QScopedValueRollback<int> ri(i);
39 QScopedValueRollback<bool> rb(b);
40 QScopedValueRollback<bool> rb2(b2, true);
41 QScopedValueRollback<QString> rs(s);
42 QCOMPARE(b, false);
43 QCOMPARE(b2, true);
44 QCOMPARE(i, 0);
45 QCOMPARE(s, QString("This is useful"));
46 b = true;
47 i = 1;
48 s = "Useless";
49 QCOMPARE(b, true);
50 QCOMPARE(i, 1);
51 QCOMPARE(s, QString("Useless"));
52 }
53 QCOMPARE(b, false);
54 QCOMPARE(b2, false);
55 QCOMPARE(i, 0);
56 QCOMPARE(s, QString("This is useful"));
57 }
58
59 void tst_QScopedValueRollback::leavingScopeAfterCommit()
60 {
61 int i = 0;
62 bool b = false;
63 QString s("This is useful");
64
65 //test rollback on going out of scope
66 {
67 QScopedValueRollback<int> ri(i);
68 QScopedValueRollback<bool> rb(b);
69 QScopedValueRollback<QString> rs(s);
70 QCOMPARE(b, false);
71 QCOMPARE(i, 0);
72 QCOMPARE(s, QString("This is useful"));
73 b = true;
74 i = 1;
75 s = "Useless";
76 QCOMPARE(b, true);
77 QCOMPARE(i, 1);
78 QCOMPARE(s, QString("Useless"));
79 ri.commit();
80 rb.commit();
81 rs.commit();
82 }
83 QCOMPARE(b, true);
84 QCOMPARE(i, 1);
85 QCOMPARE(s, QString("Useless"));
86 }
87
88 void tst_QScopedValueRollback::rollbackToPreviousCommit()
89 {
90 int i=0;
91 {
92 QScopedValueRollback<int> ri(i);
93 i++;
94 ri.commit();
95 i++;
96 }
97 QCOMPARE(i,1);
98 {
99 QScopedValueRollback<int> ri1(i);
100 i++;
101 ri1.commit();
102 i++;
103 ri1.commit();
104 i++;
105 }
106 QCOMPARE(i,3);
107 }
108
109 void tst_QScopedValueRollback::exceptions()
110 {
111 bool b = false;
112 bool caught = false;
113 QT_TRY
114 {
115 QScopedValueRollback<bool> rb(b);
116 b = true;
117 QT_THROW(std::bad_alloc()); //if Qt compiled without exceptions this is noop
118 rb.commit(); //if Qt compiled without exceptions, true is committed
119 }
120 QT_CATCH(...)
121 {
122 caught = true;
123 }
124 QCOMPARE(b, !caught); //expect false if exception was thrown, true otherwise
125 }
126
127 void tst_QScopedValueRollback::earlyExitScope()
128 {
129 int i=0;
130 int j=0;
131 while (true) {
132 QScopedValueRollback<int> ri(i);
133 i++;
134 j=i;
135 if (i>8) break;
136 ri.commit();
137 }
138 QCOMPARE(i,8);
139 QCOMPARE(j,9);
140
141 for (i = 0; i < 5; i++) {
142 j=1;
143 earlyExitScope_helper(i,j);
144 QCOMPARE(j, 1<<i);
145 }
146 }
147
148 void tst_QScopedValueRollback::earlyExitScope_helper(int exitpoint, int& member)
149 {
150 QScopedValueRollback<int> r(member);
151 member *= 2;
152 if (exitpoint == 0)
153 return;
154 r.commit();
155 member *= 2;
156 if (exitpoint == 1)
157 return;
158 r.commit();
159 member *= 2;
160 if (exitpoint == 2)
161 return;
162 r.commit();
163 member *= 2;
164 if (exitpoint == 3)
165 return;
166 r.commit();
167 }
168
169 void tst_QScopedValueRollback::moveOnly()
170 {
171 std::unique_ptr<int> uniquePtr;
172 std::unique_ptr<int> newVal(new int(5));
173 QVERIFY(!uniquePtr);
174 {
175 QScopedValueRollback<std::unique_ptr<int>> r(uniquePtr, std::move(newVal));
176 QVERIFY(uniquePtr);
177 }
178 QVERIFY(!uniquePtr);
179 }
180
181 QTEST_MAIN(tst_QScopedValueRollback)
182 #include "tst_qscopedvaluerollback.moc"