//////////////////////////////////////////////////////////////////////////// // $Id: IVldValidate.cxx,v 1.1 2011/01/18 05:49:20 finch Exp $ // // COMET::IVldValidate // // COMET::IVldValidate is a test harness for Validity package // // Author: R. Hatcher 2001.01.03 // //////////////////////////////////////////////////////////////////////////// #include "IVldValidate.hxx" #include "IVldContext.hxx" #include "IVldRange.hxx" #include "TFile.h" #include "TSystem.h" #include #include using std::endl; #include #include #include ClassImp(COMET::IVldValidate) //______________________________________________________________________________ COMET::IVldValidate::IVldValidate() { // Default constructor } //______________________________________________________________________________ COMET::IVldValidate::~IVldValidate() { // delete all the owned sub-objects } //______________________________________________________________________________ Bool_t COMET::IVldValidate::RunAllTests() { Int_t fail = 0; Int_t tests = 0; tests++; if (!TestTimeStamp()) fail++; tests++; if (!TestContext()) fail++; tests++; if (!TestRange()) fail++; tests++; if (!TestFileIO()) fail++; COMETInfo( "VldValidate::RunAllTests had " << fail << " failures in " << tests << " tests" << " " << endl); return (!fail); } //______________________________________________________________________________ Bool_t COMET::IVldValidate::TestTimeStamp(void) { // Test COMET::IVldTimeStamp behaviour cout << "Test COMET::IVldTimeStamp" << endl << endl; Int_t fail = 0; Int_t tests = 0; cout << "default ctor: " << flush; COMET::IVldTimeStamp defctor; defctor.Print(""); COMETInfo( " Test output to MSG service: " << defctor << " " << endl); time_t nowtime; time(&nowtime); long int nsec = 12345; struct timespec nowts; nowts.tv_sec = nowtime; nowts.tv_nsec = nsec; COMETInfo( " original timespec: {" << nowtime << "," << nsec << "} " << " "); COMET::IVldTimeStamp vldnow(nowts); // do these as separate // reuses a common buffer space COMETLog( " vldnow as timespec: " << vldnow.AsString("2") << " "); COMETInfo( " vldnow default AsString: " << vldnow.AsString("") << " "); COMETInfo( " vldnow local AsString: " << vldnow.AsString("l") << " "); struct timespec thents = vldnow.GetTimeSpec(); COMETInfo( " recovered timespec: {" << thents.tv_sec << "," << thents.tv_nsec << "} " << " "); tests++; if (nowts.tv_sec != thents.tv_sec || nowts.tv_nsec != thents.tv_nsec) fail++; // test various ctor's // all should map to Jan 04 2001, 01:26:03 GMT time_t mytime = 978571563; Long_t arbitrary = 123456; // fake nsec part timespec mytimespec = {mytime,arbitrary}; COMET::IVldTimeStamp vtr(mytime,arbitrary); COMET::IVldTimeStamp vtsr(mytimespec); Int_t year = 2001; Int_t month = 1; Int_t day = 4; Int_t hour = 1; Int_t min = 26; Int_t sec = 3; Int_t secOffset = vtr.GetZoneOffset(); COMET::IVldTimeStamp vl1(year,month,day,hour,min,sec,arbitrary); COMET::IVldTimeStamp vl2(year,month,day,hour,min,sec,arbitrary,kFALSE,-secOffset); COMET::IVldTimeStamp vl3(year,month,day,hour-8,min,sec,arbitrary,kTRUE,8*60*60); COMET::IVldTimeStamp vl4(year,month,day-1,hour+16,min,sec,arbitrary,kTRUE,8*60*60); // this will only give correct results in PST zone COMET::IVldTimeStamp vlpst(year,month,day,hour-8,min,sec,arbitrary,kFALSE); COMET::IVldTimeStamp vly1(year-1900,month,day,hour,min,sec,arbitrary); COMET::IVldTimeStamp vly2(year-2000,month,day,hour,min,sec,arbitrary); Int_t date = year*10000 + month*100 + day; Int_t time = hour*10000 + min*100 + sec; COMET::IVldTimeStamp vs1(date,time,arbitrary); COMET::IVldTimeStamp vs2(date,time,arbitrary,kFALSE,-secOffset); // these next two aren't a smart way of dealing with local time COMET::IVldTimeStamp vs3(date-1,time+160000,arbitrary,kTRUE,8*60*60); COMET::IVldTimeStamp vs4(date,time-80000,arbitrary,kTRUE,8*60*60); // the next two will only give correct results in PST zone COMET::IVldTimeStamp vspst1(date,time-80000,arbitrary,kFALSE); COMET::IVldTimeStamp vspst2(date-1,time+160000,arbitrary,kFALSE); COMET::IVldTimeStamp vsy1(date-19000000,time,arbitrary); COMET::IVldTimeStamp vsy2(date-20000000,time,arbitrary); COMETInfo( " current TimeOffset is " << vtr.GetZoneOffset() << " "); COMETInfo( " "); COMETInfo( " std (vtr) " << vtr.AsString() << " "); TString byhand = "Thu, 04 Jan 2001 01:26:03 +0000 (GMT) + 123456 nsec"; TString byclass = vtr.AsString(); tests++; if (byhand != byclass) { COMETInfo( " strings don't match!" << " "); fail++; } else { COMETInfo( " strings match as they should " << " "); } tests++; if (!CompareTimeStamps(" vtsr ",vtsr,vtr)) fail++; tests++; if (!CompareTimeStamps(" vl1 ",vl1,vtr)) fail++; tests++; if (!CompareTimeStamps(" vl2 ",vl2,vtr)) fail++; tests++; if (!CompareTimeStamps(" vl3 ",vl3,vtr)) fail++; tests++; if (!CompareTimeStamps(" vl4 ",vl4,vtr)) fail++; tests++; if (!CompareTimeStamps(" vly1 ",vly1,vtr)) fail++; tests++; if (!CompareTimeStamps(" vly2 ",vly2,vtr)) fail++; tests++; if (!CompareTimeStamps(" vs1 ",vs1,vtr)) fail++; tests++; if (!CompareTimeStamps(" vs2 ",vs2,vtr)) fail++; tests++; if (!CompareTimeStamps(" vs3 ",vs3,vtr)) fail++; tests++; if (!CompareTimeStamps(" vs4 ",vs4,vtr)) fail++; tests++; if (!CompareTimeStamps(" vsy1 ",vsy1,vtr)) fail++; tests++; if (!CompareTimeStamps(" vsy2 ",vsy2,vtr)) fail++; if (secOffset == 28800) { tests++; if (!CompareTimeStamps(" vlpst ",vlpst,vtr)) fail++; tests++; if (!CompareTimeStamps(" vspst1 ",vspst1,vtr)) fail++; tests++; if (!CompareTimeStamps(" vspst2 ",vspst2,vtr)) fail++; } COMETInfo( " next test expects to be a mismatch" << " "); CompareTimeStamps(" now is unlikely to match arbitrary time ",vldnow,vtr); COMETInfo( " Alternative formats" << " "); COMETInfo( " \"\" " << vtr.AsString("") << " "); COMETInfo( " \"l\" " << vtr.AsString("l") << " "); COMETInfo( " \"c\" " << vtr.AsString("c") << " "); COMETInfo( " \"lc\" " << vtr.AsString("lc") << " "); COMETInfo( " \"2\" " << vtr.AsString("2") << " "); tests++; if (vtr.GetDate() != date) fail++; tests++; if (vtr.GetTime() != time) fail++; IMsgFormat i6("%6d"); COMETInfo( " GMT GetDate: " << vtr.GetDate() << " GetTime: " << i6(vtr.GetTime()) << " "); COMETInfo( " local GetDate: " << vtr.GetDate(kFALSE) << " GetTime: " << i6(vtr.GetTime(kFALSE)) << " "); COMETInfo( "VldValidate::TestTimeStamp had " << fail << " failures in " << tests << " tests" << " " << endl); return (!fail); } //______________________________________________________________________________ Bool_t COMET::IVldValidate::CompareTimeStamps(const char *label, COMET::IVldTimeStamp& test, COMET::IVldTimeStamp& std) { if (test == std) { COMETInfo( " exact match " << " "); return kTRUE; } else { COMETInfo( " ** mismatch ** " << " "); COMETInfo( " " << test.AsString() << " "); COMETInfo( " " << std.AsString() << " "); return kFALSE; } } //______________________________________________________________________________ Bool_t COMET::IVldValidate::TestContext(void) { // Test COMET::IVldContext cout << "Test COMET::IVldContext" << endl << endl; Int_t fail = 0; map vldc_map; long int entry = 0; COMET::IVldContext defctor; COMETInfo( "VldContext default ctor: " << defctor.AsString("") << " "); vldc_map[defctor] = entry++; // gSystem->Sleep(2000); // sleep for 2 sec so timestamp advances COMET::IVldContext defctor2; vldc_map[defctor2] = entry++; // gSystem->Sleep(2000); // sleep for 2 sec so timestamp advances // for the same timestamp try different combinations of detector & simflag // in the expected order that it will sort // (detector primary over simflag --> detector in outer loop) COMETInfo( "VldContext test map" << " "); COMET::IVldTimeStamp now; for (Int_t ibit_det = 0; ibit_det<3; ibit_det++) { COMET::DbiDetector::Detector_t det = (COMET::DbiDetector::Detector_t) (1<::const_iterator vcl_itr; int expect = 0; bool pass = true; for (vcl_itr p = vldc_map.begin(); p != vldc_map.end(); ++p, ++expect) { COMET::IVldContext ac = p->first; long al = p->second; if (al != expect) pass = false; COMETInfo( " " << setw(2) << expect << " ? " << setw(2) << al << " : " << ac.AsString() << " "); } if (!pass) fail++; COMETInfo( " Test output to MSG service: " << defctor << " "); COMETInfo( "VldValidate::TestContext had " << fail << " failures " << " " << endl); return (!fail); } //______________________________________________________________________________ Bool_t COMET::IVldValidate::TestRange(void) { // Test COMET::IVldRange cout << "Test COMET::IVldRange" << endl << endl; Int_t fail = 0; cout << "default ctor: "; COMET::IVldRange defctor; defctor.Print(""); COMETInfo( " Test output to MSG service: " << defctor << " "); COMETInfo( "VldValidate::TestRange had " << fail << " failures " << " " << endl); return (!fail); } //______________________________________________________________________________ Bool_t COMET::IVldValidate::TestFileIO(void) { // Test write/read Validity objects to/from a file cout << "Test file IO" << endl << endl; // UInt_t sleep_msec = 2000; // ****************** OUTPUT ******************************* TFile fout("vld_io.root","RECREATE"); COMET::IVldTimeStamp out_ts; COMET::IVldContext out_c; COMET::IVldRange out_r; //VldTimeStamp not derived from TObject // out_ts.Write(); out_c.Write(); out_r.Write(); fout.ls(); fout.Close(); // ****************** Pause ******************************* COMETInfo( " " << endl); // gSystem->Sleep(sleep_msec); COMET::IVldContext pause_c; // ****************** INPUT ******************************* TFile fin("vld_io.root","READ"); fin.ls(); // COMET::IVldTimeStamp *in_ts = dynamic_cast(fin.Get("VldTimeStamp")); COMET::IVldContext *in_c = dynamic_cast(fin.Get("VldContext")); fin.Close(); // ****************** Comparison ******************************* COMETInfo( " " << endl); // gSystem->Sleep(sleep_msec); COMET::IVldContext final_c; // out_ts.Print(); // in_ts->Print(); COMETInfo( " wrote COMET::IVldContext: " << out_c << " "); COMETInfo( " interm COMET::IVldContext: " << pause_c << " "); COMETInfo( " read COMET::IVldContext: " << (*in_c) << " "); COMETInfo( " final COMET::IVldContext: " << final_c << " "); Int_t fail = 0; // cout << "default ctor: "; // COMET::IVldRange defctor; // defctor.Print(""); COMETInfo( "VldValidate::TestRange had " << fail << " failures " << " " << endl); return (!fail); } //______________________________________________________________________________