CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
testEngineCopy.cc
Go to the documentation of this file.
1// -*- C++ -*-
2//
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
29#ifdef __clang__
30 #pragma clang diagnostic push
31 #pragma clang diagnostic ignored "-Wunknown-warning-option"
32 #pragma clang diagnostic ignored "-Wunused-but-set-variable"
33#endif
34
35using namespace CLHEP;
36
37// Absolutely Safe Equals Without Registers Screwing Us Up
38bool equals01(const std::vector<double> &ab) {
39 return ab[1]==ab[0];
40}
41bool equals(double a, double b) {
42 std::vector<double> ab(2);
43 ab[0]=a; ab[1]=b;
44 return (equals01(ab));
45}
46
47std::vector<double> aSequence(int n) {
48 std::vector<double> v;
49 DualRand e(13542);
50 RandFlat f(e);
51 for (int i=0; i<n; i++) {
52 v.push_back(f());
53 }
54 return v;
55}
56
57
58// ----------- Copy of engines -----------
59
60template <class E>
61int vectorTest64(int n) {
62 output << "Copy 64bit test for " << E::engineName() << "\n";
63
64 E e;
65 double x = 0;
66 for (int i=0; i<n; i++) x += e.flat();
67 E f( e );
68 x = e.flat();
69 output << "x = " << x << std::endl;
70
71 double y = f.flat();
72 output << "y = " << y << std::endl;
73 if( x != y ) return n;
74
75 for( int i=0; i<1000; ++i ) {
76 x = e.flat();
77 y = f.flat();
78 if( !equals(x,y) ) {
79 output << "i = " << i << " x, y " << x << " " << y
80 << " vectorTest64 problem: e != f \n";
81 return n+i;
82 }
83 }
84
85 return 0;
86}
87// special case for NonRandomEngine
88template <>
90 output << "Copy 64bit test for " << NonRandomEngine::engineName() << "\n";
91
92 std::vector<double> nonRand = aSequence(500);
94 e.setRandomSequence(&nonRand[0], (int)nonRand.size());
95
96 double x = 0;
97 for (int i=0; i<n; i++) x += e.flat();
98 std::vector<unsigned long> v = e.put();
100 x = e.flat();
101 output << "x = " << x << std::endl;
102
103 double y = f.flat();
104 output << "y = " << y << std::endl;
105 if( x != y ) return n;
106
107 for( int i=0; i<300; ++i ) {
108 if( e.flat() != f.flat() ) {
109 output << "i = " << i << " vectorTest64 for NonRandomEngine problem: e != f \n";
110 return n+i;
111 }
112 }
113
114 return 0;
115}
116
117template <class E>
118E vectorRestore1(int n, std::vector<double> & v) {
119 output << "Copy for " << E::engineName() << "\n";
120 E e(97538466);
121 double r=0;
122 for (int i=0; i<n; i++) r += e.flat();
123 E f(e);
124 for (int j=0; j<25; j++) v.push_back(e.flat());
125#ifdef VERBOSER2
126 output << "First four of v are: "
127 << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
128#endif
129 return f;
130}
131
132template <>
134vectorRestore1<NonRandomEngine> (int n, std::vector<double> & v) {
135#ifdef VERBOSER2
136 output << "Copy for " << NonRandomEngine::engineName() << "\n";
137#endif
138 std::vector<double> nonRand = aSequence(500);
140 e.setRandomSequence(&nonRand[0], (int)nonRand.size());
141 double r=0;
142 for (int i=0; i<n; i++) r += e.flat();
144 for (int j=0; j<25; j++) v.push_back(e.flat());
145#ifdef VERBOSER2
146 output << "First four of v are: "
147 << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
148#endif
149 return f;
150}
151
152template <class E>
153int vectorRestore2(E & f, const std::vector<double> & v) {
154 int stat = 0;
155 std::vector<double> k;
156 for (int j=0; j<25; j++) k.push_back(f.flat());
157#ifdef VERBOSER2
158 output << "First four of k are: "
159 << k[0] << ", " << k[1] << ", " << k[2] << ", " << k[3] << "\n";
160#endif
161 for (int m1=0; m1<25; m1++) {
162 if ( v[m1] != k[m1] ) {
163 std::cout << "???? Incorrect copy restored value for engine: "
164 << E::engineName() << "\n";
165 #ifdef CLEAN_OUTPUT
166 output << "???? Incorrect copy restored value for engine: "
167 << E::engineName() << "\n";
168 #endif
169 stat |= 1048576;
170 return stat;
171 }
172 }
173 return stat;
174}
175
176
177template <class E>
178int vectorRestore(int n) {
179 std::vector<double> v;
180 int status1 = vectorTest64<E>(n);
181 E f = vectorRestore1<E>(n,v);
182 int status2 = vectorRestore2<E>(f, v);
183 return (status1 | status2);
184}
185
186
187
188// ---------------------------------------------
189// ---------------------------------------------
190// ---------------------------------------------
191
192
193int main() {
194 int stat = 0;
195
196#ifdef TEST_ENGINE_COPY
197 output << "\n=================================\n";
198 output << " Part IX \n";
199 output << " Copy test of engines\n";
200 output << "=================================\n\n";
201
202 stat |= vectorRestore<DualRand>(113);
203 // copies of DRand48Engine are not allowed
204 //stat |= vectorRestore<DRand48Engine>(114);
205 stat |= vectorRestore<Hurd160Engine>(115);
206 stat |= vectorRestore<Hurd288Engine>(116);
207 stat |= vectorRestore<HepJamesRandom>(117);
208 stat |= vectorRestore<MixMaxRng>(149);
209 stat |= vectorRestore<MTwistEngine>(118);
210 stat |= vectorRestore<RanecuEngine>(139);
211 stat |= vectorRestore<Ranlux64Engine>(119);
212 stat |= vectorRestore<RanluxEngine>(120);
213 stat |= vectorRestore<RanluxppEngine>(119);
214 stat |= vectorRestore<RanshiEngine>(121);
215 stat |= vectorRestore<TripleRand>(122);
216 stat |= vectorRestore<NonRandomEngine>(123);
217 // anonymous engines are not copyable
218 //stat |= vectorRestore<RandEngine>(129);
219#endif
220
221 output << "\n=============================================\n\n";
222
223 if (stat != 0) {
224 std::cout << "One or more problems detected: stat = " << stat << "\n";
225 output << "One or more problems detected: stat = " << stat << "\n";
226 } else {
227 output << "ranRestoreTest passed with no problems detected.\n";
228 }
229
230 if (stat == 0) return 0;
231 if (stat > 0) return -(stat|1);
232 return stat|1;
233}
234
235#ifdef __clang__
236 #pragma clang diagnostic pop
237#endif
238
virtual std::ostream & put(std::ostream &os) const
void setRandomSequence(double *s, int n)
static std::string engineName()
void f(void g())
Definition: excDblThrow.cc:38
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()