Warning: That file was not part of the compilation database. It may have many parsing errors.
1 | /**************************************************************************** |
---|---|
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of Qt Creator. |
7 | ** |
8 | ** Commercial License Usage |
9 | ** Licensees holding valid commercial Qt licenses may use this file in |
10 | ** accordance with the commercial license agreement provided with the |
11 | ** Software or, alternatively, in accordance with the terms contained in |
12 | ** a written agreement between you and The Qt Company. For licensing terms |
13 | ** and conditions see https://www.qt.io/terms-conditions. For further |
14 | ** information use the contact form at https://www.qt.io/contact-us. |
15 | ** |
16 | ** GNU General Public License Usage |
17 | ** Alternatively, this file may be used under the terms of the GNU |
18 | ** General Public License version 3 as published by the Free Software |
19 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
20 | ** included in the packaging of this file. Please review the following |
21 | ** information to ensure the GNU General Public License requirements will |
22 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
23 | ** |
24 | ****************************************************************************/ |
25 | |
26 | #include <QtTest> |
27 | |
28 | #include <array> |
29 | #include <deque> |
30 | #include <list> |
31 | #include <memory> |
32 | #include <unordered_map> |
33 | #include <unordered_set> |
34 | #include <valarray> |
35 | |
36 | // must get included after the containers above or gcc4.9 will have a problem using |
37 | // initializer_list related code on the templates inside algorithm.h |
38 | #include <utils/algorithm.h> |
39 | |
40 | class tst_Algorithm : public QObject |
41 | { |
42 | Q_OBJECT |
43 | |
44 | private slots: |
45 | void anyOf(); |
46 | void transform(); |
47 | void sort(); |
48 | void contains(); |
49 | void findOr(); |
50 | void findOrDefault(); |
51 | void toReferences(); |
52 | void take(); |
53 | }; |
54 | |
55 | |
56 | int stringToInt(const QString &s) |
57 | { |
58 | return s.toInt(); |
59 | } |
60 | |
61 | namespace { |
62 | |
63 | struct BaseStruct |
64 | { |
65 | BaseStruct(int m) : member(m) {} |
66 | bool operator==(const BaseStruct &other) const { return member == other.member; } |
67 | |
68 | int member; |
69 | }; |
70 | |
71 | struct Struct : public BaseStruct |
72 | { |
73 | Struct(int m) : BaseStruct(m) {} |
74 | bool isOdd() const { return member % 2 == 1; } |
75 | bool isEven() const { return !isOdd(); } |
76 | |
77 | int getMember() const { return member; } |
78 | |
79 | }; |
80 | } |
81 | |
82 | void tst_Algorithm::anyOf() |
83 | { |
84 | { |
85 | const QList<QString> strings({"1", "3", "132"}); |
86 | QVERIFY(Utils::anyOf(strings, [](const QString &s) { return s == "132"; })); |
87 | QVERIFY(!Utils::anyOf(strings, [](const QString &s) { return s == "1324"; })); |
88 | } |
89 | { |
90 | const QList<Struct> list({2, 4, 6, 8}); |
91 | QVERIFY(Utils::anyOf(list, &Struct::isEven)); |
92 | QVERIFY(!Utils::anyOf(list, &Struct::isOdd)); |
93 | } |
94 | { |
95 | const QList<Struct> list({0, 0, 0, 0, 1, 0, 0}); |
96 | QVERIFY(Utils::anyOf(list, &Struct::member)); |
97 | } |
98 | { |
99 | const QList<Struct> list({0, 0, 0, 0, 0, 0, 0}); |
100 | QVERIFY(!Utils::anyOf(list, &Struct::member)); |
101 | } |
102 | |
103 | } |
104 | |
105 | void tst_Algorithm::transform() |
106 | { |
107 | // same container type |
108 | { |
109 | // QList has standard inserter |
110 | const QList<QString> strings({"1", "3", "132"}); |
111 | const QList<int> i1 = Utils::transform(strings, [](const QString &s) { return s.toInt(); }); |
112 | QCOMPARE(i1, QList<int>({1, 3, 132})); |
113 | const QList<int> i2 = Utils::transform(strings, stringToInt); |
114 | QCOMPARE(i2, QList<int>({1, 3, 132})); |
115 | const QList<int> i3 = Utils::transform(strings, &QString::size); |
116 | QCOMPARE(i3, QList<int>({1, 1, 3})); |
117 | } |
118 | { |
119 | // QStringList |
120 | const QStringList strings({"1", "3", "132"}); |
121 | const QList<int> i1 = Utils::transform(strings, [](const QString &s) { return s.toInt(); }); |
122 | QCOMPARE(i1, QList<int>({1, 3, 132})); |
123 | const QList<int> i2 = Utils::transform(strings, stringToInt); |
124 | QCOMPARE(i2, QList<int>({1, 3, 132})); |
125 | const QList<int> i3 = Utils::transform(strings, &QString::size); |
126 | QCOMPARE(i3, QList<int>({1, 1, 3})); |
127 | } |
128 | { |
129 | // QSet internally needs special inserter |
130 | const QSet<QString> strings({QString("1"), QString( "3"), QString( "132")}); |
131 | const QSet<int> i1 = Utils::transform(strings, [](const QString &s) { return s.toInt(); }); |
132 | QCOMPARE(i1, QSet<int>({1, 3, 132})); |
133 | const QSet<int> i2 = Utils::transform(strings, stringToInt); |
134 | QCOMPARE(i2, QSet<int>({1, 3, 132})); |
135 | const QSet<int> i3 = Utils::transform(strings, &QString::size); |
136 | QCOMPARE(i3, QSet<int>({1, 3})); |
137 | } |
138 | |
139 | // different container types |
140 | { |
141 | // QList to QSet |
142 | const QList<QString> strings({"1", "3", "132"}); |
143 | const QSet<int> i1 = Utils::transform<QSet>(strings, [](const QString &s) { return s.toInt(); }); |
144 | QCOMPARE(i1, QSet<int>({1, 3, 132})); |
145 | const QSet<int> i2 = Utils::transform<QSet>(strings, stringToInt); |
146 | QCOMPARE(i2, QSet<int>({1, 3, 132})); |
147 | const QSet<int> i3 = Utils::transform<QSet>(strings, &QString::size); |
148 | QCOMPARE(i3, QSet<int>({1, 3})); |
149 | } |
150 | { |
151 | // QStringList to QSet |
152 | const QStringList strings({"1", "3", "132"}); |
153 | const QSet<int> i1 = Utils::transform<QSet>(strings, [](const QString &s) { return s.toInt(); }); |
154 | QCOMPARE(i1, QSet<int>({1, 3, 132})); |
155 | const QSet<int> i2 = Utils::transform<QSet>(strings, stringToInt); |
156 | QCOMPARE(i2, QSet<int>({1, 3, 132})); |
157 | const QSet<int> i3 = Utils::transform<QSet>(strings, &QString::size); |
158 | QCOMPARE(i3, QSet<int>({1, 3})); |
159 | } |
160 | { |
161 | // QSet to QList |
162 | const QSet<QString> strings({QString("1"), QString( "3"), QString( "132")}); |
163 | QList<int> i1 = Utils::transform<QList>(strings, [](const QString &s) { return s.toInt(); }); |
164 | Utils::sort(i1); |
165 | QCOMPARE(i1, QList<int>({1, 3, 132})); |
166 | QList<int> i2 = Utils::transform<QList>(strings, stringToInt); |
167 | Utils::sort(i2); |
168 | QCOMPARE(i2, QList<int>({1, 3, 132})); |
169 | QList<int> i3 = Utils::transform<QList>(strings, &QString::size); |
170 | Utils::sort(i3); |
171 | QCOMPARE(i3, QList<int>({1, 1, 3})); |
172 | } |
173 | { |
174 | const QList<Struct> list({4, 3, 2, 1, 2}); |
175 | const QList<int> trans = Utils::transform(list, &Struct::member); |
176 | QCOMPARE(trans, QList<int>({4, 3, 2, 1, 2})); |
177 | } |
178 | { |
179 | // QList -> std::vector |
180 | const QList<int> v({1, 2, 3, 4}); |
181 | const std::vector<int> trans = Utils::transform<std::vector>(v, [](int i) { return i + 1; }); |
182 | QCOMPARE(trans, std::vector<int>({2, 3, 4, 5})); |
183 | } |
184 | { |
185 | // QList -> std::vector |
186 | const QList<Struct> v({1, 2, 3, 4}); |
187 | const std::vector<int> trans = Utils::transform<std::vector>(v, &Struct::getMember); |
188 | QCOMPARE(trans, std::vector<int>({1, 2, 3, 4})); |
189 | } |
190 | { |
191 | // QList -> std::vector |
192 | const QList<Struct> v({1, 2, 3, 4}); |
193 | const std::vector<int> trans = Utils::transform<std::vector>(v, &Struct::member); |
194 | QCOMPARE(trans, std::vector<int>({1, 2, 3, 4})); |
195 | } |
196 | { |
197 | // std::vector -> QList |
198 | const std::vector<int> v({1, 2, 3, 4}); |
199 | const QList<int> trans = Utils::transform<QList>(v, [](int i) { return i + 1; }); |
200 | QCOMPARE(trans, QList<int>({2, 3, 4, 5})); |
201 | } |
202 | { |
203 | // std::vector -> QList |
204 | const std::vector<Struct> v({1, 2, 3, 4}); |
205 | const QList<int> trans = Utils::transform<QList>(v, &Struct::getMember); |
206 | QCOMPARE(trans, QList<int>({1, 2, 3, 4})); |
207 | } |
208 | { |
209 | // std::vector -> QList |
210 | const std::vector<Struct> v({1, 2, 3, 4}); |
211 | const QList<int> trans = Utils::transform<QList>(v, &Struct::member); |
212 | QCOMPARE(trans, QList<int>({1, 2, 3, 4})); |
213 | } |
214 | { |
215 | // std::deque -> std::vector |
216 | const std::deque<int> v({1, 2, 3, 4}); |
217 | const std::vector<int> trans = Utils::transform<std::vector>(v, [](int i) { return i + 1; }); |
218 | QCOMPARE(trans, std::vector<int>({2, 3, 4, 5})); |
219 | } |
220 | { |
221 | // std::deque -> std::vector |
222 | const std::deque<Struct> v({1, 2, 3, 4}); |
223 | const std::vector<int> trans = Utils::transform<std::vector>(v, &Struct::getMember); |
224 | QCOMPARE(trans, std::vector<int>({1, 2, 3, 4})); |
225 | } |
226 | { |
227 | // std::deque -> std::vector |
228 | const std::deque<Struct> v({1, 2, 3, 4}); |
229 | const std::vector<int> trans = Utils::transform<std::vector>(v, &Struct::member); |
230 | QCOMPARE(trans, std::vector<int>({1, 2, 3, 4})); |
231 | } |
232 | { |
233 | // std::vector -> std::vector |
234 | const std::vector<int> v({1, 2, 3, 4}); |
235 | const std::vector<int> trans = Utils::transform(v, [](int i) { return i + 1; }); |
236 | QCOMPARE(trans, std::vector<int>({2, 3, 4, 5})); |
237 | } |
238 | { |
239 | // std::vector -> std::vector |
240 | const std::vector<Struct> v({1, 2, 3, 4}); |
241 | const std::vector<int> trans = Utils::transform(v, &Struct::getMember); |
242 | QCOMPARE(trans, std::vector<int>({1, 2, 3, 4})); |
243 | } |
244 | { |
245 | // std::vector -> std::vector |
246 | const std::vector<Struct> v({1, 2, 3, 4}); |
247 | const std::vector<int> trans = Utils::transform(v, &Struct::member); |
248 | QCOMPARE(trans, std::vector<int>({1, 2, 3, 4})); |
249 | } |
250 | { |
251 | // std::unordered_map -> QList |
252 | std::unordered_map<int, double> m; |
253 | m.emplace(1, 1.5); |
254 | m.emplace(3, 2.5); |
255 | m.emplace(5, 3.5); |
256 | QList<double> trans = Utils::transform<QList>(m, [](const std::pair<int, double> &in) { |
257 | return in.first * in.second; |
258 | }); |
259 | Utils::sort(trans); |
260 | QCOMPARE(trans, QList<double>({1.5, 7.5, 17.5})); |
261 | } |
262 | { |
263 | // specific result container with one template parameter (QVector) |
264 | std::vector<int> v({1, 2, 3, 4}); |
265 | const QVector<BaseStruct *> trans = Utils::transform<QVector<BaseStruct *>>(v, [](int i) { |
266 | return new Struct(i); |
267 | }); |
268 | QCOMPARE(trans.size(), 4); |
269 | QCOMPARE(trans.at(0)->member, 1); |
270 | QCOMPARE(trans.at(1)->member, 2); |
271 | QCOMPARE(trans.at(2)->member, 3); |
272 | QCOMPARE(trans.at(3)->member, 4); |
273 | qDeleteAll(trans); |
274 | } |
275 | { |
276 | // specific result container with one of two template parameters (std::vector) |
277 | std::vector<int> v({1, 2, 3, 4}); |
278 | const std::vector<BaseStruct *> trans |
279 | = Utils::transform<std::vector<BaseStruct *>>(v, [](int i) { return new Struct(i); }); |
280 | QCOMPARE(trans.size(), static_cast<std::vector<int>::size_type>(4ul)); |
281 | QCOMPARE(trans.at(0)->member, 1); |
282 | QCOMPARE(trans.at(1)->member, 2); |
283 | QCOMPARE(trans.at(2)->member, 3); |
284 | QCOMPARE(trans.at(3)->member, 4); |
285 | qDeleteAll(trans); |
286 | } |
287 | { |
288 | // specific result container with two template parameters (std::vector) |
289 | std::vector<int> v({1, 2, 3, 4}); |
290 | const std::vector<BaseStruct *, std::allocator<BaseStruct *>> trans |
291 | = Utils::transform<std::vector<BaseStruct *, std::allocator<BaseStruct *>>>(v, [](int i) { |
292 | return new Struct(i); |
293 | }); |
294 | QCOMPARE(trans.size(), static_cast<std::vector<int>::size_type>(4ul)); |
295 | QCOMPARE(trans.at(0)->member, 1); |
296 | QCOMPARE(trans.at(1)->member, 2); |
297 | QCOMPARE(trans.at(2)->member, 3); |
298 | QCOMPARE(trans.at(3)->member, 4); |
299 | qDeleteAll(trans); |
300 | } |
301 | { |
302 | // specific result container with member function |
303 | QList<Struct> v({1, 2, 3, 4}); |
304 | const QVector<double> trans = Utils::transform<QVector<double>>(v, &Struct::getMember); |
305 | QCOMPARE(trans, QVector<double>({1.0, 2.0, 3.0, 4.0})); |
306 | } |
307 | { |
308 | // specific result container with member |
309 | QList<Struct> v({1, 2, 3, 4}); |
310 | const QVector<double> trans = Utils::transform<QVector<double>>(v, &Struct::member); |
311 | QCOMPARE(trans, QVector<double>({1.0, 2.0, 3.0, 4.0})); |
312 | } |
313 | { |
314 | // non-const container and function parameter |
315 | // same container type |
316 | std::vector<Struct> v({1, 2, 3, 4}); |
317 | const std::vector<std::reference_wrapper<Struct>> trans |
318 | = Utils::transform(v, [](Struct &s) { return std::ref(s); }); |
319 | // different container type |
320 | QVector<Struct> v2({1, 2, 3, 4}); |
321 | const std::vector<std::reference_wrapper<Struct>> trans2 |
322 | = Utils::transform<std::vector>(v, [](Struct &s) { return std::ref(s); }); |
323 | // temporaries |
324 | const auto tempv = [] { return QList<Struct>({1, 2, 3, 4}); }; |
325 | // temporary with member function |
326 | const QList<int> trans3 = Utils::transform(tempv(), &Struct::getMember); |
327 | const std::vector<int> trans4 = Utils::transform<std::vector>(tempv(), &Struct::getMember); |
328 | const std::vector<int> trans5 = Utils::transform<std::vector<int>>(tempv(), &Struct::getMember); |
329 | // temporary with member |
330 | const QList<int> trans6 = Utils::transform(tempv(), &Struct::member); |
331 | const std::vector<int> trans7 = Utils::transform<std::vector>(tempv(), &Struct::member); |
332 | const std::vector<int> trans8 = Utils::transform<std::vector<int>>(tempv(), &Struct::member); |
333 | // temporary with function |
334 | const QList<int> trans9 = Utils::transform(tempv(), |
335 | [](const Struct &s) { return s.getMember(); }); |
336 | const std::vector<int> trans10 = Utils::transform<std::vector>(tempv(), [](const Struct &s) { |
337 | return s.getMember(); |
338 | }); |
339 | const std::vector<int> trans11 = Utils::transform<std::vector<int>>(tempv(), |
340 | [](const Struct &s) { |
341 | return s.getMember(); |
342 | }); |
343 | } |
344 | // target containers without reserve(...) |
345 | { |
346 | // std::vector -> std::deque |
347 | const std::vector<int> v({1, 2, 3, 4}); |
348 | const std::deque<int> trans = Utils::transform<std::deque>(v, [](int i) { return i + 1; }); |
349 | QCOMPARE(trans, std::deque<int>({2, 3, 4, 5})); |
350 | } |
351 | { |
352 | // std::vector -> std::list |
353 | const std::vector<int> v({1, 2, 3, 4}); |
354 | const std::list<int> trans = Utils::transform<std::list>(v, [](int i) { return i + 1; }); |
355 | QCOMPARE(trans, std::list<int>({2, 3, 4, 5})); |
356 | } |
357 | { |
358 | // std::vector -> std::set |
359 | const std::vector<int> v({1, 2, 3, 4}); |
360 | const std::set<int> trans = Utils::transform<std::set<int>>(v, [](int i) { return i + 1; }); |
361 | QCOMPARE(trans, std::set<int>({2, 3, 4, 5})); |
362 | } |
363 | // various map/set/hash without push_back |
364 | { |
365 | // std::vector -> std::map |
366 | const std::vector<int> v({1, 2, 3, 4}); |
367 | const std::map<int, int> trans = Utils::transform<std::map<int, int>>(v, [](int i) { |
368 | return std::make_pair(i, i + 1); |
369 | }); |
370 | const std::map<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}}); |
371 | QCOMPARE(trans, expected); |
372 | } |
373 | { |
374 | // std::vector -> std::unordered_set |
375 | const std::vector<int> v({1, 2, 3, 4}); |
376 | const std::unordered_set<int> trans = Utils::transform<std::unordered_set<int>>(v, [](int i) { |
377 | return i + 1; |
378 | }); |
379 | QCOMPARE(trans, std::unordered_set<int>({2, 3, 4, 5})); |
380 | } |
381 | { |
382 | // std::vector -> std::unordered_map |
383 | const std::vector<int> v({1, 2, 3, 4}); |
384 | const std::unordered_map<int, int> trans |
385 | = Utils::transform<std::unordered_map<int, int>>(v, [](int i) { |
386 | return std::make_pair(i, i + 1); |
387 | }); |
388 | const std::unordered_map<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}}); |
389 | QCOMPARE(trans, expected); |
390 | } |
391 | { |
392 | // std::vector -> QMap using std::pair |
393 | const std::vector<int> v({1, 2, 3, 4}); |
394 | const QMap<int, int> trans = Utils::transform<QMap<int, int>>(v, [](int i) { |
395 | return std::make_pair(i, i + 1); |
396 | }); |
397 | const QMap<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}}); |
398 | QCOMPARE(trans, expected); |
399 | } |
400 | { |
401 | // std::vector -> QMap using QPair |
402 | const std::vector<int> v({1, 2, 3, 4}); |
403 | const QMap<int, int> trans = Utils::transform<QMap<int, int>>(v, [](int i) { |
404 | return qMakePair(i, i + 1); |
405 | }); |
406 | const QMap<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}}); |
407 | QCOMPARE(trans, expected); |
408 | } |
409 | { |
410 | // std::vector -> QHash using std::pair |
411 | const std::vector<int> v({1, 2, 3, 4}); |
412 | const QHash<int, int> trans = Utils::transform<QHash<int, int>>(v, [](int i) { |
413 | return std::make_pair(i, i + 1); |
414 | }); |
415 | const QHash<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}}); |
416 | QCOMPARE(trans, expected); |
417 | } |
418 | { |
419 | // std::vector -> QHash using QPair |
420 | const std::vector<int> v({1, 2, 3, 4}); |
421 | const QHash<int, int> trans = Utils::transform<QHash<int, int>>(v, [](int i) { |
422 | return qMakePair(i, i + 1); |
423 | }); |
424 | const QHash<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}}); |
425 | QCOMPARE(trans, expected); |
426 | } |
427 | } |
428 | |
429 | void tst_Algorithm::sort() |
430 | { |
431 | QStringList s1({"3", "2", "1"}); |
432 | Utils::sort(s1); |
433 | QCOMPARE(s1, QStringList({"1", "2", "3"})); |
434 | QStringList s2({"13", "31", "22"}); |
435 | Utils::sort(s2, [](const QString &a, const QString &b) { return a[1] < b[1]; }); |
436 | QCOMPARE(s2, QStringList({"31", "22", "13"})); |
437 | QList<QString> s3({"12345", "3333", "22"}); |
438 | Utils::sort(s3, &QString::size); |
439 | QCOMPARE(s3, QList<QString>({"22", "3333", "12345"})); |
440 | QList<Struct> s4({4, 3, 2, 1}); |
441 | Utils::sort(s4, &Struct::member); |
442 | QCOMPARE(s4, QList<Struct>({1, 2, 3, 4})); |
443 | // member function with pointers |
444 | QList<QString> arr1({"12345", "3333", "22"}); |
445 | QList<QString *> s5({&arr1[0], &arr1[1], &arr1[2]}); |
446 | Utils::sort(s5, &QString::size); |
447 | QCOMPARE(s5, QList<QString *>({&arr1[2], &arr1[1], &arr1[0]})); |
448 | // member with pointers |
449 | QList<Struct> arr2({4, 1, 3}); |
450 | QList<Struct *> s6({&arr2[0], &arr2[1], &arr2[2]}); |
451 | Utils::sort(s6, &Struct::member); |
452 | QCOMPARE(s6, QList<Struct *>({&arr2[1], &arr2[2], &arr2[0]})); |
453 | // std::array: |
454 | std::array<int, 4> array = {{4, 10, 8, 1}}; |
455 | Utils::sort(array); |
456 | std::array<int, 4> arrayResult = {{1, 4, 8, 10}}; |
457 | QCOMPARE(array, arrayResult); |
458 | // valarray (no begin/end member functions): |
459 | std::valarray<int> valarray(array.data(), array.size()); |
460 | std::valarray<int> valarrayResult(arrayResult.data(), arrayResult.size()); |
461 | Utils::sort(valarray); |
462 | QCOMPARE(valarray.size(), valarrayResult.size()); |
463 | for (size_t i = 0; i < valarray.size(); ++i) |
464 | QCOMPARE(valarray[i], valarrayResult[i]); |
465 | } |
466 | |
467 | void tst_Algorithm::contains() |
468 | { |
469 | std::vector<int> v1{1, 2, 3, 4}; |
470 | QVERIFY(Utils::contains(v1, [](int i) { return i == 2; })); |
471 | QVERIFY(!Utils::contains(v1, [](int i) { return i == 5; })); |
472 | std::vector<Struct> structs = {2, 4, 6, 8}; |
473 | QVERIFY(Utils::contains(structs, &Struct::isEven)); |
474 | QVERIFY(!Utils::contains(structs, &Struct::isOdd)); |
475 | QList<Struct> structQlist = {2, 4, 6, 8}; |
476 | QVERIFY(Utils::contains(structQlist, &Struct::isEven)); |
477 | QVERIFY(!Utils::contains(structQlist, &Struct::isOdd)); |
478 | } |
479 | |
480 | void tst_Algorithm::findOr() |
481 | { |
482 | std::vector<int> v1{1, 2, 3, 4}; |
483 | QCOMPARE(Utils::findOr(v1, 10, [](int i) { return i == 2; }), 2); |
484 | QCOMPARE(Utils::findOr(v1, 10, [](int i) { return i == 5; }), 10); |
485 | } |
486 | |
487 | void tst_Algorithm::findOrDefault() |
488 | { |
489 | { |
490 | std::vector<int> v1{1, 2, 3, 4}; |
491 | QCOMPARE(Utils::findOrDefault(v1, [](int i) { return i == 2; }), 2); |
492 | QCOMPARE(Utils::findOrDefault(v1, [](int i) { return i == 5; }), 0); |
493 | } |
494 | { |
495 | std::vector<std::shared_ptr<Struct>> v4; |
496 | v4.emplace_back(std::make_shared<Struct>(1)); |
497 | v4.emplace_back(std::make_shared<Struct>(3)); |
498 | v4.emplace_back(std::make_shared<Struct>(5)); |
499 | v4.emplace_back(std::make_shared<Struct>(7)); |
500 | QCOMPARE(Utils::findOrDefault(v4, &Struct::isOdd), v4.at(0)); |
501 | QCOMPARE(Utils::findOrDefault(v4, &Struct::isEven), std::shared_ptr<Struct>()); |
502 | } |
503 | } |
504 | |
505 | void tst_Algorithm::toReferences() |
506 | { |
507 | // toReference |
508 | { |
509 | // std::vector -> std::vector |
510 | std::vector<Struct> v; |
511 | const std::vector<std::reference_wrapper<Struct>> x = Utils::toReferences(v); |
512 | } |
513 | { |
514 | // QList -> std::vector |
515 | QList<Struct> v; |
516 | const std::vector<std::reference_wrapper<Struct>> x = Utils::toReferences<std::vector>(v); |
517 | } |
518 | { |
519 | // std::vector -> QList |
520 | std::vector<Struct> v; |
521 | const QList<std::reference_wrapper<Struct>> x = Utils::toReferences<QList>(v); |
522 | } |
523 | { |
524 | // std::vector -> std::list |
525 | std::vector<Struct> v; |
526 | const std::list<std::reference_wrapper<Struct>> x = Utils::toReferences<std::list>(v); |
527 | } |
528 | // toConstReference |
529 | { |
530 | // std::vector -> std::vector |
531 | const std::vector<Struct> v; |
532 | const std::vector<std::reference_wrapper<const Struct>> x = Utils::toConstReferences(v); |
533 | } |
534 | { |
535 | // QList -> std::vector |
536 | const QList<Struct> v; |
537 | const std::vector<std::reference_wrapper<const Struct>> x |
538 | = Utils::toConstReferences<std::vector>(v); |
539 | } |
540 | { |
541 | // std::vector -> QList |
542 | const std::vector<Struct> v; |
543 | const QList<std::reference_wrapper<const Struct>> x = Utils::toConstReferences<QList>(v); |
544 | } |
545 | { |
546 | // std::vector -> std::list |
547 | const std::vector<Struct> v; |
548 | const std::list<std::reference_wrapper<const Struct>> x |
549 | = Utils::toConstReferences<std::list>(v); |
550 | } |
551 | } |
552 | |
553 | void tst_Algorithm::take() |
554 | { |
555 | { |
556 | QList<Struct> v {1, 3, 5, 6, 7, 8, 9, 11, 13, 15, 13, 16, 17}; |
557 | Utils::optional<Struct> r1 = Utils::take(v, [](const Struct &s) { return s.member == 13; }); |
558 | QVERIFY(static_cast<bool>(r1)); |
559 | QCOMPARE(r1.value().member, 13); |
560 | Utils::optional<Struct> r2 = Utils::take(v, [](const Struct &s) { return s.member == 13; }); |
561 | QVERIFY(static_cast<bool>(r2)); |
562 | QCOMPARE(r2.value().member, 13); |
563 | Utils::optional<Struct> r3 = Utils::take(v, [](const Struct &s) { return s.member == 13; }); |
564 | QVERIFY(!static_cast<bool>(r3)); |
565 | |
566 | Utils::optional<Struct> r4 = Utils::take(v, &Struct::isEven); |
567 | QVERIFY(static_cast<bool>(r4)); |
568 | QCOMPARE(r4.value().member, 6); |
569 | } |
570 | { |
571 | QList<Struct> v {0, 0, 0, 0, 0, 0, 1, 2, 3}; |
572 | Utils::optional<Struct> r1 = Utils::take(v, &Struct::member); |
573 | QVERIFY(static_cast<bool>(r1)); |
574 | QCOMPARE(r1.value().member, 1); |
575 | } |
576 | } |
577 | |
578 | QTEST_MAIN(tst_Algorithm) |
579 | |
580 | #include "tst_algorithm.moc" |
581 |
Warning: That file was not part of the compilation database. It may have many parsing errors.