Avoid value copy when fulfilled from promise

This commit is contained in:
Simon Brunel
2017-08-22 21:52:28 +02:00
parent c4aab4ef36
commit 49a1d6a57b
4 changed files with 92 additions and 29 deletions

View File

@ -14,6 +14,7 @@ private Q_SLOTS:
void valueResolve();
void valueReject();
void valueThen();
void valueDelayed();
void errorReject();
void errorThen();
@ -162,6 +163,36 @@ void tst_benchmark::valueThen()
}
}
void tst_benchmark::valueDelayed()
{
{ // should not copy the value on continutation if fulfilled
int value = -1;
Data::logs().reset();
QPromise<int>::resolve(42).then([&](int res) {
return QPromise<Data>::resolve(Data(res + 1));
}).then([&](const Data& res) {
value = res.value();
}).wait();
QCOMPARE(Data::logs().ctor, 1);
QCOMPARE(Data::logs().copy, 0);
QCOMPARE(Data::logs().move, 1); // move value to the input promise data
QCOMPARE(Data::logs().refs, 0);
QCOMPARE(value, 43);
}
{ // should not create value on continutation if rejected
Data::logs().reset();
QPromise<int>::resolve(42).then([&]() {
return QPromise<Data>::reject(QString("foo"));
}).wait();
QCOMPARE(Data::logs().ctor, 0);
QCOMPARE(Data::logs().copy, 0);
QCOMPARE(Data::logs().move, 0);
QCOMPARE(Data::logs().refs, 0);
}
}
void tst_benchmark::errorReject()
{
{ // should create one copy of the error when rejected by rvalue