summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorOlivier Goffart <olivier.goffart@nokia.com>2011-04-18 12:07:32 (GMT)
committerOlivier Goffart <olivier.goffart@nokia.com>2011-05-19 13:09:31 (GMT)
commit917f2ff617209bcc283eb3590b422bcf239c0537 (patch)
tree8a58ff1f428c85546303864eea7229c8e341cbe9 /tests
parent837f18f043b18410c1d93b9f1156acf729dad510 (diff)
downloadQt-917f2ff617209bcc283eb3590b422bcf239c0537.zip
Qt-917f2ff617209bcc283eb3590b422bcf239c0537.tar.gz
Qt-917f2ff617209bcc283eb3590b422bcf239c0537.tar.bz2
Support of lambdas in QtConcurrent::run
Reviewed-by: Joao
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qtconcurrentrun/tst_qtconcurrentrun.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/auto/qtconcurrentrun/tst_qtconcurrentrun.cpp b/tests/auto/qtconcurrentrun/tst_qtconcurrentrun.cpp
index 1069cb5..be79a13 100644
--- a/tests/auto/qtconcurrentrun/tst_qtconcurrentrun.cpp
+++ b/tests/auto/qtconcurrentrun/tst_qtconcurrentrun.cpp
@@ -67,6 +67,8 @@ private slots:
#if 0
void createFunctor();
#endif
+ void functor();
+ void lambda();
};
#if 0
@@ -444,6 +446,71 @@ void tst_QtConcurrentRun::createFunctor()
}
#endif
+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) { }
+};
+
+void tst_QtConcurrentRun::functor()
+{
+ //this test functor without result_type, decltype need to be supported by the compiler
+#ifndef Q_COMPILER_DECLTYPE
+ QSKIP("Compiler do not suport decltype", SkipAll);
+#else
+ 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();
+ }
+#endif
+}
+
+
+void tst_QtConcurrentRun::lambda()
+{
+#ifndef Q_COMPILER_LAMBDA
+ QSKIP("Compiler do not suport lambda", SkipAll);
+#else
+
+ 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
+
+#endif
+}
+
+
#include "tst_qtconcurrentrun.moc"
#else