Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
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"
47 
48 
49 //
50 // Utilities
51 //
52 
53 
54 namespace {
55 
56 
57 class DummyValidator : public Teuchos::ParameterEntryValidator
58 {
59 public:
60 
61  const std::string getXMLTypeName() const { return ""; }
62  virtual void printDoc(std::string const& docString, std::ostream &out) const {}
63  virtual ValidStringsList validStringValues() const { return Teuchos::null; }
64  virtual void validate(
65  Teuchos::ParameterEntry const& entry,
66  std::string const& paramName,
67  std::string const& sublistName
68  ) const
69  {}
70 };
71 
72 
73 } // namespace
74 
75 
76 namespace Teuchos {
77 
78 
79 //
80 // Test help utilities
81 //
82 
83 
85 {
86  ParameterList PL_Main("PL_Main");
87  const std::string Direction_Doc = "This sublist controls how direction is computed.";
88  ParameterList &PL_Direction = PL_Main.sublist("Direction", false, Direction_Doc);
89  ParameterList &PL_Newton = PL_Direction.sublist("Newton");
90  PL_Newton.sublist("Linear Solver");
91  PL_Main.sublist("Line Search");
92  return PL_Main;
93 }
94 
95 
96 
98 {
99 
100  ParameterList PL_Main_valid("PL_Main_valid");
101  PL_Main_valid.setParameters(createMainPL());
102 
103  // Create a validator for the "Nonlinear Solver" parameter
104  setStringToIntegralParameter<int>(
105  "Nonlinear Solver",
106  "Line Search Based",
107  "Selects the type of nonlinear solver to use",
108  tuple<std::string>("Line Search Based","Trust Region Based"),
109  &PL_Main_valid
110  );
111 
112  // Create a validator for the parameter "Line Search"->"Polynomial"->"Max Iters"
113  // that accepts an 'int', a 'double' or a 'std::string' value!
116  linesearchMaxItersValiator = rcp(
119  AcceptedTypes(false).allowInt(true).allowDouble(true).allowString(true)
120  )
121  );
122  PL_Main_valid.sublist("Line Search").sublist("Polynomial").set(
123  "Max Iters",3
124  ,"The maximum number of inner linear search iterations allowed."
125  ,linesearchMaxItersValiator
126  );
127 
128  // Create a validator for the parameter "Direction"->"Newton"->"Linear Solver"->"Tol"
129  // that accepts a 'double' or a 'std::string' value!
132  linSolveTolValidator = rcp(
135  AcceptedTypes(false).allowDouble(true).allowString(true)
136  )
137  );
138  PL_Main_valid.sublist("Direction",true).sublist("Newton",true)
139  .sublist("Linear Solver",true).set(
140  "Tol", double(1e-5)
141  ,"Select the linear solve tolerance"
142  ,linSolveTolValidator
143  );
144 
145  return PL_Main_valid;
146 
147 }
148 
149 
150 //
151 // Unit tests
152 //
153 
154 
155 TEUCHOS_UNIT_TEST( ParameterList, construct_default )
156 {
157  ParameterList pl;
158  TEST_EQUALITY_CONST(pl.name(), "ANONYMOUS");
160 }
161 
162 
163 TEUCHOS_UNIT_TEST( ParameterList, construct_withName )
164 {
165  ParameterList pl("someName");
166  TEST_EQUALITY_CONST(pl.name(), "someName");
168 }
169 
170 
171 TEUCHOS_UNIT_TEST( ParameterList, createParameterList_empty )
172 {
173  RCP<ParameterList> pl = createParameterList();
174  TEST_ASSERT(nonnull(pl));
175  TEST_EQUALITY_CONST(pl->name(), "ANONYMOUS");
176 }
177 
178 
179 TEUCHOS_UNIT_TEST( ParameterList, createParameterList_withName )
180 {
181  RCP<ParameterList> pl = createParameterList("dummyName");
182  TEST_ASSERT(nonnull(pl));
183  TEST_EQUALITY_CONST(pl->name(), "dummyName");
184 }
185 
186 
188 {
189  ParameterList pl;
190 
191  out << "\n";
192  ECHO(pl.set("my int", 3));
193 
194  out << "\n";
195  ECHO(const ParameterEntry& my_int_c_param = getConst(pl).getEntry("my int"));
196  TEST_EQUALITY_CONST(my_int_c_param.isUsed(), false);
197  TEST_EQUALITY_CONST(my_int_c_param.isList(), false);
198  TEST_EQUALITY_CONST(my_int_c_param.isDefault(), false);
199  TEST_EQUALITY_CONST(my_int_c_param.docString(), "");
200  TEST_ASSERT(is_null(my_int_c_param.validator()));
201  TEST_EQUALITY_CONST(getValue<int>(my_int_c_param), 3);
202  ECHO(const bool param_isType_int1 = my_int_c_param.isType<int>());
203  TEST_EQUALITY_CONST(param_isType_int1, true);
204  ECHO(const bool param_isType_double1 = my_int_c_param.isType<double>());
205  TEST_EQUALITY_CONST(param_isType_double1, false);
206 
207  out << "\n";
208  ECHO(const ParameterEntry& my_int_param = pl.getEntry("my int"));
209  TEST_EQUALITY_CONST(my_int_param.isUsed(), true);
210 
211  out << "\n";
212  ECHO(const int my_int = pl.get<int>("my int"));
213  TEST_EQUALITY_CONST(my_int, 3);
214 
215 }
216 
217 
218 TEUCHOS_UNIT_TEST( ParameterList, param_isParameter_isSublist_isType )
219 {
220  ParameterList pl;
221  ECHO(pl.set("my int", 3));
222  ECHO(const int my_int = pl.get<int>("my int"));
223  TEST_EQUALITY_CONST(my_int, 3);
224  TEST_EQUALITY_CONST(pl.isParameter("my int"), true);
225  TEST_EQUALITY_CONST(pl.isParameter("Does not Exist"), false);
226  TEST_EQUALITY_CONST(pl.isSublist("my int"), false);
227  TEST_EQUALITY_CONST(pl.isSublist("Does not exist"), false);
228  TEST_EQUALITY_CONST(pl.isType<int>("my int"), true);
229  TEST_EQUALITY_CONST(pl.isType<double>("my int"), false);
230  TEST_EQUALITY_CONST(pl.isType("my int", static_cast<int*>(0)), true);
231  TEST_EQUALITY_CONST(pl.isType("my int", static_cast<double*>(0)), false);
232 }
233 
234 
235 TEUCHOS_UNIT_TEST( ParameterList, sublist_isParameter_isSublist_isType )
236 {
237  ParameterList pl;
238  ECHO(pl.sublist("my sublist").set("my int", 3));
239  ECHO(const int my_int = getConst(pl).sublist("my sublist").get<int>("my int"));
240  TEST_EQUALITY_CONST(my_int, 3);
241  TEST_EQUALITY_CONST(pl.isParameter("my sublist"), true); // Should be false, but backward compatiable!
242  TEST_EQUALITY_CONST(pl.isParameter("Does not Exist"), false);
243  TEST_EQUALITY_CONST(pl.isSublist("my sublist"), true);
244  TEST_EQUALITY_CONST(pl.isType<ParameterList>("my sublist"), true);
245  TEST_EQUALITY_CONST(pl.isType<double>("my sublist"), false);
246  TEST_EQUALITY_CONST(pl.isType("my sublist", static_cast<ParameterList*>(0)), true);
247  TEST_EQUALITY_CONST(pl.isType("my sublist", static_cast<double*>(0)), false);
248 }
249 
250 
252 {
253  ParameterList pl;
254  ECHO(pl.set("my int", 3, "Some documentation"));
255  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
256  TEST_EQUALITY_CONST(my_int_param.docString(), "Some documentation");
257  TEST_ASSERT(is_null(my_int_param.validator()));
258 }
259 
260 
261 TEUCHOS_UNIT_TEST( ParameterList, set_doc_validator )
262 {
263  ParameterList pl;
264  ECHO(pl.set("my int", 3, "Some documentation", rcp(new DummyValidator)));
265  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
266  TEST_EQUALITY_CONST(my_int_param.docString(), "Some documentation");
267  TEST_NOTHROW(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true));
268 }
269 
270 
271 TEUCHOS_UNIT_TEST( ParameterList, set_invalid_int_first )
272 {
273  ParameterList pl;
275  validator(new Teuchos::EnhancedNumberValidator<int>(0, 1)));
276  TEST_THROW(pl.set("my int", -1, "", validator),
279 }
280 
281 
282 TEUCHOS_UNIT_TEST( ParameterList, set_invalid_int_second )
283 {
284  ParameterList pl;
286  validator(new Teuchos::EnhancedNumberValidator<int>(0, 1)));
287  TEST_NOTHROW(pl.set("my int", 1, "", validator));
289  TEST_EQUALITY_CONST(pl.get<int>("my int"), 1);
291  TEST_EQUALITY_CONST(pl.get<int>("my int"), 1);
292 }
293 
294 
296 {
297  ParameterList pl;
298  ECHO(pl.setEntry("my int", ParameterEntry(as<int>(3), true, true, "Some doc", rcp(new DummyValidator))));
299  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
300  TEST_EQUALITY_CONST(my_int_param.docString(), "Some doc");
301  ECHO(const int my_int_1 = my_int_param.getValue<int>(0));
302  TEST_EQUALITY_CONST(my_int_1, 3);
303  TEST_EQUALITY_CONST(my_int_param.isUsed(), true);
304  TEST_EQUALITY_CONST(my_int_param.isList(), false); // The isList entry is ignored!
305  TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null);
306 }
307 
308 
309 TEUCHOS_UNIT_TEST( ParameterList, set_int_twice_keep_validator )
310 {
311  ParameterList pl;
312  ECHO(pl.setEntry("my int", ParameterEntry(as<int>(3), true, true, "Some doc", rcp(new DummyValidator))));
313  {
314  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
315  TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null);
316  }
317  TEST_EQUALITY_CONST(pl.get<int>("my int"), 3);
318  ECHO(pl.set("my int", 4));
319  TEST_EQUALITY_CONST(pl.get<int>("my int"), 4);
320  {
321  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
322  TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null);
323  }
324 }
325 
326 
327 TEUCHOS_UNIT_TEST( ParameterList, set_get_char_str )
328 {
329  ParameterList pl;
330 
331  ECHO(char dummy_str_1[] = "dummy str 1");
332  ECHO(pl.set("dummy 1", dummy_str_1));
333  ECHO(const std::string dummy_1 = pl.get<std::string>("dummy 1"));
334  TEST_EQUALITY_CONST(dummy_1, "dummy str 1");
335 
336  ECHO(const char dummy_str_const_2[] = "dummy str 2");
337  ECHO(pl.set("dummy 2", dummy_str_const_2));
338  ECHO(const std::string dummy_2 = pl.get<std::string>("dummy 2"));
339  TEST_EQUALITY_CONST(dummy_2, "dummy str 2");
340 
341 }
342 
343 
345 {
346  ParameterList pl;
347 
348  ECHO(const std::string dummy_str = "dummy str");
349  ECHO(pl.set("my str", dummy_str));
350  ECHO(const std::string my_str = pl.get<std::string>("my str"));
351  TEST_EQUALITY_CONST(my_str, "dummy str");
352 
353 }
354 
355 
356 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_param )
357 {
358  ParameterList pl;
359  TEST_THROW(pl.getEntry("Does not exist 1"), Exceptions::InvalidParameterName);
360  TEST_THROW(pl.get<int>("Does not exist 2"), Exceptions::InvalidParameterName);
361  TEST_THROW(getConst(pl).get<int>("Does not exist 3"), Exceptions::InvalidParameterName);
362  TEST_EQUALITY(pl.getPtr<int>("Does not exist 4"), static_cast<int*>(0));
363  TEST_EQUALITY(getConst(pl).getPtr<int>("Does not exist 5"), static_cast<const int*>(0));
364  ECHO(char raw_str[] = "dummy");
365  TEST_EQUALITY_CONST(pl.get("Does not exist 6", raw_str), "dummy");
366  ECHO(const char raw_c_str[] = "dummy");
367  TEST_EQUALITY_CONST(pl.get("Does not exist 7", raw_c_str), "dummy");
368  ECHO(const std::string str = "dummy");
369  TEST_EQUALITY_CONST(pl.get("Does not exist 8", str), "dummy");
370  TEST_THROW(pl.getEntry("Does not exist 9"), Exceptions::InvalidParameterName);
371  TEST_THROW(getConst(pl).getEntry("Does not exist 10"), Exceptions::InvalidParameterName);
372  TEST_EQUALITY(pl.getEntryPtr("Does not exist 11"), static_cast<ParameterEntry*>(0));
373  TEST_EQUALITY(getConst(pl).getEntryPtr("Does not exist 12"), static_cast<const ParameterEntry*>(0));
374  TEST_EQUALITY(pl.getEntryRCP("Does not exist 13"), RCP<ParameterEntry>());
375  TEST_EQUALITY(getConst(pl).getEntryRCP("Does not exist 14"), RCP<const ParameterEntry>());
376 }
377 
378 
379 TEUCHOS_UNIT_TEST( ParameterList, get_existing_incorrect_type )
380 {
381  ParameterList pl;
382  pl.set("my int", 4);
383  TEST_THROW(pl.get<double>("my int"), Exceptions::InvalidParameterType);
384  // ToDo: Assert the contents of the error message
385 }
386 
387 
389 {
390  ParameterList pl;
391  pl.set("my int", 4);
392  TEST_EQUALITY_CONST(pl.getPtr<int>("Does not Exist"), static_cast<int*>(0));
393  TEST_INEQUALITY_CONST(pl.getPtr<int>("my int"), static_cast<int*>(0));
394  TEST_EQUALITY_CONST(*pl.getPtr<int>("my int"), 4);
395  TEST_EQUALITY_CONST(pl.getPtr<double>("my int"), static_cast<double*>(0));
396  TEST_EQUALITY_CONST(getConst(pl).getPtr<int>("Does not Exist"), static_cast<const int*>(0));
397  TEST_INEQUALITY_CONST(getConst(pl).getPtr<int>("my int"), static_cast<int*>(0));
398  TEST_EQUALITY_CONST(*getConst(pl).getPtr<int>("my int"), 4);
399  TEST_EQUALITY_CONST(getConst(pl).getPtr<double>("my int"), static_cast<const double*>(0));
400 }
401 
402 
404 {
405  ParameterList pl;
406  pl.set("my int", 4);
407  TEST_EQUALITY_CONST(pl.getEntryRCP("Does not Exist"), null);
408  TEST_INEQUALITY_CONST(pl.getEntryRCP("my int"), null);
409  TEST_EQUALITY_CONST(pl.getEntryRCP("my int")->getValue<int>(0), 4);
410  TEST_EQUALITY_CONST(getConst(pl).getEntryRCP("Does not Exist"), null);
411  TEST_INEQUALITY_CONST(getConst(pl).getEntryRCP("my int"), null);
412  TEST_EQUALITY_CONST(getConst(pl).getEntryRCP("my int")->getValue<int>(0), 4);
413 }
414 
415 
416 // Test nonconstFind()
417 
418 // Test find()
419 
420 
421 TEUCHOS_UNIT_TEST( ParameterList, get_default_then_change )
422 {
423  ParameterList pl;
424  ECHO(int &my_int = pl.get("my int", 3));
425  TEST_EQUALITY_CONST(my_int, 3);
426  TEST_EQUALITY_CONST(pl.get<int>("my int"), 3);
427  ECHO(my_int = 5);
428  TEST_EQUALITY_CONST(pl.get<int>("my int"), 5);
429 }
430 
431 
433 {
434  ParameterList pl;
436  ECHO(pl.set("my int", 2));
438  TEST_EQUALITY_CONST(pl.get<int>("my int"), 2);
439  ECHO(const bool param_was_removed_1 = pl.remove("my int"));
440  TEST_EQUALITY_CONST(param_was_removed_1, true);
442  TEST_THROW(pl.get<int>("my int"), Exceptions::InvalidParameterName);
444  ECHO(const bool param_was_removed_2 = pl.remove("my int", false));
445  TEST_EQUALITY_CONST(param_was_removed_2, false);
446 }
447 
448 
449 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_default )
450 {
451  ParameterList pl("Base");
452  ECHO(pl.sublist("my sublist"));
453  ECHO(const ParameterEntry &sublistParam = pl.getEntry("my sublist"));
454  TEST_EQUALITY_CONST(sublistParam.isUsed(), false);
455  TEST_EQUALITY_CONST(sublistParam.isList(), true);
456  TEST_EQUALITY_CONST(sublistParam.isDefault(), false);
457  TEST_EQUALITY_CONST(sublistParam.docString(), "");
458  TEST_EQUALITY_CONST(sublistParam.getValue<ParameterList>(0).name(), "Base->my sublist");
459 }
460 
461 
462 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_docString )
463 {
464  ParameterList pl("Base");
465  ECHO(pl.sublist("my sublist", false, "My great sublist"));
466  ECHO(const ParameterEntry &sublistParam = pl.getEntry("my sublist"));
467  TEST_EQUALITY_CONST(sublistParam.isUsed(), false);
468  TEST_EQUALITY_CONST(sublistParam.isList(), true);
469  TEST_EQUALITY_CONST(sublistParam.isDefault(), false);
470  TEST_EQUALITY_CONST(sublistParam.docString(), "My great sublist");
471  TEST_EQUALITY_CONST(sublistParam.getValue<ParameterList>(0).name(), "Base->my sublist");
472 }
473 
474 
475 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_mustAlreadyExist )
476 {
477  ParameterList pl("Base");
478  TEST_THROW(pl.sublist("my sublist", true), Exceptions::InvalidParameterName);
479  // ToDo: Examine the actual structure of the error message
480 }
481 
482 
483 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_nonsublist )
484 {
485  ParameterList pl("Base");
486  ECHO(pl.set("my sublist", 1)); // Not a sublist!
488  // ToDo: Examine the actual structure of the error message
489 }
490 
491 
492 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_nonconst )
493 {
494  ParameterList pl("Base");
495  ECHO(pl.sublist("my sublist").set("my int", 2));
496  ECHO(const int my_int = pl.sublist("my sublist").get<int>("my int"));
497  TEST_EQUALITY_CONST(my_int, 2);
498 }
499 
500 
501 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_const )
502 {
503  ParameterList pl("Base");
504  ECHO(pl.sublist("my sublist").set("my int", 2));
505  ECHO(const int my_int = getConst(pl).sublist("my sublist").get<int>("my int"));
506  TEST_EQUALITY_CONST(my_int, 2);
507 }
508 
509 
510 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_const )
511 {
512  ParameterList pl("Base");
513  TEST_THROW(getConst(pl).sublist("my sublist"), Exceptions::InvalidParameterName);
514  // ToDo: Examine the actual structure of the error message
515 }
516 
517 
518 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_const_nonsublist )
519 {
520  ParameterList pl("Base");
521  ECHO(pl.set("my sublist", 1)); // Not a sublist!
522  TEST_THROW(getConst(pl).sublist("my sublist"), Exceptions::InvalidParameterType);
523  // ToDo: Examine the actual structure of the error message
524 }
525 
526 
528 {
529  ParameterList PL_Main("PL_Main");
530  const std::string Direction_Doc = "This sublist controls how direction is computed.";
531  ParameterList& PL_Direction = PL_Main.sublist("Direction", false, Direction_Doc);
532  ParameterList& PL_LineSearch = PL_Main.sublist("Line Search");
533  out << "PL_Main=\n" << PL_Main << "\n";
534  TEST_EQUALITY_CONST(PL_Main.name(), "PL_Main");
535  TEST_EQUALITY_CONST(PL_Main.isSublist("Direction"), true);
536  TEST_EQUALITY_CONST(PL_Main.isSublist("Line Search"), true);
537  ECHO(const ParameterList& PL_Direction_2 = getConst(PL_Main).sublist("Direction"));
538  TEST_EQUALITY(&PL_Direction, &PL_Direction_2);
539  ECHO(const ParameterList& PL_LineSearch_2 = getConst(PL_Main).sublist("Line Search"));
540  TEST_EQUALITY(&PL_LineSearch, &PL_LineSearch_2);
541  TEST_EQUALITY_CONST(getConst(PL_Main).sublist("Direction").name(), "PL_Main->Direction");
542  TEST_EQUALITY_CONST(PL_Direction.name(), "PL_Main->Direction");
543  TEST_EQUALITY_CONST(PL_LineSearch.name(), "PL_Main->Line Search");
544 }
545 
546 
547 TEUCHOS_UNIT_TEST( ParameterList, sublist_scenario_1 )
548 {
549  // This is the scenario in the orginal testing program
550  ParameterList PL_Main("PL_Main");
551  const std::string Direction_Doc = "This sublist controls how direction is computed.";
552  ParameterList &PL_Direction = PL_Main.sublist("Direction", false, Direction_Doc);
553  ParameterList &PL_Newton = PL_Direction.sublist("Newton");
554  ParameterList &PL_LinSol = PL_Newton.sublist("Linear Solver");
555  ParameterList &PL_LineSearch = PL_Main.sublist("Line Search");
556  out << "PL_Main=\n" << PL_Main << "\n";
557  TEST_EQUALITY_CONST(PL_Main.name(), "PL_Main");
558  TEST_EQUALITY_CONST(PL_Main.isSublist("Direction"), true);
559  ECHO(const ParameterList& PL_Direction_2 = getConst(PL_Main).sublist("Direction"));
560  TEST_EQUALITY(&PL_Direction, &PL_Direction_2);
561  TEST_EQUALITY_CONST(getConst(PL_Main).sublist("Direction").name(), "PL_Main->Direction");
562  TEST_EQUALITY_CONST(PL_Direction.name(), "PL_Main->Direction");
563  TEST_EQUALITY_CONST(PL_Direction.isSublist("Newton"), true);
564  TEST_EQUALITY_CONST(PL_Newton.isSublist("Linear Solver"), true);
565  TEST_EQUALITY_CONST(PL_Newton.name(), "PL_Main->Direction->Newton");
566  TEST_EQUALITY_CONST(PL_Newton.isSublist("Linear Solver"), true);
567  TEST_EQUALITY_CONST(PL_LinSol.name(), "PL_Main->Direction->Newton->Linear Solver");
568  TEST_EQUALITY_CONST(PL_Main.isSublist("Line Search"), true);
569  TEST_EQUALITY_CONST(PL_LineSearch.name(), "PL_Main->Line Search");
570 }
571 
572 
573 TEUCHOS_UNIT_TEST( ParameterList, copy_constructor )
574 {
575  ECHO(ParameterList pl1("A"));
576  ECHO(pl1.set("my int", 2));
577  ECHO(ParameterList pl2(pl1));
578  TEST_EQUALITY_CONST(pl2.name(), "A");
579  TEST_EQUALITY_CONST(pl2.get<int>("my int"), 2);
580 }
581 
582 
583 TEUCHOS_UNIT_TEST( ParameterList, assignment_operator )
584 {
585  ECHO(ParameterList pl1("A"));
586  ECHO(pl1.set("my int", 2));
587  ECHO(ParameterList pl2);
588  ECHO(const ParameterList &pl2_ref = pl2 = pl1);
589  TEST_EQUALITY_CONST(&pl2_ref, &pl2);
590  TEST_EQUALITY_CONST(pl2.name(), "A");
591  TEST_EQUALITY_CONST(pl2.get<int>("my int"), 2);
592 }
593 
594 
595 TEUCHOS_UNIT_TEST( ParameterList, iterator_params )
596 {
597  typedef ParameterList::ConstIterator ConstIter;
598  ParameterList pl;
599  pl.set("c", 1);
600  pl.set("a", 2);
601  pl.set("b", 3);
602  ConstIter pl_itr = pl.begin();
603  TEST_EQUALITY_CONST(pl_itr->first, "c");
604  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 1);
605  ECHO(++pl_itr);
606  TEST_EQUALITY_CONST(pl_itr->first, "a");
607  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 2);
608  ECHO(++pl_itr);
609  TEST_EQUALITY_CONST(pl_itr->first, "b");
610  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 3);
611  ECHO(++pl_itr);
612  TEST_ITER_EQUALITY(pl_itr, pl.end());
613 }
614 
615 
616 TEUCHOS_UNIT_TEST( ParameterList, iterator_params_sublists )
617 {
618  typedef ParameterList::ConstIterator ConstIter;
619  ParameterList pl("base");
620  pl.set("c", 1);
621  pl.sublist("a");
622  pl.set("b", 3);
623  ConstIter pl_itr = pl.begin();
624  TEST_EQUALITY_CONST(pl_itr->first, "c");
625  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 1);
626  ECHO(++pl_itr);
627  TEST_EQUALITY_CONST(pl_itr->first, "a");
628  TEST_EQUALITY_CONST(pl_itr->second.getValue<ParameterList>(0).name(), "base->a");
629  ECHO(++pl_itr);
630  TEST_EQUALITY_CONST(pl_itr->first, "b");
631  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 3);
632  ECHO(++pl_itr);
633  TEST_ITER_EQUALITY(pl_itr, pl.end());
634 }
635 
636 // Test iterator access after removing params
637 
638 // Test iterator access after removing sublists
639 
640 
641 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityWithEmpty )
642 {
643  // An empty list should not be equal to a full list
646  TEST_ASSERT( A == B );
647  A.set("Hello","World");
648  TEST_ASSERT( A != B );
649  B.set("Hello","World");
650  TEST_ASSERT( A == B );
651 }
652 
653 
654 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityDifferentSublistNames )
655 {
656  // Sublists with different names should not be equal
659  A.sublist("Bob");
660  B.sublist("Tom");
661  TEST_ASSERT( A != B );
662 }
663 
664 
665 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityDifferentLengths )
666 {
669  A.set("A","a");
670  A.set("B","b");
671  A.set("C","c");
672  A.print(out);
673 
674  B.set("A","a");
675  B.set("B","b");
676  B.print(out);
677 
678  TEST_ASSERT( A != B );
679 
680  B.set("C","c");
681  TEST_ASSERT( A == B );
682 }
683 
684 
685 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesWithEmpty )
686 {
689  TEST_ASSERT( haveSameValues(A,B) );
690  A.set("Hello","World");
691  TEST_ASSERT( !haveSameValues(A,B) );
692  B.set("Hello","World");
693  TEST_ASSERT( haveSameValues(A,B) );
694 }
695 
696 
697 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesDifferentSublistNames )
698 {
701  A.sublist("Smith").set("People",4);
702  B.sublist("Jones").set("People",4);
703  TEST_ASSERT( !haveSameValues(A,B) ); // sublist names matter
704 }
705 
706 
707 TEUCHOS_UNIT_TEST( ParameterList, validateAgainstSelf )
708 {
709  ParameterList PL_Main = createMainPL();
710  ParameterList PL_Main_valid = createValidMainPL();
711  TEST_NOTHROW(PL_Main.validateParameters(PL_Main_valid));
712 }
713 
714 
715 TEUCHOS_UNIT_TEST( ParameterList, validateParametersAndSetDefaults )
716 {
717  ParameterList PL_Main = createMainPL();
718  ParameterList PL_Main_valid = createValidMainPL();
719  ECHO(PL_Main.validateParametersAndSetDefaults(PL_Main_valid));
720  TEST_NOTHROW(
721  rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<int> >(
722  PL_Main.getEntry("Nonlinear Solver").validator(), true ) );
723 }
724 
725 
726 TEUCHOS_UNIT_TEST( ParameterList, getIntegralValue_int )
727 {
728  ParameterList PL_Main = createMainPL();
729  ParameterList PL_Main_valid = createValidMainPL();
730  ECHO(PL_Main.set("Nonlinear Solver", "Line Search Based"));
731  ECHO(PL_Main.validateParametersAndSetDefaults(PL_Main_valid));
732  ECHO(const int lineSearchValue = getIntegralValue<int>(PL_Main, "Nonlinear Solver"));
733  TEST_EQUALITY_CONST(lineSearchValue, 0);
734  ECHO(PL_Main.set("Nonlinear Solver", "Trust Region Based"));
735  ECHO(const int trustRegionValue = getIntegralValue<int>(PL_Main, "Nonlinear Solver"));
736  TEST_EQUALITY_CONST(trustRegionValue, 1);
737 }
738 
739 
740 } // namespace Teuchos
741 
742 
743 
ParameterList createMainPL()
C++ Standard Library compatable filtered iterator.
#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.
Definition: PackageB.cpp:3
#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.
ConstIterator begin() const
An iterator pointing to the first entry.
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 "value" 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 TEST_ITER_EQUALITY(iter1, iter2)
Assert that two iterators are equal.
bool isSublist(const std::string &name) const
Whether the given sublist exists in this list.
#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.
ParameterList createValidMainPL()
Definition: PackageA.cpp:3
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.
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.
ConstIterator end() const
An iterator pointing beyond the last entry.
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.
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...
bool isType(const std::string &name) const
Whether the given parameter exists in this list and has type T.
virtual ValidStringsList validStringValues() const =0
Return an array of strings of valid values if applicable.
RCP< const ParameterEntryValidator > validator() const
Return the (optional) validator object.
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.
ParameterList & setParameters(const ParameterList &source)
Abstract interface for an object that can validate a ParameterEntry&#39;s value.
const std::string & name() const
The name of this ParameterList.
Class uesd to validate a particular type of number.
TEST_ASSERT(castedDep1->getValuesAndValidators().size()==2)
bool isParameter(const std::string &name) const
Whether the given parameter exists in this list.
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.
TEST_EQUALITY(rcp_dynamic_cast< const EnhancedNumberValidator< double > >(castedDep1->getValuesAndValidators().find("val1") ->second, true) ->getMax(), double1Vali->getMax())
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.
Definition of Teuchos::as, for conversions between types.
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 ...