2769 |
11 Jul 12 |
peter |
// $Id$ |
2769 |
11 Jul 12 |
peter |
2 |
|
2769 |
11 Jul 12 |
peter |
3 |
/* |
3999 |
08 Oct 20 |
peter |
Copyright (C) 2012, 2013, 2020 Peter Johansson |
2769 |
11 Jul 12 |
peter |
5 |
|
2769 |
11 Jul 12 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
2769 |
11 Jul 12 |
peter |
7 |
|
2769 |
11 Jul 12 |
peter |
The yat library is free software; you can redistribute it and/or |
2769 |
11 Jul 12 |
peter |
modify it under the terms of the GNU General Public License as |
2769 |
11 Jul 12 |
peter |
published by the Free Software Foundation; either version 3 of the |
2769 |
11 Jul 12 |
peter |
License, or (at your option) any later version. |
2769 |
11 Jul 12 |
peter |
12 |
|
2769 |
11 Jul 12 |
peter |
The yat library is distributed in the hope that it will be useful, |
2769 |
11 Jul 12 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2769 |
11 Jul 12 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
2769 |
11 Jul 12 |
peter |
General Public License for more details. |
2769 |
11 Jul 12 |
peter |
17 |
|
2769 |
11 Jul 12 |
peter |
You should have received a copy of the GNU General Public License |
2769 |
11 Jul 12 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
2769 |
11 Jul 12 |
peter |
20 |
*/ |
2769 |
11 Jul 12 |
peter |
21 |
|
2881 |
18 Nov 12 |
peter |
22 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
23 |
|
2769 |
11 Jul 12 |
peter |
24 |
#include "Suite.h" |
2769 |
11 Jul 12 |
peter |
25 |
|
2769 |
11 Jul 12 |
peter |
26 |
#include "yat/random/random.h" |
2769 |
11 Jul 12 |
peter |
27 |
|
2769 |
11 Jul 12 |
peter |
28 |
#include <algorithm> |
2943 |
04 Jan 13 |
peter |
29 |
#include <cstdlib> |
2769 |
11 Jul 12 |
peter |
30 |
#include <iostream> |
2769 |
11 Jul 12 |
peter |
31 |
#include <iterator> |
2943 |
04 Jan 13 |
peter |
32 |
#include <stdexcept> |
3949 |
20 Jul 20 |
peter |
33 |
#include <thread> |
2769 |
11 Jul 12 |
peter |
34 |
#include <vector> |
2769 |
11 Jul 12 |
peter |
35 |
|
2802 |
29 Jul 12 |
peter |
36 |
using namespace theplu::yat; |
2769 |
11 Jul 12 |
peter |
37 |
using namespace theplu::yat::random; |
2769 |
11 Jul 12 |
peter |
38 |
|
2769 |
11 Jul 12 |
peter |
39 |
class Visitor |
2769 |
11 Jul 12 |
peter |
40 |
{ |
2769 |
11 Jul 12 |
peter |
41 |
public: |
2769 |
11 Jul 12 |
peter |
42 |
Visitor(std::vector<int>& x) |
2769 |
11 Jul 12 |
peter |
43 |
: begin_(x.begin()), end_(x.end()) {} |
2769 |
11 Jul 12 |
peter |
44 |
|
2769 |
11 Jul 12 |
peter |
45 |
void operator()(void) |
2769 |
11 Jul 12 |
peter |
46 |
{ |
2943 |
04 Jan 13 |
peter |
47 |
try { |
2943 |
04 Jan 13 |
peter |
48 |
for (std::vector<int>::iterator i=begin_; i!=end_; ++i) |
2943 |
04 Jan 13 |
peter |
49 |
*i = rnd_(100); |
2943 |
04 Jan 13 |
peter |
50 |
} |
2943 |
04 Jan 13 |
peter |
51 |
catch (std::exception& e) { |
2943 |
04 Jan 13 |
peter |
52 |
std::cerr << e.what() << "\n"; |
2943 |
04 Jan 13 |
peter |
53 |
exit(EXIT_FAILURE); |
2943 |
04 Jan 13 |
peter |
54 |
} |
2769 |
11 Jul 12 |
peter |
55 |
} |
2769 |
11 Jul 12 |
peter |
56 |
|
2769 |
11 Jul 12 |
peter |
57 |
private: |
2769 |
11 Jul 12 |
peter |
58 |
DiscreteUniform rnd_; |
2769 |
11 Jul 12 |
peter |
59 |
std::vector<int>::iterator begin_; |
2769 |
11 Jul 12 |
peter |
60 |
std::vector<int>::iterator end_; |
2769 |
11 Jul 12 |
peter |
61 |
}; |
2769 |
11 Jul 12 |
peter |
62 |
|
2802 |
29 Jul 12 |
peter |
63 |
void test1(test::Suite& suite); |
2802 |
29 Jul 12 |
peter |
64 |
void test2(test::Suite& suite); |
2802 |
29 Jul 12 |
peter |
65 |
void test3(test::Suite& suite); |
2769 |
11 Jul 12 |
peter |
66 |
|
2769 |
11 Jul 12 |
peter |
67 |
int main(int argc, char* argv[]) |
2769 |
11 Jul 12 |
peter |
68 |
{ |
2769 |
11 Jul 12 |
peter |
69 |
theplu::yat::test::Suite suite(argc, argv); |
2802 |
29 Jul 12 |
peter |
70 |
test1(suite); |
2802 |
29 Jul 12 |
peter |
71 |
test2(suite); |
2802 |
29 Jul 12 |
peter |
72 |
test3(suite); |
2769 |
11 Jul 12 |
peter |
73 |
|
2802 |
29 Jul 12 |
peter |
74 |
return suite.return_value(); |
2802 |
29 Jul 12 |
peter |
75 |
} |
2802 |
29 Jul 12 |
peter |
76 |
|
2802 |
29 Jul 12 |
peter |
77 |
|
2802 |
29 Jul 12 |
peter |
78 |
void test1(test::Suite& suite) |
2802 |
29 Jul 12 |
peter |
79 |
{ |
2802 |
29 Jul 12 |
peter |
80 |
suite.out() << "test1\n"; |
3949 |
20 Jul 20 |
peter |
81 |
std::vector<std::thread> threads; |
2769 |
11 Jul 12 |
peter |
82 |
|
2769 |
11 Jul 12 |
peter |
83 |
std::vector<int> x(10); |
2769 |
11 Jul 12 |
peter |
84 |
std::vector<int> y(x); |
2769 |
11 Jul 12 |
peter |
85 |
|
2769 |
11 Jul 12 |
peter |
86 |
Visitor visitor1(x); |
2769 |
11 Jul 12 |
peter |
87 |
Visitor visitor2(y); |
3949 |
20 Jul 20 |
peter |
88 |
threads.push_back(std::thread(Visitor(x))); |
3949 |
20 Jul 20 |
peter |
89 |
threads.push_back(std::thread(Visitor(y))); |
3949 |
20 Jul 20 |
peter |
90 |
for (size_t i=0; i<threads.size(); ++i) |
3949 |
20 Jul 20 |
peter |
91 |
threads[i].join(); |
2769 |
11 Jul 12 |
peter |
92 |
|
2769 |
11 Jul 12 |
peter |
93 |
suite.out() << "x: "; |
2769 |
11 Jul 12 |
peter |
94 |
std::copy(x.begin(), x.end(), std::ostream_iterator<int>(suite.out(), " ")); |
2769 |
11 Jul 12 |
peter |
95 |
suite.out() << "\ny: "; |
2769 |
11 Jul 12 |
peter |
96 |
std::copy(y.begin(), y.end(), std::ostream_iterator<int>(suite.out(), " ")); |
2943 |
04 Jan 13 |
peter |
97 |
suite.out() << std::endl; |
2769 |
11 Jul 12 |
peter |
98 |
|
2802 |
29 Jul 12 |
peter |
99 |
if (x==y) { |
2802 |
29 Jul 12 |
peter |
100 |
suite.add(false); |
2802 |
29 Jul 12 |
peter |
101 |
suite.err() << "x and y are equal\n"; |
2802 |
29 Jul 12 |
peter |
102 |
} |
2769 |
11 Jul 12 |
peter |
103 |
} |
2802 |
29 Jul 12 |
peter |
104 |
|
2802 |
29 Jul 12 |
peter |
105 |
|
2943 |
04 Jan 13 |
peter |
106 |
std::vector<int> rnd_vec(void) |
2802 |
29 Jul 12 |
peter |
107 |
{ |
2802 |
29 Jul 12 |
peter |
108 |
std::vector<int> x(10); |
2802 |
29 Jul 12 |
peter |
109 |
Visitor visitor1(x); |
3949 |
20 Jul 20 |
peter |
110 |
std::thread thread(visitor1); |
3949 |
20 Jul 20 |
peter |
111 |
thread.join(); |
2943 |
04 Jan 13 |
peter |
112 |
return x; |
2943 |
04 Jan 13 |
peter |
113 |
} |
2943 |
04 Jan 13 |
peter |
114 |
|
2943 |
04 Jan 13 |
peter |
115 |
|
2943 |
04 Jan 13 |
peter |
116 |
void test2(test::Suite& suite) |
2943 |
04 Jan 13 |
peter |
117 |
{ |
2943 |
04 Jan 13 |
peter |
118 |
suite.out() << "test2\n"; |
2943 |
04 Jan 13 |
peter |
// test that we get same number with same seed |
2802 |
29 Jul 12 |
peter |
120 |
RNG::instance()->seed(0); |
2943 |
04 Jan 13 |
peter |
121 |
std::vector<int> y = rnd_vec(); |
2943 |
04 Jan 13 |
peter |
122 |
suite.out() << "y: "; |
2943 |
04 Jan 13 |
peter |
123 |
std::copy(y.begin(), y.end(), std::ostream_iterator<int>(suite.out(), " ")); |
2943 |
04 Jan 13 |
peter |
124 |
suite.out() << std::endl; |
2943 |
04 Jan 13 |
peter |
125 |
|
2943 |
04 Jan 13 |
peter |
126 |
RNG::instance()->seed(0); |
2943 |
04 Jan 13 |
peter |
127 |
std::vector<int> x = rnd_vec(); |
2802 |
29 Jul 12 |
peter |
128 |
suite.out() << "x: "; |
2802 |
29 Jul 12 |
peter |
129 |
std::copy(x.begin(), x.end(), std::ostream_iterator<int>(suite.out(), " ")); |
2943 |
04 Jan 13 |
peter |
130 |
suite.out() << std::endl; |
2802 |
29 Jul 12 |
peter |
131 |
if (x!=y) { |
2802 |
29 Jul 12 |
peter |
132 |
suite.add(false); |
2802 |
29 Jul 12 |
peter |
133 |
suite.err() << "x not equal to y\n"; |
2802 |
29 Jul 12 |
peter |
134 |
} |
2802 |
29 Jul 12 |
peter |
135 |
else { |
2802 |
29 Jul 12 |
peter |
136 |
suite.out() << "ok\n"; |
2802 |
29 Jul 12 |
peter |
137 |
} |
2802 |
29 Jul 12 |
peter |
138 |
} |
2802 |
29 Jul 12 |
peter |
139 |
|
2802 |
29 Jul 12 |
peter |
140 |
|
2802 |
29 Jul 12 |
peter |
141 |
void test3(test::Suite& suite) |
2802 |
29 Jul 12 |
peter |
142 |
{ |
2802 |
29 Jul 12 |
peter |
143 |
suite.out() << "test3\n"; |
2802 |
29 Jul 12 |
peter |
// test that we get different numbers in thread in which we seed RNG |
2802 |
29 Jul 12 |
peter |
// and subsequent thread |
2802 |
29 Jul 12 |
peter |
146 |
RNG::instance()->seed(1); |
2802 |
29 Jul 12 |
peter |
147 |
std::vector<int> x(10); |
2802 |
29 Jul 12 |
peter |
148 |
Visitor visitor(x); |
2802 |
29 Jul 12 |
peter |
149 |
visitor(); |
2802 |
29 Jul 12 |
peter |
150 |
suite.out() << "x: "; |
2802 |
29 Jul 12 |
peter |
151 |
std::copy(x.begin(), x.end(), std::ostream_iterator<int>(suite.out(), " ")); |
2943 |
04 Jan 13 |
peter |
152 |
suite.out() << std::endl; |
2943 |
04 Jan 13 |
peter |
153 |
|
2943 |
04 Jan 13 |
peter |
154 |
std::vector<int> y = rnd_vec(); |
2943 |
04 Jan 13 |
peter |
155 |
suite.out() << "y: "; |
2802 |
29 Jul 12 |
peter |
156 |
std::copy(y.begin(), y.end(), std::ostream_iterator<int>(suite.out(), " ")); |
2943 |
04 Jan 13 |
peter |
157 |
suite.out() << std::endl; |
2802 |
29 Jul 12 |
peter |
158 |
|
2802 |
29 Jul 12 |
peter |
159 |
if (x==y) { |
2802 |
29 Jul 12 |
peter |
160 |
suite.add(false); |
2802 |
29 Jul 12 |
peter |
161 |
suite.err() << "x equal to y\n"; |
2802 |
29 Jul 12 |
peter |
162 |
} |
2802 |
29 Jul 12 |
peter |
163 |
else { |
2802 |
29 Jul 12 |
peter |
164 |
suite.out() << "ok\n"; |
2802 |
29 Jul 12 |
peter |
165 |
} |
2802 |
29 Jul 12 |
peter |
166 |
} |