testReader.cc
Go to the documentation of this file.
1 
11 #include <cppunit/extensions/HelperMacros.h>
12 
13 #include <iostream>
14 #include <string>
15 #include <sstream>
16 #include <vector>
17 #include <cmath>
18 #include <boost/lexical_cast.hpp>
19 #include <utl/Reader.h>
20 #include <utl/ReaderErrorReporter.h>
21 #include <utl/AugerUnits.h>
22 #include <utl/TimeStamp.h>
23 #include <utl/TabulatedFunction.h>
24 #include <utl/Function.h>
25 #include <utl/UTCDateTime.h>
26 #include <utl/ErrorLogger.h>
27 #include <utl/AugerException.h>
28 #include <tst/Verify.h>
29 
30 using namespace std;
31 using namespace utl;
32 using namespace tst;
33 using namespace xercesc;
34 
35 
36 #define ASSERT_EQUAL(x, y) CPPUNIT_ASSERT(Verify<Equal>(x, y))
37 #define ASSERT_NOTEQUAL(x, y) CPPUNIT_ASSERT(Verify<Not<Equal> >(x, y))
38 #define ASSERT_CLOSE(x, y) CPPUNIT_ASSERT(Verify<CloseTo>(x, y))
39 #define ASSERT_CLOSEEPS(x, y, eps) CPPUNIT_ASSERT(Verify<CloseTo>(x, y, eps))
40 
41 
45 class ReaderTest : public CppUnit::TestFixture {
46 
47  CPPUNIT_TEST_SUITE(ReaderTest);
48  CPPUNIT_TEST(testHierarchy);
49  CPPUNIT_TEST(testAttributes);
50  CPPUNIT_TEST(testCastings);
51  CPPUNIT_TEST(testStringParse);
52  CPPUNIT_TEST(testXMLTime);
53  CPPUNIT_TEST(testEmpties);
54  CPPUNIT_TEST(testOperators);
55  CPPUNIT_TEST(testEmptyReader);
56  CPPUNIT_TEST(testWarning);
57  CPPUNIT_TEST(testEvaluatorUnits);
58  //CPPUNIT_TEST(testXMLEntityResolver);
59  CPPUNIT_TEST(testTabulatedFunction);
60  CPPUNIT_TEST(testFunction);
61  CPPUNIT_TEST_SUITE_END();
62 
63 private:
66 
67 public:
68  void
70  {
71  ErrorLogger::GetInstance().SetVerbosity(Verbosity::eAnnoying);
72  fReader = new Reader(XMLFILE, Reader::eDTD);
73  fSchemaReader = new Reader(XMLSCHEMAFILE, Reader::eSCHEMA);
74  }
75 
76  void
78  {
79  delete fReader;
80  delete fSchemaReader;
81  }
82 
83  void
85  {
86  //Attributes
87  Branch attB = fReader->GetTopBranch().GetChild("attributeTest");
88 
89  // Find the Branch with a certain set of attributes
90  AttributeMap attMap;
91  attMap["firstName"] = "Jim";
92  attMap["lastName"] = "Cronin";
93  string responsibility;
94  attB.GetChild("collaborator", attMap).GetChild("responsibility").GetData(responsibility);
95  ASSERT_EQUAL(responsibility, string("Spokesperson Emeritus"));
96  // alternatively
97  attB.GetChild("collaborator",
98  "firstName=Jim lastName=Cronin").GetChild("responsibility").GetData(responsibility);
99  ASSERT_EQUAL(responsibility, string("Spokesperson Emeritus"));
100 
101  // Get the attributes belonging to a Branch
102  AttributeMap theAttributes = attB.GetFirstChild().GetAttributes();
103 
104  ASSERT_EQUAL(theAttributes.size(), size_t(2));
105  ASSERT_EQUAL(theAttributes["firstName"], string("Alan"));
106  ASSERT_EQUAL(theAttributes["lastName"], string("Watson"));
107 
108  // Note that the "unit" and "variables" attributes are treated as a special case.
109  // Unit and variable attributes
110  // which appear either in the attribute map you pass to the GetChild method, or
111  // in the XML file, will be ignored for purposes of finding the requested Branch.
112  // The unit attribute will, however, still be used by the GetData method to
113  // convert the requested quantity into the base units. Here is an example:
114  //
115  AttributeMap apertureAttributes;
116  apertureAttributes["site"] = "Malargue";
117  double aperture;
118  attB.GetChild("aperture", apertureAttributes).GetData(aperture);
119  ASSERT_EQUAL(aperture, 7000*sr*km2);
120  // alternatively
121  attB.GetChild("aperture", "site=Malargue").GetData(aperture);
122  ASSERT_EQUAL(aperture, 7000*sr*km2);
123 
124  double flux;
125  Branch fluxBranch = attB.GetChild("flux");
126  fluxBranch.GetData(flux);
127  ASSERT_CLOSEEPS(flux, 1e-3/(cm2*s*sr*GeV), 1e-8/(cm2*s*sr*GeV));
128 
129  fluxBranch = fluxBranch.GetNextSibling();
130  fluxBranch.GetData(flux);
131  ASSERT_CLOSEEPS(flux, 1e-3/(m2*s*sr*GeV), 1e-8/(m2*s*sr*GeV));
132 
133  fluxBranch = fluxBranch.GetNextSibling();
134  fluxBranch.GetData(flux);
135  ASSERT_CLOSEEPS(flux, 1e-3/(cm2*s*sr*MeV), 1e-8/(cm2*s*sr*MeV));
136 
137  fluxBranch = fluxBranch.GetNextSibling();
138  bool caught = false;
139  try {
140  fluxBranch.GetData(flux);
141  } catch (XMLParseException& e) {
142  //Minus error here!
143  const std::string s = e.GetMessage();;
144  caught = true;
145  }
146  CPPUNIT_ASSERT(caught);
147 
148  fluxBranch = fluxBranch.GetNextSibling();
149  caught = false;
150  try {
151  fluxBranch.GetData(flux);
152  } catch (XMLParseException& e) {
153  //Unknown symbol here!
154  const std::string s = e.GetMessage();;
155  caught = true;
156  }
157  CPPUNIT_ASSERT(caught);
158 
159 
160  fluxBranch = fluxBranch.GetNextSibling();
161  caught = false;
162  try {
163  fluxBranch.GetData(flux);
164  } catch (XMLParseException& e) {
165  //Bracket problem here!
166  const std::string s = e.GetMessage();;
167  caught = true;
168  }
169  CPPUNIT_ASSERT(caught);
170 
171  fluxBranch = fluxBranch.GetNextSibling();
172  fluxBranch.GetData(flux);
173  ASSERT_CLOSE(flux, 256.0); //(2.0^2.0)^(2^2) = 256.0
174 
175  }
176 
177  void
179  {
180  // Parsing a string in memory
181 
182  ostringstream trimStream;
183  trimStream << "first line \t \n"
184  " 1 2 3 foo bar";
185  ostringstream inpStream;
186  inpStream << "<AtmosphereFuncList>\n"
187  " <!-- some stupid comment --> \n"
188  " <attenuationFunc> <!-- comment --> myAttenFunc </attenuationFunc> \n"
189  " <scatteringFunc> DefaultScattering </scatteringFunc> "
190  " <trimming> \t "
191  " \t " << trimStream.str() << " \t \n \r "
192  " </trimming> "
193  " <qualityFunc> DefaultQuality </qualityFunc> "
194  "</AtmosphereFuncList>";
195 
196  ReaderStringInput readerInput(inpStream);
197  Reader theReaderFromMem(readerInput); // construct from ReaderStringInput
198 
199  Branch memTopB = theReaderFromMem.GetTopBranch();
200 
201  ASSERT_EQUAL(memTopB.GetName(), string("AtmosphereFuncList"));
202  ASSERT_EQUAL(memTopB.GetFirstChild().GetName(), string("attenuationFunc"));
203  ASSERT_EQUAL(memTopB.GetFirstChild().GetNextSibling().GetName(), string("scatteringFunc"));
204 
205  string data;
206  memTopB.GetFirstChild().GetData(data);
207  ASSERT_EQUAL(data, string("myAttenFunc"));
208  memTopB.GetFirstChild().GetNextSibling().GetData(data);
209  ASSERT_EQUAL(data, string("DefaultScattering"));
210  memTopB.GetChild("qualityFunc").GetData(data);
211  ASSERT_EQUAL(data, string("DefaultQuality"));
212 
213  // new way
214  CPPUNIT_ASSERT(!memTopB.GetChild("qualityFunc").GetNextSibling());
215 
216  memTopB.GetChild("trimming").GetData(data);
217  ASSERT_EQUAL(data, trimStream.str());
218  }
219 
220  void
222  {
223  CPPUNIT_ASSERT(fReader->GetTopBranch());
224  CPPUNIT_ASSERT(!fReader->GetTopBranch().GetParent());
225  CPPUNIT_ASSERT(fReader->GetTopBranch().GetFirstChild().GetParent() == fReader->GetTopBranch());
226  ASSERT_EQUAL(fReader->GetTopBranch().GetName(), string("document"));
227 
228  Branch hierarchyB = fReader->GetTopBranch().GetChild("hierarchyTest");
229 
230  int i = 0;
231  for (Branch child = hierarchyB.GetChild("parent1").GetFirstChild();
232  child; child = child.GetNextSibling()) {
233  ++i;
234  ASSERT_EQUAL(child.GetName(), string("child"));
235  ASSERT_EQUAL(boost::lexical_cast<int>(child.GetAttributes()["id"]), i);
236  }
237  ASSERT_EQUAL(i, 3);
238 
239  // sugar coated version down and up the tree
240  i = 0;
241  Branch last;
242  for (Branch sib = hierarchyB.GetChild("parent1").GetFirstChild();
243  sib; ++sib) {
244  ++i;
245  ASSERT_EQUAL(sib.GetName(), string("child"));
246  ASSERT_EQUAL(boost::lexical_cast<int>(sib.GetAttributes()["id"]), i);
247  last = sib;
248  }
249  ASSERT_EQUAL(i, 3);
250 
251  for (Branch sib = last ; sib ; --sib) {
252  ASSERT_EQUAL(sib.GetName(), string("child"));
253  ASSERT_EQUAL(boost::lexical_cast<int>(sib.GetAttributes()["id"]), i);
254  --i;
255  }
256  ASSERT_EQUAL(i, 0);
257 
258  ASSERT_EQUAL(
259  hierarchyB.GetChild("parent1").GetChild("child", "id=3").
260  GetFirstChild().GetName(), string("grandChild")
261  );
262 
263  CPPUNIT_ASSERT(!hierarchyB.GetChild("parent2").GetFirstChild());
264  }
265 
266  void
268  {
269  string dataString;
270  fReader->GetTopBranch().GetChild("castingTest").GetChild("list").GetData(dataString);
271  ASSERT_EQUAL(dataString, string("2.14 2.16 2.19 2.23 2.27 2.32"));
272 
273  list<double> myList;
274  myList.push_back(2.14*mm);
275  myList.push_back(2.16*mm);
276  myList.push_back(2.19*mm);
277  myList.push_back(2.23*mm);
278  myList.push_back(2.27*mm);
279  myList.push_back(2.32*mm);
280 
281  list<double> dataList;
282  fReader->GetTopBranch().GetChild("castingTest").GetChild("list").GetData(dataList);
283 
284  CPPUNIT_ASSERT(dataList == myList);
285  }
286 
287  void
289  {
290  string time = "1982-05-12T01:23:43.56789Z";
291  UTCDateTime utc = boost::lexical_cast<UTCDateTime>(time);
292  ASSERT_EQUAL(utc.GetYear(), 1982);
293  ASSERT_EQUAL(utc.GetMonth(), 5);
294  ASSERT_EQUAL(utc.GetDay(), 12);
295  ASSERT_EQUAL(utc.GetHour(), 1);
296  ASSERT_EQUAL(utc.GetMinute(), 23);
297  ASSERT_EQUAL(utc.GetSecond(), 43);
298  ASSERT_EQUAL(utc.GetNanosecond(), 1000000000*0.56789);
299 
300  vector<TimeStamp> tv;
301  fReader->GetTopBranch().GetChild("timeTest").GetData(tv);
302  ASSERT_EQUAL(tv.size(), size_t(5));
303 
304  cout << endl;
305  const double ns[] = { 0, 1, 1500, 94, 1370000 };
306  for (int i = 0; i < 5; ++i) {
307  const UTCDateTime utc(tv[i]);
308  cout << tv[i] << " " << utc << endl;
309  ASSERT_EQUAL(utc.GetYear(), 2002);
310  ASSERT_EQUAL(utc.GetMonth(), 5);
311  ASSERT_EQUAL(utc.GetDay(), 12);
312  ASSERT_EQUAL(utc.GetHour(), i);
313  ASSERT_EQUAL(utc.GetMinute(), 23);
314  ASSERT_EQUAL(utc.GetSecond(), 45);
315  ASSERT_EQUAL(tv[i].GetGPSNanoSecond(), ns[i]);
316  ASSERT_EQUAL(utc.GetNanosecond(), ns[i]);
317  }
318  }
319 
320  void
322  {
323  Branch empty1 = fReader->GetTopBranch().GetChild("emptyTests").GetChild("emptyElement1");
324  string emptyString1;
325  empty1.GetData(emptyString1);
326  ASSERT_EQUAL(emptyString1, string(""));
327 
328  Branch empty2 = fReader->GetTopBranch().GetChild("emptyTests").GetChild("emptyElement2");
329  string emptyString2;
330  empty2.GetData(emptyString2);
331  ASSERT_EQUAL(emptyString2, string(""));
332  }
333 
334  void
336  {
337  Branch b1 = fReader->GetTopBranch().GetChild("hierarchyTest");
338  Branch b2 = fReader->GetTopBranch().GetChild("hierarchyTest");
339  Branch b3 = fReader->GetTopBranch().GetChild("castingTest");
340 
341  CPPUNIT_ASSERT(b1 == b2);
342  CPPUNIT_ASSERT(b2 != b3);
343  }
344 
345  void
347  {
348  Branch branch;
349  Reader r(branch);
350  }
351 
352  void
354  {
355  Branch b = fReader->GetTopBranch();
356  Branch nobody = b.GetChild("nobody");
357  Branch noone = b.GetChild("noone");
358  // The error given when it crashes should be the one that actually made it crash!
359  bool caught = false;
360  try {
361  Branch crash_1 = nobody.GetFirstChild();
362  } catch (XMLParseException& e) {
363  const std::string s = e.GetMessage();
364  CPPUNIT_ASSERT(s.find("noone") == std::string::npos);
365  CPPUNIT_ASSERT(s.find("nobody") != std::string::npos);
366  caught = true;
367  }
368  CPPUNIT_ASSERT(caught);
369  //TODO, tests: GetFirstChild().GetFirstChild() GetChild().GetFirstChild()
370  // GetSibling().GetFirstChild() Clone().GetFirstChild()
371  }
372 
373  void
375  {
376  // Test that units are set up correctly in the AugerUnitParser
377 
378  Branch evalB = fReader->GetTopBranch().GetChild("evaluatorUnitsTest");
379 
380  double length;
381  evalB.GetChild("length").GetData(length);
382  ASSERT_CLOSE(length, 1.0*meter);
383 
384  double mass;
385  evalB.GetChild("mass").GetData(mass);
386  ASSERT_CLOSE(mass, 1.0*kilogram);
387 
388  double time;
389  evalB.GetChild("time").GetData(time);
390  ASSERT_CLOSE(time, 1.0*second);
391 
392  double current;
393  evalB.GetChild("current").GetData(current);
394  ASSERT_CLOSE(current, 1.0*ampere);
395 
396  double temperature;
397  evalB.GetChild("temperature").GetData(temperature);
398  ASSERT_CLOSE(temperature, 1.0*kelvin);
399 
400  double lumi;
401  evalB.GetChild("lumi").GetData(lumi);
402  ASSERT_CLOSE(lumi, 1.0*candela);
403  }
404 
405  void
407  {
408  Branch b = fReader->GetTopBranch().GetChild("tabulatedFunctionTest");
409  const TabulatedFunction tf = b.GetChild("foo").Get<TabulatedFunction>();
410  for (unsigned int i = 0, n = tf.GetNPoints(); i < n; ++i) {
411  const Pair p = tf[i];
412  ASSERT_CLOSE(p.X(), p.Y());
413  }
414  }
415 
416  void
418  {
419  std::cout << "testFunction" << std::endl;
420  const utl::Function func1 = fReader->GetTopBranch().GetChild("functionTest1").Get<Function>();
421  const utl::Function func2 = fReader->GetTopBranch().GetChild("functionTest2").Get<Function>();
422 
423  double E = 1e18;
424  ASSERT_CLOSE(func1(E), 15 + log10(E/PeV)*10);
425 
426 
427  std::cout << func2.GetFunction() << std::endl;
428  utl::SymbolTable st = func2.GetVariables() ;
429  for (const auto& v : st) {
430  std::cout << v.first << " " << v.second << std::endl;
431  }
432 
433  E = 1e17;
434  double slope = 1.5;
435 
436  bool caught = false;
437  try {
438  func2(E);
439  } catch (XMLParseException& e) {
440  // wrong number of arguments here!
441  const std::string s = e.GetMessage();;
442  caught = true;
443  }
444  CPPUNIT_ASSERT(caught);
445 
446  ASSERT_CLOSE(func2({E, slope}), log10(pow((E/eV),(-slope))) + log10(E/PeV)*10);
447  }
448 
449 };
450 
451 
void testEmpties()
Definition: testReader.cc:321
Branch GetTopBranch() const
Definition: Branch.cc:63
void testFunction()
Definition: testReader.cc:417
#define ASSERT_EQUAL(x, y)
Definition: testReader.cc:36
const double eV
Definition: GalacticUnits.h:35
unsigned int GetNPoints() const
utl::Reader * fReader
Definition: testReader.cc:64
constexpr double mm
Definition: AugerUnits.h:113
const double ampere
Definition: GalacticUnits.h:46
double GetNanosecond() const
Definition: UTCDateTime.h:60
const double PeV
int GetHour() const
Definition: UTCDateTime.h:54
constexpr double kilogram
Definition: AugerUnits.h:194
void testTabulatedFunction()
Definition: testReader.cc:406
void testEmptyReader()
Definition: testReader.cc:346
Class to hold collection (x,y) points and provide interpolation between them.
std::map< std::string, std::string > AttributeMap
Definition: Branch.h:24
const double meter
Definition: GalacticUnits.h:29
constexpr double m2
Definition: AugerUnits.h:122
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
double pow(const double x, const unsigned int i)
constexpr double km2
Definition: AugerUnits.h:126
int GetYear() const
Definition: UTCDate.h:44
int GetMinute() const
Definition: UTCDateTime.h:56
Exception for errors encountered when parsing XML.
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
Branch GetTopBranch() const
Get the top Branch (represents same entity as document node)
Definition: Reader.h:45
constexpr double MeV
Definition: AugerUnits.h:184
double X() const
Definition: Pair.h:31
void tearDown()
Definition: testReader.cc:77
T Get() const
Definition: Branch.h:271
AttributeMap GetAttributes() const
Get a map&lt;string, string&gt; containing all the attributes of this Branch.
Definition: Branch.cc:267
Branch GetNextSibling() const
Get next sibling of this branch.
Definition: Branch.cc:284
Utility for parsing XML files.
Definition: Reader.h:25
Class representing a document branch.
Definition: Branch.h:107
constexpr double s
Definition: AugerUnits.h:163
This just defines a type which holds some character data to be parsed by the Reader.
Definition: Reader.h:88
int GetSecond() const
Definition: UTCDateTime.h:58
const double ns
void testOperators()
Definition: testReader.cc:335
really everything
Definition: Verbosity.h:18
const double second
Definition: GalacticUnits.h:32
constexpr double candela
Definition: AugerUnits.h:272
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
std::string GetName() const
function to get the Branch name
Definition: Branch.cc:374
std::map< std::string, double > SymbolTable
Definition: SymbolTable.h:10
Evaluate functions given in a string. The real work is done by the ExpressionParser class...
Definition: Function.h:27
#define ASSERT_CLOSE(x, y)
Definition: testReader.cc:38
constexpr double kelvin
Definition: AugerUnits.h:259
int GetMonth() const
Definition: UTCDate.h:46
void testCastings()
Definition: testReader.cc:267
void testWarning()
Definition: testReader.cc:353
void setUp()
Definition: testReader.cc:69
void testHierarchy()
Definition: testReader.cc:221
constexpr double GeV
Definition: AugerUnits.h:187
int GetDay() const
Definition: UTCDate.h:48
void testEvaluatorUnits()
Definition: testReader.cc:374
uint16_t * data
Definition: dump1090.h:228
constexpr double sr
Definition: AugerUnits.h:139
utl::Reader * fSchemaReader
Definition: testReader.cc:65
Branch GetFirstChild() const
Get first child of this Branch.
Definition: Branch.cc:98
a pair of graph points (x,y)
Definition: Pair.h:25
void testStringParse()
Definition: testReader.cc:178
double Y() const
Definition: Pair.h:32
const std::string & GetMessage() const
Retrieve the message from the exception.
#define ASSERT_CLOSEEPS(x, y, eps)
Definition: testReader.cc:39
void testAttributes()
Definition: testReader.cc:84
void testXMLTime()
Definition: testReader.cc:288
constexpr double cm2
Definition: AugerUnits.h:118

, generated on Tue Sep 26 2023.