EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ValValidate.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ValValidate.cxx
1 #include "ValValidate.h"
2 
3 #include "ValContext.h"
4 #include "ValRange.h"
5 
6 #include "TFile.h"
7 #include "TSystem.h"
8 
9 #include <iomanip>
10 #include <map>
11 
13 
14 //______________________________________________________________________________
16 {
17 
18 }
19 
20 //______________________________________________________________________________
22 {
23  // delete all the owned sub-objects
24 
25 }
26 
27 //______________________________________________________________________________
29 {
30  Int_t fail = 0;
31  Int_t tests = 0;
32 
33  tests++;
34  if (!TestTimeStamp()) { fail++; }
35 
36  tests++;
37  if (!TestContext()) { fail++; }
38 
39  tests++;
40  if (!TestRange()) { fail++; }
41 
42  tests++;
43  if (!TestFileIO()) { fail++; }
44 
45 
46  cout << "-I- ValValidate::RunAllTests had " << fail
47  << " failures in " << tests << " tests"
48  << endl << endl;
49 
50  return (!fail);
51 
52 }
53 
54 //______________________________________________________________________________
56 {
57  // Test ValTimeStamp behaviour
58 
59  cout << "-I- Test ValTimeStamp" << endl << endl;
60 
61  Int_t fail = 0;
62  Int_t tests = 0;
63 
64  ValTimeStamp defctor;
65  defctor.Print("");
66 
67 
68  time_t nowtime;
69  time(&nowtime);
70  long int nsec = 12345;
71 
72  struct timespec nowts;
73  nowts.tv_sec = nowtime;
74  nowts.tv_nsec = nsec;
75 
76 
77  cout << "-I- original timespec: {" << nowtime << "," << nsec << "} " << endl;
78 
79  ValTimeStamp vldnow(nowts);
80  // reuses a common buffer space
81 
82  cout << " -I- vldnow as timespec: " << vldnow.AsString("2") << endl;
83  cout << " vldnow default AsString: " << vldnow.AsString("") << endl;
84 
85  cout << " vldnow local AsString: " << vldnow.AsString("l") << endl;
86 
87  struct timespec thents = vldnow.GetTimeSpec();
88 
89  cout << " recovered timespec: {" << thents.tv_sec << "," << thents.tv_nsec << "} " << endl;
90 
91  tests++;
92  if (nowts.tv_sec != thents.tv_sec || nowts.tv_nsec != thents.tv_nsec) { fail++; }
93 
94 
95  // test various ctor's
96  // all should map to Jan 04 2001, 01:26:03 GMT
97  time_t mytime = 978571563;
98  Long_t arbitrary = 123456; // fake nsec part
99 
100  timespec mytimespec = {mytime,arbitrary};
101  ValTimeStamp vtr(mytime,arbitrary);
102  ValTimeStamp vtsr(mytimespec);
103 
104  Int_t year = 2001;
105  Int_t month = 1;
106  Int_t day = 4;
107  Int_t hour = 1;
108  Int_t min = 26;
109  Int_t sec = 3;
110  Int_t secOffset = vtr.GetZoneOffset();
111 
112  ValTimeStamp vl1(year,month,day,hour,min,sec,arbitrary);
113  ValTimeStamp vl2(year,month,day,hour,min,sec,arbitrary,kFALSE,-secOffset);
114  ValTimeStamp vl3(year,month,day,hour-8,min,sec,arbitrary,kTRUE,8*60*60);
115  ValTimeStamp vl4(year,month,day-1,hour+16,min,sec,arbitrary,kTRUE,8*60*60);
116 // this will only give correct results in PST zone
117  ValTimeStamp vlpst(year,month,day,hour-8,min,sec,arbitrary,kFALSE);
118 
119  ValTimeStamp vly1(year-1900,month,day,hour,min,sec,arbitrary);
120  ValTimeStamp vly2(year-2000,month,day,hour,min,sec,arbitrary);
121 
122  Int_t date = year*10000 + month*100 + day;
123  Int_t time = hour*10000 + min*100 + sec;
124 
125  ValTimeStamp vs1(date,time,arbitrary);
126  ValTimeStamp vs2(date,time,arbitrary,kFALSE,-secOffset);
127 // these next two aren't a smart way of dealing with local time
128  ValTimeStamp vs3(date-1,time+160000,arbitrary,kTRUE,8*60*60);
129  ValTimeStamp vs4(date,time-80000,arbitrary,kTRUE,8*60*60);
130 // the next two will only give correct results in PST zone
131  ValTimeStamp vspst1(date,time-80000,arbitrary,kFALSE);
132  ValTimeStamp vspst2(date-1,time+160000,arbitrary,kFALSE);
133  ValTimeStamp vsy1(date-19000000,time,arbitrary);
134  ValTimeStamp vsy2(date-20000000,time,arbitrary);
135 
136  cout << " current TimeOffset is " << vtr.GetZoneOffset() << endl;
137 
138  cout << " std (vtr) " << vtr.AsString() << endl;
139 
140  TString byhand = "Thu, 04 Jan 2001 01:26:03 +0000 (GMT) + 123456 nsec";
141  TString byclass = vtr.AsString();
142  tests++;
143  if (byhand != byclass) {
144  cout << " strings don't match!" << endl;
145  fail++;
146  } else {
147  cout << " strings match as they should " << endl;
148  }
149 
150  tests++;
151  if (!CompareTimeStamps(" vtsr ",vtsr,vtr)) { fail++; }
152  tests++;
153  if (!CompareTimeStamps(" vl1 ",vl1,vtr)) { fail++; }
154  tests++;
155  if (!CompareTimeStamps(" vl2 ",vl2,vtr)) { fail++; }
156  tests++;
157  if (!CompareTimeStamps(" vl3 ",vl3,vtr)) { fail++; }
158  tests++;
159  if (!CompareTimeStamps(" vl4 ",vl4,vtr)) { fail++; }
160  tests++;
161  if (!CompareTimeStamps(" vly1 ",vly1,vtr)) { fail++; }
162  tests++;
163  if (!CompareTimeStamps(" vly2 ",vly2,vtr)) { fail++; }
164  tests++;
165  if (!CompareTimeStamps(" vs1 ",vs1,vtr)) { fail++; }
166  tests++;
167  if (!CompareTimeStamps(" vs2 ",vs2,vtr)) { fail++; }
168  tests++;
169  if (!CompareTimeStamps(" vs3 ",vs3,vtr)) { fail++; }
170  tests++;
171  if (!CompareTimeStamps(" vs4 ",vs4,vtr)) { fail++; }
172  tests++;
173  if (!CompareTimeStamps(" vsy1 ",vsy1,vtr)) { fail++; }
174  tests++;
175  if (!CompareTimeStamps(" vsy2 ",vsy2,vtr)) { fail++; }
176 
177  if (secOffset == 28800) {
178  tests++;
179  if (!CompareTimeStamps(" vlpst ",vlpst,vtr)) { fail++; }
180  tests++;
181  if (!CompareTimeStamps(" vspst1 ",vspst1,vtr)) { fail++; }
182  tests++;
183  if (!CompareTimeStamps(" vspst2 ",vspst2,vtr)) { fail++; }
184  }
185 
186  cout << " next test expects to be a mismatch" << endl;
187  CompareTimeStamps(" now is unlikely to match arbitrary time ",vldnow,vtr);
188 
189  cout << " Alternative formats" << endl;
190  cout << " \"\" " << vtr.AsString("") << endl;
191  cout << " \"l\" " << vtr.AsString("l") << endl;
192  cout<< " \"c\" " << vtr.AsString("c") << endl;
193  cout << " \"lc\" " << vtr.AsString("lc") << endl;
194  cout << " \"2\" " << vtr.AsString("2") << endl;
195 
196  tests++;
197  if (vtr.GetDate() != date) { fail++; }
198  tests++;
199  if (vtr.GetTime() != time) { fail++; }
200 
201 
202  cout << " GMT GetDate: " << vtr.GetDate()
203  << " GetTime: " << vtr.GetTime() << endl;
204 
205  cout << " local GetDate: " << vtr.GetDate(kFALSE)
206  << " GetTime: " << vtr.GetTime(kFALSE) << endl;
207 
208  cout << "ValValidate::TestTimeStamp had " << fail
209  << " failures in " << tests << " tests"
210  << endl << endl;
211 
212  return (!fail);
213 
214 }
215 
216 //______________________________________________________________________________
217 Bool_t ValValidate::CompareTimeStamps(const char* label,
218  ValTimeStamp& test, ValTimeStamp& std)
219 {
220  if (test == std) {
221  cout << label << " exact match " << endl;
222  return kTRUE;
223  } else {
224  cout << label << " ** mismatch ** " << endl;
225  cout << " " << test.AsString() << endl;
226  cout<< " " << std.AsString() << endl;
227  return kFALSE;
228  }
229 }
230 
231 //______________________________________________________________________________
233 {
234  // Test ValContext
235 
236  cout << "Test ValContext" << endl << endl;
237 
238  Int_t fail = 0;
239 
240  map<ValContext,long> vldc_map;
241  long int entry = 0;
242 
243  ValContext defctor;
244  cout<< "ValContext default ctor: "
245  << defctor.AsString("") << endl;
246  vldc_map[defctor] = entry++;
247 // gSystem->Sleep(2000); // sleep for 2 sec so timestamp advances
248 
249  ValContext defctor2;
250  vldc_map[defctor2] = entry++;
251 // gSystem->Sleep(2000); // sleep for 2 sec so timestamp advances
252 
253  // for the same timestamp try different combinations of detector & simflag
254  // in the expected order that it will sort
255  // (detector primary over simflag --> detector in outer loop)
256  cout << endl << "ValContext test map<ValContext,long>" << endl;
257  ValTimeStamp now;
258  for (Int_t ibit_det = 0; ibit_det<3; ibit_det++) {
260  (Detector::Detector_t) (1<<ibit_det);
261  for (Int_t ibit_sim = 0; ibit_sim<4; ibit_sim++) {
262  SimFlag::SimFlag_t sim = (SimFlag::SimFlag_t) (1<<ibit_sim);
263 
264  ValContext vldc(det,sim,now);
265  vldc_map[vldc] = entry++;
266 
267  }
268  }
269  typedef map<ValContext,long>::const_iterator vcl_itr;
270  int expect = 0;
271  bool pass = true;
272  for (vcl_itr p = vldc_map.begin(); p != vldc_map.end(); ++p, ++expect) {
273  ValContext ac = p->first;
274  long al = p->second;
275  if (al != expect) { pass = false; }
276 
277  cout << " " << setw(2) << expect << " ? " << setw(2) << al << " : "
278  << ac.AsString()
279  << endl;
280  }
281  if (!pass) { fail++; }
282 
283 
284 
285  cout << "ValValidate::TestContext had " << fail << " failures "
286  << endl << endl;
287 
288  return (!fail);
289 }
290 
291 //______________________________________________________________________________
293 {
294  // Test ValRange
295 
296  cout << "Test ValRange" << endl << endl;
297 
298  Int_t fail = 0;
299 
300  ValRange defctor;
301  defctor.Print("");
302 
303 
304  cout << "ValValidate::TestRange had " << fail << " failures "
305  << endl << endl;
306 
307  return (!fail);
308 
309 }
310 
311 //______________________________________________________________________________
313 {
314  // Test write/read Validity objects to/from a file
315 
316  cout << "Test file IO" << endl << endl;
317 
318 // UInt_t sleep_msec = 2000;
319 
320  // ****************** OUTPUT *******************************
321 
322  TFile fout("vld_io.root","RECREATE");
323 
324  ValTimeStamp out_ts;
325  ValContext out_c;
326  ValRange out_r;
327 
328 //ValTimeStamp not derived from TObject
329 // out_ts.Write();
330  out_c.Write();
331  out_r.Write();
332 
333  fout.ls();
334  fout.Close();
335 
336  // ****************** Pause *******************************
337 
338 // gSystem->Sleep(sleep_msec);
339  ValContext pause_c;
340 
341  // ****************** INPUT *******************************
342 
343  TFile fin("vld_io.root","READ");
344  fin.ls();
345 
346 // ValTimeStamp *in_ts = dynamic_cast<ValTimeStamp*>(fin.Get("ValTimeStamp"));
347  ValContext* in_c = dynamic_cast<ValContext*>(fin.Get("ValContext"));
348 
349  fin.Close();
350 
351  // ****************** Comparison *******************************
352 
353 
354 // gSystem->Sleep(sleep_msec);
355  ValContext final_c;
356 
357  cout << " wrote ValContext: " << out_c << endl;
358  cout << " interm ValContext: " << pause_c << endl;
359  cout << " read ValContext: " << (*in_c) << endl;
360  cout << " final ValContext: " << final_c << endl;
361 
362  Int_t fail = 0;
363 
364  cout << "ValValidate::TestRange had " << fail << " failures "
365  << endl << endl;
366 
367  return (!fail);
368 
369 }
370 
371 //______________________________________________________________________________