File: concurrentrun_test.cpp

package info (click to toggle)
clementine 1.3.1%2Bgit276-g3485bbe43%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 26,544 kB
  • sloc: cpp: 114,212; xml: 5,193; python: 3,704; sql: 858; ansic: 123; sh: 71; makefile: 29
file content (157 lines) | stat: -rw-r--r-- 3,942 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "gtest/gtest.h"

#include <functional>

#include <QEventLoop>
#include <QFutureWatcher>
#include <QThreadPool>

#include "core/concurrentrun.h"
#include "test_utils.h"

int f() {
  return 1337;
}

TEST(ConcurrentRunTest, ConcurrentRun0StartAndWait) {
  QThreadPool threadpool;
  QFuture<int> future = ConcurrentRun::Run<int>(&threadpool, &f);
  QFutureWatcher<int> watcher;
  watcher.setFuture(future);
  QEventLoop loop;
  QObject::connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()));
  loop.exec();
  EXPECT_EQ(1337, watcher.result());
}

int g(int i) {
  return ++i;
}

TEST(ConcurrentRunTest, ConcurrentRun1StartAndWait) {
  QThreadPool threadpool;
  int i = 1336;
  QFuture<int> future = ConcurrentRun::Run<int, int>(&threadpool, &g, i);
  QFutureWatcher<int> watcher;
  watcher.setFuture(future);
  QEventLoop loop;
  QObject::connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()));
  loop.exec();
  EXPECT_EQ(1337, watcher.result());
}

int max(int i, int j) {
  return (i > j ? i : j);
}

TEST(ConcurrentRunTest, ConcurrentRun2StartAndWait) {
  int i = 10;
  int j = 42;
  QThreadPool threadpool;
  QFuture<int> future = ConcurrentRun::Run<int, int, int>(&threadpool, &max, i, j);
  QFutureWatcher<int> watcher;
  watcher.setFuture(future);
  QEventLoop loop;
  QObject::connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()));
  loop.exec();
  EXPECT_EQ(42, watcher.result());
}

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;
  QFuture<int> future = ConcurrentRun::Run<int, int, int, int>(&threadpool, &sum, i, j, k);
  QFutureWatcher<int> watcher;
  watcher.setFuture(future);
  QEventLoop loop;
  QObject::connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()));
  loop.exec();
  EXPECT_EQ(102, watcher.result());
}

void aFunction(int* n) {
  *n = 1337;
}

void bFunction(int* n, int *m) {
  aFunction(n);
  *m = 1338;
}

void cFunction(int* n, int *m, int *o) {
  bFunction(n, m);
  *o = 1339;
}

TEST(ConcurrentRunTest, ConcurrentRunVoidFunction1Start) {
  QThreadPool threadpool;

  int n = 10;
  QFuture<void> future = ConcurrentRun::Run<void, int*>(&threadpool, &aFunction, &n);
  QFutureWatcher<void> watcher;
  watcher.setFuture(future);
  QEventLoop loop;
  QObject::connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()));
  loop.exec();
  EXPECT_EQ(1337, n);
}

TEST(ConcurrentRunTest, ConcurrentRunVoidFunction2Start) {
  QThreadPool threadpool;

  int n = 10, m = 11;
  QFuture<void> future = ConcurrentRun::Run<void, int*, int*>(&threadpool, &bFunction, &n, &m);
  QFutureWatcher<void> watcher;
  watcher.setFuture(future);
  QEventLoop loop;
  QObject::connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()));
  loop.exec();
  EXPECT_EQ(1337, n);
  EXPECT_EQ(1338, m);
}

TEST(ConcurrentRunTest, ConcurrentRunVoidFunction3Start) {
  QThreadPool threadpool;

  int n = 10, m = 11, o = 12;
  QFuture<void> future = ConcurrentRun::Run<void, int*, int*, int*>(&threadpool, &cFunction, &n, &m, &o);
  QFutureWatcher<void> watcher;
  watcher.setFuture(future);
  QEventLoop loop;
  QObject::connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()));
  loop.exec();
  EXPECT_EQ(1337, n);
  EXPECT_EQ(1338, m);
  EXPECT_EQ(1339, o);
}

class A {
 public:
  void f(int* i) { 
    *i = *i + 1;
  }
};

TEST(ConcurrentRunTest, ConcurrentRunVoidBindFunctionStart) {
  QThreadPool threadpool;

  A a;
  int nb = 10;
  QFuture<void> future = ConcurrentRun::Run<void>(&threadpool, std::bind(&A::f, &a, &nb));
  QFutureWatcher<void> watcher;
  watcher.setFuture(future);
  QEventLoop loop;
  QObject::connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()));
  loop.exec();
  EXPECT_EQ(11, nb);
}

// TODO: add some more complex test cases? (e.g. with several CPU-consuming
// tasks launched in parallel, with/without threadpool's threads numbers
// decreased, etc.)