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, param_isParameter_isSublist_isType )
301 {
302  ParameterList pl;
303  ECHO(pl.set("my int", 3));
304  ECHO(const int my_int = pl.get<int>("my int"));
305  TEST_EQUALITY_CONST(my_int, 3);
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 
317 TEUCHOS_UNIT_TEST( ParameterList, sublist_isParameter_isSublist_isType )
318 {
319  ParameterList pl;
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"));
322  TEST_EQUALITY_CONST(my_int, 3);
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 {
335  ParameterList pl;
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 
343 TEUCHOS_UNIT_TEST( ParameterList, set_doc_validator )
344 {
345  ParameterList pl;
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");
349  TEST_NOTHROW(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true));
350 }
351 
352 
353 TEUCHOS_UNIT_TEST( ParameterList, set_invalid_int_first )
354 {
355  ParameterList pl;
357  validator(new Teuchos::EnhancedNumberValidator<int>(0, 1)));
358  TEST_THROW(pl.set("my int", -1, "", validator),
361 }
362 
363 
364 TEUCHOS_UNIT_TEST( ParameterList, set_invalid_int_second )
365 {
366  ParameterList pl;
368  validator(new Teuchos::EnhancedNumberValidator<int>(0, 1)));
369  TEST_NOTHROW(pl.set("my int", 1, "", validator));
371  TEST_EQUALITY_CONST(pl.get<int>("my int"), 1);
373  TEST_EQUALITY_CONST(pl.get<int>("my int"), 1);
374 }
375 
376 
378 {
379  ParameterList pl;
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));
384  TEST_EQUALITY_CONST(my_int_1, 3);
385  TEST_EQUALITY_CONST(my_int_param.isUsed(), true);
386  TEST_EQUALITY_CONST(my_int_param.isList(), false); // The isList entry is ignored!
387  TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null);
388 }
389 
390 
391 TEUCHOS_UNIT_TEST( ParameterList, set_int_twice_keep_validator )
392 {
393  ParameterList pl;
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"));
397  TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null);
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"));
404  TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null);
405  }
406 }
407 
408 
409 TEUCHOS_UNIT_TEST( ParameterList, set_get_char_str )
410 {
411  ParameterList pl;
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 {
428  ParameterList pl;
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 
438 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_param )
439 {
440  ParameterList pl;
441  TEST_THROW(pl.getEntry("Does not exist 1"), Exceptions::InvalidParameterName);
442  TEST_THROW(pl.get<int>("Does not exist 2"), Exceptions::InvalidParameterName);
443  TEST_THROW(getConst(pl).get<int>("Does not exist 3"), 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 
461 TEUCHOS_UNIT_TEST( ParameterList, get_existing_incorrect_type )
462 {
463  ParameterList pl;
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 {
472  ParameterList pl;
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));
480  TEST_EQUALITY_CONST(*getConst(pl).getPtr<int>("my int"), 4);
481  TEST_EQUALITY_CONST(getConst(pl).getPtr<double>("my int"), static_cast<const double*>(0));
482 }
483 
484 
486 {
487  ParameterList pl;
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 
503 TEUCHOS_UNIT_TEST( ParameterList, get_default_then_change )
504 {
505  ParameterList pl;
506  ECHO(int &my_int = pl.get("my int", 3));
507  TEST_EQUALITY_CONST(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 {
516  ParameterList pl;
518  ECHO(pl.set("my int", 2));
520  TEST_EQUALITY_CONST(pl.get<int>("my int"), 2);
521  ECHO(const bool param_was_removed_1 = pl.remove("my int"));
522  TEST_EQUALITY_CONST(param_was_removed_1, true);
524  TEST_THROW(pl.get<int>("my int"), Exceptions::InvalidParameterName);
526  ECHO(const bool param_was_removed_2 = pl.remove("my int", false));
527  TEST_EQUALITY_CONST(param_was_removed_2, false);
528 }
529 
530 
531 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_default )
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 
544 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_docString )
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 
557 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_mustAlreadyExist )
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 
565 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_nonsublist )
566 {
567  ParameterList pl("Base");
568  ECHO(pl.set("my sublist", 1)); // Not a sublist!
570  // ToDo: Examine the actual structure of the error message
571 }
572 
573 
574 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_nonconst )
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"));
579  TEST_EQUALITY_CONST(my_int, 2);
580 }
581 
582 
583 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_const )
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"));
588  TEST_EQUALITY_CONST(my_int, 2);
589 }
590 
591 
592 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_const )
593 {
594  ParameterList pl("Base");
595  TEST_THROW(getConst(pl).sublist("my sublist"), Exceptions::InvalidParameterName);
596  // ToDo: Examine the actual structure of the error message
597 }
598 
599 
600 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_const_nonsublist )
601 {
602  ParameterList pl("Base");
603  ECHO(pl.set("my sublist", 1)); // Not a sublist!
604  TEST_THROW(getConst(pl).sublist("my sublist"), Exceptions::InvalidParameterType);
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"));
620  TEST_EQUALITY(&PL_Direction, &PL_Direction_2);
621  ECHO(const ParameterList& PL_LineSearch_2 = getConst(PL_Main).sublist("Line Search"));
622  TEST_EQUALITY(&PL_LineSearch, &PL_LineSearch_2);
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 
629 TEUCHOS_UNIT_TEST( ParameterList, sublist_scenario_1 )
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"));
642  TEST_EQUALITY(&PL_Direction, &PL_Direction_2);
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 
655 TEUCHOS_UNIT_TEST( ParameterList, copy_constructor )
656 {
657  ECHO(ParameterList pl1("A"));
658  ECHO(pl1.set("my int", 2));
659  ECHO(ParameterList pl2(pl1));
660  TEST_EQUALITY_CONST(pl2.name(), "A");
661  TEST_EQUALITY_CONST(pl2.get<int>("my int"), 2);
662 }
663 
664 
665 TEUCHOS_UNIT_TEST( ParameterList, assignment_operator )
666 {
667  ECHO(ParameterList pl1("A"));
668  ECHO(pl1.set("my int", 2));
669  ECHO(ParameterList pl2);
670  ECHO(const ParameterList &pl2_ref = pl2 = pl1);
671  TEST_EQUALITY_CONST(&pl2_ref, &pl2);
672  TEST_EQUALITY_CONST(pl2.name(), "A");
673  TEST_EQUALITY_CONST(pl2.get<int>("my int"), 2);
674 }
675 
676 
677 TEUCHOS_UNIT_TEST( ParameterList, iterator_params )
678 {
679  typedef ParameterList::ConstIterator ConstIter;
680  ParameterList pl;
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);
694  TEST_ITER_EQUALITY(pl_itr, pl.end());
695 }
696 
697 
698 TEUCHOS_UNIT_TEST( ParameterList, iterator_params_sublists )
699 {
700  typedef ParameterList::ConstIterator ConstIter;
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);
715  TEST_ITER_EQUALITY(pl_itr, pl.end());
716 }
717 
718 // Test iterator access after removing params
719 
720 // Test iterator access after removing sublists
721 
722 
723 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityWithEmpty )
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 
736 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityDifferentSublistNames )
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 
747 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityDifferentLengths )
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 
767 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesWithEmpty )
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 
781 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesDifferentSublistNames )
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 
791 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesSortedReversedOrder )
792 {
793  ParameterList A, B;
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 
805 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesSortedNested)
806 {
807  ParameterList A, B;
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 
821 TEUCHOS_UNIT_TEST( ParameterList, validateAgainstSelf )
822 {
823  ParameterList PL_Main = createMainPL();
824  ParameterList PL_Main_valid = createValidMainPL();
825  TEST_NOTHROW(PL_Main.validateParameters(PL_Main_valid));
826 }
827 
828 
829 TEUCHOS_UNIT_TEST( ParameterList, validateParametersAndSetDefaults )
830 {
831  ParameterList PL_Main = createMainPL();
832  ParameterList PL_Main_valid = createValidMainPL();
833  ECHO(PL_Main.validateParametersAndSetDefaults(PL_Main_valid));
834  TEST_NOTHROW(
835  rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<int> >(
836  PL_Main.getEntry("Nonlinear Solver").validator(), true ) );
837 }
838 
839 
840 TEUCHOS_UNIT_TEST( ParameterList, getIntegralValue_int )
841 {
842  ParameterList PL_Main = createMainPL();
843  ParameterList PL_Main_valid = createValidMainPL();
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"));
847  TEST_EQUALITY_CONST(lineSearchValue, 0);
848  ECHO(PL_Main.set("Nonlinear Solver", "Trust Region Based"));
849  ECHO(const int trustRegionValue = getIntegralValue<int>(PL_Main, "Nonlinear Solver"));
850  TEST_EQUALITY_CONST(trustRegionValue, 1);
851 }
852 
853 
854 TEUCHOS_UNIT_TEST( ParameterList, replaceScalarParameterWithArray ) {
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);
860  replaceParameterWithArray<int>("B", "B", pl);
861  ParameterList expected_pl = ParameterList("Parameter List with Array Parameter");
862  Array<int> a_array = tuple<int>(a_val), b_array = tuple<int>(b_val);
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 
874 TEUCHOS_UNIT_TEST( ParameterList, simpleModifierModifyReconcile )
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);
891  ParameterList expected_valid_pl(valid_pl);
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
903  TEST_EQUALITY(valid_pl, expected_valid_pl);
904 // std::cout << haveSameValuesSorted(expected_valid_pl, valid_pl, true) << std::endl;
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
910  ParameterList copy_valid_pl(valid_pl);
911  TEST_EQUALITY(valid_pl, copy_valid_pl);
912 }
913 
914 
915 TEUCHOS_UNIT_TEST( ParameterList, nestedSublistExpansion ) {
916  Teuchos::RCP<SimpleModifier> modifier = Teuchos::rcp(new SimpleModifier());
917  Teuchos::RCP<SimpleSubModifier> sub_modifier = Teuchos::rcp(new SimpleSubModifier());
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
958  TEST_ASSERT(valid_pl != expected_valid_pl);
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 
974 TEUCHOS_UNIT_TEST( ParameterList, disableRecursion ) {
975  Teuchos::RCP<SimpleModifier> modifier = Teuchos::rcp(new SimpleModifier());
976  Teuchos::RCP<SimpleSubModifier> sub_modifier = Teuchos::rcp(new SimpleSubModifier());
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
995  ParameterList expected_valid_pl("valid_pl");
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
1003  ParameterList copy_pl(pl);
1004  // The float validator will cast integers in `pl` to floats
1005  RCP<AnyNumberParameterEntryValidator> float_validator = rcp(
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 
1021 TEUCHOS_UNIT_TEST( ParameterList, recursiveValidation ) {
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
1032  RCP<AnyNumberParameterEntryValidator> float_validator = rcp(
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 
1043 TEUCHOS_UNIT_TEST( ParameterList, recursiveReconciliation ) {
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 
1067 TEUCHOS_UNIT_TEST( ParameterList, attachValidatorRecursively ) {
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
1084  RCP<AnyNumberParameterEntryValidator> float_validator = rcp(
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);
1107  ParameterList::PrintOptions printOptions;
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);
1118  ParameterList::PrintOptions printOptions;
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 
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.
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.
ParameterList createValidMainPL()
ParameterEntry * getEntryPtr(const std::string &name)
Retrieves the pointer for an entry with the name name if it exists.
ParameterList & setEntry(const std::string &name, const ParameterEntry &entry)
Set a parameter directly as a ParameterEntry.
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.
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.
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 ...