00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "TVldValidate.hxx"
00013
00014 #include "TVldContext.hxx"
00015 #include "TVldRange.hxx"
00016
00017 #include "TFile.h"
00018 #include "TSystem.h"
00019
00020 #include <TSK_DBI_Log.hxx>
00021 #include <MsgFormat.h>
00022 using std::endl;
00023 #include <MsgFormat.h>
00024
00025 #include <iomanip>
00026 #include <map>
00027
00028 ClassImp(TVldValidate)
00029
00030
00031 TVldValidate::TVldValidate()
00032 {
00033
00034
00035
00036
00037 }
00038
00039
00040 TVldValidate::~TVldValidate()
00041 {
00042
00043
00044 }
00045
00046
00047 Bool_t TVldValidate::RunAllTests()
00048 {
00049 Int_t fail = 0;
00050 Int_t tests = 0;
00051
00052 tests++;
00053 if (!TestTimeStamp()) fail++;
00054
00055 tests++;
00056 if (!TestContext()) fail++;
00057
00058 tests++;
00059 if (!TestRange()) fail++;
00060
00061 tests++;
00062 if (!TestFileIO()) fail++;
00063
00064 SK_DBI_Info( "VldValidate::RunAllTests had " << fail
00065 << " failures in " << tests << " tests"
00066 << " " << endl);
00067
00068 return (!fail);
00069
00070 }
00071
00072
00073 Bool_t TVldValidate::TestTimeStamp(void)
00074 {
00075
00076
00077 cout << "Test TVldTimeStamp" << endl << endl;
00078
00079 Int_t fail = 0;
00080 Int_t tests = 0;
00081
00082 cout << "default ctor: " << flush;
00083 TVldTimeStamp defctor;
00084 defctor.Print("");
00085
00086 SK_DBI_Info( " Test output to MSG service: " << defctor << " " << endl);
00087
00088 time_t nowtime;
00089 time(&nowtime);
00090 long int nsec = 12345;
00091
00092 struct timespec nowts;
00093 nowts.tv_sec = nowtime;
00094 nowts.tv_nsec = nsec;
00095
00096 SK_DBI_Info( " original timespec: {" << nowtime << "," << nsec << "} " << " ");
00097
00098 TVldTimeStamp vldnow(nowts);
00099
00100 SK_DBI_Log( " vldnow as timespec: " << vldnow.AsString("2") << " ");
00101 SK_DBI_Info( " vldnow default AsString: " << vldnow.AsString("") << " ");
00102 SK_DBI_Info( " vldnow local AsString: " << vldnow.AsString("l") << " ");
00103
00104 struct timespec thents = vldnow.GetTimeSpec();
00105
00106 SK_DBI_Info( " recovered timespec: {" << thents.tv_sec << "," << thents.tv_nsec << "} " << " ");
00107
00108 tests++;
00109 if (nowts.tv_sec != thents.tv_sec || nowts.tv_nsec != thents.tv_nsec) fail++;
00110
00111
00112
00113
00114 time_t mytime = 978571563;
00115 Long_t arbitrary = 123456;
00116
00117 timespec mytimespec = {mytime,arbitrary};
00118 TVldTimeStamp vtr(mytime,arbitrary);
00119 TVldTimeStamp vtsr(mytimespec);
00120
00121 Int_t year = 2001;
00122 Int_t month = 1;
00123 Int_t day = 4;
00124 Int_t hour = 1;
00125 Int_t min = 26;
00126 Int_t sec = 3;
00127 Int_t secOffset = vtr.GetZoneOffset();
00128
00129 TVldTimeStamp vl1(year,month,day,hour,min,sec,arbitrary);
00130 TVldTimeStamp vl2(year,month,day,hour,min,sec,arbitrary,kFALSE,-secOffset);
00131 TVldTimeStamp vl3(year,month,day,hour-8,min,sec,arbitrary,kTRUE,8*60*60);
00132 TVldTimeStamp vl4(year,month,day-1,hour+16,min,sec,arbitrary,kTRUE,8*60*60);
00133
00134 TVldTimeStamp vlpst(year,month,day,hour-8,min,sec,arbitrary,kFALSE);
00135
00136 TVldTimeStamp vly1(year-1900,month,day,hour,min,sec,arbitrary);
00137 TVldTimeStamp vly2(year-2000,month,day,hour,min,sec,arbitrary);
00138
00139 Int_t date = year*10000 + month*100 + day;
00140 Int_t time = hour*10000 + min*100 + sec;
00141
00142 TVldTimeStamp vs1(date,time,arbitrary);
00143 TVldTimeStamp vs2(date,time,arbitrary,kFALSE,-secOffset);
00144
00145 TVldTimeStamp vs3(date-1,time+160000,arbitrary,kTRUE,8*60*60);
00146 TVldTimeStamp vs4(date,time-80000,arbitrary,kTRUE,8*60*60);
00147
00148 TVldTimeStamp vspst1(date,time-80000,arbitrary,kFALSE);
00149 TVldTimeStamp vspst2(date-1,time+160000,arbitrary,kFALSE);
00150 TVldTimeStamp vsy1(date-19000000,time,arbitrary);
00151 TVldTimeStamp vsy2(date-20000000,time,arbitrary);
00152
00153 SK_DBI_Info( " current TimeOffset is " << vtr.GetZoneOffset() << " ");
00154
00155 SK_DBI_Info( " ");
00156 SK_DBI_Info( " std (vtr) " << vtr.AsString() << " ");
00157
00158 TString byhand = "Thu, 04 Jan 2001 01:26:03 +0000 (GMT) + 123456 nsec";
00159 TString byclass = vtr.AsString();
00160 tests++;
00161 if (byhand != byclass) {
00162 SK_DBI_Info( " strings don't match!" << " ");
00163 fail++;
00164 }
00165 else {
00166 SK_DBI_Info( " strings match as they should " << " ");
00167 }
00168
00169 tests++; if (!CompareTimeStamps(" vtsr ",vtsr,vtr)) fail++;
00170 tests++; if (!CompareTimeStamps(" vl1 ",vl1,vtr)) fail++;
00171 tests++; if (!CompareTimeStamps(" vl2 ",vl2,vtr)) fail++;
00172 tests++; if (!CompareTimeStamps(" vl3 ",vl3,vtr)) fail++;
00173 tests++; if (!CompareTimeStamps(" vl4 ",vl4,vtr)) fail++;
00174 tests++; if (!CompareTimeStamps(" vly1 ",vly1,vtr)) fail++;
00175 tests++; if (!CompareTimeStamps(" vly2 ",vly2,vtr)) fail++;
00176 tests++; if (!CompareTimeStamps(" vs1 ",vs1,vtr)) fail++;
00177 tests++; if (!CompareTimeStamps(" vs2 ",vs2,vtr)) fail++;
00178 tests++; if (!CompareTimeStamps(" vs3 ",vs3,vtr)) fail++;
00179 tests++; if (!CompareTimeStamps(" vs4 ",vs4,vtr)) fail++;
00180 tests++; if (!CompareTimeStamps(" vsy1 ",vsy1,vtr)) fail++;
00181 tests++; if (!CompareTimeStamps(" vsy2 ",vsy2,vtr)) fail++;
00182
00183 if (secOffset == 28800) {
00184 tests++; if (!CompareTimeStamps(" vlpst ",vlpst,vtr)) fail++;
00185 tests++; if (!CompareTimeStamps(" vspst1 ",vspst1,vtr)) fail++;
00186 tests++; if (!CompareTimeStamps(" vspst2 ",vspst2,vtr)) fail++;
00187 }
00188
00189 SK_DBI_Info( " next test expects to be a mismatch" << " ");
00190 CompareTimeStamps(" now is unlikely to match arbitrary time ",vldnow,vtr);
00191
00192 SK_DBI_Info( " Alternative formats" << " ");
00193 SK_DBI_Info( " \"\" " << vtr.AsString("") << " ");
00194 SK_DBI_Info( " \"l\" " << vtr.AsString("l") << " ");
00195 SK_DBI_Info( " \"c\" " << vtr.AsString("c") << " ");
00196 SK_DBI_Info( " \"lc\" " << vtr.AsString("lc") << " ");
00197 SK_DBI_Info( " \"2\" " << vtr.AsString("2") << " ");
00198
00199 tests++; if (vtr.GetDate() != date) fail++;
00200 tests++; if (vtr.GetTime() != time) fail++;
00201
00202 MsgFormat i6("%6d");
00203
00204 SK_DBI_Info( " GMT GetDate: " << vtr.GetDate()
00205 << " GetTime: " << i6(vtr.GetTime()) << " ");
00206 SK_DBI_Info( " local GetDate: " << vtr.GetDate(kFALSE)
00207 << " GetTime: " << i6(vtr.GetTime(kFALSE)) << " ");
00208
00209 SK_DBI_Info( "VldValidate::TestTimeStamp had " << fail
00210 << " failures in " << tests << " tests"
00211 << " " << endl);
00212
00213 return (!fail);
00214
00215 }
00216
00217
00218 Bool_t TVldValidate::CompareTimeStamps(const char *label,
00219 TVldTimeStamp& test, TVldTimeStamp& std)
00220 {
00221 if (test == std) {
00222 SK_DBI_Info( " exact match " << " ");
00223 return kTRUE;
00224 }
00225 else {
00226 SK_DBI_Info( " ** mismatch ** " << " ");
00227 SK_DBI_Info( " " << test.AsString() << " ");
00228 SK_DBI_Info( " " << std.AsString() << " ");
00229 return kFALSE;
00230 }
00231 }
00232
00233
00234 Bool_t TVldValidate::TestContext(void)
00235 {
00236
00237
00238 cout << "Test TVldContext" << endl << endl;
00239
00240 Int_t fail = 0;
00241
00242 map<TVldContext,long> vldc_map;
00243 long int entry = 0;
00244
00245 TVldContext defctor;
00246 SK_DBI_Info( "VldContext default ctor: "
00247 << defctor.AsString("") << " ");
00248 vldc_map[defctor] = entry++;
00249
00250
00251 TVldContext defctor2;
00252 vldc_map[defctor2] = entry++;
00253
00254
00255
00256
00257
00258 SK_DBI_Info( "VldContext test map<TVldContext,long>" << " ");
00259 TVldTimeStamp now;
00260 for (Int_t ibit_det = 0; ibit_det<3; ibit_det++) {
00261 DbiDetector::Detector_t det =
00262 (DbiDetector::Detector_t) (1<<ibit_det);
00263 for (Int_t ibit_sim = 0; ibit_sim<4; ibit_sim++) {
00264 DbiSimFlag::SimFlag_t sim = (DbiSimFlag::SimFlag_t) (1<<ibit_sim);
00265
00266 TVldContext vldc(det,sim,now);
00267
00268
00269
00270
00271 vldc_map[vldc] = entry++;
00272
00273 }
00274 }
00275 typedef map<TVldContext,long>::const_iterator vcl_itr;
00276 int expect = 0;
00277 bool pass = true;
00278 for (vcl_itr p = vldc_map.begin(); p != vldc_map.end(); ++p, ++expect) {
00279 TVldContext ac = p->first;
00280 long al = p->second;
00281 if (al != expect) pass = false;
00282 SK_DBI_Info( " " << setw(2) << expect << " ? " << setw(2) << al << " : "
00283 << ac.AsString()
00284 << " ");
00285 }
00286 if (!pass) fail++;
00287
00288
00289 SK_DBI_Info( " Test output to MSG service: " << defctor << " ");
00290
00291 SK_DBI_Info( "VldValidate::TestContext had " << fail << " failures "
00292 << " " << endl);
00293
00294 return (!fail);
00295 }
00296
00297
00298 Bool_t TVldValidate::TestRange(void)
00299 {
00300
00301
00302 cout << "Test TVldRange" << endl << endl;
00303
00304 Int_t fail = 0;
00305
00306 cout << "default ctor: ";
00307 TVldRange defctor;
00308 defctor.Print("");
00309
00310 SK_DBI_Info( " Test output to MSG service: " << defctor << " ");
00311
00312 SK_DBI_Info( "VldValidate::TestRange had " << fail << " failures "
00313 << " " << endl);
00314
00315 return (!fail);
00316
00317 }
00318
00319
00320 Bool_t TVldValidate::TestFileIO(void)
00321 {
00322
00323
00324 cout << "Test file IO" << endl << endl;
00325
00326
00327
00328
00329
00330 TFile fout("vld_io.root","RECREATE");
00331
00332 TVldTimeStamp out_ts;
00333 TVldContext out_c;
00334 TVldRange out_r;
00335
00336
00337
00338 out_c.Write();
00339 out_r.Write();
00340
00341 fout.ls();
00342 fout.Close();
00343
00344
00345
00346 SK_DBI_Info( " " << endl);
00347
00348 TVldContext pause_c;
00349
00350
00351
00352 TFile fin("vld_io.root","READ");
00353 fin.ls();
00354
00355
00356 TVldContext *in_c = dynamic_cast<TVldContext*>(fin.Get("VldContext"));
00357
00358 fin.Close();
00359
00360
00361
00362 SK_DBI_Info( " " << endl);
00363
00364
00365 TVldContext final_c;
00366
00367
00368
00369
00370 SK_DBI_Info( " wrote TVldContext: " << out_c << " ");
00371 SK_DBI_Info( " interm TVldContext: " << pause_c << " ");
00372 SK_DBI_Info( " read TVldContext: " << (*in_c) << " ");
00373 SK_DBI_Info( " final TVldContext: " << final_c << " ");
00374
00375 Int_t fail = 0;
00376
00377
00378
00379
00380
00381
00382 SK_DBI_Info( "VldValidate::TestRange had " << fail << " failures "
00383 << " " << endl);
00384
00385 return (!fail);
00386
00387 }
00388
00389
00390