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"
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  linesearchMaxItersValidator = 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  ,linesearchMaxItersValidator
126  );
127 
128  // Create a validator for the parameter "Direction"->"Newton"->"Linear Solver"->"Tol"
129  // that accepts a 'double' or a 'std::string' value!
131  linSolveTolValidator = rcp(
134  AcceptedTypes(false).allowDouble(true).allowString(true)
135  )
136  );
137  PL_Main_valid.sublist("Direction",true).sublist("Newton",true)
138  .sublist("Linear Solver",true).set(
139  "Tol", double(1e-5)
140  ,"Select the linear solve tolerance"
141  ,linSolveTolValidator
142  );
143 
144  // Create a validator for the parameter "Elements"
145  // that accepts an 'int', a 'long long' or a 'std::string' value!
147  elementsValidator = rcp(
150  AcceptedTypes(false).allowInt(true).allowLongLong(true).allowString(true)
151  )
152  );
153  typedef long long LL;
154  PL_Main_valid.set(
155  "Elements", LL(72057594037927936ll) // 2^56
156  ,"Number of finite elements to generate"
157  ,elementsValidator
158  );
159 
160  return PL_Main_valid;
161 
162 }
163 
164 
165 //
166 // Unit tests
167 //
168 
169 
170 TEUCHOS_UNIT_TEST( ParameterList, construct_default )
171 {
172  ParameterList pl;
173  TEST_EQUALITY_CONST(pl.name(), "ANONYMOUS");
175 }
176 
177 
178 TEUCHOS_UNIT_TEST( ParameterList, construct_withName )
179 {
180  ParameterList pl("someName");
181  TEST_EQUALITY_CONST(pl.name(), "someName");
183 }
184 
185 
186 TEUCHOS_UNIT_TEST( ParameterList, createParameterList_empty )
187 {
188  RCP<ParameterList> pl = createParameterList();
189  TEST_ASSERT(nonnull(pl));
190  TEST_EQUALITY_CONST(pl->name(), "ANONYMOUS");
191 }
192 
193 
194 TEUCHOS_UNIT_TEST( ParameterList, createParameterList_withName )
195 {
196  RCP<ParameterList> pl = createParameterList("dummyName");
197  TEST_ASSERT(nonnull(pl));
198  TEST_EQUALITY_CONST(pl->name(), "dummyName");
199 }
200 
201 
203 {
204  ParameterList pl;
205 
206  out << "\n";
207  ECHO(pl.set("my int", 3));
208 
209  out << "\n";
210  ECHO(const ParameterEntry& my_int_c_param = getConst(pl).getEntry("my int"));
211  TEST_EQUALITY_CONST(my_int_c_param.isUsed(), false);
212  TEST_EQUALITY_CONST(my_int_c_param.isList(), false);
213  TEST_EQUALITY_CONST(my_int_c_param.isDefault(), false);
214  TEST_EQUALITY_CONST(my_int_c_param.docString(), "");
215  TEST_ASSERT(is_null(my_int_c_param.validator()));
216  TEST_EQUALITY_CONST(getValue<int>(my_int_c_param), 3);
217  ECHO(const bool param_isType_int1 = my_int_c_param.isType<int>());
218  TEST_EQUALITY_CONST(param_isType_int1, true);
219  ECHO(const bool param_isType_double1 = my_int_c_param.isType<double>());
220  TEST_EQUALITY_CONST(param_isType_double1, false);
221 
222  out << "\n";
223  ECHO(const ParameterEntry& my_int_param = pl.getEntry("my int"));
224  TEST_EQUALITY_CONST(my_int_param.isUsed(), true);
225 
226  out << "\n";
227  ECHO(const int my_int = pl.get<int>("my int"));
228  TEST_EQUALITY_CONST(my_int, 3);
229 
230 }
231 
232 
233 TEUCHOS_UNIT_TEST( ParameterList, param_isParameter_isSublist_isType )
234 {
235  ParameterList pl;
236  ECHO(pl.set("my int", 3));
237  ECHO(const int my_int = pl.get<int>("my int"));
238  TEST_EQUALITY_CONST(my_int, 3);
239  TEST_EQUALITY_CONST(pl.isParameter("my int"), true);
240  TEST_EQUALITY_CONST(pl.isParameter("Does not Exist"), false);
241  TEST_EQUALITY_CONST(pl.isSublist("my int"), false);
242  TEST_EQUALITY_CONST(pl.isSublist("Does not exist"), false);
243  TEST_EQUALITY_CONST(pl.isType<int>("my int"), true);
244  TEST_EQUALITY_CONST(pl.isType<double>("my int"), false);
245  TEST_EQUALITY_CONST(pl.isType("my int", static_cast<int*>(0)), true);
246  TEST_EQUALITY_CONST(pl.isType("my int", static_cast<double*>(0)), false);
247 }
248 
249 
250 TEUCHOS_UNIT_TEST( ParameterList, sublist_isParameter_isSublist_isType )
251 {
252  ParameterList pl;
253  ECHO(pl.sublist("my sublist").set("my int", 3));
254  ECHO(const int my_int = getConst(pl).sublist("my sublist").get<int>("my int"));
255  TEST_EQUALITY_CONST(my_int, 3);
256  TEST_EQUALITY_CONST(pl.isParameter("my sublist"), true); // Should be false, but backward compatiable!
257  TEST_EQUALITY_CONST(pl.isParameter("Does not Exist"), false);
258  TEST_EQUALITY_CONST(pl.isSublist("my sublist"), true);
259  TEST_EQUALITY_CONST(pl.isType<ParameterList>("my sublist"), true);
260  TEST_EQUALITY_CONST(pl.isType<double>("my sublist"), false);
261  TEST_EQUALITY_CONST(pl.isType("my sublist", static_cast<ParameterList*>(0)), true);
262  TEST_EQUALITY_CONST(pl.isType("my sublist", static_cast<double*>(0)), false);
263 }
264 
265 
267 {
268  ParameterList pl;
269  ECHO(pl.set("my int", 3, "Some documentation"));
270  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
271  TEST_EQUALITY_CONST(my_int_param.docString(), "Some documentation");
272  TEST_ASSERT(is_null(my_int_param.validator()));
273 }
274 
275 
276 TEUCHOS_UNIT_TEST( ParameterList, set_doc_validator )
277 {
278  ParameterList pl;
279  ECHO(pl.set("my int", 3, "Some documentation", rcp(new DummyValidator)));
280  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
281  TEST_EQUALITY_CONST(my_int_param.docString(), "Some documentation");
282  TEST_NOTHROW(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true));
283 }
284 
285 
286 TEUCHOS_UNIT_TEST( ParameterList, set_invalid_int_first )
287 {
288  ParameterList pl;
290  validator(new Teuchos::EnhancedNumberValidator<int>(0, 1)));
291  TEST_THROW(pl.set("my int", -1, "", validator),
294 }
295 
296 
297 TEUCHOS_UNIT_TEST( ParameterList, set_invalid_int_second )
298 {
299  ParameterList pl;
301  validator(new Teuchos::EnhancedNumberValidator<int>(0, 1)));
302  TEST_NOTHROW(pl.set("my int", 1, "", validator));
304  TEST_EQUALITY_CONST(pl.get<int>("my int"), 1);
306  TEST_EQUALITY_CONST(pl.get<int>("my int"), 1);
307 }
308 
309 
311 {
312  ParameterList pl;
313  ECHO(pl.setEntry("my int", ParameterEntry(as<int>(3), true, true, "Some doc", rcp(new DummyValidator))));
314  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
315  TEST_EQUALITY_CONST(my_int_param.docString(), "Some doc");
316  ECHO(const int my_int_1 = my_int_param.getValue<int>(0));
317  TEST_EQUALITY_CONST(my_int_1, 3);
318  TEST_EQUALITY_CONST(my_int_param.isUsed(), true);
319  TEST_EQUALITY_CONST(my_int_param.isList(), false); // The isList entry is ignored!
320  TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null);
321 }
322 
323 
324 TEUCHOS_UNIT_TEST( ParameterList, set_int_twice_keep_validator )
325 {
326  ParameterList pl;
327  ECHO(pl.setEntry("my int", ParameterEntry(as<int>(3), true, true, "Some doc", rcp(new DummyValidator))));
328  {
329  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
330  TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null);
331  }
332  TEST_EQUALITY_CONST(pl.get<int>("my int"), 3);
333  ECHO(pl.set("my int", 4));
334  TEST_EQUALITY_CONST(pl.get<int>("my int"), 4);
335  {
336  ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int"));
337  TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null);
338  }
339 }
340 
341 
342 TEUCHOS_UNIT_TEST( ParameterList, set_get_char_str )
343 {
344  ParameterList pl;
345 
346  ECHO(char dummy_str_1[] = "dummy str 1");
347  ECHO(pl.set("dummy 1", dummy_str_1));
348  ECHO(const std::string dummy_1 = pl.get<std::string>("dummy 1"));
349  TEST_EQUALITY_CONST(dummy_1, "dummy str 1");
350 
351  ECHO(const char dummy_str_const_2[] = "dummy str 2");
352  ECHO(pl.set("dummy 2", dummy_str_const_2));
353  ECHO(const std::string dummy_2 = pl.get<std::string>("dummy 2"));
354  TEST_EQUALITY_CONST(dummy_2, "dummy str 2");
355 
356 }
357 
358 
360 {
361  ParameterList pl;
362 
363  ECHO(const std::string dummy_str = "dummy str");
364  ECHO(pl.set("my str", dummy_str));
365  ECHO(const std::string my_str = pl.get<std::string>("my str"));
366  TEST_EQUALITY_CONST(my_str, "dummy str");
367 
368 }
369 
370 
371 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_param )
372 {
373  ParameterList pl;
374  TEST_THROW(pl.getEntry("Does not exist 1"), Exceptions::InvalidParameterName);
375  TEST_THROW(pl.get<int>("Does not exist 2"), Exceptions::InvalidParameterName);
376  TEST_THROW(getConst(pl).get<int>("Does not exist 3"), Exceptions::InvalidParameterName);
377  TEST_EQUALITY(pl.getPtr<int>("Does not exist 4"), static_cast<int*>(0));
378  TEST_EQUALITY(getConst(pl).getPtr<int>("Does not exist 5"), static_cast<const int*>(0));
379  ECHO(char raw_str[] = "dummy");
380  TEST_EQUALITY_CONST(pl.get("Does not exist 6", raw_str), "dummy");
381  ECHO(const char raw_c_str[] = "dummy");
382  TEST_EQUALITY_CONST(pl.get("Does not exist 7", raw_c_str), "dummy");
383  ECHO(const std::string str = "dummy");
384  TEST_EQUALITY_CONST(pl.get("Does not exist 8", str), "dummy");
385  TEST_THROW(pl.getEntry("Does not exist 9"), Exceptions::InvalidParameterName);
386  TEST_THROW(getConst(pl).getEntry("Does not exist 10"), Exceptions::InvalidParameterName);
387  TEST_EQUALITY(pl.getEntryPtr("Does not exist 11"), static_cast<ParameterEntry*>(0));
388  TEST_EQUALITY(getConst(pl).getEntryPtr("Does not exist 12"), static_cast<const ParameterEntry*>(0));
389  TEST_EQUALITY(pl.getEntryRCP("Does not exist 13"), RCP<ParameterEntry>());
390  TEST_EQUALITY(getConst(pl).getEntryRCP("Does not exist 14"), RCP<const ParameterEntry>());
391 }
392 
393 
394 TEUCHOS_UNIT_TEST( ParameterList, get_existing_incorrect_type )
395 {
396  ParameterList pl;
397  pl.set("my int", 4);
398  TEST_THROW(pl.get<double>("my int"), Exceptions::InvalidParameterType);
399  // ToDo: Assert the contents of the error message
400 }
401 
402 
404 {
405  ParameterList pl;
406  pl.set("my int", 4);
407  TEST_EQUALITY_CONST(pl.getPtr<int>("Does not Exist"), static_cast<int*>(0));
408  TEST_INEQUALITY_CONST(pl.getPtr<int>("my int"), static_cast<int*>(0));
409  TEST_EQUALITY_CONST(*pl.getPtr<int>("my int"), 4);
410  TEST_EQUALITY_CONST(pl.getPtr<double>("my int"), static_cast<double*>(0));
411  TEST_EQUALITY_CONST(getConst(pl).getPtr<int>("Does not Exist"), static_cast<const int*>(0));
412  TEST_INEQUALITY_CONST(getConst(pl).getPtr<int>("my int"), static_cast<int*>(0));
413  TEST_EQUALITY_CONST(*getConst(pl).getPtr<int>("my int"), 4);
414  TEST_EQUALITY_CONST(getConst(pl).getPtr<double>("my int"), static_cast<const double*>(0));
415 }
416 
417 
419 {
420  ParameterList pl;
421  pl.set("my int", 4);
422  TEST_EQUALITY_CONST(pl.getEntryRCP("Does not Exist"), null);
423  TEST_INEQUALITY_CONST(pl.getEntryRCP("my int"), null);
424  TEST_EQUALITY_CONST(pl.getEntryRCP("my int")->getValue<int>(0), 4);
425  TEST_EQUALITY_CONST(getConst(pl).getEntryRCP("Does not Exist"), null);
426  TEST_INEQUALITY_CONST(getConst(pl).getEntryRCP("my int"), null);
427  TEST_EQUALITY_CONST(getConst(pl).getEntryRCP("my int")->getValue<int>(0), 4);
428 }
429 
430 
431 // Test nonconstFind()
432 
433 // Test find()
434 
435 
436 TEUCHOS_UNIT_TEST( ParameterList, get_default_then_change )
437 {
438  ParameterList pl;
439  ECHO(int &my_int = pl.get("my int", 3));
440  TEST_EQUALITY_CONST(my_int, 3);
441  TEST_EQUALITY_CONST(pl.get<int>("my int"), 3);
442  ECHO(my_int = 5);
443  TEST_EQUALITY_CONST(pl.get<int>("my int"), 5);
444 }
445 
446 
448 {
449  ParameterList pl;
451  ECHO(pl.set("my int", 2));
453  TEST_EQUALITY_CONST(pl.get<int>("my int"), 2);
454  ECHO(const bool param_was_removed_1 = pl.remove("my int"));
455  TEST_EQUALITY_CONST(param_was_removed_1, true);
457  TEST_THROW(pl.get<int>("my int"), Exceptions::InvalidParameterName);
459  ECHO(const bool param_was_removed_2 = pl.remove("my int", false));
460  TEST_EQUALITY_CONST(param_was_removed_2, false);
461 }
462 
463 
464 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_default )
465 {
466  ParameterList pl("Base");
467  ECHO(pl.sublist("my sublist"));
468  ECHO(const ParameterEntry &sublistParam = pl.getEntry("my sublist"));
469  TEST_EQUALITY_CONST(sublistParam.isUsed(), false);
470  TEST_EQUALITY_CONST(sublistParam.isList(), true);
471  TEST_EQUALITY_CONST(sublistParam.isDefault(), false);
472  TEST_EQUALITY_CONST(sublistParam.docString(), "");
473  TEST_EQUALITY_CONST(sublistParam.getValue<ParameterList>(0).name(), "Base->my sublist");
474 }
475 
476 
477 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_docString )
478 {
479  ParameterList pl("Base");
480  ECHO(pl.sublist("my sublist", false, "My great sublist"));
481  ECHO(const ParameterEntry &sublistParam = pl.getEntry("my sublist"));
482  TEST_EQUALITY_CONST(sublistParam.isUsed(), false);
483  TEST_EQUALITY_CONST(sublistParam.isList(), true);
484  TEST_EQUALITY_CONST(sublistParam.isDefault(), false);
485  TEST_EQUALITY_CONST(sublistParam.docString(), "My great sublist");
486  TEST_EQUALITY_CONST(sublistParam.getValue<ParameterList>(0).name(), "Base->my sublist");
487 }
488 
489 
490 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_mustAlreadyExist )
491 {
492  ParameterList pl("Base");
493  TEST_THROW(pl.sublist("my sublist", true), Exceptions::InvalidParameterName);
494  // ToDo: Examine the actual structure of the error message
495 }
496 
497 
498 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_nonsublist )
499 {
500  ParameterList pl("Base");
501  ECHO(pl.set("my sublist", 1)); // Not a sublist!
503  // ToDo: Examine the actual structure of the error message
504 }
505 
506 
507 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_nonconst )
508 {
509  ParameterList pl("Base");
510  ECHO(pl.sublist("my sublist").set("my int", 2));
511  ECHO(const int my_int = pl.sublist("my sublist").get<int>("my int"));
512  TEST_EQUALITY_CONST(my_int, 2);
513 }
514 
515 
516 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_const )
517 {
518  ParameterList pl("Base");
519  ECHO(pl.sublist("my sublist").set("my int", 2));
520  ECHO(const int my_int = getConst(pl).sublist("my sublist").get<int>("my int"));
521  TEST_EQUALITY_CONST(my_int, 2);
522 }
523 
524 
525 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_const )
526 {
527  ParameterList pl("Base");
528  TEST_THROW(getConst(pl).sublist("my sublist"), Exceptions::InvalidParameterName);
529  // ToDo: Examine the actual structure of the error message
530 }
531 
532 
533 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_const_nonsublist )
534 {
535  ParameterList pl("Base");
536  ECHO(pl.set("my sublist", 1)); // Not a sublist!
537  TEST_THROW(getConst(pl).sublist("my sublist"), Exceptions::InvalidParameterType);
538  // ToDo: Examine the actual structure of the error message
539 }
540 
541 
543 {
544  ParameterList PL_Main("PL_Main");
545  const std::string Direction_Doc = "This sublist controls how direction is computed.";
546  ParameterList& PL_Direction = PL_Main.sublist("Direction", false, Direction_Doc);
547  ParameterList& PL_LineSearch = PL_Main.sublist("Line Search");
548  out << "PL_Main=\n" << PL_Main << "\n";
549  TEST_EQUALITY_CONST(PL_Main.name(), "PL_Main");
550  TEST_EQUALITY_CONST(PL_Main.isSublist("Direction"), true);
551  TEST_EQUALITY_CONST(PL_Main.isSublist("Line Search"), true);
552  ECHO(const ParameterList& PL_Direction_2 = getConst(PL_Main).sublist("Direction"));
553  TEST_EQUALITY(&PL_Direction, &PL_Direction_2);
554  ECHO(const ParameterList& PL_LineSearch_2 = getConst(PL_Main).sublist("Line Search"));
555  TEST_EQUALITY(&PL_LineSearch, &PL_LineSearch_2);
556  TEST_EQUALITY_CONST(getConst(PL_Main).sublist("Direction").name(), "PL_Main->Direction");
557  TEST_EQUALITY_CONST(PL_Direction.name(), "PL_Main->Direction");
558  TEST_EQUALITY_CONST(PL_LineSearch.name(), "PL_Main->Line Search");
559 }
560 
561 
562 TEUCHOS_UNIT_TEST( ParameterList, sublist_scenario_1 )
563 {
564  // This is the scenario in the orginal testing program
565  ParameterList PL_Main("PL_Main");
566  const std::string Direction_Doc = "This sublist controls how direction is computed.";
567  ParameterList &PL_Direction = PL_Main.sublist("Direction", false, Direction_Doc);
568  ParameterList &PL_Newton = PL_Direction.sublist("Newton");
569  ParameterList &PL_LinSol = PL_Newton.sublist("Linear Solver");
570  ParameterList &PL_LineSearch = PL_Main.sublist("Line Search");
571  out << "PL_Main=\n" << PL_Main << "\n";
572  TEST_EQUALITY_CONST(PL_Main.name(), "PL_Main");
573  TEST_EQUALITY_CONST(PL_Main.isSublist("Direction"), true);
574  ECHO(const ParameterList& PL_Direction_2 = getConst(PL_Main).sublist("Direction"));
575  TEST_EQUALITY(&PL_Direction, &PL_Direction_2);
576  TEST_EQUALITY_CONST(getConst(PL_Main).sublist("Direction").name(), "PL_Main->Direction");
577  TEST_EQUALITY_CONST(PL_Direction.name(), "PL_Main->Direction");
578  TEST_EQUALITY_CONST(PL_Direction.isSublist("Newton"), true);
579  TEST_EQUALITY_CONST(PL_Newton.isSublist("Linear Solver"), true);
580  TEST_EQUALITY_CONST(PL_Newton.name(), "PL_Main->Direction->Newton");
581  TEST_EQUALITY_CONST(PL_Newton.isSublist("Linear Solver"), true);
582  TEST_EQUALITY_CONST(PL_LinSol.name(), "PL_Main->Direction->Newton->Linear Solver");
583  TEST_EQUALITY_CONST(PL_Main.isSublist("Line Search"), true);
584  TEST_EQUALITY_CONST(PL_LineSearch.name(), "PL_Main->Line Search");
585 }
586 
587 
588 TEUCHOS_UNIT_TEST( ParameterList, copy_constructor )
589 {
590  ECHO(ParameterList pl1("A"));
591  ECHO(pl1.set("my int", 2));
592  ECHO(ParameterList pl2(pl1));
593  TEST_EQUALITY_CONST(pl2.name(), "A");
594  TEST_EQUALITY_CONST(pl2.get<int>("my int"), 2);
595 }
596 
597 
598 TEUCHOS_UNIT_TEST( ParameterList, assignment_operator )
599 {
600  ECHO(ParameterList pl1("A"));
601  ECHO(pl1.set("my int", 2));
602  ECHO(ParameterList pl2);
603  ECHO(const ParameterList &pl2_ref = pl2 = pl1);
604  TEST_EQUALITY_CONST(&pl2_ref, &pl2);
605  TEST_EQUALITY_CONST(pl2.name(), "A");
606  TEST_EQUALITY_CONST(pl2.get<int>("my int"), 2);
607 }
608 
609 
610 TEUCHOS_UNIT_TEST( ParameterList, iterator_params )
611 {
612  typedef ParameterList::ConstIterator ConstIter;
613  ParameterList pl;
614  pl.set("c", 1);
615  pl.set("a", 2);
616  pl.set("b", 3);
617  ConstIter pl_itr = pl.begin();
618  TEST_EQUALITY_CONST(pl_itr->first, "c");
619  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 1);
620  ECHO(++pl_itr);
621  TEST_EQUALITY_CONST(pl_itr->first, "a");
622  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 2);
623  ECHO(++pl_itr);
624  TEST_EQUALITY_CONST(pl_itr->first, "b");
625  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 3);
626  ECHO(++pl_itr);
627  TEST_ITER_EQUALITY(pl_itr, pl.end());
628 }
629 
630 
631 TEUCHOS_UNIT_TEST( ParameterList, iterator_params_sublists )
632 {
633  typedef ParameterList::ConstIterator ConstIter;
634  ParameterList pl("base");
635  pl.set("c", 1);
636  pl.sublist("a");
637  pl.set("b", 3);
638  ConstIter pl_itr = pl.begin();
639  TEST_EQUALITY_CONST(pl_itr->first, "c");
640  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 1);
641  ECHO(++pl_itr);
642  TEST_EQUALITY_CONST(pl_itr->first, "a");
643  TEST_EQUALITY_CONST(pl_itr->second.getValue<ParameterList>(0).name(), "base->a");
644  ECHO(++pl_itr);
645  TEST_EQUALITY_CONST(pl_itr->first, "b");
646  TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 3);
647  ECHO(++pl_itr);
648  TEST_ITER_EQUALITY(pl_itr, pl.end());
649 }
650 
651 // Test iterator access after removing params
652 
653 // Test iterator access after removing sublists
654 
655 
656 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityWithEmpty )
657 {
658  // An empty list should not be equal to a full list
661  TEST_ASSERT( A == B );
662  A.set("Hello","World");
663  TEST_ASSERT( A != B );
664  B.set("Hello","World");
665  TEST_ASSERT( A == B );
666 }
667 
668 
669 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityDifferentSublistNames )
670 {
671  // Sublists with different names should not be equal
674  A.sublist("Bob");
675  B.sublist("Tom");
676  TEST_ASSERT( A != B );
677 }
678 
679 
680 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityDifferentLengths )
681 {
684  A.set("A","a");
685  A.set("B","b");
686  A.set("C","c");
687  A.print(out);
688 
689  B.set("A","a");
690  B.set("B","b");
691  B.print(out);
692 
693  TEST_ASSERT( A != B );
694 
695  B.set("C","c");
696  TEST_ASSERT( A == B );
697 }
698 
699 
700 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesWithEmpty )
701 {
704  TEST_ASSERT( haveSameValues(A,B) );
705  A.set("a",1);
706  TEST_ASSERT( !haveSameValues(A,B) );
707  A.set("b",2);
708  B.set("a",1);
709  B.set("b",2);
710  TEST_ASSERT( haveSameValues(A,B) );
711 }
712 
713 
714 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesDifferentSublistNames )
715 {
718  A.sublist("Smith").set("People",4);
719  B.sublist("Jones").set("People",4);
720  TEST_ASSERT( !haveSameValues(A,B) ); // sublist names matter
721 }
722 
723 
724 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesSortedReversedOrder )
725 {
726  ParameterList A, B;
727  A.set("a",1);
728  A.set("b",2);
729  // Create second list with the same entries but different order
730  B.set("b",2);
731  B.set("a",1);
732  TEST_ASSERT( haveSameValuesSorted(A,B) );
733  B.set("c",3);
734  TEST_ASSERT( !haveSameValuesSorted(A,B) ); // check for length
735 }
736 
737 
738 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesSortedNested)
739 {
740  ParameterList A, B;
741  ParameterList &asublist = A.sublist("A");
742  asublist.set("a",1);
743  asublist.set("b",2);
744  ParameterList &bsublist = B.sublist("A");
745  bsublist.set("a",1);
746  bsublist.set("b",2);
747  TEST_ASSERT( haveSameValuesSorted(A,B) );
748  asublist.set("c",3);
749  bsublist.set("c",4);
750  TEST_ASSERT( !haveSameValuesSorted(A,B) );
751 }
752 
753 
754 TEUCHOS_UNIT_TEST( ParameterList, validateAgainstSelf )
755 {
756  ParameterList PL_Main = createMainPL();
757  ParameterList PL_Main_valid = createValidMainPL();
758  TEST_NOTHROW(PL_Main.validateParameters(PL_Main_valid));
759 }
760 
761 
762 TEUCHOS_UNIT_TEST( ParameterList, validateParametersAndSetDefaults )
763 {
764  ParameterList PL_Main = createMainPL();
765  ParameterList PL_Main_valid = createValidMainPL();
766  ECHO(PL_Main.validateParametersAndSetDefaults(PL_Main_valid));
767  TEST_NOTHROW(
768  rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<int> >(
769  PL_Main.getEntry("Nonlinear Solver").validator(), true ) );
770 }
771 
772 
773 TEUCHOS_UNIT_TEST( ParameterList, getIntegralValue_int )
774 {
775  ParameterList PL_Main = createMainPL();
776  ParameterList PL_Main_valid = createValidMainPL();
777  ECHO(PL_Main.set("Nonlinear Solver", "Line Search Based"));
778  ECHO(PL_Main.validateParametersAndSetDefaults(PL_Main_valid));
779  ECHO(const int lineSearchValue = getIntegralValue<int>(PL_Main, "Nonlinear Solver"));
780  TEST_EQUALITY_CONST(lineSearchValue, 0);
781  ECHO(PL_Main.set("Nonlinear Solver", "Trust Region Based"));
782  ECHO(const int trustRegionValue = getIntegralValue<int>(PL_Main, "Nonlinear Solver"));
783  TEST_EQUALITY_CONST(trustRegionValue, 1);
784 }
785 
786 
787 } // namespace Teuchos
788 
789 
790 
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.
#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.
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 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.
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.
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.
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...
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)
Abstract interface for an object that can validate a ParameterEntry&#39;s value.
Class uesd to validate a particular type of number.
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.
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 ...