Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParameterListModifier_UnitTests.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_StrUtils.hpp"
15 
16 //
17 // Utilities
18 //
19 
20 
21 namespace {
22 
23 
24 class EmptyModifier : public Teuchos::ParameterListModifier {
25 
26 public:
27 
28  EmptyModifier() : Teuchos::ParameterListModifier("Empty Modifier"){}
29 
30 };
31 
32 
33 } // namespace
34 
35 
36 namespace Teuchos {
37 
38 
39 TEUCHOS_UNIT_TEST( ParameterListModifier, findMatchingBaseNames ){
40  ParameterList pl = ParameterList("Parameter List with Matching Base Names");
41  const std::string base_name = "foo", non_base_name = "bar";
42  const std::string param1 = base_name, param2 = base_name + " 1";
43  const std::string sub1 = base_name + "_sublist";
44  pl.set(non_base_name, 1);
45  pl.set(non_base_name + base_name, 2);
46  pl.set(param1, 1);
47  pl.set(param2, 2);
48  pl.sublist(sub1).set("A", 1);
49  RCP<EmptyModifier> empty_modifier = rcp(new EmptyModifier());
50  Array<std::string> matches = empty_modifier->findMatchingBaseNames(pl, base_name);
51  Array<std::string> expected = tuple(param1, param2, sub1);
52  TEST_EQUALITY(matches, expected);
53  matches = empty_modifier->findMatchingBaseNames(pl, base_name, true, false);
54  expected = Teuchos::tuple(param1, param2);
55  TEST_EQUALITY(matches, expected);
56  matches = empty_modifier->findMatchingBaseNames(pl, base_name, false, true);
57  expected = Teuchos::tuple(sub1);
58  TEST_EQUALITY(matches, expected);
59 }
60 
61 
63  RCP<EmptyModifier> empty_modifier = rcp(new EmptyModifier());
64  ParameterList pl = ParameterList("Parameter List with Expanded Parameters");
65  const std::string param_template_name = "Template Parameter";
66  ParameterList valid_pl = ParameterList("Parameter List with Template for Parameter Expansion");
67  valid_pl.set(param_template_name, 1);
68  auto valid_pl_copy = valid_pl;
69  pl.set("A", 2);
70  pl.set("B", 3);
71  empty_modifier->expandParameters(param_template_name, pl, valid_pl);
72  ParameterList expected_valid_pl = ParameterList("Parameter List with Template for Parameter Expansion");
73  expected_valid_pl.set("A", 1);
74  expected_valid_pl.set("B", 1);
75  TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
76  // Now test for excluding a parameter
77  valid_pl = valid_pl_copy;
78  pl.set("C", 4);
79  pl.set("D", 5);
80  empty_modifier->expandParameters(param_template_name, pl, valid_pl, tuple<std::string>("C", "D"));
81  TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
82 }
83 
84 
86  RCP<EmptyModifier> empty_modifier = rcp(new EmptyModifier());
87  ParameterList pl = ParameterList("Parameter List with Expanded Sublists");
88  const std::string sublist_template_name = "Template Sublist";
89  ParameterList valid_pl = ParameterList("Parameter List with Template for Sublist Expansion");
90  // Make sure `expandSublists` ignores normal parameters
91  valid_pl.set("var", 1);
92  valid_pl.sublist(sublist_template_name);
93  auto valid_pl_copy = valid_pl;
94  pl.sublist("A");
95  pl.sublist("B");
96  pl.set("var", 1);
97  empty_modifier->expandSublists(sublist_template_name, pl, valid_pl);
98  ParameterList expected_valid_pl = ParameterList("Parameter List with Template for Parameter Expansion");
99  expected_valid_pl.sublist("A");
100  expected_valid_pl.sublist("B");
101  expected_valid_pl.set("var", 1);
102  TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
103  // Now test for excluding a parameter
104  valid_pl = valid_pl_copy;
105  pl.sublist("C");
106  pl.sublist("D");
107  empty_modifier->expandSublists(sublist_template_name, pl, valid_pl, tuple<std::string>("C", "D"));
108  TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
109 }
110 
111 
112 TEUCHOS_UNIT_TEST( ParameterListModifier, expandSublistsUsingBaseName ){
113  RCP<EmptyModifier> empty_modifier = rcp(new EmptyModifier());
114  ParameterList pl = ParameterList("Parameter List with Expanded Sublists");
115  ParameterList valid_pl = ParameterList("Parameter List with Template for Expanding Sublists");
116  const std::string base_name = "A";
117  auto &vsub = valid_pl.sublist(base_name, empty_modifier, "Sublist A2");
118  vsub.set("Val1", 1);
119  ParameterList copy_valid_pl(valid_pl);
120  pl.sublist("A1");
121  pl.sublist("A2");
122  empty_modifier->expandSublistsUsingBaseName(base_name, pl, valid_pl);
123  ParameterList expected_valid_pl = ParameterList("Parameter List with Template for Expanding Sublists");
124  expected_valid_pl.sublist("A1", empty_modifier, "Sublist A2").set("Val1", 1);
125  expected_valid_pl.sublist("A2", empty_modifier, "Sublist A2").set("Val1", 1);
126  TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
127  // Test for enabling the `allow_base_name` option
128  pl.sublist(base_name);
129  valid_pl = copy_valid_pl;
130  expected_valid_pl.sublist(base_name, empty_modifier, "Sublist A2").set("Val1", 1);
131  empty_modifier->expandSublistsUsingBaseName(base_name, pl, valid_pl, true);
132  TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
133  // Test for disabling the `allow_base_name` option
134  valid_pl = copy_valid_pl;
135  TEST_THROW(empty_modifier->expandSublistsUsingBaseName(base_name, pl, valid_pl, false), std::logic_error);
136 }
137 
138 
139 TEUCHOS_UNIT_TEST( ParameterListModifier, setDefaultsInSublists ) {
140  RCP<EmptyModifier> empty_modifier = rcp(new EmptyModifier());
141  ParameterList pl = ParameterList("Parameter List with Default Parameter");
142  const int a_val = 2;
143  pl.sublist("AA");
144  pl.sublist("AB").set("A", 3);
145  ParameterList expected_pl(pl);
146  pl.set("A", a_val);
147  expected_pl.sublist("AA").set("A", a_val);
148  empty_modifier->setDefaultsInSublists("A", pl, tuple<std::string>("AA", "AB"));
149  // The AA sublist should change and the "A" parameter should be deleted
150  // but the AB sublist should remain the same
151  TEST_ASSERT(haveSameValuesSorted(expected_pl, pl, true));
152 }
153 
154 
156 {
157 public:
158 
159  SimpleModifier(const std::string &name) : Teuchos::ParameterListModifier(name){}
160 };
161 
162 
165  RCP<ParameterListModifier> modifier1 = rcp(new ParameterListModifier("Modifier 1"));
166  RCP<ParameterListModifier> modifier2 = rcp(new ParameterListModifier("Modifier 2"));
167  RCP<ParameterListModifier> modifier3 = rcp(new ParameterListModifier("Modifier 1"));
168  RCP<SimpleModifier> modifier4 = rcp(new SimpleModifier("Modifier 1"));
169 
170  // These modifiers have the same type but different names
171  TEST_INEQUALITY(*modifier1, *modifier2);
172  // These modifiers have the same type and name
173  TEST_EQUALITY(*modifier1, *modifier3);
174  // These modifiers have different types but the same name
175  TEST_INEQUALITY(*modifier1, *modifier4);
176  // Test the `haveSameModifiers` function
177  {
178  ParameterList pl = ParameterList("pl", modifier1);
179  ParameterList expected_pl = ParameterList("expected_pl", modifier1);
180  TEST_ASSERT(haveSameModifiers(expected_pl, pl));
181  }
182  {
183  ParameterList pl = ParameterList("pl");
184  pl.sublist("Asub", modifier1);
185  ParameterList expected_pl = ParameterList("expected_pl");
186  expected_pl.sublist("Asub", modifier1);
187  TEST_ASSERT(haveSameModifiers(expected_pl, pl));
188  pl.sublist("Asub").setModifier(modifier2);
189  TEST_ASSERT(!haveSameModifiers(expected_pl, pl));
190  }
191  // const int a_val = 2;
192  // pl.sublist("AA");
193  // pl.sublist("AB").set("A", 3);
194  // ParameterList expected_pl(pl);
195  // pl.set("A", a_val);
196  // expected_pl.sublist("AA").set("A", a_val);
197  // empty_modifier->setDefaultsInSublists("A", pl, tuple<std::string>("AA", "AB"));
198  // // The AA sublist should change and the "A" parameter should be deleted
199  // // but the AB sublist should remain the same
200  // TEST_ASSERT(haveSameValuesSorted(expected_pl, pl, true));
201 }
202 
203 
204 } // namespace Teuchos
205 
206 
207 
#define TEST_INEQUALITY(v1, v2)
Assert the inequality of v1 and v2.
#define TEST_ASSERT(v1)
Assert the given statement is true.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Templated set method.
Abstract interface for an object that can modify both a parameter list and the parameter list being u...
A std::string utilities class for Teuchos.
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
Unit testing support.
A list of parameters of arbitrary type.
Parameter List Modifier class.
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...
Smart reference counting pointer class for automatic garbage collection.
void setModifier(RCP< const ParameterListModifier > const &modifier)