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

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

Define PP variables in config.h rather than in CPPFLAGS. Include
config.h into all source files. Only ammend CXXFLAGS with '-Wall
-pedantic' when --enable-debug. In default mode we respect CXXFLAGS
value set by user, or set to default value '-O3'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1// $Id: rng-mt.cc 2881 2012-11-18 01:28:05Z 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 <iostream>
32#include <iterator>
33#include <vector>
34
35using namespace theplu::yat;
36using namespace theplu::yat::random;
37
38class Visitor
39{
40public:
41  Visitor(std::vector<int>& x)
42    : begin_(x.begin()), end_(x.end()) {}
43
44  void operator()(void)
45  {
46    for (std::vector<int>::iterator i=begin_; i!=end_; ++i)
47      *i = rnd_(100);
48  }
49
50private:
51  DiscreteUniform rnd_;
52  std::vector<int>::iterator begin_;
53  std::vector<int>::iterator end_;
54};
55
56void test1(test::Suite& suite);
57void test2(test::Suite& suite);
58void test3(test::Suite& suite);
59
60int main(int argc, char* argv[])
61{
62  theplu::yat::test::Suite suite(argc, argv);
63  test1(suite);
64  test2(suite);
65  test3(suite);
66
67  return suite.return_value();
68}
69
70
71void test1(test::Suite& suite)
72{
73  suite.out() << "test1\n";
74  boost::thread_group threads;
75
76  std::vector<int> x(10);
77  std::vector<int> y(x);
78
79  Visitor visitor1(x);
80  Visitor visitor2(y);
81  threads.create_thread(visitor1);
82  threads.create_thread(visitor2);
83  threads.join_all();
84
85  suite.out() << "x: ";
86  std::copy(x.begin(), x.end(), std::ostream_iterator<int>(suite.out(), " "));
87  suite.out() << "\ny: ";
88  std::copy(y.begin(), y.end(), std::ostream_iterator<int>(suite.out(), " "));
89  suite.out() << "\n";
90
91  if (x==y) {
92    suite.add(false);
93    suite.err() << "x and y are equal\n";
94  }
95
96}
97
98
99void test2(test::Suite& suite)
100{
101  suite.out() << "test2\n";
102  // test that we get same number with same seed
103  RNG::instance()->seed(0);
104  std::vector<int> x(10);
105  Visitor visitor1(x);
106  boost::thread_group threads;
107  threads.create_thread(visitor1);
108  threads.join_all();
109  std::vector<int> y(x);
110  RNG::instance()->seed(0);
111  threads.create_thread(visitor1);
112  threads.join_all();
113  suite.out() << "x: ";
114  std::copy(x.begin(), x.end(), std::ostream_iterator<int>(suite.out(), " "));
115  suite.out() << "\ny: ";
116  std::copy(y.begin(), y.end(), std::ostream_iterator<int>(suite.out(), " "));
117  suite.out() << "\n";
118  if (x!=y) {
119    suite.add(false);
120    suite.err() << "x not equal to y\n";
121  }
122  else {
123    suite.out() << "ok\n";
124  }
125}
126
127
128void test3(test::Suite& suite)
129{
130  suite.out() << "test3\n";
131  // test that we get different numbers in thread in which we seed RNG
132  // and subsequent thread
133  RNG::instance()->seed(1);
134  std::vector<int> x(10);
135  Visitor visitor(x);
136  visitor();
137
138  std::vector<int> y(x);
139  Visitor visitor1(y);
140  boost::thread_group threads;
141  threads.create_thread(visitor1);
142  threads.join_all();
143
144  suite.out() << "x: ";
145  std::copy(x.begin(), x.end(), std::ostream_iterator<int>(suite.out(), " "));
146  suite.out() << "\ny: ";
147  std::copy(y.begin(), y.end(), std::ostream_iterator<int>(suite.out(), " "));
148  suite.out() << "\n";
149
150  if (x==y) {
151    suite.add(false);
152    suite.err() << "x equal to y\n";
153  }
154  else {
155    suite.out() << "ok\n";
156  }
157}
Note: See TracBrowser for help on using the repository browser.