Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
55 namespace {
56 
57 
58 class DummyValidator : public Teuchos::ParameterEntryValidator
59 {
60 public:
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 
74 class SimpleModifier : public Teuchos::ParameterListModifier
75 {
76 public:
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 
94 class SimpleSubModifier : public Teuchos::ParameterListModifier {
95 
96 public:
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 
114 class ReconciliationModifier1 : public Teuchos::ParameterListModifier
115 {
116 public:
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 
128 class ReconciliationModifier2 : public Teuchos::ParameterListModifier
129 {
130 public:
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 
143 namespace 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
171  setStringToIntegralParameter<int>(
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"),
176  &PL_Main_valid
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!
183  linesearchMaxItersValidator = rcp(
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."
192  ,linesearchMaxItersValidator
193  );
194 
195  // Create a validator for the parameter "Direction"->"Newton"->"Linear Solver"->"Tol"
196  // that accepts a 'double' or a 'std::string' value!
198  linSolveTolValidator = rcp(
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"
208  ,linSolveTolValidator
209  );
210 
211  // Create a validator for the parameter "Elements"
212  // that accepts an 'int', a 'long long' or a 'std::string' value!
214  elementsValidator = rcp(
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"
224  ,elementsValidator
225  );
226 
227  return PL_Main_valid;
228 
229 }
230 
231 
232 //
233 // Unit tests
234 //
235 
236 
237 TEUCHOS_UNIT_TEST( ParameterList, construct_default )
238 {
239  ParameterList pl;
240  TEST_EQUALITY_CONST(pl.name(), "ANONYMOUS");
242 }
243 
244 
245 TEUCHOS_UNIT_TEST( ParameterList, construct_withName )
246 {
247  ParameterList pl("someName");
248  TEST_EQUALITY_CONST(pl.name(), "someName");
250 }
251 
252 
253 TEUCHOS_UNIT_TEST( ParameterList, createParameterList_empty )
254 {
255  RCP<ParameterList> pl = createParameterList();
256  TEST_ASSERT(nonnull(pl));
257  TEST_EQUALITY_CONST(pl->name(), "ANONYMOUS");
258 }
259 
260 
261 TEUCHOS_UNIT_TEST( ParameterList, createParameterList_withName )
262 {
263  RCP<ParameterList> pl = createParameterList("dummyName");
264  TEST_ASSERT(nonnull(pl));
265  TEST_EQUALITY_CONST(pl->name(), "dummyName");
266 }
267 
268 
270 {
271  ParameterList pl;
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()));
283  TEST_EQUALITY_CONST(getValue<int>(my_int_c_param), 3);
284  ECHO(const bool param_isType_int1 = my_int_c_param.isType<int>());
285  TEST_EQUALITY_CONST(param_isType_int1, true);
286  ECHO(const bool param_isType_double1 = my_int_c_param.isType<double>());
287  TEST_EQUALITY_CONST(param_isType_double1, false);
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"));
295  TEST_EQUALITY_CONST(my_int, 3);
296 
297 }
298 
299 
300 TEUCHOS_UNIT_TEST( ParameterList, setParametersWithModifier )
301 {
303  RCP<ParameterListModifier> modifier1 = rcp(new ParameterListModifier("Modifier 1"));
304  RCP<ParameterListModifier> modifier2 = rcp(new ParameterListModifier("Modifier 2"));
305  //pl1:
306  // A: 1.0
307  // SubA: # with `modifier1`
308  // B: 2
309  ParameterList pl1("pl");
310  pl1.set("A", 1.0);
311  pl1.sublist("SubA", modifier1).set("B", 2);
312  ParameterList pl2("pl");
313  pl2.setParameters(pl1);
314  // Show that all values and the modifier were copied from `pl1` to `pl2`
315  TEST_EQUALITY(*pl2.sublist("SubA").getModifier(), *modifier1);
316  TEST_EQUALITY(pl1, pl2);
317  pl1.sublist("SubA").setModifier(Teuchos::null);
318  TEST_ASSERT(is_null(pl1.sublist("SubA").getModifier()));
319  TEST_EQUALITY(*pl2.sublist("SubA").getModifier(), *modifier1);
320  // Now test that `setParametersNotAlreadySet` has the correct behavior
321  pl1.sublist("SubA").setModifier(modifier1);
322  pl1.sublist("SubB", modifier2).set("C", 3);
324  TEST_EQUALITY(*pl2.sublist("SubB").getModifier(), *modifier2);
325  TEST_EQUALITY(pl1, pl2);
326  // Test that sublists with their modifiers and parameters are correctly
327  // overwritten in `setParameters`
328  pl1 = ParameterList();
329  pl1.sublist("SubA", modifier1).set("A", 1);
330  pl2 = ParameterList();
331  pl2.sublist("SubA", modifier2).set("B", 2);
332  pl2.setParameters(pl1);
333  // pl2 should look just like pl1 except with the extra "B" parameter
334  ParameterList pl_expected = ParameterList();
335  pl_expected.sublist("SubA", modifier1).set("B", 2).set("A", 1);
336  TEST_EQUALITY(pl_expected, pl2);
337 }
338 
339 
340 TEUCHOS_UNIT_TEST( ParameterList, param_isParameter_isSublist_isType )
341 {
342  ParameterList pl;
343  ECHO(pl.set("my int", 3));
344  ECHO(const int my_int = pl.get<int>("my int"));
345  TEST_EQUALITY_CONST(my_int, 3);
346  TEST_EQUALITY_CONST(pl.isParameter("my int"), true);
347  TEST_EQUALITY_CONST(pl.isParameter("Does not Exist"), false);
348  TEST_EQUALITY_CONST(pl.isSublist("my int"), false);
349  TEST_EQUALITY_CONST(pl.isSublist("Does not exist"), false);
350  TEST_EQUALITY_CONST(pl.isType<int>("my int"), true);
351  TEST_EQUALITY_CONST(pl.isType<double>("my int"), false);
352  TEST_EQUALITY_CONST(pl.isType("my int", static_cast<int*>(0)), true);
353  TEST_EQUALITY_CONST(pl.isType("my int", static_cast<double*>(0)), false);
354 }
355 
356 
357 TEUCHOS_UNIT_TEST( ParameterList, sublist_isParameter_isSublist_isType )
358 {
359  ParameterList pl;
360  ECHO(pl.sublist("my sublist").set("my int", 3));
361  ECHO(const int my_int = getConst(pl).sublist("my sublist").get<int>("my int"));
362  TEST_EQUALITY_CONST(my_int, 3);
363  TEST_EQUALITY_CONST(pl.isParameter("my sublist"), true); // Should be false, but backward compatiable!
364  TEST_EQUALITY_CONST(pl.isParameter("Does not Exist"), false);
365  TEST_EQUALITY_CONST(pl.isSublist("my sublist"), true);
366  TEST_EQUALITY_CONST(pl.isType<ParameterList>("my sublist"), true);
367  TEST_EQUALITY_CONST(pl.isType<double>("my sublist"), false);
368  TEST_EQUALITY_CONST(pl.isType("my sublist", static_cast<ParameterList*>(0)), true);
369  TEST_EQUALITY_CONST(pl.isType("my sublist", static_cast<double*>(0)), false);
370 }
371 
372 
374 {
375  ParameterList pl;
376  ECHO(pl.set("my int", 3, "Some documentation"));
377  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
378  TEST_EQUALITY_CONST(my_int_param.docString(), "Some documentation");
379  TEST_ASSERT(is_null(my_int_param.validator()));
380 }
381 
382 
383 TEUCHOS_UNIT_TEST( ParameterList, set_doc_validator )
384 {
385  ParameterList pl;
386  ECHO(pl.set("my int", 3, "Some documentation", rcp(new DummyValidator)));
387  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
388  TEST_EQUALITY_CONST(my_int_param.docString(), "Some documentation");
389  TEST_NOTHROW(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true));
390 }
391 
392 
393 TEUCHOS_UNIT_TEST( ParameterList, set_invalid_int_first )
394 {
395  ParameterList pl;
397  validator(new Teuchos::EnhancedNumberValidator<int>(0, 1)));
398  TEST_THROW(pl.set("my int", -1, "", validator),
401 }
402 
403 
404 TEUCHOS_UNIT_TEST( ParameterList, set_invalid_int_second )
405 {
406  ParameterList pl;
408  validator(new Teuchos::EnhancedNumberValidator<int>(0, 1)));
409  TEST_NOTHROW(pl.set("my int", 1, "", validator));
411  TEST_EQUALITY_CONST(pl.get<int>("my int"), 1);
413  TEST_EQUALITY_CONST(pl.get<int>("my int"), 1);
414 }
415 
416 
418 {
419  ParameterList pl;
420  ECHO(pl.setEntry("my int", ParameterEntry(as<int>(3), true, true, "Some doc", rcp(new DummyValidator))));
421  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
422  TEST_EQUALITY_CONST(my_int_param.docString(), "Some doc");
423  ECHO(const int my_int_1 = my_int_param.getValue<int>(0));
424  TEST_EQUALITY_CONST(my_int_1, 3);
425  TEST_EQUALITY_CONST(my_int_param.isUsed(), true);
426  TEST_EQUALITY_CONST(my_int_param.isList(), false); // The isList entry is ignored!
427  TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null);
428 }
429 
430 
431 TEUCHOS_UNIT_TEST( ParameterList, set_int_twice_keep_validator )
432 {
433  ParameterList pl;
434  ECHO(pl.setEntry("my int", ParameterEntry(as<int>(3), true, true, "Some doc", rcp(new DummyValidator))));
435  {
436  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
437  TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null);
438  }
439  TEST_EQUALITY_CONST(pl.get<int>("my int"), 3);
440  ECHO(pl.set("my int", 4));
441  TEST_EQUALITY_CONST(pl.get<int>("my int"), 4);
442  {
443  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
444  TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null);
445  }
446 }
447 
448 
449 TEUCHOS_UNIT_TEST( ParameterList, set_get_char_str )
450 {
451  ParameterList pl;
452 
453  ECHO(char dummy_str_1[] = "dummy str 1");
454  ECHO(pl.set("dummy 1", dummy_str_1));
455  ECHO(const std::string dummy_1 = pl.get<std::string>("dummy 1"));
456  TEST_EQUALITY_CONST(dummy_1, "dummy str 1");
457 
458  ECHO(const char dummy_str_const_2[] = "dummy str 2");
459  ECHO(pl.set("dummy 2", dummy_str_const_2));
460  ECHO(const std::string dummy_2 = pl.get<std::string>("dummy 2"));
461  TEST_EQUALITY_CONST(dummy_2, "dummy str 2");
462 
463 }
464 
465 
467 {
468  ParameterList pl;
469 
470  ECHO(const std::string dummy_str = "dummy str");
471  ECHO(pl.set("my str", dummy_str));
472  ECHO(const std::string my_str = pl.get<std::string>("my str"));
473  TEST_EQUALITY_CONST(my_str, "dummy str");
474 
475 }
476 
477 
478 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_param )
479 {
480  ParameterList pl;
481  TEST_THROW(pl.getEntry("Does not exist 1"), Exceptions::InvalidParameterName);
482  TEST_THROW(pl.get<int>("Does not exist 2"), Exceptions::InvalidParameterName);
483  TEST_THROW(getConst(pl).get<int>("Does not exist 3"), Exceptions::InvalidParameterName);
484  TEST_EQUALITY(pl.getPtr<int>("Does not exist 4"), static_cast<int*>(0));
485  TEST_EQUALITY(getConst(pl).getPtr<int>("Does not exist 5"), static_cast<const int*>(0));
486  ECHO(char raw_str[] = "dummy");
487  TEST_EQUALITY_CONST(pl.get("Does not exist 6", raw_str), "dummy");
488  ECHO(const char raw_c_str[] = "dummy");
489  TEST_EQUALITY_CONST(pl.get("Does not exist 7", raw_c_str), "dummy");
490  ECHO(const std::string str = "dummy");
491  TEST_EQUALITY_CONST(pl.get("Does not exist 8", str), "dummy");
492  TEST_THROW(pl.getEntry("Does not exist 9"), Exceptions::InvalidParameterName);
493  TEST_THROW(getConst(pl).getEntry("Does not exist 10"), Exceptions::InvalidParameterName);
494  TEST_EQUALITY(pl.getEntryPtr("Does not exist 11"), static_cast<ParameterEntry*>(0));
495  TEST_EQUALITY(getConst(pl).getEntryPtr("Does not exist 12"), static_cast<const ParameterEntry*>(0));
496  TEST_EQUALITY(pl.getEntryRCP("Does not exist 13"), RCP<ParameterEntry>());
497  TEST_EQUALITY(getConst(pl).getEntryRCP("Does not exist 14"), RCP<const ParameterEntry>());
498 }
499 
500 
501 TEUCHOS_UNIT_TEST( ParameterList, get_existing_incorrect_type )
502 {
503  ParameterList pl;
504  pl.set("my int", 4);
505  TEST_THROW(pl.get<double>("my int"), Exceptions::InvalidParameterType);
506  // ToDo: Assert the contents of the error message
507 }
508 
509 
511 {
512  ParameterList pl;
513  pl.set("my int", 4);
514  TEST_EQUALITY_CONST(pl.getPtr<int>("Does not Exist"), static_cast<int*>(0));
515  TEST_INEQUALITY_CONST(pl.getPtr<int>("my int"), static_cast<int*>(0));
516  TEST_EQUALITY_CONST(*pl.getPtr<int>("my int"), 4);
517  TEST_EQUALITY_CONST(pl.getPtr<double>("my int"), static_cast<double*>(0));
518  TEST_EQUALITY_CONST(getConst(pl).getPtr<int>("Does not Exist"), static_cast<const int*>(0));
519  TEST_INEQUALITY_CONST(getConst(pl).getPtr<int>("my int"), static_cast<int*>(0));
520  TEST_EQUALITY_CONST(*getConst(pl).getPtr<int>("my int"), 4);
521  TEST_EQUALITY_CONST(getConst(pl).getPtr<double>("my int"), static_cast<const double*>(0));
522 }
523 
524 
526 {
527  ParameterList pl;
528  pl.set("my int", 4);
529  TEST_EQUALITY_CONST(pl.getEntryRCP("Does not Exist"), null);
530  TEST_INEQUALITY_CONST(pl.getEntryRCP("my int"), null);
531  TEST_EQUALITY_CONST(pl.getEntryRCP("my int")->getValue<int>(0), 4);
532  TEST_EQUALITY_CONST(getConst(pl).getEntryRCP("Does not Exist"), null);
533  TEST_INEQUALITY_CONST(getConst(pl).getEntryRCP("my int"), null);
534  TEST_EQUALITY_CONST(getConst(pl).getEntryRCP("my int")->getValue<int>(0), 4);
535 }
536 
537 
538 // Test nonconstFind()
539 
540 // Test find()
541 
542 
543 TEUCHOS_UNIT_TEST( ParameterList, get_default_then_change )
544 {
545  ParameterList pl;
546  ECHO(int &my_int = pl.get("my int", 3));
547  TEST_EQUALITY_CONST(my_int, 3);
548  TEST_EQUALITY_CONST(pl.get<int>("my int"), 3);
549  ECHO(my_int = 5);
550  TEST_EQUALITY_CONST(pl.get<int>("my int"), 5);
551 }
552 
553 
555 {
556  ParameterList pl;
558  ECHO(pl.set("my int", 2));
560  TEST_EQUALITY_CONST(pl.get<int>("my int"), 2);
561  ECHO(const bool param_was_removed_1 = pl.remove("my int"));
562  TEST_EQUALITY_CONST(param_was_removed_1, true);
564  TEST_THROW(pl.get<int>("my int"), Exceptions::InvalidParameterName);
566  ECHO(const bool param_was_removed_2 = pl.remove("my int", false));
567  TEST_EQUALITY_CONST(param_was_removed_2, false);
568 }
569 
570 
571 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_default )
572 {
573  ParameterList pl("Base");
574  ECHO(pl.sublist("my sublist"));
575  ECHO(const ParameterEntry &sublistParam = pl.getEntry("my sublist"));
576  TEST_EQUALITY_CONST(sublistParam.isUsed(), false);
577  TEST_EQUALITY_CONST(sublistParam.isList(), true);
578  TEST_EQUALITY_CONST(sublistParam.isDefault(), false);
579  TEST_EQUALITY_CONST(sublistParam.docString(), "");
580  TEST_EQUALITY_CONST(sublistParam.getValue<ParameterList>(0).name(), "Base->my sublist");
581 }
582 
583 
584 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_docString )
585 {
586  ParameterList pl("Base");
587  ECHO(pl.sublist("my sublist", false, "My great sublist"));
588  ECHO(const ParameterEntry &sublistParam = pl.getEntry("my sublist"));
589  TEST_EQUALITY_CONST(sublistParam.isUsed(), false);
590  TEST_EQUALITY_CONST(sublistParam.isList(), true);
591  TEST_EQUALITY_CONST(sublistParam.isDefault(), false);
592  TEST_EQUALITY_CONST(sublistParam.docString(), "My great sublist");
593  TEST_EQUALITY_CONST(sublistParam.getValue<ParameterList>(0).name(), "Base->my sublist");
594 }
595 
596 
597 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_mustAlreadyExist )
598 {
599  ParameterList pl("Base");
600  TEST_THROW(pl.sublist("my sublist", true), Exceptions::InvalidParameterName);
601  // ToDo: Examine the actual structure of the error message
602 }
603 
604 
605 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_nonsublist )
606 {
607  ParameterList pl("Base");
608  ECHO(pl.set("my sublist", 1)); // Not a sublist!
610  // ToDo: Examine the actual structure of the error message
611 }
612 
613 
614 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_nonconst )
615 {
616  ParameterList pl("Base");
617  ECHO(pl.sublist("my sublist").set("my int", 2));
618  ECHO(const int my_int = pl.sublist("my sublist").get<int>("my int"));
619  TEST_EQUALITY_CONST(my_int, 2);
620 }
621 
622 
623 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_const )
624 {
625  ParameterList pl("Base");
626  ECHO(pl.sublist("my sublist").set("my int", 2));
627  ECHO(const int my_int = getConst(pl).sublist("my sublist").get<int>("my int"));
628  TEST_EQUALITY_CONST(my_int, 2);
629 }
630 
631 
632 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_const )
633 {
634  ParameterList pl("Base");
635  TEST_THROW(getConst(pl).sublist("my sublist"), Exceptions::InvalidParameterName);
636  // ToDo: Examine the actual structure of the error message
637 }
638 
639 
640 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_const_nonsublist )
641 {
642  ParameterList pl("Base");
643  ECHO(pl.set("my sublist", 1)); // Not a sublist!
644  TEST_THROW(getConst(pl).sublist("my sublist"), Exceptions::InvalidParameterType);
645  // ToDo: Examine the actual structure of the error message
646 }
647 
648 
650 {
651  ParameterList PL_Main("PL_Main");
652  const std::string Direction_Doc = "This sublist controls how direction is computed.";
653  ParameterList& PL_Direction = PL_Main.sublist("Direction", false, Direction_Doc);
654  ParameterList& PL_LineSearch = PL_Main.sublist("Line Search");
655  out << "PL_Main=\n" << PL_Main << "\n";
656  TEST_EQUALITY_CONST(PL_Main.name(), "PL_Main");
657  TEST_EQUALITY_CONST(PL_Main.isSublist("Direction"), true);
658  TEST_EQUALITY_CONST(PL_Main.isSublist("Line Search"), true);
659  ECHO(const ParameterList& PL_Direction_2 = getConst(PL_Main).sublist("Direction"));
660  TEST_EQUALITY(&PL_Direction, &PL_Direction_2);
661  ECHO(const ParameterList& PL_LineSearch_2 = getConst(PL_Main).sublist("Line Search"));
662  TEST_EQUALITY(&PL_LineSearch, &PL_LineSearch_2);
663  TEST_EQUALITY_CONST(getConst(PL_Main).sublist("Direction").name(), "PL_Main->Direction");
664  TEST_EQUALITY_CONST(PL_Direction.name(), "PL_Main->Direction");
665  TEST_EQUALITY_CONST(PL_LineSearch.name(), "PL_Main->Line Search");
666 }
667 
668 
669 TEUCHOS_UNIT_TEST( ParameterList, sublist_scenario_1 )
670 {
671  // This is the scenario in the orginal testing program
672  ParameterList PL_Main("PL_Main");
673  const std::string Direction_Doc = "This sublist controls how direction is computed.";
674  ParameterList &PL_Direction = PL_Main.sublist("Direction", false, Direction_Doc);
675  ParameterList &PL_Newton = PL_Direction.sublist("Newton");
676  ParameterList &PL_LinSol = PL_Newton.sublist("Linear Solver");
677  ParameterList &PL_LineSearch = PL_Main.sublist("Line Search");
678  out << "PL_Main=\n" << PL_Main << "\n";
679  TEST_EQUALITY_CONST(PL_Main.name(), "PL_Main");
680  TEST_EQUALITY_CONST(PL_Main.isSublist("Direction"), true);
681  ECHO(const ParameterList& PL_Direction_2 = getConst(PL_Main).sublist("Direction"));
682  TEST_EQUALITY(&PL_Direction, &PL_Direction_2);
683  TEST_EQUALITY_CONST(getConst(PL_Main).sublist("Direction").name(), "PL_Main->Direction");
684  TEST_EQUALITY_CONST(PL_Direction.name(), "PL_Main->Direction");
685  TEST_EQUALITY_CONST(PL_Direction.isSublist("Newton"), true);
686  TEST_EQUALITY_CONST(PL_Newton.isSublist("Linear Solver"), true);
687  TEST_EQUALITY_CONST(PL_Newton.name(), "PL_Main->Direction->Newton");
688  TEST_EQUALITY_CONST(PL_Newton.isSublist("Linear Solver"), true);
689  TEST_EQUALITY_CONST(PL_LinSol.name(), "PL_Main->Direction->Newton->Linear Solver");
690  TEST_EQUALITY_CONST(PL_Main.isSublist("Line Search"), true);
691  TEST_EQUALITY_CONST(PL_LineSearch.name(), "PL_Main->Line Search");
692 }
693 
694 
695 TEUCHOS_UNIT_TEST( ParameterList, copy_constructor )
696 {
697  ECHO(ParameterList pl1("A"));
698  ECHO(pl1.set("my int", 2));
699  ECHO(ParameterList pl2(pl1));
700  TEST_EQUALITY_CONST(pl2.name(), "A");
701  TEST_EQUALITY_CONST(pl2.get<int>("my int"), 2);
702 }
703 
704 
705 TEUCHOS_UNIT_TEST( ParameterList, assignment_operator )
706 {
707  ECHO(ParameterList pl1("A"));
708  ECHO(pl1.set("my int", 2));
709  ECHO(ParameterList pl2);
710  ECHO(const ParameterList &pl2_ref = pl2 = pl1);
711  TEST_EQUALITY_CONST(&pl2_ref, &pl2);
712  TEST_EQUALITY_CONST(pl2.name(), "A");
713  TEST_EQUALITY_CONST(pl2.get<int>("my int"), 2);
714 }
715 
716 
717 TEUCHOS_UNIT_TEST( ParameterList, iterator_params )
718 {
719  typedef ParameterList::ConstIterator ConstIter;
720  ParameterList pl;
721  pl.set("c", 1);
722  pl.set("a", 2);
723  pl.set("b", 3);
724  ConstIter pl_itr = pl.begin();
725  TEST_EQUALITY_CONST(pl_itr->first, "c");
726  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 1);
727  ECHO(++pl_itr);
728  TEST_EQUALITY_CONST(pl_itr->first, "a");
729  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 2);
730  ECHO(++pl_itr);
731  TEST_EQUALITY_CONST(pl_itr->first, "b");
732  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 3);
733  ECHO(++pl_itr);
734  TEST_ITER_EQUALITY(pl_itr, pl.end());
735 }
736 
737 
738 TEUCHOS_UNIT_TEST( ParameterList, iterator_params_sublists )
739 {
740  typedef ParameterList::ConstIterator ConstIter;
741  ParameterList pl("base");
742  pl.set("c", 1);
743  pl.sublist("a");
744  pl.set("b", 3);
745  ConstIter pl_itr = pl.begin();
746  TEST_EQUALITY_CONST(pl_itr->first, "c");
747  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 1);
748  ECHO(++pl_itr);
749  TEST_EQUALITY_CONST(pl_itr->first, "a");
750  TEST_EQUALITY_CONST(pl_itr->second.getValue<ParameterList>(0).name(), "base->a");
751  ECHO(++pl_itr);
752  TEST_EQUALITY_CONST(pl_itr->first, "b");
753  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 3);
754  ECHO(++pl_itr);
755  TEST_ITER_EQUALITY(pl_itr, pl.end());
756 }
757 
758 // Test iterator access after removing params
759 
760 // Test iterator access after removing sublists
761 
762 
763 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityWithEmpty )
764 {
765  // An empty list should not be equal to a full list
768  TEST_ASSERT( A == B );
769  A.set("Hello","World");
770  TEST_ASSERT( A != B );
771  B.set("Hello","World");
772  TEST_ASSERT( A == B );
773 }
774 
775 
776 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityDifferentSublistNames )
777 {
778  // Sublists with different names should not be equal
781  A.sublist("Bob");
782  B.sublist("Tom");
783  TEST_ASSERT( A != B );
784 }
785 
786 
787 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityDifferentLengths )
788 {
791  A.set("A","a");
792  A.set("B","b");
793  A.set("C","c");
794  A.print(out);
795 
796  B.set("A","a");
797  B.set("B","b");
798  B.print(out);
799 
800  TEST_ASSERT( A != B );
801 
802  B.set("C","c");
803  TEST_ASSERT( A == B );
804 }
805 
806 
807 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesWithEmpty )
808 {
811  TEST_ASSERT( haveSameValues(A,B) );
812  A.set("a",1);
813  TEST_ASSERT( !haveSameValues(A,B) );
814  A.set("b",2);
815  B.set("a",1);
816  B.set("b",2);
817  TEST_ASSERT( haveSameValues(A,B) );
818 }
819 
820 
821 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesDifferentSublistNames )
822 {
825  A.sublist("Smith").set("People",4);
826  B.sublist("Jones").set("People",4);
827  TEST_ASSERT( !haveSameValues(A,B) ); // sublist names matter
828 }
829 
830 
831 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesSortedReversedOrder )
832 {
833  ParameterList A, B;
834  A.set("a",1);
835  A.set("b",2);
836  // Create second list with the same entries but different order
837  B.set("b",2);
838  B.set("a",1);
839  TEST_ASSERT( haveSameValuesSorted(A,B) );
840  B.set("c",3);
841  TEST_ASSERT( !haveSameValuesSorted(A,B) ); // check for length
842 }
843 
844 
845 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesSortedNested)
846 {
847  ParameterList A, B;
848  ParameterList &asublist = A.sublist("A");
849  asublist.set("a",1);
850  asublist.set("b",2);
851  ParameterList &bsublist = B.sublist("A");
852  bsublist.set("a",1);
853  bsublist.set("b",2);
854  TEST_ASSERT( haveSameValuesSorted(A,B) );
855  asublist.set("c",3);
856  bsublist.set("c",4);
857  TEST_ASSERT( !haveSameValuesSorted(A,B) );
858 }
859 
860 
861 TEUCHOS_UNIT_TEST( ParameterList, validateAgainstSelf )
862 {
863  ParameterList PL_Main = createMainPL();
864  ParameterList PL_Main_valid = createValidMainPL();
865  TEST_NOTHROW(PL_Main.validateParameters(PL_Main_valid));
866 }
867 
868 
869 TEUCHOS_UNIT_TEST( ParameterList, validateParametersAndSetDefaults_default )
870 {
871  // Test for proper behavior when the user doesn't set `Nonlinear Solver`
872  ParameterList PL_Main = createMainPL();
873  ParameterList PL_Main_valid = createValidMainPL();
874  ECHO(PL_Main.validateParametersAndSetDefaults(PL_Main_valid));
875  TEST_NOTHROW(
876  rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<int> >(
877  PL_Main.getEntry("Nonlinear Solver").validator(), true ) );
878  // Make sure the parameter entry is set to default and unused after validation
879  const ParameterEntry &default_entry = PL_Main.getEntry("Nonlinear Solver");
880  TEST_EQUALITY(default_entry.isDefault(), true);
881  TEST_EQUALITY(default_entry.isUsed(), false);
882  // Make sure the value is stored as an integer after validation
883 #if defined(HAVE_TEUCHOS_MODIFY_DEFAULTS_DURING_VALIDATION)
884  TEST_NOTHROW(Teuchos::any_cast<int>(default_entry.getAny()));
885 #endif
886 }
887 
888 
889 TEUCHOS_UNIT_TEST( ParameterList, validateParametersAndSetDefaults_noDefault )
890 {
891  // Now make sure we have the correct behavior when not using a default value
892  ParameterList PL_Main = createMainPL();
893  PL_Main.set("Nonlinear Solver", "Trust Region Based");
894  ParameterList PL_Main_valid = createValidMainPL();
895  PL_Main.validateParametersAndSetDefaults(PL_Main_valid);
896  const ParameterEntry &entry = PL_Main.getEntry("Nonlinear Solver");
897  TEST_EQUALITY(entry.isDefault(), false);
898  TEST_EQUALITY(entry.isUsed(), false);
899 #if defined(HAVE_TEUCHOS_MODIFY_DEFAULTS_DURING_VALIDATION)
900  TEST_NOTHROW(Teuchos::any_cast<int>(entry.getAny()));
901 #endif
902 }
903 
904 
905 TEUCHOS_UNIT_TEST( ParameterList, getIntegralValue_int )
906 {
907  ParameterList PL_Main = createMainPL();
908  ParameterList PL_Main_valid = createValidMainPL();
909  ECHO(PL_Main.set("Nonlinear Solver", "Line Search Based"));
910  ECHO(PL_Main.validateParametersAndSetDefaults(PL_Main_valid));
911  ECHO(const int lineSearchValue = getIntegralValue<int>(PL_Main, "Nonlinear Solver"));
912  TEST_EQUALITY_CONST(lineSearchValue, 0);
913  ECHO(PL_Main.set("Nonlinear Solver", "Trust Region Based"));
914  ECHO(const int trustRegionValue = getIntegralValue<int>(PL_Main, "Nonlinear Solver"));
915  TEST_EQUALITY_CONST(trustRegionValue, 1);
916 }
917 
918 
919 TEUCHOS_UNIT_TEST( ParameterList, replaceScalarParameterWithArray ) {
920  ParameterList pl = ParameterList("Parameter List with Scalar Parameter");
921  const int a_val = 2, b_val = 3;
922  pl.set("A", a_val);
923  pl.set("B", b_val);
924  replaceParameterWithArray<int>("A", "A array", pl);
925  replaceParameterWithArray<int>("B", "B", pl);
926  ParameterList expected_pl = ParameterList("Parameter List with Array Parameter");
927  Array<int> a_array = tuple<int>(a_val), b_array = tuple<int>(b_val);
928  expected_pl.set("A array", a_array);
929  expected_pl.set("B", b_array);
930  TEST_ASSERT(haveSameValuesSorted(expected_pl, pl, true));
931  // Throw an error when trying to overwrite a parameter that already exists but
932  // doesn't have the same name.
933  pl.set("C", 1);
934  TEST_THROW(replaceParameterWithArray<int>("C", "B", pl), std::logic_error);
935  pl.print();
936 }
937 
938 
939 TEUCHOS_UNIT_TEST( ParameterList, simpleModifierModifyReconcile )
940 {
941  RCP<SimpleModifier> modifier = rcp(new SimpleModifier());
942  ParameterList valid_pl("My Valid Parameter List with a Modifier", modifier);
943  //valid_pl before modification
944  // A: 1.0
945  // B: 0.1
946  // SubA:
947  // C: 1
948  valid_pl.set("A", 1.0);
949  valid_pl.set("B", 0.1);
950  valid_pl.sublist("SubA").set("C", 1);
951  ParameterList pl("My Parameter List");
952  pl.set("A", 5.0);
953  pl.set("B", -0.1);
954  pl.sublist("SubA 1").set("C", 3);
955  pl.sublist("SubA 2").set("C", 4);
956  ParameterList expected_valid_pl(valid_pl);
957  expected_valid_pl.remove("SubA");
958  expected_valid_pl.sublist("SubA 1").set("C", 1);
959  expected_valid_pl.sublist("SubA 2").set("C", 1);
960  pl.modifyParameterList(valid_pl);
961  //valid_pl after modification
962  // A: 1.0
963  // B: 0.1
964  // SubA 1:
965  // C: 1
966  // SubA 2:
967  // C: 1
968  TEST_EQUALITY(valid_pl, expected_valid_pl);
969 // std::cout << haveSameValuesSorted(expected_valid_pl, valid_pl, true) << std::endl;
971  TEST_NOTHROW(pl.reconcileParameterList(valid_pl));
972  pl.set("A", -1.0);
973  TEST_THROW(pl.reconcileParameterList(valid_pl), std::logic_error);
974  // Test the copy constructor
975  ParameterList copy_valid_pl(valid_pl);
976  TEST_EQUALITY(valid_pl, copy_valid_pl);
977 }
978 
979 
980 TEUCHOS_UNIT_TEST( ParameterList, nestedSublistExpansion ) {
982  Teuchos::RCP<SimpleSubModifier> sub_modifier = Teuchos::rcp(new SimpleSubModifier());
983  // The unmodified (template-like) validation parameter list
984  ParameterList valid_pl("valid_pl", modifier);
985  valid_pl.set("A", 1.0);
986  valid_pl.set("B", 1.0);
987  valid_pl.sublist("SubA").setModifier(sub_modifier);
988  valid_pl.sublist("SubA").set("C", 3);
989  valid_pl.sublist("SubA").set("D", 4);
990  valid_pl.sublist("SubA").sublist("SubB").set("E", 10);
991  valid_pl.sublist("SubA").sublist("SubB").set("F", 11);
992  // The user's input parameter list
993  ParameterList pl("pl");
994  pl.set("A", 1.0);
995  pl.set("B", 2.0);
996  pl.sublist("SubA 1").set("C", 3);
997  pl.sublist("SubA 1").set("D", 4);
998  pl.sublist("SubA 1").sublist("SubB 1").set("E", 51);
999  pl.sublist("SubA 1").sublist("SubB 1").set("F", 61);
1000  pl.sublist("SubA 1").sublist("SubB 2").set("E", 52);
1001  pl.sublist("SubA 1").sublist("SubB 2").set("F", 62);
1002  pl.sublist("SubA 2").set("C", 3);
1003  pl.sublist("SubA 2").set("D", 4);
1004  pl.sublist("SubA 2").sublist("SubB 3").set("E", 53);
1005  pl.sublist("SubA 2").sublist("SubB 3").set("F", 63);
1006  // The expanded valid parameter list after modification
1007  ParameterList expected_valid_pl("valid_pl_expanded");
1008  expected_valid_pl.set("A", 1.0);
1009  expected_valid_pl.set("B", 1.0);
1010  expected_valid_pl.sublist("SubA 1").set("C", 3);
1011  expected_valid_pl.sublist("SubA 1").set("D", 4);
1012  expected_valid_pl.sublist("SubA 1").sublist("SubB 1").set("E", 10);
1013  expected_valid_pl.sublist("SubA 1").sublist("SubB 1").set("F", 11);
1014  expected_valid_pl.sublist("SubA 1").sublist("SubB 2").set("E", 10);
1015  expected_valid_pl.sublist("SubA 1").sublist("SubB 2").set("F", 11);
1016  expected_valid_pl.sublist("SubA 2").set("C", 3);
1017  expected_valid_pl.sublist("SubA 2").set("D", 4);
1018  expected_valid_pl.sublist("SubA 2").sublist("SubB 3").set("E", 10);
1019  expected_valid_pl.sublist("SubA 2").sublist("SubB 3").set("F", 11);
1020  // Expand the validation parameter list based on the user's input parameter list
1021  pl.modifyParameterList(valid_pl);
1022  // Modified parameter lists aren't equal because they don't have the same modifiers
1023  TEST_ASSERT(valid_pl != expected_valid_pl);
1024  // Test that they are the same except for the modifiers
1025  TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
1026  // Check the equality of the modifiers
1027  expected_valid_pl.setModifier(modifier);
1028  expected_valid_pl.sublist("SubA 1", true).setModifier(sub_modifier);
1029  expected_valid_pl.sublist("SubA 2", true).setModifier(sub_modifier);
1030  TEST_ASSERT(haveSameModifiers(valid_pl, expected_valid_pl));
1031  // Now test the recursive reconciliation
1032  TEST_NOTHROW(pl.reconcileParameterList(valid_pl));
1033  pl.sublist("SubA 1").set("C", 11);
1034  pl.sublist("SubA 1").set("D", 11);
1035  TEST_THROW(pl.reconcileParameterList(valid_pl), std::logic_error);
1036 }
1037 
1038 
1039 TEUCHOS_UNIT_TEST( ParameterList, disableRecursion ) {
1041  Teuchos::RCP<SimpleSubModifier> sub_modifier = Teuchos::rcp(new SimpleSubModifier());
1042  // The unmodified (template-like) validation parameter list
1043  ParameterList valid_pl("valid_pl", modifier);
1044  valid_pl.set("A", 1.0);
1045  valid_pl.set("B", 1.0);
1046  valid_pl.sublist("SubA").setModifier(sub_modifier);
1047  valid_pl.sublist("SubA").set("C", 3.0);
1048  valid_pl.sublist("SubA").set("D", 4);
1049  valid_pl.sublist("SubA").sublist("SubB").set("E", 10);
1050  valid_pl.sublist("SubA").sublist("SubB").set("F", 11);
1051  // The user's input parameter list
1052  ParameterList pl("pl");
1053  pl.set("A", 1.0);
1054  pl.set("B", 2.0);
1055  pl.sublist("SubA 1").set("C", 3);
1056  pl.sublist("SubA 1").set("D", 4);
1057  pl.sublist("SubA 1").sublist("SubB").set("E", 53);
1058  pl.sublist("SubA 1").sublist("SubB").set("E", 63);
1059  // The expanded valid parameter list after modification
1060  ParameterList expected_valid_pl("valid_pl");
1061  expected_valid_pl.set("A", 1.0);
1062  expected_valid_pl.set("B", 1.0);
1063  expected_valid_pl.sublist("SubA 1").set("C", 3.0);
1064  expected_valid_pl.sublist("SubA 1").set("D", 4);
1065  expected_valid_pl.sublist("SubA 1").sublist("SubB").set("E", 10);
1066  expected_valid_pl.sublist("SubA 1").sublist("SubB").set("F", 11);
1067  // Make a copy of the user's input parameter list before it is validated
1068  ParameterList copy_pl(pl);
1069  // The float validator will cast integers in `pl` to floats
1070  RCP<AnyNumberParameterEntryValidator> float_validator = rcp(
1072  AnyNumberParameterEntryValidator::AcceptedTypes(false).allowInt(true).allowDouble(true)));
1073  valid_pl.sublist("SubA").getEntry("C").setValidator(float_validator);
1074  // Don't modify `SubA`
1075  valid_pl.sublist("SubA").disableRecursiveModification();
1076  pl.modifyParameterList(valid_pl);
1077  TEST_ASSERT(haveSameValuesSorted(expected_valid_pl, valid_pl, true));
1078  // Don't validate `SubA 1`
1079  valid_pl.sublist("SubA 1").disableRecursiveValidation();
1080  pl.validateParametersAndSetDefaults(valid_pl);
1081  // If we were to validate `SubA 1` then parameter C would turn into a float and the following test would fail
1082  TEST_ASSERT(haveSameValuesSorted(pl, copy_pl, true));
1083 }
1084 
1085 
1086 TEUCHOS_UNIT_TEST( ParameterList, recursiveValidation ) {
1087  ParameterList valid_pl("valid_pl");
1088  valid_pl.set("A", 1);
1089  valid_pl.sublist("SubA").set("B", 1);
1090  ParameterList pl("pl");
1091  pl.set("A", 1.0);
1092  pl.sublist("SubA").set("B", 2);
1093  ParameterList validated_pl("valid_pl");
1094  validated_pl.set("A", 1.0);
1095  validated_pl.sublist("SubA").set("B", 2.0);
1096  // The float validator will cast integers in `pl` to floats
1097  RCP<AnyNumberParameterEntryValidator> float_validator = rcp(
1099  AnyNumberParameterEntryValidator::AcceptedTypes(false).allowInt(true).allowDouble(true)));
1100  valid_pl.getEntry("A").setValidator(float_validator);
1101  valid_pl.sublist("SubA").getEntry("B").setValidator(float_validator);
1102  pl.validateParametersAndSetDefaults(valid_pl);
1103  // All of the integers in `pl` should be casted to floats in `validated_pl`
1104  TEST_ASSERT(haveSameValuesSorted(validated_pl, pl, true));
1105 }
1106 
1107 
1108 TEUCHOS_UNIT_TEST( ParameterList, recursiveReconciliation ) {
1109  Teuchos::RCP<ReconciliationModifier1> modifier1 = Teuchos::rcp(new ReconciliationModifier1());
1110  Teuchos::RCP<ReconciliationModifier2> modifier2 = Teuchos::rcp(new ReconciliationModifier2());
1111  ParameterList valid_pl("valid_pl");
1112  valid_pl.set("a", 1);
1113  valid_pl.setModifier(modifier1);
1114  valid_pl.sublist("A").setModifier(modifier2);
1115  valid_pl.sublist("A").set("c", 1);
1116  valid_pl.sublist("A").set("d", 1);
1117  ParameterList pl("pl");
1118  pl.set("a", 1);
1119  pl.sublist("A").set("c", 2);
1120  pl.sublist("A").set("d", 3);
1121  ParameterList reconciled_pl("reconciled_pl");
1122  reconciled_pl.set("a", 1);
1123  reconciled_pl.set("b", 5);
1124  reconciled_pl.sublist("A").set("c", 2);
1125  reconciled_pl.sublist("A").set("d", 3);
1126  reconciled_pl.sublist("A").set("e", 5);
1127  pl.reconcileParameterList(valid_pl);
1128  TEST_ASSERT(haveSameValuesSorted(reconciled_pl, pl, true));
1129 }
1130 
1131 
1132 TEUCHOS_UNIT_TEST( ParameterList, attachValidatorRecursively ) {
1133  ParameterList valid_pl("valid_pl");
1134  valid_pl.set("a", 0.);
1135  valid_pl.sublist("A").set("b", 0.);
1136  valid_pl.sublist("A").set("c", 0.);
1137  valid_pl.sublist("A").sublist("AA").set("d", 0.);
1138  ParameterList pl("pl");
1139  pl.set("a", 1);
1140  pl.sublist("A").set("b", 2);
1141  pl.sublist("A").set("c", 3);
1142  pl.sublist("A").sublist("AA").set("d", 4);
1143  ParameterList validated_pl("validated_pl");
1144  validated_pl.set("a", 1.);
1145  validated_pl.sublist("A").set("b", 2.);
1146  validated_pl.sublist("A").set("c", 3.);
1147  validated_pl.sublist("A").sublist("AA").set("d", 4.);
1148  // The float validator will cast integers in `pl` to floats
1149  RCP<AnyNumberParameterEntryValidator> float_validator = rcp(
1151  AnyNumberParameterEntryValidator::AcceptedTypes(false).allowInt(true).allowDouble(true)));
1152  valid_pl.recursivelySetValidator<double>(float_validator, 1);
1153  // This should fail since we only set the float validator on the top level of `valid_pl`
1154  TEST_THROW(pl.validateParametersAndSetDefaults(valid_pl), std::logic_error);
1155  // Now attach the validator to every double
1156  valid_pl.recursivelySetValidator<double>(float_validator);
1157  pl.validateParametersAndSetDefaults(valid_pl);
1158  TEST_ASSERT(haveSameValuesSorted(validated_pl, pl, true));
1159 }
1160 
1161 // TODO: test printing of modifiers
1162 // valid_pl.print(std::cout, ParameterList::PrintOptions().showDoc(true).indent(2).showTypes(true));
1163 
1165  ParameterList valid_pl("valid_pl");
1166  valid_pl.set("a", 0.);
1167 
1168  {
1169  ParameterList pl("pl");
1170  pl.set("a", 1.);
1171  pl.validateParametersAndSetDefaults(valid_pl);
1172  ParameterList::PrintOptions printOptions;
1173  printOptions.showDefault(false);
1174  std::ostringstream ss;
1175  pl.print(ss, printOptions);
1176  std::cout << ss.str();
1177  TEST_ASSERT(ss.str().size() > 0);
1178  }
1179 
1180  {
1181  ParameterList pl("pl");
1182  pl.validateParametersAndSetDefaults(valid_pl);
1183  ParameterList::PrintOptions printOptions;
1184  std::ostringstream ss;
1185  pl.print(ss, printOptions);
1186  std::cout << ss.str();
1187  TEST_ASSERT(ss.str().size() > 0);
1188 
1189  ss.str("");
1190  printOptions.showDefault(false);
1191  pl.print(ss, printOptions);
1192  std::cout << ss.str();
1193  TEST_ASSERT(ss.str().size() == 0);
1194  }
1195 }
1196 
1197 } // namespace Teuchos
1198 
1199 
1200 
ParameterList createMainPL()
void print() const
Print function to use in debugging in a debugger.
const std::string & name() const
The name of this ParameterList.
C++ Standard Library compatable filtered iterator.
#define TEST_ASSERT(v1)
Assert the given statement is true.
ParameterList & setEntry(const std::string &name, U &&entry)
Set a parameter directly as a ParameterEntry.
ConstIterator end() const
An iterator pointing beyond the last entry.
#define TEST_INEQUALITY_CONST(v1, v2)
Assert the inequality of v1 and constant v2.
RCP< ParameterEntry > getEntryRCP(const std::string &name)
Retrieves the RCP for an entry with the name name if it exists.
ParameterList & disableRecursiveValidation()
#define TEST_NOTHROW(code)
Asserr that the statement &#39;code&#39; does not thrown any excpetions.
#define ECHO(statement)
Echo the given statement before it is executed.
void setValidator(RCP< const ParameterEntryValidator > const &validator)
Set the validator.
RCP< const ParameterEntryValidator > validator() const
Return the (optional) validator object.
T & get(const std::string &name, T def_value)
Return the parameter&#39;s value, or the default value if it is not there.
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.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
This object is held as the &quot;value&quot; in the Teuchos::ParameterList std::map.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
virtual void printDoc(std::string const &docString, std::ostream &out) const =0
Print documentation for this parameter.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
#define TEST_ITER_EQUALITY(iter1, iter2)
Assert that two iterators are equal.
#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)...
virtual const std::string getXMLTypeName() const =0
Get a string that should be used as a value of the type attribute when serializing it to XML...
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
Ordinal numParams() const
Get the number of stored parameters.
Abstract interface for an object that can modify both a parameter list and the parameter list being u...
void recursivelySetValidator(RCP< const ParameterEntryValidator > const &validator, int const depth=1000)
Recursively attach a validator to parameters of type T.
bool isDefault() const
Indicate whether this entry takes on the default value.
ParameterList createValidMainPL()
ParameterEntry * getEntryPtr(const std::string &name)
Retrieves the pointer for an entry with the name name if it exists.
PrintOptions & showDefault(bool _showDefault)
bool isParameter(const std::string &name) const
Whether the given parameter exists in this list.
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
bool remove(std::string const &name, bool throwIfNotExists=true)
Remove a parameter (does not depend on the type of the parameter).
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
virtual void modify(ParameterList &paramList, ParameterList &validParamList) const
Modify a parameter list and/or the valid parameter list being used to validate it and throw std::exce...
bool isSublist(const std::string &name) const
Whether the given sublist exists in this list.
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
Validate the parameters in this list given valid selections in the input list and set defaults for th...
int expandSublistsUsingBaseName(const std::string &baseName, ParameterList &paramList, ParameterList &validParamList, const bool &allowBaseName=true) const
Create sublists in the valid parameter list using a base name and the corresponding sublists in the p...
Utility class for setting and passing in print options.
virtual ValidStringsList validStringValues() const =0
Return an array of strings of valid values if applicable.
ConstIterator begin() const
An iterator pointing to the first entry.
T * getPtr(const std::string &name)
Retrieves the pointer for parameter name of type T from a list. A null pointer is returned if this pa...
A list of parameters of arbitrary type.
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
Validate the parameters in this list given valid selections in the input list.
ParameterList & setParameters(const ParameterList &source)
Parameter List Modifier class.
void modifyParameterList(ParameterList &validParamList, int const depth=1000)
Modify the valid parameter list prior to validation.
Abstract interface for an object that can validate a ParameterEntry&#39;s value.
any & getAny(bool activeQry=true)
Direct access to the Teuchos::any data value underlying this object. The bool argument activeQry (def...
RCP< const ParameterListModifier > getModifier() const
Return the optional modifier object.
ParameterList & setParametersNotAlreadySet(const ParameterList &source)
Class uesd to validate a particular type of number.
virtual void reconcile(ParameterList &paramList) const
Reconcile a parameter list and/or the valid parameter list being used to validate it and throw std::e...
bool isType(const std::string &name) const
Whether the given parameter exists in this list and has type T.
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.
bool isUsed() const
Return whether or not the value has been used; i.e., whether or not the value has been retrieved via ...
Standard implementation of a ParameterEntryValidator that accepts numbers from a number of different ...
ParameterEntry & getEntry(const std::string &name)
Retrieves an entry with the name name.
ParameterList & disableRecursiveModification()
Definition of Teuchos::as, for conversions between types.
void reconcileParameterList(ParameterList &validParamList, const bool left_to_right=true)
Reconcile a parameter list after validation.
void setModifier(RCP< const ParameterListModifier > const &modifier)
virtual void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const =0
Validate a parameter entry value and throw std::exception (with a great error message) if validation ...