3402 |
31 Mar 15 |
peter |
// $Id$ |
3402 |
31 Mar 15 |
peter |
2 |
|
3402 |
31 Mar 15 |
peter |
3 |
/* |
3855 |
02 Jan 20 |
peter |
Copyright (C) 2015, 2019 Peter Johansson |
3402 |
31 Mar 15 |
peter |
5 |
|
3402 |
31 Mar 15 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
3402 |
31 Mar 15 |
peter |
7 |
|
3402 |
31 Mar 15 |
peter |
The yat library is free software; you can redistribute it and/or |
3402 |
31 Mar 15 |
peter |
modify it under the terms of the GNU General Public License as |
3402 |
31 Mar 15 |
peter |
published by the Free Software Foundation; either version 3 of the |
3402 |
31 Mar 15 |
peter |
License, or (at your option) any later version. |
3402 |
31 Mar 15 |
peter |
12 |
|
3402 |
31 Mar 15 |
peter |
The yat library is distributed in the hope that it will be useful, |
3402 |
31 Mar 15 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
3402 |
31 Mar 15 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3402 |
31 Mar 15 |
peter |
General Public License for more details. |
3402 |
31 Mar 15 |
peter |
17 |
|
3402 |
31 Mar 15 |
peter |
You should have received a copy of the GNU General Public License |
3402 |
31 Mar 15 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
3402 |
31 Mar 15 |
peter |
20 |
*/ |
3402 |
31 Mar 15 |
peter |
21 |
|
3402 |
31 Mar 15 |
peter |
22 |
#include <config.h> |
3402 |
31 Mar 15 |
peter |
23 |
|
3402 |
31 Mar 15 |
peter |
24 |
#include "Suite.h" |
3402 |
31 Mar 15 |
peter |
25 |
|
3402 |
31 Mar 15 |
peter |
26 |
#include "yat/utility/Scheduler.h" |
3402 |
31 Mar 15 |
peter |
27 |
|
3402 |
31 Mar 15 |
peter |
28 |
#include <boost/make_shared.hpp> |
3402 |
31 Mar 15 |
peter |
29 |
#include <boost/shared_ptr.hpp> |
3402 |
31 Mar 15 |
peter |
30 |
|
3402 |
31 Mar 15 |
peter |
31 |
#include <cstdlib> |
3402 |
31 Mar 15 |
peter |
32 |
#include <iostream> |
3402 |
31 Mar 15 |
peter |
33 |
#include <set> |
3402 |
31 Mar 15 |
peter |
34 |
#include <unistd.h> |
3402 |
31 Mar 15 |
peter |
35 |
|
3402 |
31 Mar 15 |
peter |
36 |
using namespace theplu::yat; |
3402 |
31 Mar 15 |
peter |
37 |
using namespace theplu::yat::utility; |
3402 |
31 Mar 15 |
peter |
38 |
|
3402 |
31 Mar 15 |
peter |
39 |
class Task : public Scheduler::Job |
3402 |
31 Mar 15 |
peter |
40 |
{ |
3402 |
31 Mar 15 |
peter |
41 |
public: |
3402 |
31 Mar 15 |
peter |
42 |
Task(int x, std::vector<int>& res) : value(x), result(res) {} |
3402 |
31 Mar 15 |
peter |
43 |
void operator()(void) |
3402 |
31 Mar 15 |
peter |
44 |
{ |
3402 |
31 Mar 15 |
peter |
45 |
result.push_back(value); |
3402 |
31 Mar 15 |
peter |
46 |
sleep(1); |
3402 |
31 Mar 15 |
peter |
47 |
} |
3402 |
31 Mar 15 |
peter |
48 |
private: |
3402 |
31 Mar 15 |
peter |
49 |
int value; |
3848 |
23 Sep 19 |
peter |
// We are running test with single-threaded scheduler, so an unsafe |
3848 |
23 Sep 19 |
peter |
// vector is okay. |
3402 |
31 Mar 15 |
peter |
52 |
std::vector<int>& result; |
3402 |
31 Mar 15 |
peter |
53 |
}; |
3402 |
31 Mar 15 |
peter |
54 |
|
3402 |
31 Mar 15 |
peter |
55 |
|
3402 |
31 Mar 15 |
peter |
56 |
int main(int argc, char* argv[]) |
3402 |
31 Mar 15 |
peter |
57 |
{ |
3402 |
31 Mar 15 |
peter |
58 |
test::Suite suite(argc, argv); |
3402 |
31 Mar 15 |
peter |
59 |
|
3402 |
31 Mar 15 |
peter |
60 |
std::vector<int> result; |
3402 |
31 Mar 15 |
peter |
61 |
|
3402 |
31 Mar 15 |
peter |
// use only one thread to make test deterministic |
3402 |
31 Mar 15 |
peter |
63 |
Scheduler scheduler(1); |
3402 |
31 Mar 15 |
peter |
64 |
std::vector<boost::shared_ptr<Task> > task; |
3402 |
31 Mar 15 |
peter |
65 |
task.reserve(5); |
3402 |
31 Mar 15 |
peter |
66 |
task.push_back(boost::shared_ptr<Task>(new Task(0, result))); |
3402 |
31 Mar 15 |
peter |
67 |
task.push_back(boost::shared_ptr<Task>(new Task(1, result))); |
3402 |
31 Mar 15 |
peter |
68 |
scheduler.add_dependency(task[1], task[0]); |
3402 |
31 Mar 15 |
peter |
69 |
scheduler.submit(task[1]); |
3402 |
31 Mar 15 |
peter |
70 |
|
3402 |
31 Mar 15 |
peter |
71 |
task.push_back(boost::shared_ptr<Task>(new Task(2, result))); |
3402 |
31 Mar 15 |
peter |
72 |
task.push_back(boost::shared_ptr<Task>(new Task(3, result))); |
3402 |
31 Mar 15 |
peter |
73 |
scheduler.add_dependency(task[3], task[2]); |
3402 |
31 Mar 15 |
peter |
74 |
scheduler.submit(task[3]); |
3402 |
31 Mar 15 |
peter |
75 |
|
3402 |
31 Mar 15 |
peter |
76 |
task.push_back(boost::shared_ptr<Task>(new Task(4, result))); |
3402 |
31 Mar 15 |
peter |
77 |
task.push_back(boost::shared_ptr<Task>(new Task(5, result))); |
3402 |
31 Mar 15 |
peter |
78 |
scheduler.add_dependency(task[5], task[4]); |
3402 |
31 Mar 15 |
peter |
79 |
|
3402 |
31 Mar 15 |
peter |
80 |
scheduler.submit(task[5]); |
3402 |
31 Mar 15 |
peter |
81 |
scheduler.wait(); |
3402 |
31 Mar 15 |
peter |
82 |
|
3402 |
31 Mar 15 |
peter |
// check that task[1] was done prior task[3] |
3402 |
31 Mar 15 |
peter |
84 |
for (size_t i=0; i<result.size(); ++i) { |
3402 |
31 Mar 15 |
peter |
85 |
suite.out() << i << " " << result[i] << "\n"; |
3402 |
31 Mar 15 |
peter |
86 |
} |
3402 |
31 Mar 15 |
peter |
87 |
|
3402 |
31 Mar 15 |
peter |
// first task first |
3402 |
31 Mar 15 |
peter |
89 |
suite.add(result[0] == 0); |
3402 |
31 Mar 15 |
peter |
// second job to run should be either second or third job |
3402 |
31 Mar 15 |
peter |
91 |
suite.add(result[1]==1 || result[1]==2); |
3402 |
31 Mar 15 |
peter |
92 |
suite.add(result[2]==1 || result[2]==2); |
3402 |
31 Mar 15 |
peter |
// Task3 is run before Task4 |
3402 |
31 Mar 15 |
peter |
94 |
suite.add(result[3] < result[4]); |
3402 |
31 Mar 15 |
peter |
95 |
suite.add(result[5] == 5); |
3402 |
31 Mar 15 |
peter |
96 |
|
3402 |
31 Mar 15 |
peter |
97 |
return suite.return_value(); |
3402 |
31 Mar 15 |
peter |
98 |
} |