CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

testEngineCopy.cc
Go to the documentation of this file.
1// -*- C++ -*-
2// $Id: testEngineCopy.cc,v 1.2 2010/06/16 17:24:53 garren Exp $
3// ----------------------------------------------------------------------
4#include "CLHEP/Units/GlobalPhysicalConstants.h" // used to provoke shadowing warnings
5#include "CLHEP/Random/Randomize.h"
6#include "CLHEP/Random/NonRandomEngine.h"
7#include "CLHEP/Random/defs.h"
8#include <iostream>
9#include <iomanip>
10#include <vector>
11
12#define CLEAN_OUTPUT
13#ifdef CLEAN_OUTPUT
14 std::ofstream output("testEngineCopy.cout");
15#else
16 std::ostream & output = std::cout;
17#endif
18
19// Normally on for routine validation:
20
21#ifdef TURNOFF
22#endif
23
24#define TEST_ENGINE_COPY
25
26#define VERBOSER
27#define VERBOSER2
28
29using namespace CLHEP;
30
31// Absolutely Safe Equals Without Registers Screwing Us Up
32bool equals01(const std::vector<double> &ab) {
33 return ab[1]==ab[0];
34}
35bool equals(double a, double b) {
36 std::vector<double> ab(2);
37 ab[0]=a; ab[1]=b;
38 return (equals01(ab));
39}
40
41std::vector<double> aSequence(int n) {
42 std::vector<double> v;
43 DualRand e(13542);
44 RandFlat f(e);
45 for (int i=0; i<n; i++) {
46 v.push_back(f());
47 }
48 return v;
49}
50
51
52// ----------- Copy of engines -----------
53
54template <class E>
55int vectorTest64(int n) {
56 output << "Copy 64bit test for " << E::engineName() << "\n";
57
58 E e;
59 double x = 0;
60 for (int i=0; i<n; i++) x += e.flat();
61 E f( e );
62 x = e.flat();
63 output << "x = " << x << std::endl;
64
65 double y = f.flat();
66 output << "y = " << y << std::endl;
67 if( x != y ) return n;
68
69 for( int i=0; i<1000; ++i ) {
70 x = e.flat();
71 y = f.flat();
72 if( !equals(x,y) ) {
73 output << "i = " << i << " x, y " << x << " " << y
74 << " vectorTest64 problem: e != f \n";
75 return n+i;
76 }
77 }
78
79 return 0;
80}
81// special case for NonRandomEngine
82template <>
84 output << "Copy 64bit test for " << NonRandomEngine::engineName() << "\n";
85
86 std::vector<double> nonRand = aSequence(500);
88 e.setRandomSequence(&nonRand[0], nonRand.size());
89
90 double x = 0;
91 for (int i=0; i<n; i++) x += e.flat();
92 std::vector<unsigned long> v = e.put();
94 x = e.flat();
95 output << "x = " << x << std::endl;
96
97 double y = f.flat();
98 output << "y = " << y << std::endl;
99 if( x != y ) return n;
100
101 for( int i=0; i<300; ++i ) {
102 if( e.flat() != f.flat() ) {
103 output << "i = " << i << " vectorTest64 for NonRandomEngine problem: e != f \n";
104 return n+i;
105 }
106 }
107
108 return 0;
109}
110
111template <class E>
112E vectorRestore1(int n, std::vector<double> & v) {
113 output << "Copy for " << E::engineName() << "\n";
114 E e(97538466);
115 double r=0;
116 for (int i=0; i<n; i++) r += e.flat();
117 E f(e);
118 for (int j=0; j<25; j++) v.push_back(e.flat());
119#ifdef VERBOSER2
120 output << "First four of v are: "
121 << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
122#endif
123 return f;
124}
125
126template <>
128vectorRestore1<NonRandomEngine> (int n, std::vector<double> & v) {
129#ifdef VERBOSER2
130 output << "Copy for " << NonRandomEngine::engineName() << "\n";
131#endif
132 std::vector<double> nonRand = aSequence(500);
134 e.setRandomSequence(&nonRand[0], nonRand.size());
135 double r=0;
136 for (int i=0; i<n; i++) r += e.flat();
138 for (int j=0; j<25; j++) v.push_back(e.flat());
139#ifdef VERBOSER2
140 output << "First four of v are: "
141 << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
142#endif
143 return f;
144}
145
146template <class E>
147int vectorRestore2(E & f, const std::vector<double> & v) {
148 int stat = 0;
149 std::vector<double> k;
150 for (int j=0; j<25; j++) k.push_back(f.flat());
151#ifdef VERBOSER2
152 output << "First four of k are: "
153 << k[0] << ", " << k[1] << ", " << k[2] << ", " << k[3] << "\n";
154#endif
155 for (int m1=0; m1<25; m1++) {
156 if ( v[m1] != k[m1] ) {
157 std::cout << "???? Incorrect copy restored value for engine: "
158 << E::engineName() << "\n";
159 #ifdef CLEAN_OUTPUT
160 output << "???? Incorrect copy restored value for engine: "
161 << E::engineName() << "\n";
162 #endif
163 stat |= 1048576;
164 return stat;
165 }
166 }
167 return stat;
168}
169
170
171template <class E>
172int vectorRestore(int n) {
173 std::vector<double> v;
174 int status1 = vectorTest64<E>(n);
175 E f = vectorRestore1<E>(n,v);
176 int status2 = vectorRestore2<E>(f, v);
177 return (status1 | status2);
178}
179
180
181
182// ---------------------------------------------
183// ---------------------------------------------
184// ---------------------------------------------
185
186
187int main() {
188 int stat = 0;
189
190#ifdef TEST_ENGINE_COPY
191 output << "\n=================================\n";
192 output << " Part IX \n";
193 output << " Copy test of engines\n";
194 output << "=================================\n\n";
195
196 stat |= vectorRestore<DualRand>(113);
197 // copies of DRand48Engine are not allowed
198 //stat |= vectorRestore<DRand48Engine>(114);
199 stat |= vectorRestore<Hurd160Engine>(115);
200 stat |= vectorRestore<Hurd288Engine>(116);
201 stat |= vectorRestore<HepJamesRandom>(117);
202 stat |= vectorRestore<MTwistEngine>(118);
203 stat |= vectorRestore<RanecuEngine>(139);
204 stat |= vectorRestore<Ranlux64Engine>(119);
205 stat |= vectorRestore<RanluxEngine>(120);
206 stat |= vectorRestore<RanshiEngine>(121);
207 stat |= vectorRestore<TripleRand>(122);
208 stat |= vectorRestore<NonRandomEngine>(123);
209 // anonymous engines are not copyable
210 //stat |= vectorRestore<RandEngine>(129);
211#endif
212
213 output << "\n=============================================\n\n";
214
215 if (stat != 0) {
216 std::cout << "One or more problems detected: stat = " << stat << "\n";
217 output << "One or more problems detected: stat = " << stat << "\n";
218 } else {
219 output << "ranRestoreTest passed with no problems detected.\n";
220 }
221
222 if (stat == 0) return 0;
223 if (stat > 0) return -(stat|1);
224 return stat|1;
225}
226
virtual std::ostream & put(std::ostream &os) const
void setRandomSequence(double *s, int n)
void f(void g())
E vectorRestore1(int n, std::vector< double > &v)
int vectorRestore2(E &f, const std::vector< double > &v)
int vectorTest64< NonRandomEngine >(int n)
int vectorTest64(int n)
std::vector< double > aSequence(int n)
int vectorRestore(int n)
std::ofstream output("testEngineCopy.cout")
bool equals(double a, double b)
bool equals01(const std::vector< double > &ab)
NonRandomEngine vectorRestore1< NonRandomEngine >(int n, std::vector< double > &v)
int main()