Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XMLParser_ordered.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
13 #include "Teuchos_XMLParser.hpp"
15 
16 using std::string;
17 using std::endl;
18 
19 namespace Teuchos {
20 
21 
22 /* Simple test of Teuchos XMLParser class */
23 TEUCHOS_UNIT_TEST( XMLParser, orderedWriteRead )
24 {
25  out << endl;
26 
27  /* create a ParameterList object */
28  string xmlstring1;
29  ParameterList wrotepl1("Parent");
30  {
31  ParameterList c1("Child1");
32  ParameterList c2("Child2");
33  c1.set("cp1", "first1");
34  c1.set("cp2", "second1");
35  c2.set("cp3", "first2");
36  c2.set("cp4", "second2");
37  wrotepl1.set("FirstSublist",c1);
38  wrotepl1.set("SecondSublist",c2);
39  /* create an XML object from the ParameterList and write it to a string */
41  XMLObject xmlprob = xml2pl.toXML(wrotepl1);
42  std::ostringstream ss;
43  ss << xmlprob;
44  xmlstring1 = ss.str();
45  out << "*** String 1" << endl;
46  out << xmlstring1 << endl;
47  }
48 
49  string xmlstring2;
50  ParameterList wrotepl2("Parent");
51  {
52  ParameterList c1("Child1");
53  ParameterList c2("Child2");
54  // swap the ordering
55  c1.set("cp2", "second1");
56  c1.set("cp1", "first1");
57  c2.set("cp4", "second2");
58  c2.set("cp3", "first2");
59  wrotepl2.set("SecondSublist",c2);
60  wrotepl2.set("FirstSublist",c1);
61  /* create an XML object from the ParameterList and write it to a string */
63  XMLObject xmlprob = xml2pl.toXML(wrotepl2);
64  std::ostringstream ss;
65  ss << xmlprob;
66  xmlstring2 = ss.str();
67  out << "*** String 2" << endl;
68  out << xmlstring2 << endl;
69  }
70 
71  // the different PL orderings should be reflected in the ParameterLists and their XML string representations
72  TEST_INEQUALITY(wrotepl1, wrotepl2);
73  TEST_INEQUALITY(xmlstring1, xmlstring2);
74 
75  /* create a input source, parser to read the string */
76  ParameterList readpl1, readpl2;
77  {
78  StringInputSource src(xmlstring1);
79  XMLParser parser(src.stream());
80  XMLObject xmlprob = parser.parse();
82  readpl1 = pl2xml.toParameterList(xmlprob);
83  }
84  {
85  StringInputSource src(xmlstring2);
86  XMLParser parser(src.stream());
87  XMLObject xmlprob = parser.parse();
89  readpl2 = pl2xml.toParameterList(xmlprob);
90  }
91 
92  /* check that the parameter lists do not match */
93  TEST_INEQUALITY(readpl1, readpl2);
94 
95 }
96 
97 
98 TEUCHOS_UNIT_TEST( XMLParser, simpleOrderedRead )
99 {
100 
101  /* create a ParameterList object */
102  string xmlstring1;
103  ParameterList plGold("ParentList");
104  {
105  ParameterList c1("Z");
106  ParameterList c2("A");
107  c1.set("A", "first1");
108  c1.set("Z", "second1");
109  c2.set("Z", "first2");
110  c2.set("A", "second2");
111  plGold.set("9FirstSublist",c1);
112  plGold.set("1SecondSublist",c2);
113  }
114 
115  string xmlsrc(
116  "<ParameterList name=\"ParentList\">\n"
117  " <ParameterList name=\"9FirstSublist\">\n"
118  " <Parameter name=\"A\" type=\"string\" value=\"first1\"/>\n"
119  " <Parameter name=\"Z\" type=\"string\" value=\"second1\"/>\n"
120  " </ParameterList>\n"
121  " <ParameterList name=\"1SecondSublist\">\n"
122  " <Parameter name=\"Z\" type=\"string\" value=\"first2\"/>\n"
123  " <Parameter name=\"A\" type=\"string\" value=\"second2\"/>\n"
124  " </ParameterList>\n"
125  "</ParameterList>\n");
126  ParameterList plTest;
127  {
128  StringInputSource src(xmlsrc);
129  XMLParser parser(src.stream());
130  XMLObject xmlprob = parser.parse();
131  XMLParameterListReader pl2xml;
132  plTest = pl2xml.toParameterList(xmlprob);
133  }
134 
135  TEST_EQUALITY(plTest, plGold);
136 }
137 
138 
139 } // namespace Teuchos
Writes an XML object to a parameter list.
XMLObject toXML(const ParameterList &p, RCP< const DependencySheet > depSheet=null) const
#define TEST_INEQUALITY(v1, v2)
Assert the inequality of v1 and v2.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Templated set method.
XMLParser consumes characters from an XMLInputStream object, parsing the XML and using a TreeBuilding...
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
Writes a ParameterList to an XML object.
Unit testing support.
virtual RCP< XMLInputStream > stream() const
Create a StringInputStream.
RCP< ParameterList > toParameterList(const XMLObject &xml, RCP< DependencySheet > depSheet) const
A list of parameters of arbitrary type.
Definition of XMLInputSource derived class for reading XML from a std::string.
Writes an XML object to a parameter list.
Instantiation of XMLInputSource class for reading XML from a std::string.
A class providing a simple XML parser. Methods can be overloaded to exploit external XML parsing libr...
Writes a ParameterList to an XML object.