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