2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2015-01-28 08:44:43 +00:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
** Contact: http://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the test suite of the Qt Toolkit.
|
|
|
|
**
|
2014-08-21 13:51:22 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL21$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see http://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at http://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-08-21 13:51:22 +00:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
2015-01-28 08:44:43 +00:00
|
|
|
** As a special exception, The Qt Company gives you certain additional
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2011-04-27 10:05:43 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
#include <qtconcurrentrun.h>
|
|
|
|
#include <qfuture.h>
|
|
|
|
#include <QString>
|
|
|
|
#include <QtTest/QtTest>
|
|
|
|
|
|
|
|
using namespace QtConcurrent;
|
|
|
|
|
|
|
|
class tst_QtConcurrentRun: public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
private slots:
|
|
|
|
void runLightFunction();
|
|
|
|
void runHeavyFunction();
|
|
|
|
void returnValue();
|
|
|
|
void functionObject();
|
|
|
|
void memberFunctions();
|
|
|
|
void implicitConvertibleTypes();
|
|
|
|
void runWaitLoop();
|
|
|
|
void recursive();
|
|
|
|
#ifndef QT_NO_EXCEPTIONS
|
|
|
|
void exceptions();
|
|
|
|
#endif
|
2012-10-17 08:02:57 +00:00
|
|
|
#ifdef Q_COMPILER_DECLTYPE
|
2011-04-18 12:07:32 +00:00
|
|
|
void functor();
|
2012-10-17 08:02:57 +00:00
|
|
|
#endif
|
|
|
|
#ifdef Q_COMPILER_LAMBDA
|
2011-04-18 12:07:32 +00:00
|
|
|
void lambda();
|
2012-10-17 08:02:57 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void light()
|
|
|
|
{
|
|
|
|
qDebug("in function");
|
|
|
|
qDebug("done function");
|
|
|
|
}
|
|
|
|
|
|
|
|
void heavy()
|
|
|
|
{
|
|
|
|
qDebug("in function");
|
|
|
|
QString str;
|
|
|
|
for (int i = 0; i < 1000000; ++i)
|
|
|
|
str.append("a");
|
|
|
|
qDebug("done function");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void tst_QtConcurrentRun::runLightFunction()
|
|
|
|
{
|
|
|
|
qDebug("starting function");
|
2011-10-31 01:14:58 +00:00
|
|
|
QFuture<void> future = run(light);
|
2011-04-27 10:05:43 +00:00
|
|
|
qDebug("waiting");
|
|
|
|
future.waitForFinished();
|
|
|
|
qDebug("done");
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QtConcurrentRun::runHeavyFunction()
|
|
|
|
{
|
2014-07-31 12:09:11 +00:00
|
|
|
QThreadPool pool;
|
2011-04-27 10:05:43 +00:00
|
|
|
qDebug("starting function");
|
2014-07-31 12:09:11 +00:00
|
|
|
QFuture<void> future = run(&pool, heavy);
|
2011-04-27 10:05:43 +00:00
|
|
|
qDebug("waiting");
|
|
|
|
future.waitForFinished();
|
|
|
|
qDebug("done");
|
|
|
|
}
|
|
|
|
|
|
|
|
int returnInt0()
|
|
|
|
{
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
int returnInt1(int i)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int member0() { return 10; }
|
|
|
|
int member1(int in) { return in; }
|
|
|
|
|
|
|
|
typedef int result_type;
|
|
|
|
int operator()() { return 10; }
|
|
|
|
int operator()(int in) { return in; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class AConst
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int member0() const { return 10; }
|
|
|
|
int member1(int in) const { return in; }
|
|
|
|
|
|
|
|
typedef int result_type;
|
|
|
|
int operator()() const { return 10; }
|
|
|
|
int operator()(int in) const { return in; }
|
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QtConcurrentRun::returnValue()
|
|
|
|
{
|
2014-07-31 12:09:11 +00:00
|
|
|
QThreadPool pool;
|
2011-04-27 10:05:43 +00:00
|
|
|
QFuture<int> f;
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-10-31 01:14:58 +00:00
|
|
|
f = run(returnInt0);
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(f.result(), 10);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, returnInt0);
|
|
|
|
QCOMPARE(f.result(), 10);
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
A a;
|
2011-10-31 01:14:58 +00:00
|
|
|
f = run(&a, &A::member0);
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(f.result(), 10);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, &a, &A::member0);
|
|
|
|
QCOMPARE(f.result(), 10);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-10-31 01:14:58 +00:00
|
|
|
f = run(&a, &A::member1, 20);
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(f.result(), 20);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, &a, &A::member1, 20);
|
|
|
|
QCOMPARE(f.result(), 20);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-10-31 01:14:58 +00:00
|
|
|
f = run(a, &A::member0);
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(f.result(), 10);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, a, &A::member0);
|
|
|
|
QCOMPARE(f.result(), 10);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-10-31 01:14:58 +00:00
|
|
|
f = run(a, &A::member1, 20);
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(f.result(), 20);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, a, &A::member1, 20);
|
|
|
|
QCOMPARE(f.result(), 20);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
f = run(a);
|
|
|
|
QCOMPARE(f.result(), 10);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, a);
|
|
|
|
QCOMPARE(f.result(), 10);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
f = run(&a);
|
|
|
|
QCOMPARE(f.result(), 10);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, &a);
|
|
|
|
QCOMPARE(f.result(), 10);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
f = run(a, 20);
|
|
|
|
QCOMPARE(f.result(), 20);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, a, 20);
|
|
|
|
QCOMPARE(f.result(), 20);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
f = run(&a, 20);
|
|
|
|
QCOMPARE(f.result(), 20);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, &a, 20);
|
|
|
|
QCOMPARE(f.result(), 20);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
const AConst aConst = AConst();
|
|
|
|
f = run(&aConst, &AConst::member0);
|
|
|
|
QCOMPARE(f.result(), 10);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, &aConst, &AConst::member0);
|
|
|
|
QCOMPARE(f.result(), 10);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-10-31 01:14:58 +00:00
|
|
|
f = run(&aConst, &AConst::member1, 20);
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(f.result(), 20);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, &aConst, &AConst::member1, 20);
|
|
|
|
QCOMPARE(f.result(), 20);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-10-31 01:14:58 +00:00
|
|
|
f = run(aConst, &AConst::member0);
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(f.result(), 10);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, aConst, &AConst::member0);
|
|
|
|
QCOMPARE(f.result(), 10);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-10-31 01:14:58 +00:00
|
|
|
f = run(aConst, &AConst::member1, 20);
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(f.result(), 20);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, aConst, &AConst::member1, 20);
|
|
|
|
QCOMPARE(f.result(), 20);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
f = run(aConst);
|
|
|
|
QCOMPARE(f.result(), 10);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, aConst);
|
|
|
|
QCOMPARE(f.result(), 10);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
f = run(&aConst);
|
|
|
|
QCOMPARE(f.result(), 10);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, &aConst);
|
|
|
|
QCOMPARE(f.result(), 10);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
f = run(aConst, 20);
|
|
|
|
QCOMPARE(f.result(), 20);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, aConst, 20);
|
|
|
|
QCOMPARE(f.result(), 20);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
f = run(&aConst, 20);
|
|
|
|
QCOMPARE(f.result(), 20);
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, &aConst, 20);
|
|
|
|
QCOMPARE(f.result(), 20);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct TestClass
|
|
|
|
{
|
|
|
|
void foo() { }
|
|
|
|
typedef void result_type;
|
|
|
|
void operator()() { }
|
|
|
|
void operator()(int) { }
|
|
|
|
void fooInt(int){ };
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestConstClass
|
|
|
|
{
|
|
|
|
void foo() const { }
|
|
|
|
typedef void result_type;
|
|
|
|
void operator()() const { }
|
|
|
|
void operator()(int) const { }
|
|
|
|
void fooInt(int) const { };
|
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QtConcurrentRun::functionObject()
|
|
|
|
{
|
2014-07-31 12:09:11 +00:00
|
|
|
QThreadPool pool;
|
2011-04-27 10:05:43 +00:00
|
|
|
QFuture<void> f;
|
|
|
|
TestClass c;
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
f = run(c);
|
2011-10-31 01:14:58 +00:00
|
|
|
f = run(&c);
|
2011-04-27 10:05:43 +00:00
|
|
|
f = run(c, 10);
|
2011-10-31 01:14:58 +00:00
|
|
|
f = run(&c, 10);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2014-07-31 12:09:11 +00:00
|
|
|
f = run(&pool, c);
|
|
|
|
f = run(&pool, &c);
|
|
|
|
f = run(&pool, c, 10);
|
|
|
|
f = run(&pool, &c, 10);
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
const TestConstClass cc = TestConstClass();
|
|
|
|
f = run(cc);
|
2011-10-31 01:14:58 +00:00
|
|
|
f = run(&cc);
|
2011-04-27 10:05:43 +00:00
|
|
|
f = run(cc, 10);
|
2011-10-31 01:14:58 +00:00
|
|
|
f = run(&cc, 10);
|
2014-07-31 12:09:11 +00:00
|
|
|
|
|
|
|
f = run(&pool, cc);
|
|
|
|
f = run(&pool, &cc);
|
|
|
|
f = run(&pool, cc, 10);
|
|
|
|
f = run(&pool, &cc, 10);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void tst_QtConcurrentRun::memberFunctions()
|
|
|
|
{
|
2014-07-31 12:09:11 +00:00
|
|
|
QThreadPool pool;
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
TestClass c;
|
|
|
|
|
2011-10-31 01:14:58 +00:00
|
|
|
run(c, &TestClass::foo).waitForFinished();
|
|
|
|
run(&c, &TestClass::foo).waitForFinished();
|
|
|
|
run(c, &TestClass::fooInt, 10).waitForFinished();
|
|
|
|
run(&c, &TestClass::fooInt, 10).waitForFinished();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2014-07-31 12:09:11 +00:00
|
|
|
run(&pool, c, &TestClass::foo).waitForFinished();
|
|
|
|
run(&pool, &c, &TestClass::foo).waitForFinished();
|
|
|
|
run(&pool, c, &TestClass::fooInt, 10).waitForFinished();
|
|
|
|
run(&pool, &c, &TestClass::fooInt, 10).waitForFinished();
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
const TestConstClass cc = TestConstClass();
|
2011-10-31 01:14:58 +00:00
|
|
|
run(cc, &TestConstClass::foo).waitForFinished();
|
|
|
|
run(&cc, &TestConstClass::foo).waitForFinished();
|
|
|
|
run(cc, &TestConstClass::fooInt, 10).waitForFinished();
|
|
|
|
run(&cc, &TestConstClass::fooInt, 10).waitForFinished();
|
2014-07-31 12:09:11 +00:00
|
|
|
|
|
|
|
run(&pool, cc, &TestConstClass::foo).waitForFinished();
|
|
|
|
run(&pool, &cc, &TestConstClass::foo).waitForFinished();
|
|
|
|
run(&pool, cc, &TestConstClass::fooInt, 10).waitForFinished();
|
|
|
|
run(&pool, &cc, &TestConstClass::fooInt, 10).waitForFinished();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void doubleFunction(double)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void stringConstRefFunction(const QString &)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void stringRefFunction(QString &)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void stringFunction(QString)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void stringIntFunction(QString)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void tst_QtConcurrentRun::implicitConvertibleTypes()
|
|
|
|
{
|
2014-07-31 12:09:11 +00:00
|
|
|
QThreadPool pool;
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
double d;
|
2011-10-31 01:14:58 +00:00
|
|
|
run(doubleFunction, d).waitForFinished();
|
2014-07-31 12:09:11 +00:00
|
|
|
run(&pool, doubleFunction, d).waitForFinished();
|
2011-04-27 10:05:43 +00:00
|
|
|
int i;
|
2011-10-31 01:14:58 +00:00
|
|
|
run(doubleFunction, d).waitForFinished();
|
2014-07-31 12:09:11 +00:00
|
|
|
run(&pool, doubleFunction, d).waitForFinished();
|
2011-10-31 01:14:58 +00:00
|
|
|
run(doubleFunction, i).waitForFinished();
|
2014-07-31 12:09:11 +00:00
|
|
|
run(&pool, doubleFunction, i).waitForFinished();
|
2011-10-31 01:14:58 +00:00
|
|
|
run(doubleFunction, 10).waitForFinished();
|
2014-07-31 12:09:11 +00:00
|
|
|
run(&pool, doubleFunction, 10).waitForFinished();
|
2011-10-31 01:14:58 +00:00
|
|
|
run(stringFunction, QLatin1String("Foo")).waitForFinished();
|
2014-07-31 12:09:11 +00:00
|
|
|
run(&pool, stringFunction, QLatin1String("Foo")).waitForFinished();
|
2011-10-31 01:14:58 +00:00
|
|
|
run(stringConstRefFunction, QLatin1String("Foo")).waitForFinished();
|
2014-07-31 12:09:11 +00:00
|
|
|
run(&pool, stringConstRefFunction, QLatin1String("Foo")).waitForFinished();
|
2011-04-27 10:05:43 +00:00
|
|
|
QString string;
|
2011-10-31 01:14:58 +00:00
|
|
|
run(stringRefFunction, string).waitForFinished();
|
2014-07-31 12:09:11 +00:00
|
|
|
run(&pool, stringRefFunction, string).waitForFinished();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void fn() { }
|
|
|
|
|
|
|
|
void tst_QtConcurrentRun::runWaitLoop()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 1000; ++i)
|
|
|
|
run(fn).waitForFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
QAtomicInt count;
|
|
|
|
|
|
|
|
void recursiveRun(int level)
|
|
|
|
{
|
|
|
|
count.ref();
|
|
|
|
if (--level > 0) {
|
|
|
|
QFuture<void> f1 = run(recursiveRun, level);
|
|
|
|
QFuture<void> f2 = run(recursiveRun, level);
|
|
|
|
f1.waitForFinished();
|
|
|
|
f2.waitForFinished();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int recursiveResult(int level)
|
|
|
|
{
|
|
|
|
count.ref();
|
|
|
|
if (--level > 0) {
|
|
|
|
QFuture<int> f1 = run(recursiveResult, level);
|
|
|
|
QFuture<int> f2 = run(recursiveResult, level);
|
|
|
|
return f1.result() + f2.result();
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QtConcurrentRun::recursive()
|
|
|
|
{
|
|
|
|
int levels = 15;
|
|
|
|
|
|
|
|
for (int i = 0; i < QThread::idealThreadCount(); ++i) {
|
2011-11-07 12:39:35 +00:00
|
|
|
count.store(0);
|
2011-04-27 10:05:43 +00:00
|
|
|
QThreadPool::globalInstance()->setMaxThreadCount(i);
|
|
|
|
recursiveRun(levels);
|
2011-11-07 12:39:35 +00:00
|
|
|
QCOMPARE(count.load(), (int)pow(2.0, levels) - 1);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < QThread::idealThreadCount(); ++i) {
|
2011-11-07 12:39:35 +00:00
|
|
|
count.store(0);
|
2011-04-27 10:05:43 +00:00
|
|
|
QThreadPool::globalInstance()->setMaxThreadCount(i);
|
|
|
|
recursiveResult(levels);
|
2011-11-07 12:39:35 +00:00
|
|
|
QCOMPARE(count.load(), (int)pow(2.0, levels) - 1);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int e;
|
|
|
|
void vfn0()
|
|
|
|
{
|
|
|
|
++e;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fn0()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vfn1(double)
|
|
|
|
{
|
|
|
|
++e;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fn1(int)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vfn2(double, int *)
|
|
|
|
{
|
|
|
|
++e;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fn2(double, int *)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef QT_NO_EXCEPTIONS
|
|
|
|
void throwFunction()
|
|
|
|
{
|
2012-08-27 10:24:48 +00:00
|
|
|
throw QException();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int throwFunctionReturn()
|
|
|
|
{
|
2012-08-27 10:24:48 +00:00
|
|
|
throw QException();
|
2011-04-27 10:05:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QtConcurrentRun::exceptions()
|
|
|
|
{
|
2014-07-31 12:09:11 +00:00
|
|
|
QThreadPool pool;
|
|
|
|
bool caught;
|
|
|
|
|
|
|
|
caught = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
try {
|
|
|
|
QtConcurrent::run(throwFunction).waitForFinished();
|
2012-08-27 10:24:48 +00:00
|
|
|
} catch (QException &) {
|
2011-04-27 10:05:43 +00:00
|
|
|
caught = true;
|
|
|
|
}
|
|
|
|
if (!caught)
|
|
|
|
QFAIL("did not get exception");
|
|
|
|
|
2014-07-31 12:09:11 +00:00
|
|
|
caught = false;
|
|
|
|
try {
|
|
|
|
QtConcurrent::run(&pool, throwFunction).waitForFinished();
|
|
|
|
} catch (QException &) {
|
|
|
|
caught = true;
|
|
|
|
}
|
|
|
|
if (!caught)
|
|
|
|
QFAIL("did not get exception");
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
caught = false;
|
|
|
|
try {
|
|
|
|
QtConcurrent::run(throwFunctionReturn).waitForFinished();
|
2012-08-27 10:24:48 +00:00
|
|
|
} catch (QException &) {
|
2011-04-27 10:05:43 +00:00
|
|
|
caught = true;
|
|
|
|
}
|
|
|
|
if (!caught)
|
|
|
|
QFAIL("did not get exception");
|
2014-07-31 12:09:11 +00:00
|
|
|
|
|
|
|
caught = false;
|
|
|
|
try {
|
|
|
|
QtConcurrent::run(&pool, throwFunctionReturn).waitForFinished();
|
|
|
|
} catch (QException &) {
|
|
|
|
caught = true;
|
|
|
|
}
|
|
|
|
if (!caught)
|
|
|
|
QFAIL("did not get exception");
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-17 08:02:57 +00:00
|
|
|
#ifdef Q_COMPILER_DECLTYPE
|
|
|
|
// Compiler supports decltype
|
2011-04-18 12:07:32 +00:00
|
|
|
struct Functor {
|
|
|
|
int operator()() { return 42; }
|
|
|
|
double operator()(double a, double b) { return a/b; }
|
|
|
|
int operator()(int a, int b) { return a/b; }
|
|
|
|
void operator()(int) { }
|
|
|
|
void operator()(int, int, int) { }
|
|
|
|
void operator()(int, int, int, int) { }
|
|
|
|
void operator()(int, int, int, int, int) { }
|
|
|
|
void operator()(int, int, int, int, int, int) { }
|
|
|
|
};
|
|
|
|
|
2011-10-04 01:51:31 +00:00
|
|
|
// This tests functor without result_type; decltype need to be supported by the compiler.
|
2011-04-18 12:07:32 +00:00
|
|
|
void tst_QtConcurrentRun::functor()
|
|
|
|
{
|
|
|
|
Functor f;
|
|
|
|
{
|
|
|
|
QFuture<int> fut = QtConcurrent::run(f);
|
|
|
|
QCOMPARE(fut.result(), 42);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
QFuture<double> fut = QtConcurrent::run(f, 8.5, 1.8);
|
|
|
|
QCOMPARE(fut.result(), (8.5/1.8));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
QFuture<int> fut = QtConcurrent::run(f, 19, 3);
|
|
|
|
QCOMPARE(fut.result(), int(19/3));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
QtConcurrent::run(f, 1).waitForFinished();
|
|
|
|
QtConcurrent::run(f, 1,2).waitForFinished();
|
|
|
|
QtConcurrent::run(f, 1,2,3).waitForFinished();
|
|
|
|
QtConcurrent::run(f, 1,2,3,4).waitForFinished();
|
|
|
|
QtConcurrent::run(f, 1,2,3,4,5).waitForFinished();
|
|
|
|
}
|
2014-07-31 12:09:11 +00:00
|
|
|
// and now with explicit pool:
|
|
|
|
QThreadPool pool;
|
|
|
|
{
|
|
|
|
QFuture<int> fut = QtConcurrent::run(&pool, f);
|
|
|
|
QCOMPARE(fut.result(), 42);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
QFuture<double> fut = QtConcurrent::run(&pool, f, 8.5, 1.8);
|
|
|
|
QCOMPARE(fut.result(), (8.5/1.8));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
QFuture<int> fut = QtConcurrent::run(&pool, f, 19, 3);
|
|
|
|
QCOMPARE(fut.result(), int(19/3));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
QtConcurrent::run(&pool, f, 1).waitForFinished();
|
|
|
|
QtConcurrent::run(&pool, f, 1,2).waitForFinished();
|
|
|
|
QtConcurrent::run(&pool, f, 1,2,3).waitForFinished();
|
|
|
|
QtConcurrent::run(&pool, f, 1,2,3,4).waitForFinished();
|
|
|
|
QtConcurrent::run(&pool, f, 1,2,3,4,5).waitForFinished();
|
|
|
|
}
|
2011-11-15 01:36:18 +00:00
|
|
|
}
|
2012-10-17 08:02:57 +00:00
|
|
|
#endif
|
2011-04-18 12:07:32 +00:00
|
|
|
|
2012-10-17 08:02:57 +00:00
|
|
|
#ifdef Q_COMPILER_LAMBDA
|
|
|
|
// Compiler supports lambda
|
2011-04-18 12:07:32 +00:00
|
|
|
void tst_QtConcurrentRun::lambda()
|
|
|
|
{
|
|
|
|
QCOMPARE(QtConcurrent::run([](){ return 45; }).result(), 45);
|
|
|
|
QCOMPARE(QtConcurrent::run([](int a){ return a+15; }, 12).result(), 12+15);
|
|
|
|
QCOMPARE(QtConcurrent::run([](int a, double b){ return a + b; }, 12, 15).result(), double(12+15));
|
|
|
|
QCOMPARE(QtConcurrent::run([](int a , int, int, int, int b){ return a + b; }, 1, 2, 3, 4, 5).result(), 1 + 5);
|
|
|
|
|
|
|
|
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
|
|
|
{
|
|
|
|
QString str { "Hello World Foo" };
|
|
|
|
QFuture<QStringList> f1 = QtConcurrent::run([&](){ return str.split(' '); });
|
|
|
|
auto r = f1.result();
|
|
|
|
QCOMPARE(r, QStringList({"Hello", "World", "Foo"}));
|
|
|
|
}
|
|
|
|
#endif
|
2014-07-31 12:09:11 +00:00
|
|
|
|
|
|
|
// and now with explicit pool:
|
|
|
|
QThreadPool pool;
|
|
|
|
QCOMPARE(QtConcurrent::run(&pool, [](){ return 45; }).result(), 45);
|
|
|
|
QCOMPARE(QtConcurrent::run(&pool, [](int a){ return a+15; }, 12).result(), 12+15);
|
|
|
|
QCOMPARE(QtConcurrent::run(&pool, [](int a, double b){ return a + b; }, 12, 15).result(), double(12+15));
|
|
|
|
QCOMPARE(QtConcurrent::run(&pool, [](int a , int, int, int, int b){ return a + b; }, 1, 2, 3, 4, 5).result(), 1 + 5);
|
|
|
|
|
|
|
|
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
|
|
|
{
|
|
|
|
QString str { "Hello World Foo" };
|
|
|
|
QFuture<QStringList> f1 = QtConcurrent::run(&pool, [&](){ return str.split(' '); });
|
|
|
|
auto r = f1.result();
|
|
|
|
QCOMPARE(r, QStringList({"Hello", "World", "Foo"}));
|
|
|
|
}
|
|
|
|
#endif
|
2011-11-15 01:36:18 +00:00
|
|
|
}
|
2012-10-17 08:02:57 +00:00
|
|
|
#endif
|
2011-04-18 12:07:32 +00:00
|
|
|
|
2011-10-11 04:15:52 +00:00
|
|
|
QTEST_MAIN(tst_QtConcurrentRun)
|
2011-04-27 10:05:43 +00:00
|
|
|
#include "tst_qtconcurrentrun.moc"
|