source: trunk/test/rng-mt.cc @ 2943

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

merge release 0.10 into trunk

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1// $Id: rng-mt.cc 2943 2013-01-04 06:38:38Z peter $
2
3/*
4  Copyright (C) 2012, 2013 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
106std::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
117void 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
142void 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}
Note: See TracBrowser for help on using the repository browser.