Differential D18758 Diff 58398 kdevplatform/3rdparty/qtpromise/qtpromise-0.5.0/tests/auto/qtpromise/helpers/all/tst_all.cpp
Changeset View
Changeset View
Standalone View
Standalone View
kdevplatform/3rdparty/qtpromise/qtpromise-0.5.0/tests/auto/qtpromise/helpers/all/tst_all.cpp
- This file was added.
1 | #include "../../shared/utils.h" | ||||
---|---|---|---|---|---|
2 | | ||||
3 | // QtPromise | ||||
4 | #include <QtPromise> | ||||
5 | | ||||
6 | // Qt | ||||
7 | #include <QtTest> | ||||
8 | | ||||
9 | using namespace QtPromise; | ||||
10 | | ||||
11 | class tst_helpers_all : public QObject | ||||
12 | { | ||||
13 | Q_OBJECT | ||||
14 | | ||||
15 | private Q_SLOTS: | ||||
16 | void emptySequence(); | ||||
17 | void emptySequence_void(); | ||||
18 | void allPromisesSucceed(); | ||||
19 | void allPromisesSucceed_void(); | ||||
20 | void atLeastOnePromiseReject(); | ||||
21 | void atLeastOnePromiseReject_void(); | ||||
22 | void preserveOrder(); | ||||
23 | void sequenceTypes(); | ||||
24 | void sequenceTypes_void(); | ||||
25 | }; | ||||
26 | | ||||
27 | QTEST_MAIN(tst_helpers_all) | ||||
28 | #include "tst_all.moc" | ||||
29 | | ||||
30 | namespace { | ||||
31 | | ||||
32 | template <class Sequence> | ||||
33 | struct SequenceTester | ||||
34 | { | ||||
35 | Q_STATIC_ASSERT((std::is_same<typename Sequence::value_type, QPromise<int>>::value)); | ||||
36 | | ||||
37 | static void exec() | ||||
38 | { | ||||
39 | Sequence promises{ | ||||
40 | QtPromise::resolve(42), | ||||
41 | QtPromise::resolve(43), | ||||
42 | QtPromise::resolve(44) | ||||
43 | }; | ||||
44 | | ||||
45 | promises.push_back(QtPromise::resolve(45)); | ||||
46 | promises.insert(++promises.begin(), QtPromise::resolve(46)); | ||||
47 | promises.pop_back(); | ||||
48 | | ||||
49 | auto p = QtPromise::all(promises); | ||||
50 | | ||||
51 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); | ||||
52 | QCOMPARE(p.isPending(), true); | ||||
53 | QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({42, 46, 43, 44})); | ||||
54 | } | ||||
55 | }; | ||||
56 | | ||||
57 | template <template <typename, typename...> class Sequence, typename ...Args> | ||||
58 | struct SequenceTester<Sequence<QPromise<void>, Args...>> | ||||
59 | { | ||||
60 | static void exec() | ||||
61 | { | ||||
62 | Sequence<QPromise<void>, Args...> promises{ | ||||
63 | QtPromise::resolve(), | ||||
64 | QtPromise::resolve(), | ||||
65 | QtPromise::resolve() | ||||
66 | }; | ||||
67 | | ||||
68 | promises.push_back(QtPromise::resolve()); | ||||
69 | promises.insert(++promises.begin(), QtPromise::resolve()); | ||||
70 | promises.pop_back(); | ||||
71 | | ||||
72 | auto p = QtPromise::all(promises); | ||||
73 | | ||||
74 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); | ||||
75 | QCOMPARE(p.isPending(), true); | ||||
76 | QCOMPARE(waitForValue(p, -1, 42), 42); | ||||
77 | } | ||||
78 | }; | ||||
79 | | ||||
80 | } // anonymous namespace | ||||
81 | | ||||
82 | void tst_helpers_all::emptySequence() | ||||
83 | { | ||||
84 | auto p = QtPromise::all(QVector<QPromise<int>>()); | ||||
85 | | ||||
86 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); | ||||
87 | QCOMPARE(p.isFulfilled(), true); | ||||
88 | QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({})); | ||||
89 | } | ||||
90 | | ||||
91 | void tst_helpers_all::emptySequence_void() | ||||
92 | { | ||||
93 | auto p = QtPromise::all(QVector<QPromise<void>>()); | ||||
94 | | ||||
95 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); | ||||
96 | QCOMPARE(p.isFulfilled(), true); | ||||
97 | QCOMPARE(waitForValue(p, -1, 42), 42); | ||||
98 | } | ||||
99 | | ||||
100 | void tst_helpers_all::allPromisesSucceed() | ||||
101 | { | ||||
102 | auto p0 = QtPromise::resolve(42); | ||||
103 | auto p1 = QtPromise::resolve(44); | ||||
104 | auto p2 = QPromise<int>([](const QPromiseResolve<int>& resolve) { | ||||
105 | QtPromisePrivate::qtpromise_defer([=](){ | ||||
106 | resolve(43); | ||||
107 | }); | ||||
108 | }); | ||||
109 | | ||||
110 | auto p = QtPromise::all(QVector<QPromise<int>>{p0, p2, p1}); | ||||
111 | | ||||
112 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); | ||||
113 | QCOMPARE(p0.isFulfilled(), true); | ||||
114 | QCOMPARE(p1.isFulfilled(), true); | ||||
115 | QCOMPARE(p2.isPending(), true); | ||||
116 | QCOMPARE(p.isPending(), true); | ||||
117 | QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({42, 43, 44})); | ||||
118 | QCOMPARE(p2.isFulfilled(), true); | ||||
119 | } | ||||
120 | | ||||
121 | void tst_helpers_all::allPromisesSucceed_void() | ||||
122 | { | ||||
123 | auto p0 = QtPromise::resolve(); | ||||
124 | auto p1 = QtPromise::resolve(); | ||||
125 | auto p2 = QPromise<void>([](const QPromiseResolve<void>& resolve) { | ||||
126 | QtPromisePrivate::qtpromise_defer([=](){ | ||||
127 | resolve(); | ||||
128 | }); | ||||
129 | }); | ||||
130 | | ||||
131 | auto p = QtPromise::all(QVector<QPromise<void>>{p0, p2, p1}); | ||||
132 | | ||||
133 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); | ||||
134 | QCOMPARE(p0.isFulfilled(), true); | ||||
135 | QCOMPARE(p1.isFulfilled(), true); | ||||
136 | QCOMPARE(p2.isPending(), true); | ||||
137 | QCOMPARE(p.isPending(), true); | ||||
138 | QCOMPARE(waitForValue(p, -1, 42), 42); | ||||
139 | QCOMPARE(p2.isFulfilled(), true); | ||||
140 | } | ||||
141 | | ||||
142 | void tst_helpers_all::atLeastOnePromiseReject() | ||||
143 | { | ||||
144 | auto p0 = QtPromise::resolve(42); | ||||
145 | auto p1 = QtPromise::resolve(44); | ||||
146 | auto p2 = QPromise<int>([](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { | ||||
147 | QtPromisePrivate::qtpromise_defer([=](){ | ||||
148 | reject(QString("foo")); | ||||
149 | }); | ||||
150 | }); | ||||
151 | | ||||
152 | auto p = QtPromise::all(QVector<QPromise<int>>{p0, p2, p1}); | ||||
153 | | ||||
154 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); | ||||
155 | QCOMPARE(p0.isFulfilled(), true); | ||||
156 | QCOMPARE(p1.isFulfilled(), true); | ||||
157 | QCOMPARE(p2.isPending(), true); | ||||
158 | QCOMPARE(p.isPending(), true); | ||||
159 | QCOMPARE(waitForError(p, QString()), QString("foo")); | ||||
160 | QCOMPARE(p2.isRejected(), true); | ||||
161 | } | ||||
162 | | ||||
163 | void tst_helpers_all::atLeastOnePromiseReject_void() | ||||
164 | { | ||||
165 | auto p0 = QtPromise::resolve(); | ||||
166 | auto p1 = QtPromise::resolve(); | ||||
167 | auto p2 = QPromise<void>([](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) { | ||||
168 | QtPromisePrivate::qtpromise_defer([=](){ | ||||
169 | reject(QString("foo")); | ||||
170 | }); | ||||
171 | }); | ||||
172 | | ||||
173 | auto p = QtPromise::all(QVector<QPromise<void>>{p0, p2, p1}); | ||||
174 | | ||||
175 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); | ||||
176 | QCOMPARE(p0.isFulfilled(), true); | ||||
177 | QCOMPARE(p1.isFulfilled(), true); | ||||
178 | QCOMPARE(p2.isPending(), true); | ||||
179 | QCOMPARE(p.isPending(), true); | ||||
180 | QCOMPARE(waitForError(p, QString()), QString("foo")); | ||||
181 | QCOMPARE(p2.isRejected(), true); | ||||
182 | } | ||||
183 | | ||||
184 | void tst_helpers_all::preserveOrder() | ||||
185 | { | ||||
186 | auto p0 = QtPromise::resolve(42).delay(500); | ||||
187 | auto p1 = QtPromise::resolve(43).delay(100); | ||||
188 | auto p2 = QtPromise::resolve(44).delay(250); | ||||
189 | | ||||
190 | auto p = QtPromise::all(QVector<QPromise<int>>{p0, p1, p2}); | ||||
191 | | ||||
192 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); | ||||
193 | QCOMPARE(p0.isPending(), true); | ||||
194 | QCOMPARE(p1.isPending(), true); | ||||
195 | QCOMPARE(p2.isPending(), true); | ||||
196 | QCOMPARE(p.isPending(), true); | ||||
197 | QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({42, 43, 44})); | ||||
198 | QCOMPARE(p0.isFulfilled(), true); | ||||
199 | QCOMPARE(p1.isFulfilled(), true); | ||||
200 | QCOMPARE(p2.isFulfilled(), true); | ||||
201 | } | ||||
202 | | ||||
203 | // QVector::push_back/append isn't supported since it requires a default | ||||
204 | // constructor (see https://github.com/simonbrunel/qtpromise/issues/3) | ||||
205 | | ||||
206 | void tst_helpers_all::sequenceTypes() | ||||
207 | { | ||||
208 | SequenceTester<QList<QPromise<int>>>::exec(); | ||||
209 | //SequenceTester<QVector<QPromise<int>>>::exec(); | ||||
210 | SequenceTester<std::list<QPromise<int>>>::exec(); | ||||
211 | SequenceTester<std::vector<QPromise<int>>>::exec(); | ||||
212 | } | ||||
213 | | ||||
214 | void tst_helpers_all::sequenceTypes_void() | ||||
215 | { | ||||
216 | SequenceTester<QList<QPromise<void>>>::exec(); | ||||
217 | //SequenceTester<QVector<QPromise<void>>>::exec(); | ||||
218 | SequenceTester<std::list<QPromise<void>>>::exec(); | ||||
219 | SequenceTester<std::vector<QPromise<void>>>::exec(); | ||||
220 | } |