source: branches/0.10-stable/test/rng-mt.cc @ 2933

Last change on this file since 2933 was 2933, checked in by Peter, 9 years ago

avoid transfer exception between threads. Print as early as possible. Flush output more often.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1// $Id: rng-mt.cc 2933 2012-12-28 03:20:58Z 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
37using namespace theplu::yat;
38using namespace theplu::yat::random;
39
40class Visitor
41{
42public:
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
58private:
59  DiscreteUniform rnd_;
60  std::vector<int>::iterator begin_;
61  std::vector<int>::iterator end_;
62};
63
64void test1(test::Suite& suite);
65void test2(test::Suite& suite);
66void test3(test::Suite& suite);
67
68int 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
79void 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
106void test2(test::Suite& suite)
107{
108  suite.out() << "test2\n";
109  // test that we get same number with same seed
110  RNG::instance()->seed(0);
111  std::vector<int> x(10);
112  Visitor visitor1(x);
113  boost::thread_group threads;
114  threads.create_thread(visitor1);
115  threads.join_all();
116  // copy result from visitor1 to variable y
117  std::vector<int> y(x);
118  suite.out() << "y: ";
119  std::copy(y.begin(), y.end(), std::ostream_iterator<int>(suite.out(), " "));
120  suite.out() << std::endl;
121
122  RNG::instance()->seed(0);
123  threads.create_thread(visitor1);
124  threads.join_all();
125  suite.out() << "x: ";
126  std::copy(x.begin(), x.end(), std::ostream_iterator<int>(suite.out(), " "));
127  suite.out() << std::endl;
128  if (x!=y) {
129    suite.add(false);
130    suite.err() << "x not equal to y\n";
131  }
132  else {
133    suite.out() << "ok\n";
134  }
135}
136
137
138void test3(test::Suite& suite)
139{
140  suite.out() << "test3\n";
141  // test that we get different numbers in thread in which we seed RNG
142  // and subsequent thread
143  RNG::instance()->seed(1);
144  std::vector<int> x(10);
145  Visitor visitor(x);
146  visitor();
147  suite.out() << "x: ";
148  std::copy(x.begin(), x.end(), std::ostream_iterator<int>(suite.out(), " "));
149  suite.out() << std::endl;
150
151  std::vector<int> y(x);
152  Visitor visitor1(y);
153  boost::thread_group threads;
154  threads.create_thread(visitor1);
155  threads.join_all();
156
157  suite.out() << "y: ";
158  std::copy(y.begin(), y.end(), std::ostream_iterator<int>(suite.out(), " "));
159  suite.out() << std::endl;
160
161  if (x==y) {
162    suite.add(false);
163    suite.err() << "x equal to y\n";
164  }
165  else {
166    suite.out() << "ok\n";
167  }
168}
Note: See TracBrowser for help on using the repository browser.