Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
ParameterList_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
43#include "Teuchos_getConst.hpp"
44#include "Teuchos_as.hpp"
48
49
50//
51// Utilities
52//
53
54
55namespace {
56
57
58class DummyValidator : public Teuchos::ParameterEntryValidator
59{
60public:
61
62 const std::string getXMLTypeName() const { return ""; }
63 virtual void printDoc(std::string const& docString, std::ostream &out) const {}
64 virtual ValidStringsList validStringValues() const { return Teuchos::null; }
65 virtual void validate(
66 Teuchos::ParameterEntry const& entry,
67 std::string const& paramName,
68 std::string const& sublistName
69 ) const
70 {}
71};
72
73
74class SimpleModifier : public Teuchos::ParameterListModifier
75{
76public:
77
78 SimpleModifier() : Teuchos::ParameterListModifier("Simple Modifier"){}
79
80 void modify(Teuchos::ParameterList &pl, Teuchos::ParameterList &valid_pl) const
81 {
82 expandSublistsUsingBaseName("SubA", pl, valid_pl);
83 }
84
85 void reconcile(Teuchos::ParameterList &pl) const
86 {
87 // If A and B are less than 0.0 then throw an error
88 TEUCHOS_TEST_FOR_EXCEPTION(pl.get<double>("A") < 0.0 && pl.get<double>("B") < 0.0,
89 std::logic_error, "Parameters A and B can't both be less than 0.0");
90 }
91};
92
93
94class SimpleSubModifier : public Teuchos::ParameterListModifier {
95
96public:
97
98 SimpleSubModifier() : Teuchos::ParameterListModifier("Simple Sub Modifier"){}
99
100 void modify(Teuchos::ParameterList &pl, Teuchos::ParameterList &valid_pl) const
101 {
102 expandSublistsUsingBaseName("SubB", pl, valid_pl);
103 }
104 void reconcile(Teuchos::ParameterList &pl) const
105 {
106 // If E and F are less than 10 then throw an error
107 const int max_CD = 10;
108 TEUCHOS_TEST_FOR_EXCEPTION(pl.get<int>("C") > max_CD && pl.get<int>("D") > max_CD,
109 std::logic_error, "Parameters C and D can't both be greater than 10")
110 }
111};
112
113
114class ReconciliationModifier1 : public Teuchos::ParameterListModifier
115{
116public:
117 ReconciliationModifier1() : Teuchos::ParameterListModifier("Reconciliation Modifier 1"){}
118 void reconcile(Teuchos::ParameterList &pl) const
119 {
120 // This reconciliation routine needs the ReconciliationModifier2's reconcile method
121 // to be run first to create the "e" parameter.
122 Teuchos::ParameterList &subA = pl.sublist("A");
123 pl.set("b", subA.get<int>("e"));
124 }
125};
126
127
128class ReconciliationModifier2 : public Teuchos::ParameterListModifier
129{
130public:
131 ReconciliationModifier2() : Teuchos::ParameterListModifier("Reconciliation Modifier 2"){}
132 void reconcile(Teuchos::ParameterList &pl) const
133 {
134 // Add a convenience parameter
135 pl.set("e", pl.get<int>("c") + pl.get<int>("d"));
136 }
137};
138
139
140} // namespace
141
142
143namespace Teuchos {
144
145
146//
147// Test help utilities
148//
149
150
152{
153 ParameterList PL_Main("PL_Main");
154 const std::string Direction_Doc = "This sublist controls how direction is computed.";
155 ParameterList &PL_Direction = PL_Main.sublist("Direction", false, Direction_Doc);
156 ParameterList &PL_Newton = PL_Direction.sublist("Newton");
157 PL_Newton.sublist("Linear Solver");
158 PL_Main.sublist("Line Search");
159 return PL_Main;
160}
161
162
163
165{
166
167 ParameterList PL_Main_valid("PL_Main_valid");
168 PL_Main_valid.setParameters(createMainPL());
169
170 // Create a validator for the "Nonlinear Solver" parameter
172 "Nonlinear Solver",
173 "Line Search Based",
174 "Selects the type of nonlinear solver to use",
175 tuple<std::string>("Line Search Based","Trust Region Based"),
177 );
178
179 // Create a validator for the parameter "Line Search"->"Polynomial"->"Max Iters"
180 // that accepts an 'int', a 'double' or a 'std::string' value!
186 AcceptedTypes(false).allowInt(true).allowDouble(true).allowString(true)
187 )
188 );
189 PL_Main_valid.sublist("Line Search").sublist("Polynomial").set(
190 "Max Iters",3
191 ,"The maximum number of inner linear search iterations allowed."
193 );
194
195 // Create a validator for the parameter "Direction"->"Newton"->"Linear Solver"->"Tol"
196 // that accepts a 'double' or a 'std::string' value!
201 AcceptedTypes(false).allowDouble(true).allowString(true)
202 )
203 );
204 PL_Main_valid.sublist("Direction",true).sublist("Newton",true)
205 .sublist("Linear Solver",true).set(
206 "Tol", double(1e-5)
207 ,"Select the linear solve tolerance"
209 );
210
211 // Create a validator for the parameter "Elements"
212 // that accepts an 'int', a 'long long' or a 'std::string' value!
217 AcceptedTypes(false).allowInt(true).allowLongLong(true).allowString(true)
218 )
219 );
220 typedef long long LL;
221 PL_Main_valid.set(
222 "Elements", LL(72057594037927936ll) // 2^56
223 ,"Number of finite elements to generate"
225 );
226
227 return PL_Main_valid;
228
229}
230
231
232//
233// Unit tests
234//
235
236
238{
240 TEST_EQUALITY_CONST(pl.name(), "ANONYMOUS");
241 TEST_EQUALITY_CONST(pl.numParams(), 0);
242}
243
244
246{
247 ParameterList pl("someName");
248 TEST_EQUALITY_CONST(pl.name(), "someName");
249 TEST_EQUALITY_CONST(pl.numParams(), 0);
250}
251
252
254{
255 RCP<ParameterList> pl = createParameterList();
257 TEST_EQUALITY_CONST(pl->name(), "ANONYMOUS");
258}
259
260
262{
263 RCP<ParameterList> pl = createParameterList("dummyName");
265 TEST_EQUALITY_CONST(pl->name(), "dummyName");
266}
267
268
270{
272
273 out << "\n";
274 ECHO(pl.set("my int", 3));
275
276 out << "\n";
277 ECHO(const ParameterEntry& my_int_c_param = getConst(pl).getEntry("my int"));
278 TEST_EQUALITY_CONST(my_int_c_param.isUsed(), false);
279 TEST_EQUALITY_CONST(my_int_c_param.isList(), false);
280 TEST_EQUALITY_CONST(my_int_c_param.isDefault(), false);
281 TEST_EQUALITY_CONST(my_int_c_param.docString(), "");
282 TEST_ASSERT(is_null(my_int_c_param.validator()));
284 ECHO(const bool param_isType_int1 = my_int_c_param.isType<int>());
286 ECHO(const bool param_isType_double1 = my_int_c_param.isType<double>());
288
289 out << "\n";
290 ECHO(const ParameterEntry& my_int_param = pl.getEntry("my int"));
291 TEST_EQUALITY_CONST(my_int_param.isUsed(), true);
292
293 out << "\n";
294 ECHO(const int my_int = pl.get<int>("my int"));
296
297}
298
299
301{
303 ECHO(pl.set("my int", 3));
304 ECHO(const int my_int = pl.get<int>("my int"));
306 TEST_EQUALITY_CONST(pl.isParameter("my int"), true);
307 TEST_EQUALITY_CONST(pl.isParameter("Does not Exist"), false);
308 TEST_EQUALITY_CONST(pl.isSublist("my int"), false);
309 TEST_EQUALITY_CONST(pl.isSublist("Does not exist"), false);
310 TEST_EQUALITY_CONST(pl.isType<int>("my int"), true);
311 TEST_EQUALITY_CONST(pl.isType<double>("my int"), false);
312 TEST_EQUALITY_CONST(pl.isType("my int", static_cast<int*>(0)), true);
313 TEST_EQUALITY_CONST(pl.isType("my int", static_cast<double*>(0)), false);
314}
315
316
318{
320 ECHO(pl.sublist("my sublist").set("my int", 3));
321 ECHO(const int my_int = getConst(pl).sublist("my sublist").get<int>("my int"));
323 TEST_EQUALITY_CONST(pl.isParameter("my sublist"), true); // Should be false, but backward compatiable!
324 TEST_EQUALITY_CONST(pl.isParameter("Does not Exist"), false);
325 TEST_EQUALITY_CONST(pl.isSublist("my sublist"), true);
326 TEST_EQUALITY_CONST(pl.isType<ParameterList>("my sublist"), true);
327 TEST_EQUALITY_CONST(pl.isType<double>("my sublist"), false);
328 TEST_EQUALITY_CONST(pl.isType("my sublist", static_cast<ParameterList*>(0)), true);
329 TEST_EQUALITY_CONST(pl.isType("my sublist", static_cast<double*>(0)), false);
330}
331
332
334{
336 ECHO(pl.set("my int", 3, "Some documentation"));
337 ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
338 TEST_EQUALITY_CONST(my_int_param.docString(), "Some documentation");
339 TEST_ASSERT(is_null(my_int_param.validator()));
340}
341
342
344{
346 ECHO(pl.set("my int", 3, "Some documentation", rcp(new DummyValidator)));
347 ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
348 TEST_EQUALITY_CONST(my_int_param.docString(), "Some documentation");
350}
351
352
362
363
365{
368 validator(new Teuchos::EnhancedNumberValidator<int>(0, 1)));
369 TEST_NOTHROW(pl.set("my int", 1, "", validator));
370 TEST_EQUALITY_CONST(pl.numParams(), 1);
371 TEST_EQUALITY_CONST(pl.get<int>("my int"), 1);
373 TEST_EQUALITY_CONST(pl.get<int>("my int"), 1);
374}
375
376
378{
380 ECHO(pl.setEntry("my int", ParameterEntry(as<int>(3), true, true, "Some doc", rcp(new DummyValidator))));
381 ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
382 TEST_EQUALITY_CONST(my_int_param.docString(), "Some doc");
383 ECHO(const int my_int_1 = my_int_param.getValue<int>(0));
385 TEST_EQUALITY_CONST(my_int_param.isUsed(), true);
386 TEST_EQUALITY_CONST(my_int_param.isList(), false); // The isList entry is ignored!
388}
389
390
392{
394 ECHO(pl.setEntry("my int", ParameterEntry(as<int>(3), true, true, "Some doc", rcp(new DummyValidator))));
395 {
396 ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
398 }
399 TEST_EQUALITY_CONST(pl.get<int>("my int"), 3);
400 ECHO(pl.set("my int", 4));
401 TEST_EQUALITY_CONST(pl.get<int>("my int"), 4);
402 {
403 ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
405 }
406}
407
408
410{
412
413 ECHO(char dummy_str_1[] = "dummy str 1");
414 ECHO(pl.set("dummy 1", dummy_str_1));
415 ECHO(const std::string dummy_1 = pl.get<std::string>("dummy 1"));
416 TEST_EQUALITY_CONST(dummy_1, "dummy str 1");
417
418 ECHO(const char dummy_str_const_2[] = "dummy str 2");
419 ECHO(pl.set("dummy 2", dummy_str_const_2));
420 ECHO(const std::string dummy_2 = pl.get<std::string>("dummy 2"));
421 TEST_EQUALITY_CONST(dummy_2, "dummy str 2");
422
423}
424
425
427{
429
430 ECHO(const std::string dummy_str = "dummy str");
431 ECHO(pl.set("my str", dummy_str));
432 ECHO(const std::string my_str = pl.get<std::string>("my str"));
433 TEST_EQUALITY_CONST(my_str, "dummy str");
434
435}
436
437
439{
441 TEST_THROW(pl.getEntry("Does not exist 1"), Exceptions::InvalidParameterName);
442 TEST_THROW(pl.get<int>("Does not exist 2"), Exceptions::InvalidParameterName);
444 TEST_EQUALITY(pl.getPtr<int>("Does not exist 4"), static_cast<int*>(0));
445 TEST_EQUALITY(getConst(pl).getPtr<int>("Does not exist 5"), static_cast<const int*>(0));
446 ECHO(char raw_str[] = "dummy");
447 TEST_EQUALITY_CONST(pl.get("Does not exist 6", raw_str), "dummy");
448 ECHO(const char raw_c_str[] = "dummy");
449 TEST_EQUALITY_CONST(pl.get("Does not exist 7", raw_c_str), "dummy");
450 ECHO(const std::string str = "dummy");
451 TEST_EQUALITY_CONST(pl.get("Does not exist 8", str), "dummy");
452 TEST_THROW(pl.getEntry("Does not exist 9"), Exceptions::InvalidParameterName);
453 TEST_THROW(getConst(pl).getEntry("Does not exist 10"), Exceptions::InvalidParameterName);
454 TEST_EQUALITY(pl.getEntryPtr("Does not exist 11"), static_cast<ParameterEntry*>(0));
455 TEST_EQUALITY(getConst(pl).getEntryPtr("Does not exist 12"), static_cast<const ParameterEntry*>(0));
456 TEST_EQUALITY(pl.getEntryRCP("Does not exist 13"), RCP<ParameterEntry>());
457 TEST_EQUALITY(getConst(pl).getEntryRCP("Does not exist 14"), RCP<const ParameterEntry>());
458}
459
460
462{
464 pl.set("my int", 4);
465 TEST_THROW(pl.get<double>("my int"), Exceptions::InvalidParameterType);
466 // ToDo: Assert the contents of the error message
467}
468
469
471{
473 pl.set("my int", 4);
474 TEST_EQUALITY_CONST(pl.getPtr<int>("Does not Exist"), static_cast<int*>(0));
475 TEST_INEQUALITY_CONST(pl.getPtr<int>("my int"), static_cast<int*>(0));
476 TEST_EQUALITY_CONST(*pl.getPtr<int>("my int"), 4);
477 TEST_EQUALITY_CONST(pl.getPtr<double>("my int"), static_cast<double*>(0));
478 TEST_EQUALITY_CONST(getConst(pl).getPtr<int>("Does not Exist"), static_cast<const int*>(0));
479 TEST_INEQUALITY_CONST(getConst(pl).getPtr<int>("my int"), static_cast<int*>(0));
481 TEST_EQUALITY_CONST(getConst(pl).getPtr<double>("my int"), static_cast<const double*>(0));
482}
483
484
486{
488 pl.set("my int", 4);
489 TEST_EQUALITY_CONST(pl.getEntryRCP("Does not Exist"), null);
490 TEST_INEQUALITY_CONST(pl.getEntryRCP("my int"), null);
491 TEST_EQUALITY_CONST(pl.getEntryRCP("my int")->getValue<int>(0), 4);
492 TEST_EQUALITY_CONST(getConst(pl).getEntryRCP("Does not Exist"), null);
493 TEST_INEQUALITY_CONST(getConst(pl).getEntryRCP("my int"), null);
494 TEST_EQUALITY_CONST(getConst(pl).getEntryRCP("my int")->getValue<int>(0), 4);
495}
496
497
498// Test nonconstFind()
499
500// Test find()
501
502
504{
506 ECHO(int &my_int = pl.get("my int", 3));
508 TEST_EQUALITY_CONST(pl.get<int>("my int"), 3);
509 ECHO(my_int = 5);
510 TEST_EQUALITY_CONST(pl.get<int>("my int"), 5);
511}
512
513
515{
517 TEST_EQUALITY_CONST(pl.numParams(), 0);
518 ECHO(pl.set("my int", 2));
519 TEST_EQUALITY_CONST(pl.numParams(), 1);
520 TEST_EQUALITY_CONST(pl.get<int>("my int"), 2);
521 ECHO(const bool param_was_removed_1 = pl.remove("my int"));
523 TEST_EQUALITY_CONST(pl.numParams(), 0);
526 ECHO(const bool param_was_removed_2 = pl.remove("my int", false));
528}
529
530
532{
533 ParameterList pl("Base");
534 ECHO(pl.sublist("my sublist"));
535 ECHO(const ParameterEntry &sublistParam = pl.getEntry("my sublist"));
536 TEST_EQUALITY_CONST(sublistParam.isUsed(), false);
537 TEST_EQUALITY_CONST(sublistParam.isList(), true);
538 TEST_EQUALITY_CONST(sublistParam.isDefault(), false);
539 TEST_EQUALITY_CONST(sublistParam.docString(), "");
540 TEST_EQUALITY_CONST(sublistParam.getValue<ParameterList>(0).name(), "Base->my sublist");
541}
542
543
545{
546 ParameterList pl("Base");
547 ECHO(pl.sublist("my sublist", false, "My great sublist"));
548 ECHO(const ParameterEntry &sublistParam = pl.getEntry("my sublist"));
549 TEST_EQUALITY_CONST(sublistParam.isUsed(), false);
550 TEST_EQUALITY_CONST(sublistParam.isList(), true);
551 TEST_EQUALITY_CONST(sublistParam.isDefault(), false);
552 TEST_EQUALITY_CONST(sublistParam.docString(), "My great sublist");
553 TEST_EQUALITY_CONST(sublistParam.getValue<ParameterList>(0).name(), "Base->my sublist");
554}
555
556
558{
559 ParameterList pl("Base");
560 TEST_THROW(pl.sublist("my sublist", true), Exceptions::InvalidParameterName);
561 // ToDo: Examine the actual structure of the error message
562}
563
564
566{
567 ParameterList pl("Base");
568 ECHO(pl.set("my sublist", 1)); // Not a sublist!
569 TEST_THROW(pl.sublist("my sublist"), Exceptions::InvalidParameterType);
570 // ToDo: Examine the actual structure of the error message
571}
572
573
575{
576 ParameterList pl("Base");
577 ECHO(pl.sublist("my sublist").set("my int", 2));
578 ECHO(const int my_int = pl.sublist("my sublist").get<int>("my int"));
580}
581
582
584{
585 ParameterList pl("Base");
586 ECHO(pl.sublist("my sublist").set("my int", 2));
587 ECHO(const int my_int = getConst(pl).sublist("my sublist").get<int>("my int"));
589}
590
591
593{
594 ParameterList pl("Base");
596 // ToDo: Examine the actual structure of the error message
597}
598
599
601{
602 ParameterList pl("Base");
603 ECHO(pl.set("my sublist", 1)); // Not a sublist!
605 // ToDo: Examine the actual structure of the error message
606}
607
608
610{
611 ParameterList PL_Main("PL_Main");
612 const std::string Direction_Doc = "This sublist controls how direction is computed.";
613 ParameterList& PL_Direction = PL_Main.sublist("Direction", false, Direction_Doc);
614 ParameterList& PL_LineSearch = PL_Main.sublist("Line Search");
615 out << "PL_Main=\n" << PL_Main << "\n";
616 TEST_EQUALITY_CONST(PL_Main.name(), "PL_Main");
617 TEST_EQUALITY_CONST(PL_Main.isSublist("Direction"), true);
618 TEST_EQUALITY_CONST(PL_Main.isSublist("Line Search"), true);
619 ECHO(const ParameterList& PL_Direction_2 = getConst(PL_Main).sublist("Direction"));
621 ECHO(const ParameterList& PL_LineSearch_2 = getConst(PL_Main).sublist("Line Search"));
623 TEST_EQUALITY_CONST(getConst(PL_Main).sublist("Direction").name(), "PL_Main->Direction");
624 TEST_EQUALITY_CONST(PL_Direction.name(), "PL_Main->Direction");
625 TEST_EQUALITY_CONST(PL_LineSearch.name(), "PL_Main->Line Search");
626}
627
628
630{
631 // This is the scenario in the orginal testing program
632 ParameterList PL_Main("PL_Main");
633 const std::string Direction_Doc = "This sublist controls how direction is computed.";
634 ParameterList &PL_Direction = PL_Main.sublist("Direction", false, Direction_Doc);
635 ParameterList &PL_Newton = PL_Direction.sublist("Newton");
636 ParameterList &PL_LinSol = PL_Newton.sublist("Linear Solver");
637 ParameterList &PL_LineSearch = PL_Main.sublist("Line Search");
638 out << "PL_Main=\n" << PL_Main << "\n";
639 TEST_EQUALITY_CONST(PL_Main.name(), "PL_Main");
640 TEST_EQUALITY_CONST(PL_Main.isSublist("Direction"), true);
641 ECHO(const ParameterList& PL_Direction_2 = getConst(PL_Main).sublist("Direction"));
643 TEST_EQUALITY_CONST(getConst(PL_Main).sublist("Direction").name(), "PL_Main->Direction");
644 TEST_EQUALITY_CONST(PL_Direction.name(), "PL_Main->Direction");
645 TEST_EQUALITY_CONST(PL_Direction.isSublist("Newton"), true);
646 TEST_EQUALITY_CONST(PL_Newton.isSublist("Linear Solver"), true);
647 TEST_EQUALITY_CONST(PL_Newton.name(), "PL_Main->Direction->Newton");
648 TEST_EQUALITY_CONST(PL_Newton.isSublist("Linear Solver"), true);
649 TEST_EQUALITY_CONST(PL_LinSol.name(), "PL_Main->Direction->Newton->Linear Solver");
650 TEST_EQUALITY_CONST(PL_Main.isSublist("Line Search"), true);
651 TEST_EQUALITY_CONST(PL_LineSearch.name(), "PL_Main->Line Search");
652}
653
654
656{
657 ECHO(ParameterList pl1("A"));
658 ECHO(pl1.set("my int", 2));
660 TEST_EQUALITY_CONST(pl2.name(), "A");
661 TEST_EQUALITY_CONST(pl2.get<int>("my int"), 2);
662}
663
664
666{
667 ECHO(ParameterList pl1("A"));
668 ECHO(pl1.set("my int", 2));
670 ECHO(const ParameterList &pl2_ref = pl2 = pl1);
672 TEST_EQUALITY_CONST(pl2.name(), "A");
673 TEST_EQUALITY_CONST(pl2.get<int>("my int"), 2);
674}
675
676
678{
681 pl.set("c", 1);
682 pl.set("a", 2);
683 pl.set("b", 3);
684 ConstIter pl_itr = pl.begin();
685 TEST_EQUALITY_CONST(pl_itr->first, "c");
686 TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 1);
687 ECHO(++pl_itr);
688 TEST_EQUALITY_CONST(pl_itr->first, "a");
689 TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 2);
690 ECHO(++pl_itr);
691 TEST_EQUALITY_CONST(pl_itr->first, "b");
692 TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 3);
693 ECHO(++pl_itr);
695}
696
697
699{
701 ParameterList pl("base");
702 pl.set("c", 1);
703 pl.sublist("a");
704 pl.set("b", 3);
705 ConstIter pl_itr = pl.begin();
706 TEST_EQUALITY_CONST(pl_itr->first, "c");
707 TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 1);
708 ECHO(++pl_itr);
709 TEST_EQUALITY_CONST(pl_itr->first, "a");
710 TEST_EQUALITY_CONST(pl_itr->second.getValue<ParameterList>(0).name(), "base->a");
711 ECHO(++pl_itr);
712 TEST_EQUALITY_CONST(pl_itr->first, "b");
713 TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 3);
714 ECHO(++pl_itr);
716}
717
718// Test iterator access after removing params
719
720// Test iterator access after removing sublists
721
722
724{
725 // An empty list should not be equal to a full list
728 TEST_ASSERT( A == B );
729 A.set("Hello","World");
730 TEST_ASSERT( A != B );
731 B.set("Hello","World");
732 TEST_ASSERT( A == B );
733}
734
735
737{
738 // Sublists with different names should not be equal
741 A.sublist("Bob");
742 B.sublist("Tom");
743 TEST_ASSERT( A != B );
744}
745
746
748{
751 A.set("A","a");
752 A.set("B","b");
753 A.set("C","c");
754 A.print(out);
755
756 B.set("A","a");
757 B.set("B","b");
758 B.print(out);
759
760 TEST_ASSERT( A != B );
761
762 B.set("C","c");
763 TEST_ASSERT( A == B );
764}
765
766
768{
771 TEST_ASSERT( haveSameValues(A,B) );
772 A.set("a",1);
773 TEST_ASSERT( !haveSameValues(A,B) );
774 A.set("b",2);
775 B.set("a",1);
776 B.set("b",2);
777 TEST_ASSERT( haveSameValues(A,B) );
778}
779
780
782{
785 A.sublist("Smith").set("People",4);
786 B.sublist("Jones").set("People",4);
787 TEST_ASSERT( !haveSameValues(A,B) ); // sublist names matter
788}
789
790
792{
794 A.set("a",1);
795 A.set("b",2);
796 // Create second list with the same entries but different order
797 B.set("b",2);
798 B.set("a",1);
799 TEST_ASSERT( haveSameValuesSorted(A,B) );
800 B.set("c",3);
801 TEST_ASSERT( !haveSameValuesSorted(A,B) ); // check for length
802}
803
804
806{
808 ParameterList &asublist = A.sublist("A");
809 asublist.set("a",1);
810 asublist.set("b",2);
811 ParameterList &bsublist = B.sublist("A");
812 bsublist.set("a",1);
813 bsublist.set("b",2);
814 TEST_ASSERT( haveSameValuesSorted(A,B) );
815 asublist.set("c",3);
816 bsublist.set("c",4);
817 TEST_ASSERT( !haveSameValuesSorted(A,B) );
818}
819
820
827
828
829TEUCHOS_UNIT_TEST( ParameterList, validateParametersAndSetDefaults )
830{
833 ECHO(PL_Main.validateParametersAndSetDefaults(PL_Main_valid));
835 rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<int> >(
836 PL_Main.getEntry("Nonlinear Solver").validator(), true ) );
837}
838
839
841{
844 ECHO(PL_Main.set("Nonlinear Solver", "Line Search Based"));
845 ECHO(PL_Main.validateParametersAndSetDefaults(PL_Main_valid));
846 ECHO(const int lineSearchValue = getIntegralValue<int>(PL_Main, "Nonlinear Solver"));
848 ECHO(PL_Main.set("Nonlinear Solver", "Trust Region Based"));
849 ECHO(const int trustRegionValue = getIntegralValue<int>(PL_Main, "Nonlinear Solver"));
851}
852
853
855 ParameterList pl = ParameterList("Parameter List with Scalar Parameter");
856 const int a_val = 2, b_val = 3;
857 pl.set("A", a_val);
858 pl.set("B", b_val);
859 replaceParameterWithArray<int>("A", "A array", pl);
861 ParameterList expected_pl = ParameterList("Parameter List with Array Parameter");
863 expected_pl.set("A array", a_array);
864 expected_pl.set("B", b_array);
865 TEST_ASSERT(haveSameValuesSorted(expected_pl, pl, true));
866 // Throw an error when trying to overwrite a parameter that already exists but
867 // doesn't have the same name.
868 pl.set("C", 1);
869 TEST_THROW(replaceParameterWithArray<int>("C", "B", pl), std::logic_error);
870 pl.print();
871}
872
873
875{
876 RCP<SimpleModifier> modifier = rcp(new SimpleModifier());
877 ParameterList valid_pl("My Valid Parameter List with a Modifier", modifier);
878 //valid_pl before modification
879 // A: 1.0
880 // B: 0.1
881 // SubA:
882 // C: 1
883 valid_pl.set("A", 1.0);
884 valid_pl.set("B", 0.1);
885 valid_pl.sublist("SubA").set("C", 1);
886 ParameterList pl("My Parameter List");
887 pl.set("A", 5.0);
888 pl.set("B", -0.1);
889 pl.sublist("SubA 1").set("C", 3);
890 pl.sublist("SubA 2").set("C", 4);
892 expected_valid_pl.remove("SubA");
893 expected_valid_pl.sublist("SubA 1").set("C", 1);
894 expected_valid_pl.sublist("SubA 2").set("C", 1);
895 pl.modifyParameterList(valid_pl);
896 //valid_pl after modification
897 // A: 1.0
898 // B: 0.1
899 // SubA 1:
900 // C: 1
901 // SubA 2:
902 // C: 1
904// std::cout << haveSameValuesSorted(expected_valid_pl, valid_pl, true) << std::endl;
905 pl.validateParametersAndSetDefaults(valid_pl);
906 TEST_NOTHROW(pl.reconcileParameterList(valid_pl));
907 pl.set("A", -1.0);
908 TEST_THROW(pl.reconcileParameterList(valid_pl), std::logic_error);
909 // Test the copy constructor
912}
913
914
916 Teuchos::RCP<SimpleModifier> modifier = Teuchos::rcp(new SimpleModifier());
918 // The unmodified (template-like) validation parameter list
919 ParameterList valid_pl("valid_pl", modifier);
920 valid_pl.set("A", 1.0);
921 valid_pl.set("B", 1.0);
922 valid_pl.sublist("SubA").setModifier(sub_modifier);
923 valid_pl.sublist("SubA").set("C", 3);
924 valid_pl.sublist("SubA").set("D", 4);
925 valid_pl.sublist("SubA").sublist("SubB").set("E", 10);
926 valid_pl.sublist("SubA").sublist("SubB").set("F", 11);
927 // The user's input parameter list
928 ParameterList pl("pl");
929 pl.set("A", 1.0);
930 pl.set("B", 2.0);
931 pl.sublist("SubA 1").set("C", 3);
932 pl.sublist("SubA 1").set("D", 4);
933 pl.sublist("SubA 1").sublist("SubB 1").set("E", 51);
934 pl.sublist("SubA 1").sublist("SubB 1").set("F", 61);
935 pl.sublist("SubA 1").sublist("SubB 2").set("E", 52);
936 pl.sublist("SubA 1").sublist("SubB 2").set("F", 62);
937 pl.sublist("SubA 2").set("C", 3);
938 pl.sublist("SubA 2").set("D", 4);
939 pl.sublist("SubA 2").sublist("SubB 3").set("E", 53);
940 pl.sublist("SubA 2").sublist("SubB 3").set("F", 63);
941 // The expanded valid parameter list after modification
942 ParameterList expected_valid_pl("valid_pl_expanded");
943 expected_valid_pl.set("A", 1.0);
944 expected_valid_pl.set("B", 1.0);
945 expected_valid_pl.sublist("SubA 1").set("C", 3);
946 expected_valid_pl.sublist("SubA 1").set("D", 4);
947 expected_valid_pl.sublist("SubA 1").sublist("SubB 1").set("E", 10);
948 expected_valid_pl.sublist("SubA 1").sublist("SubB 1").set("F", 11);
949 expected_valid_pl.sublist("SubA 1").sublist("SubB 2").set("E", 10);
950 expected_valid_pl.sublist("SubA 1").sublist("SubB 2").set("F", 11);
951 expected_valid_pl.sublist("SubA 2").set("C", 3);
952 expected_valid_pl.sublist("SubA 2").set("D", 4);
953 expected_valid_pl.sublist("SubA 2").sublist("SubB 3").set("E", 10);
954 expected_valid_pl.sublist("SubA 2").sublist("SubB 3").set("F", 11);
955 // Expand the validation parameter list based on the user's input parameter list
956 pl.modifyParameterList(valid_pl);
957 // Modified parameter lists aren't equal because they don't have the same modifiers
959 // Test that they are the same except for the modifiers
960 TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
961 // Check the equality of the modifiers
962 expected_valid_pl.setModifier(modifier);
963 expected_valid_pl.sublist("SubA 1", true).setModifier(sub_modifier);
964 expected_valid_pl.sublist("SubA 2", true).setModifier(sub_modifier);
965 TEST_ASSERT(haveSameModifiers(valid_pl, expected_valid_pl));
966 // Now test the recursive reconciliation
967 TEST_NOTHROW(pl.reconcileParameterList(valid_pl));
968 pl.sublist("SubA 1").set("C", 11);
969 pl.sublist("SubA 1").set("D", 11);
970 TEST_THROW(pl.reconcileParameterList(valid_pl), std::logic_error);
971}
972
973
975 Teuchos::RCP<SimpleModifier> modifier = Teuchos::rcp(new SimpleModifier());
977 // The unmodified (template-like) validation parameter list
978 ParameterList valid_pl("valid_pl", modifier);
979 valid_pl.set("A", 1.0);
980 valid_pl.set("B", 1.0);
981 valid_pl.sublist("SubA").setModifier(sub_modifier);
982 valid_pl.sublist("SubA").set("C", 3.0);
983 valid_pl.sublist("SubA").set("D", 4);
984 valid_pl.sublist("SubA").sublist("SubB").set("E", 10);
985 valid_pl.sublist("SubA").sublist("SubB").set("F", 11);
986 // The user's input parameter list
987 ParameterList pl("pl");
988 pl.set("A", 1.0);
989 pl.set("B", 2.0);
990 pl.sublist("SubA 1").set("C", 3);
991 pl.sublist("SubA 1").set("D", 4);
992 pl.sublist("SubA 1").sublist("SubB").set("E", 53);
993 pl.sublist("SubA 1").sublist("SubB").set("E", 63);
994 // The expanded valid parameter list after modification
996 expected_valid_pl.set("A", 1.0);
997 expected_valid_pl.set("B", 1.0);
998 expected_valid_pl.sublist("SubA 1").set("C", 3.0);
999 expected_valid_pl.sublist("SubA 1").set("D", 4);
1000 expected_valid_pl.sublist("SubA 1").sublist("SubB").set("E", 10);
1001 expected_valid_pl.sublist("SubA 1").sublist("SubB").set("F", 11);
1002 // Make a copy of the user's input parameter list before it is validated
1004 // The float validator will cast integers in `pl` to floats
1007 AnyNumberParameterEntryValidator::AcceptedTypes(false).allowInt(true).allowDouble(true)));
1008 valid_pl.sublist("SubA").getEntry("C").setValidator(float_validator);
1009 // Don't modify `SubA`
1010 valid_pl.sublist("SubA").disableRecursiveModification();
1011 pl.modifyParameterList(valid_pl);
1012 TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
1013 // Don't validate `SubA 1`
1014 valid_pl.sublist("SubA 1").disableRecursiveValidation();
1015 pl.validateParametersAndSetDefaults(valid_pl);
1016 // If we were to validate `SubA 1` then parameter C would turn into a float and the following test would fail
1017 TEST_ASSERT(haveSameValuesSorted(pl, copy_pl, true));
1018}
1019
1020
1022 ParameterList valid_pl("valid_pl");
1023 valid_pl.set("A", 1);
1024 valid_pl.sublist("SubA").set("B", 1);
1025 ParameterList pl("pl");
1026 pl.set("A", 1.0);
1027 pl.sublist("SubA").set("B", 2);
1028 ParameterList validated_pl("valid_pl");
1029 validated_pl.set("A", 1.0);
1030 validated_pl.sublist("SubA").set("B", 2.0);
1031 // The float validator will cast integers in `pl` to floats
1034 AnyNumberParameterEntryValidator::AcceptedTypes(false).allowInt(true).allowDouble(true)));
1035 valid_pl.getEntry("A").setValidator(float_validator);
1036 valid_pl.sublist("SubA").getEntry("B").setValidator(float_validator);
1037 pl.validateParametersAndSetDefaults(valid_pl);
1038 // All of the integers in `pl` should be casted to floats in `validated_pl`
1039 TEST_ASSERT(haveSameValuesSorted(validated_pl, pl, true));
1040}
1041
1042
1044 Teuchos::RCP<ReconciliationModifier1> modifier1 = Teuchos::rcp(new ReconciliationModifier1());
1045 Teuchos::RCP<ReconciliationModifier2> modifier2 = Teuchos::rcp(new ReconciliationModifier2());
1046 ParameterList valid_pl("valid_pl");
1047 valid_pl.set("a", 1);
1048 valid_pl.setModifier(modifier1);
1049 valid_pl.sublist("A").setModifier(modifier2);
1050 valid_pl.sublist("A").set("c", 1);
1051 valid_pl.sublist("A").set("d", 1);
1052 ParameterList pl("pl");
1053 pl.set("a", 1);
1054 pl.sublist("A").set("c", 2);
1055 pl.sublist("A").set("d", 3);
1056 ParameterList reconciled_pl("reconciled_pl");
1057 reconciled_pl.set("a", 1);
1058 reconciled_pl.set("b", 5);
1059 reconciled_pl.sublist("A").set("c", 2);
1060 reconciled_pl.sublist("A").set("d", 3);
1061 reconciled_pl.sublist("A").set("e", 5);
1062 pl.reconcileParameterList(valid_pl);
1063 TEST_ASSERT(haveSameValuesSorted(reconciled_pl, pl, true));
1064}
1065
1066
1068 ParameterList valid_pl("valid_pl");
1069 valid_pl.set("a", 0.);
1070 valid_pl.sublist("A").set("b", 0.);
1071 valid_pl.sublist("A").set("c", 0.);
1072 valid_pl.sublist("A").sublist("AA").set("d", 0.);
1073 ParameterList pl("pl");
1074 pl.set("a", 1);
1075 pl.sublist("A").set("b", 2);
1076 pl.sublist("A").set("c", 3);
1077 pl.sublist("A").sublist("AA").set("d", 4);
1078 ParameterList validated_pl("validated_pl");
1079 validated_pl.set("a", 1.);
1080 validated_pl.sublist("A").set("b", 2.);
1081 validated_pl.sublist("A").set("c", 3.);
1082 validated_pl.sublist("A").sublist("AA").set("d", 4.);
1083 // The float validator will cast integers in `pl` to floats
1086 AnyNumberParameterEntryValidator::AcceptedTypes(false).allowInt(true).allowDouble(true)));
1087 valid_pl.recursivelySetValidator<double>(float_validator, 1);
1088 // This should fail since we only set the float validator on the top level of `valid_pl`
1089 TEST_THROW(pl.validateParametersAndSetDefaults(valid_pl), std::logic_error);
1090 // Now attach the validator to every double
1091 valid_pl.recursivelySetValidator<double>(float_validator);
1092 pl.validateParametersAndSetDefaults(valid_pl);
1093 TEST_ASSERT(haveSameValuesSorted(validated_pl, pl, true));
1094}
1095
1096// TODO: test printing of modifiers
1097// valid_pl.print(std::cout, ParameterList::PrintOptions().showDoc(true).indent(2).showTypes(true));
1098
1100 ParameterList valid_pl("valid_pl");
1101 valid_pl.set("a", 0.);
1102
1103 {
1104 ParameterList pl("pl");
1105 pl.set("a", 1.);
1106 pl.validateParametersAndSetDefaults(valid_pl);
1108 printOptions.showDefault(false);
1109 std::ostringstream ss;
1110 pl.print(ss, printOptions);
1111 std::cout << ss.str();
1112 TEST_ASSERT(ss.str().size() > 0);
1113 }
1114
1115 {
1116 ParameterList pl("pl");
1117 pl.validateParametersAndSetDefaults(valid_pl);
1119 std::ostringstream ss;
1120 pl.print(ss, printOptions);
1121 std::cout << ss.str();
1122 TEST_ASSERT(ss.str().size() > 0);
1123
1124 ss.str("");
1125 printOptions.showDefault(false);
1126 pl.print(ss, printOptions);
1127 std::cout << ss.str();
1128 TEST_ASSERT(ss.str().size() == 0);
1129 }
1130}
1131
1132} // namespace Teuchos
1133
1134
1135
#define TEST_ASSERT(v1)
Assert the given statement is true.
#define TEST_INEQUALITY_CONST(v1, v2)
Assert the inequality of v1 and constant v2.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_NOTHROW(code)
Asserr that the statement 'code' does not thrown any excpetions.
#define TEST_THROW(code, ExceptType)
Assert that the statement 'code' throws the exception 'ExceptType' (otherwise the test fails).
#define ECHO(statement)
Echo the given statement before it is executed.
#define TEST_ITER_EQUALITY(iter1, iter2)
Assert that two iterators are equal.
Parameter List Modifier class.
Templated Parameter List class.
Unit testing support.
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
Definition of Teuchos::as, for conversions between types.
Standard implementation of a ParameterEntryValidator that accepts numbers from a number of different ...
int size(const Comm< Ordinal > &comm)
Get the number of processes in the communicator.
Abstract interface for an object that can validate a ParameterEntry's value.
This object is held as the "value" in the Teuchos::ParameterList std::map.
Abstract interface for an object that can modify both a parameter list and the parameter list being u...
Utility class for setting and passing in print options.
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,...
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.
const std::string & name() const
The name of this ParameterList.
T & get(const std::string &name, T def_value)
Return the parameter's value, or the default value if it is not there.
Concrete serial communicator subclass.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
Definition PackageA.cpp:3
Definition PackageB.cpp:3
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
ParameterList createMainPL()
ParameterList createValidMainPL()