strawberry-audio-player-win.../tests/src/concurrentrun_test.cpp

177 lines
3.9 KiB
C++
Raw Normal View History

2019-06-28 01:33:22 +02:00
#include <functional>
#include <gtest/gtest.h>
2020-10-19 22:33:08 +02:00
#include <QtConcurrent>
2019-06-28 01:33:22 +02:00
#include <QThreadPool>
#include <QEventLoop>
#include <QFutureWatcher>
#include "test_utils.h"
2020-04-23 21:01:34 +02:00
int f();
2019-06-28 01:33:22 +02:00
int f() {
return 1337;
}
TEST(ConcurrentRunTest, ConcurrentRun0StartAndWait) {
QThreadPool threadpool;
2020-10-19 22:33:08 +02:00
QFuture<int> future = QtConcurrent::run(&threadpool, &f);
2019-06-28 01:33:22 +02:00
QFutureWatcher<int> watcher;
QEventLoop loop;
2021-01-26 16:48:04 +01:00
QObject::connect(&watcher, &QFutureWatcher<int>::finished, &loop, &QEventLoop::quit);
watcher.setFuture(future);
2019-06-28 01:33:22 +02:00
loop.exec();
EXPECT_EQ(1337, watcher.result());
}
2020-04-23 21:01:34 +02:00
int g(int i);
2019-06-28 01:33:22 +02:00
int g(int i) {
return ++i;
}
TEST(ConcurrentRunTest, ConcurrentRun1StartAndWait) {
QThreadPool threadpool;
int i = 1336;
2020-10-19 22:33:08 +02:00
QFuture<int> future = QtConcurrent::run(&threadpool, &g, i);
2019-06-28 01:33:22 +02:00
QFutureWatcher<int> watcher;
QEventLoop loop;
2021-01-26 16:48:04 +01:00
QObject::connect(&watcher, &QFutureWatcher<int>::finished, &loop, &QEventLoop::quit);
watcher.setFuture(future);
2019-06-28 01:33:22 +02:00
loop.exec();
EXPECT_EQ(1337, watcher.result());
}
2020-04-23 21:01:34 +02:00
int max(int i, int j);
2019-06-28 01:33:22 +02:00
int max(int i, int j) {
return (i > j ? i : j);
}
TEST(ConcurrentRunTest, ConcurrentRun2StartAndWait) {
int i = 10;
int j = 42;
QThreadPool threadpool;
2020-10-19 22:33:08 +02:00
QFuture<int> future = QtConcurrent::run(&threadpool, &max, i, j);
2019-06-28 01:33:22 +02:00
QFutureWatcher<int> watcher;
QEventLoop loop;
2021-01-26 16:48:04 +01:00
QObject::connect(&watcher, &QFutureWatcher<int>::finished, &loop, &QEventLoop::quit);
watcher.setFuture(future);
2019-06-28 01:33:22 +02:00
loop.exec();
EXPECT_EQ(42, watcher.result());
}
2020-04-23 21:01:34 +02:00
int sum(int a, int b, int c);
2019-06-28 01:33:22 +02:00
int sum(int a, int b, int c) {
return a + b + c;
}
TEST(ConcurrentRunTest, ConcurrentRun3StartAndWait) {
int i = 10;
int j = 42;
int k = 50;
QThreadPool threadpool;
2020-10-19 22:33:08 +02:00
QFuture<int> future = QtConcurrent::run(&threadpool, &sum, i, j, k);
2019-06-28 01:33:22 +02:00
QFutureWatcher<int> watcher;
QEventLoop loop;
2021-01-26 16:48:04 +01:00
QObject::connect(&watcher, &QFutureWatcher<int>::finished, &loop, &QEventLoop::quit);
watcher.setFuture(future);
2019-06-28 01:33:22 +02:00
loop.exec();
EXPECT_EQ(102, watcher.result());
}
2020-04-23 21:01:34 +02:00
void aFunction(int* n);
2019-06-28 01:33:22 +02:00
void aFunction(int* n) {
*n = 1337;
}
2020-04-23 21:01:34 +02:00
void bFunction(int* n, int *m);
2019-06-28 01:33:22 +02:00
void bFunction(int* n, int *m) {
aFunction(n);
*m = 1338;
}
2020-04-23 21:01:34 +02:00
void cFunction(int* n, int *m, int *o);
2019-06-28 01:33:22 +02:00
void cFunction(int* n, int *m, int *o) {
bFunction(n, m);
*o = 1339;
}
TEST(ConcurrentRunTest, ConcurrentRunVoidFunction1Start) {
QThreadPool threadpool;
int n = 10;
2020-10-19 22:33:08 +02:00
QFuture<void> future = QtConcurrent::run(&threadpool, &aFunction, &n);
2019-06-28 01:33:22 +02:00
QFutureWatcher<void> watcher;
QEventLoop loop;
2021-01-26 16:48:04 +01:00
QObject::connect(&watcher, &QFutureWatcher<int>::finished, &loop, &QEventLoop::quit);
watcher.setFuture(future);
2019-06-28 01:33:22 +02:00
loop.exec();
EXPECT_EQ(1337, n);
}
TEST(ConcurrentRunTest, ConcurrentRunVoidFunction2Start) {
QThreadPool threadpool;
int n = 10, m = 11;
2020-10-19 22:33:08 +02:00
QFuture<void> future = QtConcurrent::run(&threadpool, &bFunction, &n, &m);
2019-06-28 01:33:22 +02:00
QFutureWatcher<void> watcher;
QEventLoop loop;
2021-01-26 16:48:04 +01:00
QObject::connect(&watcher, &QFutureWatcher<int>::finished, &loop, &QEventLoop::quit);
watcher.setFuture(future);
2019-06-28 01:33:22 +02:00
loop.exec();
EXPECT_EQ(1337, n);
EXPECT_EQ(1338, m);
}
TEST(ConcurrentRunTest, ConcurrentRunVoidFunction3Start) {
QThreadPool threadpool;
int n = 10, m = 11, o = 12;
2020-10-19 22:33:08 +02:00
QFuture<void> future = QtConcurrent::run(&threadpool, &cFunction, &n, &m, &o);
2019-06-28 01:33:22 +02:00
QFutureWatcher<void> watcher;
QEventLoop loop;
2021-01-26 16:48:04 +01:00
QObject::connect(&watcher, &QFutureWatcher<int>::finished, &loop, &QEventLoop::quit);
watcher.setFuture(future);
2019-06-28 01:33:22 +02:00
loop.exec();
EXPECT_EQ(1337, n);
EXPECT_EQ(1338, m);
EXPECT_EQ(1339, o);
}
class A {
public:
2021-07-01 01:41:28 +02:00
void f(int* i) {
2019-06-28 01:33:22 +02:00
*i = *i + 1;
}
};
TEST(ConcurrentRunTest, ConcurrentRunVoidBindFunctionStart) {
QThreadPool threadpool;
A a;
int nb = 10;
2020-10-19 22:33:08 +02:00
QFuture<void> future = QtConcurrent::run(&threadpool, std::bind(&A::f, &a, &nb));
2019-06-28 01:33:22 +02:00
QFutureWatcher<void> watcher;
QEventLoop loop;
2021-01-26 16:48:04 +01:00
QObject::connect(&watcher, &QFutureWatcher<int>::finished, &loop, &QEventLoop::quit);
watcher.setFuture(future);
2019-06-28 01:33:22 +02:00
loop.exec();
EXPECT_EQ(11, nb);
}