Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XMLParameterListHelpers_UnitTest.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 
10 #include <sstream>
13 
14 namespace Teuchos {
15 
16 
17 TEUCHOS_UNIT_TEST( XMLParameterListHelpers, toFromFile )
18 {
19 
21  A.sublist("SublistA").set("param_a", "a");
22  A.sublist("SublistA").sublist("SublistB").set("param_b", "b");
23  out << "\nA:\n"; A.print(out);
24  writeParameterListToXmlFile(A, "A.xml");
25 
27  updateParametersFromXmlFile("A.xml", inoutArg(B));
28  out << "\nB:\n"; B.print(out);
29  TEST_ASSERT( A == B );
30 
31 
33  getParameter<std::string>(B.sublist("SublistA", true), "param_a"),
34  "a");
36  getParameter<std::string>(B.sublist("SublistA", true).sublist("SublistB", true), "param_b"),
37  "b");
38 
39 }
40 
41 TEUCHOS_UNIT_TEST( XMLParameterListHelpers, OverwriteTest )
42 {
44  A.set("conflicting param","a");
45  A.sublist("SublistA").set("param_a", "a");
46  A.sublist("SublistA").sublist("SublistB").set("param_b", "b");
47  out << "\nA:\n"; A.print(out);
48  std::stringstream Astream;
49  writeParameterListToXmlOStream(A,Astream);
50 
51  // Overwrite
53  B.set("conflicting param","b");
54  updateParametersFromXmlString(Astream.str(), inoutArg(B),true);
55  out << "\nB:\n"; B.print(out);
56  TEST_ASSERT( A == B );
57  TEST_EQUALITY(getParameter<std::string>(B.sublist("SublistA", true), "param_a"),"a");
58  TEST_EQUALITY(getParameter<std::string>(B.sublist("SublistA", true).sublist("SublistB", true), "param_b"),"b");
59 
60 
61  // No Overwrite
63  C.set("conflicting param","c");
64  updateParametersFromXmlString(Astream.str(), inoutArg(B),false);
65  out << "\nC:\n"; C.print(out);
66  TEST_ASSERT( C.get("conflicting param","x") == "c" )
67  TEST_EQUALITY(getParameter<std::string>(B.sublist("SublistA", true), "param_a"),"a");
68  TEST_EQUALITY(getParameter<std::string>(B.sublist("SublistA", true).sublist("SublistB", true), "param_b"),"b");
69 
70 }
71 
72 
73 
74 } // namespace Teuchos
75 
76 
77 
void print() const
Print function to use in debugging in a debugger.
#define TEST_ASSERT(v1)
Assert the given statement is true.
T & get(const std::string &name, T def_value)
Return the parameter&#39;s value, or the default value if it is not there.
#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.
Simple helper functions that make it easy to read and write XML to and from a parameterlist.
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
Unit testing support.
A list of parameters of arbitrary type.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Creates an empty sublist and returns a reference to the sublist name. If the list already exists...