Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XMLParser_test.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 
18 namespace Teuchos {
19 
20 
21 /* Simple test of Teuchos XMLParser class */
23 {
24 
25  /* create a ParameterList object */
26  ParameterList problem("Problem");
27  ParameterList solver("Solver");
28  ParameterList prec("Preconditioner");
29  prec.set("type", "ILUk"); // set some of these to isUsed for completeness
30  TEST_NOTHROW( prec.get<std::string>("type") );
31  prec.set("k", 2);
32  solver.set("Preconditioner",prec);
33  solver.set("type", "gmres"); // set some of these to isUsed for completeness
34  TEST_NOTHROW( solver.get<std::string>("type") );
35  solver.set("maxiters", 1000);
36  solver.set("restarts", 100);
37  solver.set("special1","\"&\""); // test the XML outputting and parsing for correctness
38  solver.set("special2","\'&\'"); // test the XML outputting and parsing for correctness
39  solver.set("special3","\"&\'"); // test the XML outputting and parsing for correctness
40  solver.set("tol", 1.0e-10); // set some of these to isUsed for completeness
41  TEST_NOTHROW( solver.get<double>("tol") );
42  problem.set("Solver",solver);
43 
44  std::cout << "*** ParameterList (original)" << std::endl;
45  std::cout << problem << std::endl;
46 
47  /* create an XML object from the ParameterList */
49  XMLObject xmlprob1 = xml2pl.toXML(problem);
50 
51  /* write the XML to a std::string */
52  std::ostringstream ss;
53  ss << xmlprob1;
54  std::string strproblem = ss.str();
55 
56  std::cout << "*** XML from ParameterListParameterListWriter.toXML().toString()" << std::endl;
57  std::cout << xmlprob1 << std::endl;
58 
59  /* create a input source, parser to read the std::string */
60  StringInputSource src(strproblem);
61  XMLParser parser(src.stream());
62 
63  /* parse XML in a std::string */
64  XMLObject xmlprob2 = parser.parse();
65 
66  std::cout << "*** XML from XMLParser.parse()" << std::endl;
67  std::cout << xmlprob2 << std::endl;
68 
69  /* convert the new XML object to a ParameterList */
71  ParameterList problem2 = pl2xml.toParameterList(xmlprob2);
72 
73  std::cout << "*** ParameterList from XMLParameterListReader.toParameterList()" << std::endl;
74  std::cout << problem2 << std::endl;
75 
76  /* check that the new parameter list matches the old one */
77  TEST_EQUALITY(problem2, problem);
78 
79 }
80 
81 
82 } // namespace Teuchos
83 
84 
85 
Writes an XML object to a parameter list.
XMLObject toXML(const ParameterList &p, RCP< const DependencySheet > depSheet=null) const
#define TEST_NOTHROW(code)
Asserr that the statement &#39;code&#39; does not thrown any excpetions.
T & get(const std::string &name, T def_value)
Return the parameter&#39;s value, or the default value if it is not there.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Set a parameter whose value has type T.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
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.