Differential D18758 Diff 58398 kdevplatform/3rdparty/qtpromise/qtpromise-0.5.0/tests/auto/qtpromise/helpers/map/tst_map.cpp
Changeset View
Changeset View
Standalone View
Standalone View
kdevplatform/3rdparty/qtpromise/qtpromise-0.5.0/tests/auto/qtpromise/helpers/map/tst_map.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_map : public QObject | ||||
12 | { | ||||
13 | Q_OBJECT | ||||
14 | | ||||
15 | private Q_SLOTS: | ||||
16 | void emptySequence(); | ||||
17 | void modifyValues(); | ||||
18 | void convertValues(); | ||||
19 | void delayedFulfilled(); | ||||
20 | void delayedRejected(); | ||||
21 | void functorThrows(); | ||||
22 | void functorArguments(); | ||||
23 | void preserveOrder(); | ||||
24 | void sequenceTypes(); | ||||
25 | }; | ||||
26 | | ||||
27 | QTEST_MAIN(tst_helpers_map) | ||||
28 | #include "tst_map.moc" | ||||
29 | | ||||
30 | namespace { | ||||
31 | | ||||
32 | template <class Sequence> | ||||
33 | struct SequenceTester | ||||
34 | { | ||||
35 | static void exec() | ||||
36 | { | ||||
37 | auto p = QtPromise::map(Sequence{42, 43, 44}, [](int v, ...) { | ||||
38 | return QString::number(v + 1); | ||||
39 | }); | ||||
40 | | ||||
41 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<QString>>>::value)); | ||||
42 | QCOMPARE(waitForValue(p, QVector<QString>()), QVector<QString>({"43", "44", "45"})); | ||||
43 | } | ||||
44 | }; | ||||
45 | | ||||
46 | } // anonymous namespace | ||||
47 | | ||||
48 | void tst_helpers_map::emptySequence() | ||||
49 | { | ||||
50 | auto p = QtPromise::map(QVector<int>{}, [](int v, ...) { | ||||
51 | return v + 1; | ||||
52 | }); | ||||
53 | | ||||
54 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); | ||||
55 | QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({})); | ||||
56 | } | ||||
57 | | ||||
58 | void tst_helpers_map::modifyValues() | ||||
59 | { | ||||
60 | auto p = QtPromise::map(QVector<int>{42, 43, 44}, [](int v, ...) { | ||||
61 | return v + 1; | ||||
62 | }); | ||||
63 | | ||||
64 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); | ||||
65 | QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({43, 44, 45})); | ||||
66 | } | ||||
67 | | ||||
68 | void tst_helpers_map::convertValues() | ||||
69 | { | ||||
70 | auto p = QtPromise::map(QVector<int>{42, 43, 44}, [](int v, ...) { | ||||
71 | return QString::number(v + 1); | ||||
72 | }); | ||||
73 | | ||||
74 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<QString>>>::value)); | ||||
75 | QCOMPARE(waitForValue(p, QVector<QString>()), QVector<QString>({"43", "44", "45"})); | ||||
76 | } | ||||
77 | | ||||
78 | void tst_helpers_map::delayedFulfilled() | ||||
79 | { | ||||
80 | auto p = QtPromise::map(QVector<int>{42, 43, 44}, [](int v, ...) { | ||||
81 | return QPromise<int>([&](const QPromiseResolve<int>& resolve) { | ||||
82 | QtPromisePrivate::qtpromise_defer([=]() { | ||||
83 | resolve(v + 1); | ||||
84 | }); | ||||
85 | }); | ||||
86 | }); | ||||
87 | | ||||
88 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); | ||||
89 | QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({43, 44, 45})); | ||||
90 | } | ||||
91 | | ||||
92 | void tst_helpers_map::delayedRejected() | ||||
93 | { | ||||
94 | auto p = QtPromise::map(QVector<int>{42, 43, 44}, [](int v, ...) { | ||||
95 | return QPromise<int>([&]( | ||||
96 | const QPromiseResolve<int>& resolve, | ||||
97 | const QPromiseReject<int>& reject) { | ||||
98 | QtPromisePrivate::qtpromise_defer([=]() { | ||||
99 | if (v == 43) { | ||||
100 | reject(QString("foo")); | ||||
101 | } | ||||
102 | resolve(v); | ||||
103 | }); | ||||
104 | }); | ||||
105 | }); | ||||
106 | | ||||
107 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); | ||||
108 | QCOMPARE(waitForError(p, QString()), QString("foo")); | ||||
109 | } | ||||
110 | | ||||
111 | void tst_helpers_map::functorThrows() | ||||
112 | { | ||||
113 | auto p = QtPromise::map(QVector<int>{42, 43, 44}, [](int v, ...) { | ||||
114 | if (v == 43) { | ||||
115 | throw QString("foo"); | ||||
116 | } | ||||
117 | return v + 1; | ||||
118 | }); | ||||
119 | | ||||
120 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); | ||||
121 | QCOMPARE(waitForError(p, QString()), QString("foo")); | ||||
122 | } | ||||
123 | | ||||
124 | void tst_helpers_map::functorArguments() | ||||
125 | { | ||||
126 | auto p = QtPromise::map(QVector<int>{42, 42, 42}, [](int v, int i) { | ||||
127 | return v * i; | ||||
128 | }); | ||||
129 | | ||||
130 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); | ||||
131 | QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({0, 42, 84})); | ||||
132 | } | ||||
133 | | ||||
134 | void tst_helpers_map::preserveOrder() | ||||
135 | { | ||||
136 | auto p = QtPromise::map(QVector<int>{500, 100, 250}, [](int v, ...) { | ||||
137 | return QPromise<int>::resolve(v + 1).delay(v); | ||||
138 | }); | ||||
139 | | ||||
140 | Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); | ||||
141 | QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({501, 101, 251})); | ||||
142 | } | ||||
143 | | ||||
144 | void tst_helpers_map::sequenceTypes() | ||||
145 | { | ||||
146 | SequenceTester<QList<int>>::exec(); | ||||
147 | SequenceTester<QVector<int>>::exec(); | ||||
148 | SequenceTester<std::list<int>>::exec(); | ||||
149 | SequenceTester<std::vector<int>>::exec(); | ||||
150 | } |