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 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
45 #include "Teuchos_StrUtils.hpp"
47 
48 //
49 // Utilities
50 //
51 
52 
53 namespace {
54 
55 
56 class EmptyModifier : public Teuchos::ParameterListModifier {
57 
58 public:
59 
60  EmptyModifier() : Teuchos::ParameterListModifier("Empty Modifier"){}
61 
62 };
63 
64 
65 } // namespace
66 
67 
68 namespace Teuchos {
69 
70 
71 TEUCHOS_UNIT_TEST( ParameterListModifier, findMatchingBaseNames ){
72  ParameterList pl = ParameterList("Parameter List with Matching Base Names");
73  const std::string base_name = "foo", non_base_name = "bar";
74  const std::string param1 = base_name, param2 = base_name + " 1";
75  const std::string sub1 = base_name + "_sublist";
76  pl.set(non_base_name, 1);
77  pl.set(non_base_name + base_name, 2);
78  pl.set(param1, 1);
79  pl.set(param2, 2);
80  pl.sublist(sub1).set("A", 1);
81  RCP<EmptyModifier> empty_modifier = rcp(new EmptyModifier());
82  Array<std::string> matches = empty_modifier->findMatchingBaseNames(pl, base_name);
83  Array<std::string> expected = tuple(param1, param2, sub1);
84  TEST_EQUALITY(matches, expected);
85  matches = empty_modifier->findMatchingBaseNames(pl, base_name, true, false);
86  expected = Teuchos::tuple(param1, param2);
87  TEST_EQUALITY(matches, expected);
88  matches = empty_modifier->findMatchingBaseNames(pl, base_name, false, true);
89  expected = Teuchos::tuple(sub1);
90  TEST_EQUALITY(matches, expected);
91 }
92 
93 
95  RCP<EmptyModifier> empty_modifier = rcp(new EmptyModifier());
96  ParameterList pl = ParameterList("Parameter List with Expanded Parameters");
97  const std::string param_template_name = "Template Parameter";
98  ParameterList valid_pl = ParameterList("Parameter List with Template for Parameter Expansion");
99  valid_pl.set(param_template_name, 1);
100  auto valid_pl_copy = valid_pl;
101  pl.set("A", 2);
102  pl.set("B", 3);
103  empty_modifier->expandParameters(param_template_name, pl, valid_pl);
104  ParameterList expected_valid_pl = ParameterList("Parameter List with Template for Parameter Expansion");
105  expected_valid_pl.set("A", 1);
106  expected_valid_pl.set("B", 1);
107  TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
108  // Now test for excluding a parameter
109  valid_pl = valid_pl_copy;
110  pl.set("C", 4);
111  pl.set("D", 5);
112  empty_modifier->expandParameters(param_template_name, pl, valid_pl, tuple<std::string>("C", "D"));
113  TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
114 }
115 
116 
118  RCP<EmptyModifier> empty_modifier = rcp(new EmptyModifier());
119  ParameterList pl = ParameterList("Parameter List with Expanded Sublists");
120  const std::string sublist_template_name = "Template Sublist";
121  ParameterList valid_pl = ParameterList("Parameter List with Template for Sublist Expansion");
122  // Make sure `expandSublists` ignores normal parameters
123  valid_pl.set("var", 1);
124  valid_pl.sublist(sublist_template_name);
125  auto valid_pl_copy = valid_pl;
126  pl.sublist("A");
127  pl.sublist("B");
128  pl.set("var", 1);
129  empty_modifier->expandSublists(sublist_template_name, pl, valid_pl);
130  ParameterList expected_valid_pl = ParameterList("Parameter List with Template for Parameter Expansion");
131  expected_valid_pl.sublist("A");
132  expected_valid_pl.sublist("B");
133  expected_valid_pl.set("var", 1);
134  TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
135  // Now test for excluding a parameter
136  valid_pl = valid_pl_copy;
137  pl.sublist("C");
138  pl.sublist("D");
139  empty_modifier->expandSublists(sublist_template_name, pl, valid_pl, tuple<std::string>("C", "D"));
140  TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
141 }
142 
143 
144 TEUCHOS_UNIT_TEST( ParameterListModifier, expandSublistsUsingBaseName ){
145  RCP<EmptyModifier> empty_modifier = rcp(new EmptyModifier());
146  ParameterList pl = ParameterList("Parameter List with Expanded Sublists");
147  ParameterList valid_pl = ParameterList("Parameter List with Template for Expanding Sublists");
148  const std::string base_name = "A";
149  auto &vsub = valid_pl.sublist(base_name, empty_modifier, "Sublist A2");
150  vsub.set("Val1", 1);
151  ParameterList copy_valid_pl(valid_pl);
152  pl.sublist("A1");
153  pl.sublist("A2");
154  empty_modifier->expandSublistsUsingBaseName(base_name, pl, valid_pl);
155  ParameterList expected_valid_pl = ParameterList("Parameter List with Template for Expanding Sublists");
156  expected_valid_pl.sublist("A1", empty_modifier, "Sublist A2").set("Val1", 1);
157  expected_valid_pl.sublist("A2", empty_modifier, "Sublist A2").set("Val1", 1);
158  TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
159  // Test for enabling the `allow_base_name` option
160  pl.sublist(base_name);
161  valid_pl = copy_valid_pl;
162  expected_valid_pl.sublist(base_name, empty_modifier, "Sublist A2").set("Val1", 1);
163  empty_modifier->expandSublistsUsingBaseName(base_name, pl, valid_pl, true);
164  TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
165  // Test for disabling the `allow_base_name` option
166  valid_pl = copy_valid_pl;
167  TEST_THROW(empty_modifier->expandSublistsUsingBaseName(base_name, pl, valid_pl, false), std::logic_error);
168 }
169 
170 
171 TEUCHOS_UNIT_TEST( ParameterListModifier, setDefaultsInSublists ) {
172  RCP<EmptyModifier> empty_modifier = rcp(new EmptyModifier());
173  ParameterList pl = ParameterList("Parameter List with Default Parameter");
174  const int a_val = 2;
175  pl.sublist("AA");
176  pl.sublist("AB").set("A", 3);
177  ParameterList expected_pl(pl);
178  pl.set("A", a_val);
179  expected_pl.sublist("AA").set("A", a_val);
180  empty_modifier->setDefaultsInSublists("A", pl, tuple<std::string>("AA", "AB"));
181  // The AA sublist should change and the "A" parameter should be deleted
182  // but the AB sublist should remain the same
183  TEST_ASSERT(haveSameValuesSorted(expected_pl, pl, true));
184 }
185 
186 
187 } // namespace Teuchos
188 
189 
190 
#define TEST_ASSERT(v1)
Assert the given statement is true.
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.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
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.