Teuchos Package Browser (Single Doxygen Collection)
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
parameterlist
test
ParameterList
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
42
#include "
Teuchos_ParameterList.hpp
"
43
#include "
Teuchos_ParameterListAcceptorHelpers.hpp
"
44
#include "
Teuchos_ParameterListModifier.hpp
"
45
#include "
Teuchos_StrUtils.hpp
"
46
#include "
Teuchos_UnitTestHarness.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
94
TEUCHOS_UNIT_TEST
(
ParameterListModifier
, expandParameters ) {
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
117
TEUCHOS_UNIT_TEST
(
ParameterListModifier
, expandSublists ) {
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
TEST_ASSERT
#define TEST_ASSERT(v1)
Assert the given statement is true.
Definition
Teuchos_LocalTestingHelpers.hpp:71
TEST_EQUALITY
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
Definition
Teuchos_LocalTestingHelpers.hpp:87
TEST_THROW
#define TEST_THROW(code, ExceptType)
Assert that the statement 'code' throws the exception 'ExceptType' (otherwise the test fails).
Definition
Teuchos_LocalTestingHelpers.hpp:198
Teuchos_ParameterListAcceptorHelpers.hpp
Teuchos_ParameterListModifier.hpp
Parameter List Modifier class.
Teuchos_ParameterList.hpp
Templated Parameter List class.
Teuchos_StrUtils.hpp
A std::string utilities class for Teuchos.
Teuchos_UnitTestHarness.hpp
Unit testing support.
TEUCHOS_UNIT_TEST
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
Definition
Teuchos_UnitTestHelpers.hpp:83
Teuchos::Array
Replacement for std::vector that is compatible with the Teuchos Memory Management classes.
Definition
Teuchos_Array.hpp:195
Teuchos::ParameterListModifier
Abstract interface for an object that can modify both a parameter list and the parameter list being u...
Definition
Teuchos_ParameterListModifier.hpp:68
Teuchos::ParameterList
A list of parameters of arbitrary type.
Definition
Teuchos_ParameterList.hpp:133
Teuchos::ParameterList::sublist
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,...
Definition
Teuchos_ParameterList.cpp:272
Teuchos::ParameterList::set
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.
Definition
Teuchos_ParameterList.hpp:955
Teuchos::ParameterList::haveSameValuesSorted
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool haveSameValuesSorted(const ParameterList &list1, const ParameterList &list2, bool verbose=false)
Returns true if two parameter lists have the same values independent of ordering.
Teuchos::RCP
Smart reference counting pointer class for automatic garbage collection.
Definition
Teuchos_RCPDecl.hpp:429
Teuchos::Tuple::tuple
Tuple< T, 1 > tuple(const T &a)
Create a Tuple<T,1>.
Teuchos
Definition
Teuchos_AbstractFactory.hpp:47
Teuchos::rcp
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Definition
Teuchos_RCPDecl.hpp:1266
Generated by
1.17.0