RawDAQDataUnitTest.cpp
Go to the documentation of this file.
1 /*
2  * File: RawDAQDataUnitTest.cpp
3  * Author: denis
4  *
5  * Created on September 20, 2012, 4:43 PM
6  */
7 
8 //#include "DAQDataFormats/../unittest/RawDAQDataUnitTest.h"
9 //#include "DAQDataFormats/../unittest/RawDAQDataConstructor.h"
10 #include "RawDAQDataUnitTest.h"
11 #include "RawDAQDataConstructor.h"
12 
13 #include <string>
14 
15 #include <boost/algorithm/string.hpp>
16 
17 namespace daqdataformats {
18 
19 //------------------------------------------------------------------------------
21  _dataFormatConstructor(df)
22 {
23 }
24 
25 //------------------------------------------------------------------------------
28 }
29 
30 //------------------------------------------------------------------------------
31 // Check readData for all versions of the dataformat
34  LOG_DEBUG("UNITTEST")<<"checkReadDataIntoSameVersion for version "<<iver<<std::endl;
35  if(!checkReadDataIntoSameVersion(iver)) return false;
36  )
37  return true;
38 }
39 //------------------------------------------------------------------------------
40 // Check readData for all versions of the dataformat
42 
44  LOG_DEBUG("UNITTEST")<<"checkReadDataIntoUnknownVersion for version "<<iver<<std::endl;
46  return false;
47  )
48 
49  return true;
50 }
51 
52 //------------------------------------------------------------------------------
53 // Check readData of a given version
55  return checkReadData(ver, ver);
56 }
57 
58 //------------------------------------------------------------------------------
59 // Check readData of a given version
62 }
63 
64 //------------------------------------------------------------------------------
65 bool RawDAQDataUnitTest::checkReadData(const version_t initialformat_version,
66  const version_t readingformat_version) const {
67 
68  LOG_DEBUG("UNITTEST")<<__PRETTY_FUNCTION__<<"\n"
69  <<"Versions :"
70  <<" initialformat_version = "<<initialformat_version
71  <<". readingformat_version = "<<readingformat_version<<"\n";
72 
73  /// Check Versions. they have to be valid:
74  /// 1. (initialformat_version == readingformat_version) AND (initialformat_version != UnknownVersion)
75  /// 2. (initialformat_version != UnknownVersion) AND (readingformat_version == UnknownVersion)
76  {
77  bool are_versions_valid = false;
78  if( (initialformat_version == readingformat_version ) && (initialformat_version != rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION)) are_versions_valid = true;
79  if( (initialformat_version != rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION) && (readingformat_version == rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION)) are_versions_valid = true;
80 
81  /// If versions are not valid, exit now
82  if(! are_versions_valid){
83  LOG_ERROR("UNITTEST")<<"Versions are not valid:"
84  <<" initialformat_version = "<<initialformat_version
85  <<". readingformat_version = "<<readingformat_version;
86  return false;
87  }
88  }// end of checking versions
89 
90  /// Construct DataFormats
91  RawDAQData* format_nonzero_raw = NULL;
92  try {
93  format_nonzero_raw = _dataFormatConstructor->getFilledDataFormat(initialformat_version);
94  }
95  catch(...){
96  LOG_ERROR("UNITTEST")<<"Could not get a FILLED non-zero Data format of version "<<initialformat_version<<"\n";
97  throw;
98  return false;
99  }
100  boost::shared_ptr<RawDAQData> format_nonzero(format_nonzero_raw);
101 
102 
103  RawDAQData* format_zero_raw = NULL;
104  try{
105  format_zero_raw = _dataFormatConstructor->getEmptyDataFormat(readingformat_version);
106  }
107  catch(...){
108  LOG_ERROR("UNITTEST")<<"Could not get an EMPTY zero Data format of version "<<readingformat_version<<"\n";
109  throw;
110  return false;
111  }
112  boost::shared_ptr<RawDAQData> format_zero(format_zero_raw);
113 
114  //LOG_DEBUG("UNITTEST")<<"Version"<<initialformat_version<<"\n";
115  //LOG_DEBUG("UNITTEST")<<*(format_nonzero.get());
116 
117  {
118  /// Check Printout version for format_nonzero
119  if( initialformat_version != rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION
120  && isPrintDataFormatUnknownVersion(format_nonzero.get())){
121  LOG_ERROR("UNITTEST")<<"Failure in "<<__PRETTY_FUNCTION__<<" at "<<__LINE__<<"\n"
122  <<"Nonzero format should have print with defined version, but it returns:\n"
123  <<*(format_nonzero.get());
124  return false;
125  }
126 
127  /// Check Printout version for format_zero
128  if( readingformat_version != rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION
129  && isPrintDataFormatUnknownVersion(format_zero.get())){
130  LOG_ERROR("UNITTEST")<<"Failure in "<<__PRETTY_FUNCTION__<<" at "<<__LINE__<<"\n"
131  <<"Zero format should have print with defined version, but it returns:\n"
132  <<*(format_zero.get());
133  return false;
134  }
135 
136  /// Check Printout version for format_nonzero
137  if( initialformat_version == rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION
138  && !isPrintDataFormatUnknownVersion(format_nonzero.get())){
139  LOG_ERROR("UNITTEST")<<"Failure in "<<__PRETTY_FUNCTION__<<" at "<<__LINE__<<"\n"
140  <<"Nonzero format should have print UNKNOWN format, but it returns:\n"
141  <<*(format_nonzero.get());
142  return false;
143  }
144 
145  /// Check Printout version for format_zero
146  if( initialformat_version == rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION
147  && !isPrintDataFormatUnknownVersion(format_zero.get())){
148  LOG_ERROR("UNITTEST")<<"Failure in "<<__PRETTY_FUNCTION__<<" at "<<__LINE__<<"\n"
149  <<"Zero format should have print UNKNOWN format, but it returns:\n"
150  <<*(format_zero.get());
151  return false;
152  }
153  }// end of checking
154  /// Check if DataFormats are the same
155  /// At this point they should be different
156  try{
157  if(compareDataFormats(format_nonzero.get(), format_zero.get()) != DATAFORMATS_ARE_DIFFERENT){
158  LOG_ERROR("UNITTEST")<<"Failure in "<<__PRETTY_FUNCTION__<<" at "<<__LINE__<<". "
159  <<"Formats should be Different.\n"
160  <<"Nonzero format:\n"
161  <<*(format_nonzero.get())
162  <<"Copied format:\n"
163  <<*(format_zero.get());
164  return false;
165  }// end of checking if dataformats are different
166  }
167  catch(...){
168  LOG_ERROR("UNITTEST")<<"From "<<__PRETTY_FUNCTION__<<std::endl
169  <<"Could not compare versions "
170  <<initialformat_version<<" and "<<readingformat_version<<std::endl;
171  }
172 
173  /// Temporary buffer where we are going to put content of the format_nonzero dataformat
174  std::vector<uint32_t> buffer_tmp;
175 
176  /// Need to resize buffer to the maxumum size
177  resizeBufferToSizeOfDataFormats(buffer_tmp, format_nonzero.get(), format_nonzero.get());
178 
179  /// Now, this is the pointer to the first element of the Temporary buffer
180  const uint32_t* pbuffer = (uint32_t*)(&buffer_tmp[0]);
181 
182  /// Write the first dataformat (format_nonzero) into a buffer
183  try{
184  format_nonzero->writeData(pbuffer);
185  }
186  catch(...){
187  LOG_ERROR("UNITTEST")<<"From "<<__PRETTY_FUNCTION__<<std::endl
188  <<"Could not write "<<initialformat_version<<std::endl;
189  }
190 
191  /// Read the buffer to the second (empty) dataformat
192  try{
193  format_zero->readData(pbuffer);
194  }
195  catch(...){
196  LOG_ERROR("UNITTEST")<<"From "<<__PRETTY_FUNCTION__
197  <<". Could not read "<<initialformat_version<<std::endl;
198  }
199 
200  /// Check if the two dataformats are now the same
201  /// At this point they should be the same
202  try{
203  if(compareDataFormats(format_nonzero.get(), format_zero.get()) != DATAFORMATS_ARE_THE_SAME){
204  LOG_ERROR("UNITTEST")<<"Failure in "<<__PRETTY_FUNCTION__<<" at "<<__LINE__<<". "
205  <<"Formats should be the same. But they are "
206  <<boost::lexical_cast<std::string>(compareDataFormats(format_nonzero.get(), format_zero.get()))
207  <<"\nNonzero format:\n"
208  <<*(format_nonzero.get())
209  <<"Copied format:\n"
210  <<*(format_zero.get())
211  <<"\n Difference:\n"
212  <<getDifferenceBetweenStrings(format_nonzero.get(), format_zero.get());
213  return false;
214  }// end of checking if the dataformats are the same
215  }
216  catch(...){
217  LOG_ERROR("UNITTEST")<<"From "<<__PRETTY_FUNCTION__<<std::endl
218  <<"Could not compare versions after reading"
219  <<initialformat_version<<" and "<<readingformat_version<<"\n";
220  }
221 
222  /// All tests succeded
223  return true;
224 }
225 
226 //------------------------------------------------------------------------------
228 {
229  /// Print the first input dataformat into a string
230  std::string string1;
231  format1->print(string1);
232 
233  /// Print the first input dataformat into a string
234  std::string string2;
235  format2->print(string2);
236 
237  /// Compare the two strings and see if they are the same
238  if(string1.compare(string2) == 0) return DATAFORMATS_ARE_THE_SAME;
239 
240  /// See if first string contains second
241  if(string1.find(string2) != std::string::npos) return FIRST_CONTAINS_SECOND;
242 
243  /// See if second string contains first
244  if(string2.find(string1) != std::string::npos) return SECOND_CONTAINS_FIRST;
245 
247 }
248 
249 //------------------------------------------------------------------------------
251  ,const RawDAQData* format2
252  ,const DATAFORMAT_COMPARE_RESULTS what_it_should_be
253  ) const
254 {
255 
256  /// Print the first input dataformat into a string
257  std::string string1;
258  format1->print(string1);
259 
260  /// Print the first input dataformat into a string
261  std::string string2;
262  format2->print(string2);
263 
264 
266 
267  /// Compare the two strings and see if they are the same
268  if (string1.compare(string2) == 0) result = DATAFORMATS_ARE_THE_SAME;
269  else if(string1.find(string2) != std::string::npos) result = FIRST_CONTAINS_SECOND;
270  else if(string2.find(string1) != std::string::npos) result = SECOND_CONTAINS_FIRST;
271 
272 
273  if(result != what_it_should_be){
274  LOG_ERROR("UNITTEST")<<"Failure in "<<__PRETTY_FUNCTION__<<" at "<<__LINE__<<". "
275  <<"Formats should be "<<daqdataformats::getNameForResults(what_it_should_be)
276  <<". But they are " <<daqdataformats::getNameForResults(result)
277  <<"\nFirst format:"<<string1
278  <<"\nSecond format:"<<string2
279  <<"\n Difference:\n"
280  <<getDifferenceBetweenStrings(string1, string2);
281  return false;
282  }// end of checking if the dataformats are the same
283 
284  return(result == what_it_should_be);
285 }
286 
287 
288 
289 
290 //------------------------------------------------------------------------------
292 
293  /// Print the first input dataformat into a string
294  std::string string1;
295  df->print(string1);
296 
297  /// See if first string contains second
298  if(string1.find("DAQDataFormat is of unknown version") != std::string::npos) return true;
299 
300  return false;
301 }
302 
303 //------------------------------------------------------------------------------
304 bool RawDAQDataUnitTest::resizeBufferToSizeOfDataFormats(std::vector<uint32_t>& buf, const RawDAQData* f1) const{
305 
306  if(buf.size() < f1->sizeofdata())
307  buf.resize(f1->sizeofdata());
308 
309  return true;
310 }
311 
312 bool RawDAQDataUnitTest::resizeBufferToSizeOfDataFormats(std::vector<uint32_t>& buf, const RawDAQData* f1, const RawDAQData* f2) const{
314  return resizeBufferToSizeOfDataFormats(buf, f2);
315 }
316 
317 //------------------------------------------------------------------------------
319  std::string str1, str2;
320  df1->print(str1);
321  df2->print(str2);
322  return getDifferenceBetweenStrings(str1, str2);
323 }
324 
325 //------------------------------------------------------------------------------
327 
328  std::istringstream string_stream_str1(str1);
329  std::istringstream string_stream_str2(str2);
330 
331  std::vector<std::string> lines_str1;
332  std::vector<std::string> lines_str2;
333 
334  std::string line_tmp;
335  while(std::getline(string_stream_str1, line_tmp)){
336  lines_str1.push_back(line_tmp);
337  }
338  while(std::getline(string_stream_str2, line_tmp)){
339  lines_str2.push_back(line_tmp);
340  }
341 
342 
343  const uint32_t min_lines = std::min(lines_str1.size(), lines_str2.size());
344 
346 
347  for(uint32_t iline = 0; iline < min_lines; ++iline){
348  if(lines_str1[iline].compare(lines_str1[iline]) != 0){
349  diff += "line " + boost::lexical_cast<std::string>(iline) + ": ";
350  diff += lines_str1[iline] +"VS" + lines_str2[iline] + "\n";
351  }
352  }
353 
354  diff += "ENDOFDIFF";
355 
356  return diff;
357 }
358 
359 //------------------------------------------------------------------------------
361 {
362  if (res == DATAFORMATS_ARE_DIFFERENT) return "\"Data Formats are Different\"";
363  else if(res == DATAFORMATS_ARE_THE_SAME) return "\"Data Formats are Same\"";
364  else if(res == FIRST_CONTAINS_SECOND) return "\"First Format Contains Second\"";
365  else if(res == SECOND_CONTAINS_FIRST) return "\"Second Format Contains First\"";
366  return "\"What the heck\"";
367 }
368 
369 
370 }// end of namespace
virtual daqdataformats::RawDAQData * getFilledDataFormat(const version_t) const =0
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
std::string getNameForResults(const DATAFORMAT_COMPARE_RESULTS res)
virtual DATAFORMAT_COMPARE_RESULTS compareDataFormats(const RawDAQData *, const RawDAQData *) const
Are the two data formats the same?
DATAFORMAT_COMPARE_RESULTS
Enumeration of the two dataformat comparisson results.
bool checkReadDataIntoUnknownVersion() const
Check readData for all versions of the dataformat.
virtual daqdataformats::RawDAQData * getEmptyDataFormat(const version_t) const =0
method to create an empty dataformat with a given version
Float_t f2
void print(std::ostream &os=std::cout) const
Method for printing the content of the Buffer.
Definition: RawDAQData.cpp:500
bool resizeBufferToSizeOfDataFormats(std::vector< uint32_t > &, const RawDAQData *) const
Resize input vector to at least the size of the input dataformats.
#define VERSION_LOOP_iver(ACTION)
Perform some action that depends on iver.
RawDAQDataConstructor * _dataFormatConstructor
Pointer to RawDAQDataConstructor.
Float_t f1
bool checkReadDataIntoSameVersion() const
Check readData for all versions of the dataformat.
int32_t version_t
Definition: RawDAQData.h:72
bool compare(const GFluxGenerator &g1, const GFluxGenerator &g2)
RawDAQDataUnitTest(RawDAQDataConstructor *)
static const version_t DAQDATAFORMAT_UNKNOWN_VERSION
Definition: RawDAQData.h:81
Class to hold the data from the FEBs in correct formats.
T min(const caf::Proxy< T > &a, T b)
bool checkReadData(const version_t initialformat_version, const version_t readingformat_version) const
uint32_t sizeofdata() const
Sizeof method for the current data block. Returns the size of the data block in 32bit words...
Definition: RawDAQData.cpp:990
std::string getDifferenceBetweenStrings(const std::string, const std::string) const
#define LOG_ERROR(stream)
Definition: Messenger.h:129
bool isPrintDataFormatUnknownVersion(const RawDAQData *) const
Sometimes print() can print "DAQDataFormat is of unknown version".
enum BeamMode string