|
Teuchos Package Browser (Single Doxygen Collection)
Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Teuchos: Common Tools Package 00005 // Copyright (2004) Sandia Corporation 00006 // 00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00008 // license for use of this work by or on behalf of the U.S. Government. 00009 // 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are 00012 // met: 00013 // 00014 // 1. Redistributions of source code must retain the above copyright 00015 // notice, this list of conditions and the following disclaimer. 00016 // 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // 3. Neither the name of the Corporation nor the names of the 00022 // contributors may be used to endorse or promote products derived from 00023 // this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00038 // 00039 // *********************************************************************** 00040 // @HEADER 00041 00042 #include "Teuchos_ParameterList.hpp" 00043 #include "Teuchos_getConst.hpp" 00044 #include "Teuchos_as.hpp" 00045 #include "Teuchos_StandardParameterEntryValidators.hpp" 00046 #include "Teuchos_UnitTestHarness.hpp" 00047 00048 00049 // 00050 // Utilities 00051 // 00052 00053 00054 namespace { 00055 00056 00057 class DummyValidator : public Teuchos::ParameterEntryValidator 00058 { 00059 public: 00060 00061 const std::string getXMLTypeName() const { return ""; } 00062 virtual void printDoc(std::string const& docString, std::ostream &out) const {} 00063 virtual ValidStringsList validStringValues() const { return Teuchos::null; } 00064 virtual void validate( 00065 Teuchos::ParameterEntry const& entry, 00066 std::string const& paramName, 00067 std::string const& sublistName 00068 ) const 00069 {} 00070 }; 00071 00072 00073 } // namespace 00074 00075 00076 namespace Teuchos { 00077 00078 00079 // 00080 // Test help utilities 00081 // 00082 00083 00084 ParameterList createMainPL() 00085 { 00086 ParameterList PL_Main("PL_Main"); 00087 const std::string Direction_Doc = "This sublist controls how direction is computed."; 00088 ParameterList &PL_Direction = PL_Main.sublist("Direction", false, Direction_Doc); 00089 ParameterList &PL_Newton = PL_Direction.sublist("Newton"); 00090 PL_Newton.sublist("Linear Solver"); 00091 PL_Main.sublist("Line Search"); 00092 return PL_Main; 00093 } 00094 00095 00096 00097 ParameterList createValidMainPL() 00098 { 00099 00100 ParameterList PL_Main_valid("PL_Main_valid"); 00101 PL_Main_valid.setParameters(createMainPL()); 00102 00103 // Create a validator for the "Nonlinear Solver" parameter 00104 setStringToIntegralParameter<int>( 00105 "Nonlinear Solver", 00106 "Line Search Based", 00107 "Selects the type of nonlinear solver to use", 00108 tuple<std::string>("Line Search Based","Trust Region Based"), 00109 &PL_Main_valid 00110 ); 00111 00112 // Create a validator for the parameter "Line Search"->"Polynomial"->"Max Iters" 00113 // that accepts an 'int', a 'double' or a 'std::string' value! 00114 typedef Teuchos::AnyNumberParameterEntryValidator::AcceptedTypes AcceptedTypes; 00115 Teuchos::RCP<Teuchos::AnyNumberParameterEntryValidator> 00116 linesearchMaxItersValiator = rcp( 00117 new Teuchos::AnyNumberParameterEntryValidator( 00118 Teuchos::AnyNumberParameterEntryValidator::PREFER_INT, // Not used here! 00119 AcceptedTypes(false).allowInt(true).allowDouble(true).allowString(true) 00120 ) 00121 ); 00122 PL_Main_valid.sublist("Line Search").sublist("Polynomial").set( 00123 "Max Iters",3 00124 ,"The maximum number of inner linear search iterations allowed." 00125 ,linesearchMaxItersValiator 00126 ); 00127 00128 // Create a validator for the parameter "Direction"->"Newton"->"Linear Solver"->"Tol" 00129 // that accepts a 'double' or a 'std::string' value! 00130 typedef Teuchos::AnyNumberParameterEntryValidator::AcceptedTypes AcceptedTypes; 00131 Teuchos::RCP<Teuchos::AnyNumberParameterEntryValidator> 00132 linSolveTolValidator = rcp( 00133 new Teuchos::AnyNumberParameterEntryValidator( 00134 Teuchos::AnyNumberParameterEntryValidator::PREFER_INT, // Not used here! 00135 AcceptedTypes(false).allowDouble(true).allowString(true) 00136 ) 00137 ); 00138 PL_Main_valid.sublist("Direction",true).sublist("Newton",true) 00139 .sublist("Linear Solver",true).set( 00140 "Tol", double(1e-5) 00141 ,"Select the linear solve tolerance" 00142 ,linSolveTolValidator 00143 ); 00144 00145 return PL_Main_valid; 00146 00147 } 00148 00149 00150 // 00151 // Unit tests 00152 // 00153 00154 00155 TEUCHOS_UNIT_TEST( ParameterList, construct_default ) 00156 { 00157 ParameterList pl; 00158 TEST_EQUALITY_CONST(pl.name(), "ANONYMOUS"); 00159 TEST_EQUALITY_CONST(pl.numParams(), 0); 00160 } 00161 00162 00163 TEUCHOS_UNIT_TEST( ParameterList, construct_withName ) 00164 { 00165 ParameterList pl("someName"); 00166 TEST_EQUALITY_CONST(pl.name(), "someName"); 00167 TEST_EQUALITY_CONST(pl.numParams(), 0); 00168 } 00169 00170 00171 TEUCHOS_UNIT_TEST( ParameterList, createParameterList_empty ) 00172 { 00173 RCP<ParameterList> pl = createParameterList(); 00174 TEST_ASSERT(nonnull(pl)); 00175 TEST_EQUALITY_CONST(pl->name(), "ANONYMOUS"); 00176 } 00177 00178 00179 TEUCHOS_UNIT_TEST( ParameterList, createParameterList_withName ) 00180 { 00181 RCP<ParameterList> pl = createParameterList("dummyName"); 00182 TEST_ASSERT(nonnull(pl)); 00183 TEST_EQUALITY_CONST(pl->name(), "dummyName"); 00184 } 00185 00186 00187 TEUCHOS_UNIT_TEST( ParameterList, set_get_int ) 00188 { 00189 ParameterList pl; 00190 00191 out << "\n"; 00192 ECHO(pl.set("my int", 3)); 00193 00194 out << "\n"; 00195 ECHO(const ParameterEntry& my_int_c_param = getConst(pl).getEntry("my int")); 00196 TEST_EQUALITY_CONST(my_int_c_param.isUsed(), false); 00197 TEST_EQUALITY_CONST(my_int_c_param.isList(), false); 00198 TEST_EQUALITY_CONST(my_int_c_param.isDefault(), false); 00199 TEST_EQUALITY_CONST(my_int_c_param.docString(), ""); 00200 TEST_ASSERT(is_null(my_int_c_param.validator())); 00201 TEST_EQUALITY_CONST(getValue<int>(my_int_c_param), 3); 00202 ECHO(const bool param_isType_int1 = my_int_c_param.isType<int>()); 00203 TEST_EQUALITY_CONST(param_isType_int1, true); 00204 ECHO(const bool param_isType_double1 = my_int_c_param.isType<double>()); 00205 TEST_EQUALITY_CONST(param_isType_double1, false); 00206 00207 out << "\n"; 00208 ECHO(const ParameterEntry& my_int_param = pl.getEntry("my int")); 00209 TEST_EQUALITY_CONST(my_int_param.isUsed(), true); 00210 00211 out << "\n"; 00212 ECHO(const int my_int = pl.get<int>("my int")); 00213 TEST_EQUALITY_CONST(my_int, 3); 00214 00215 } 00216 00217 00218 TEUCHOS_UNIT_TEST( ParameterList, param_isParameter_isSublist_isType ) 00219 { 00220 ParameterList pl; 00221 ECHO(pl.set("my int", 3)); 00222 ECHO(const int my_int = pl.get<int>("my int")); 00223 TEST_EQUALITY_CONST(my_int, 3); 00224 TEST_EQUALITY_CONST(pl.isParameter("my int"), true); 00225 TEST_EQUALITY_CONST(pl.isParameter("Does not Exist"), false); 00226 TEST_EQUALITY_CONST(pl.isSublist("my int"), false); 00227 TEST_EQUALITY_CONST(pl.isSublist("Does not exist"), false); 00228 TEST_EQUALITY_CONST(pl.isType<int>("my int"), true); 00229 TEST_EQUALITY_CONST(pl.isType<double>("my int"), false); 00230 TEST_EQUALITY_CONST(pl.isType("my int", static_cast<int*>(0)), true); 00231 TEST_EQUALITY_CONST(pl.isType("my int", static_cast<double*>(0)), false); 00232 } 00233 00234 00235 TEUCHOS_UNIT_TEST( ParameterList, sublist_isParameter_isSublist_isType ) 00236 { 00237 ParameterList pl; 00238 ECHO(pl.sublist("my sublist").set("my int", 3)); 00239 ECHO(const int my_int = getConst(pl).sublist("my sublist").get<int>("my int")); 00240 TEST_EQUALITY_CONST(my_int, 3); 00241 TEST_EQUALITY_CONST(pl.isParameter("my sublist"), true); // Should be false, but backward compatiable! 00242 TEST_EQUALITY_CONST(pl.isParameter("Does not Exist"), false); 00243 TEST_EQUALITY_CONST(pl.isSublist("my sublist"), true); 00244 TEST_EQUALITY_CONST(pl.isType<ParameterList>("my sublist"), true); 00245 TEST_EQUALITY_CONST(pl.isType<double>("my sublist"), false); 00246 TEST_EQUALITY_CONST(pl.isType("my sublist", static_cast<ParameterList*>(0)), true); 00247 TEST_EQUALITY_CONST(pl.isType("my sublist", static_cast<double*>(0)), false); 00248 } 00249 00250 00251 TEUCHOS_UNIT_TEST( ParameterList, set_doc ) 00252 { 00253 ParameterList pl; 00254 ECHO(pl.set("my int", 3, "Some documentation")); 00255 ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int")); 00256 TEST_EQUALITY_CONST(my_int_param.docString(), "Some documentation"); 00257 TEST_ASSERT(is_null(my_int_param.validator())); 00258 } 00259 00260 00261 TEUCHOS_UNIT_TEST( ParameterList, set_doc_validator ) 00262 { 00263 ParameterList pl; 00264 ECHO(pl.set("my int", 3, "Some documentation", rcp(new DummyValidator))); 00265 ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int")); 00266 TEST_EQUALITY_CONST(my_int_param.docString(), "Some documentation"); 00267 TEST_NOTHROW(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true)); 00268 } 00269 00270 00271 TEUCHOS_UNIT_TEST( ParameterList, set_invalid_int_first ) 00272 { 00273 ParameterList pl; 00274 ECHO(const RCP<ParameterEntryValidator> 00275 validator(new Teuchos::EnhancedNumberValidator<int>(0, 1))); 00276 TEST_THROW(pl.set("my int", -1, "", validator), 00277 Exceptions::InvalidParameterValue); 00278 TEST_EQUALITY_CONST(pl.numParams(), 0); 00279 } 00280 00281 00282 TEUCHOS_UNIT_TEST( ParameterList, set_invalid_int_second ) 00283 { 00284 ParameterList pl; 00285 ECHO(const RCP<ParameterEntryValidator> 00286 validator(new Teuchos::EnhancedNumberValidator<int>(0, 1))); 00287 TEST_NOTHROW(pl.set("my int", 1, "", validator)); 00288 TEST_EQUALITY_CONST(pl.numParams(), 1); 00289 TEST_EQUALITY_CONST(pl.get<int>("my int"), 1); 00290 TEST_THROW(pl.set("my int", -1), Exceptions::InvalidParameterValue); 00291 TEST_EQUALITY_CONST(pl.get<int>("my int"), 1); 00292 } 00293 00294 00295 TEUCHOS_UNIT_TEST( ParameterList, set_get_entry ) 00296 { 00297 ParameterList pl; 00298 ECHO(pl.setEntry("my int", ParameterEntry(as<int>(3), true, true, "Some doc", rcp(new DummyValidator)))); 00299 ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int")); 00300 TEST_EQUALITY_CONST(my_int_param.docString(), "Some doc"); 00301 ECHO(const int my_int_1 = my_int_param.getValue<int>(0)); 00302 TEST_EQUALITY_CONST(my_int_1, 3); 00303 TEST_EQUALITY_CONST(my_int_param.isUsed(), true); 00304 TEST_EQUALITY_CONST(my_int_param.isList(), false); // The isList entry is ignored! 00305 TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null); 00306 } 00307 00308 00309 TEUCHOS_UNIT_TEST( ParameterList, set_int_twice_keep_validator ) 00310 { 00311 ParameterList pl; 00312 ECHO(pl.setEntry("my int", ParameterEntry(as<int>(3), true, true, "Some doc", rcp(new DummyValidator)))); 00313 { 00314 ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int")); 00315 TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null); 00316 } 00317 TEST_EQUALITY_CONST(pl.get<int>("my int"), 3); 00318 ECHO(pl.set("my int", 4)); 00319 TEST_EQUALITY_CONST(pl.get<int>("my int"), 4); 00320 { 00321 ECHO(const ParameterEntry& my_int_param = getConst(pl).getEntry("my int")); 00322 TEST_INEQUALITY_CONST(rcp_dynamic_cast<const DummyValidator>(my_int_param.validator(), true), null); 00323 } 00324 } 00325 00326 00327 TEUCHOS_UNIT_TEST( ParameterList, set_get_char_str ) 00328 { 00329 ParameterList pl; 00330 00331 ECHO(char dummy_str_1[] = "dummy str 1"); 00332 ECHO(pl.set("dummy 1", dummy_str_1)); 00333 ECHO(const std::string dummy_1 = pl.get<std::string>("dummy 1")); 00334 TEST_EQUALITY_CONST(dummy_1, "dummy str 1"); 00335 00336 ECHO(const char dummy_str_const_2[] = "dummy str 2"); 00337 ECHO(pl.set("dummy 2", dummy_str_const_2)); 00338 ECHO(const std::string dummy_2 = pl.get<std::string>("dummy 2")); 00339 TEST_EQUALITY_CONST(dummy_2, "dummy str 2"); 00340 00341 } 00342 00343 00344 TEUCHOS_UNIT_TEST( ParameterList, set_get_string ) 00345 { 00346 ParameterList pl; 00347 00348 ECHO(const std::string dummy_str = "dummy str"); 00349 ECHO(pl.set("my str", dummy_str)); 00350 ECHO(const std::string my_str = pl.get<std::string>("my str")); 00351 TEST_EQUALITY_CONST(my_str, "dummy str"); 00352 00353 } 00354 00355 00356 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_param ) 00357 { 00358 ParameterList pl; 00359 TEST_THROW(pl.getEntry("Does not exist 1"), Exceptions::InvalidParameterName); 00360 TEST_THROW(pl.get<int>("Does not exist 2"), Exceptions::InvalidParameterName); 00361 TEST_THROW(getConst(pl).get<int>("Does not exist 3"), Exceptions::InvalidParameterName); 00362 TEST_EQUALITY(pl.getPtr<int>("Does not exist 4"), static_cast<int*>(0)); 00363 TEST_EQUALITY(getConst(pl).getPtr<int>("Does not exist 5"), static_cast<const int*>(0)); 00364 ECHO(char raw_str[] = "dummy"); 00365 TEST_EQUALITY_CONST(pl.get("Does not exist 6", raw_str), "dummy"); 00366 ECHO(const char raw_c_str[] = "dummy"); 00367 TEST_EQUALITY_CONST(pl.get("Does not exist 7", raw_c_str), "dummy"); 00368 ECHO(const std::string str = "dummy"); 00369 TEST_EQUALITY_CONST(pl.get("Does not exist 8", str), "dummy"); 00370 TEST_THROW(pl.getEntry("Does not exist 9"), Exceptions::InvalidParameterName); 00371 TEST_THROW(getConst(pl).getEntry("Does not exist 10"), Exceptions::InvalidParameterName); 00372 TEST_EQUALITY(pl.getEntryPtr("Does not exist 11"), static_cast<ParameterEntry*>(0)); 00373 TEST_EQUALITY(getConst(pl).getEntryPtr("Does not exist 12"), static_cast<const ParameterEntry*>(0)); 00374 TEST_EQUALITY(pl.getEntryRCP("Does not exist 13"), RCP<ParameterEntry>()); 00375 TEST_EQUALITY(getConst(pl).getEntryRCP("Does not exist 14"), RCP<const ParameterEntry>()); 00376 } 00377 00378 00379 TEUCHOS_UNIT_TEST( ParameterList, get_existing_incorrect_type ) 00380 { 00381 ParameterList pl; 00382 pl.set("my int", 4); 00383 TEST_THROW(pl.get<double>("my int"), Exceptions::InvalidParameterType); 00384 // ToDo: Assert the contents of the error message 00385 } 00386 00387 00388 TEUCHOS_UNIT_TEST( ParameterList, getPtr ) 00389 { 00390 ParameterList pl; 00391 pl.set("my int", 4); 00392 TEST_EQUALITY_CONST(pl.getPtr<int>("Does not Exist"), static_cast<int*>(0)); 00393 TEST_INEQUALITY_CONST(pl.getPtr<int>("my int"), static_cast<int*>(0)); 00394 TEST_EQUALITY_CONST(*pl.getPtr<int>("my int"), 4); 00395 TEST_EQUALITY_CONST(pl.getPtr<double>("my int"), static_cast<double*>(0)); 00396 TEST_EQUALITY_CONST(getConst(pl).getPtr<int>("Does not Exist"), static_cast<const int*>(0)); 00397 TEST_INEQUALITY_CONST(getConst(pl).getPtr<int>("my int"), static_cast<int*>(0)); 00398 TEST_EQUALITY_CONST(*getConst(pl).getPtr<int>("my int"), 4); 00399 TEST_EQUALITY_CONST(getConst(pl).getPtr<double>("my int"), static_cast<const double*>(0)); 00400 } 00401 00402 00403 TEUCHOS_UNIT_TEST( ParameterList, getEntryRCP ) 00404 { 00405 ParameterList pl; 00406 pl.set("my int", 4); 00407 TEST_EQUALITY_CONST(pl.getEntryRCP("Does not Exist"), null); 00408 TEST_INEQUALITY_CONST(pl.getEntryRCP("my int"), null); 00409 TEST_EQUALITY_CONST(pl.getEntryRCP("my int")->getValue<int>(0), 4); 00410 TEST_EQUALITY_CONST(getConst(pl).getEntryRCP("Does not Exist"), null); 00411 TEST_INEQUALITY_CONST(getConst(pl).getEntryRCP("my int"), null); 00412 TEST_EQUALITY_CONST(getConst(pl).getEntryRCP("my int")->getValue<int>(0), 4); 00413 } 00414 00415 00416 // Test nonconstFind() 00417 00418 // Test find() 00419 00420 00421 TEUCHOS_UNIT_TEST( ParameterList, get_default_then_change ) 00422 { 00423 ParameterList pl; 00424 ECHO(int &my_int = pl.get("my int", 3)); 00425 TEST_EQUALITY_CONST(my_int, 3); 00426 TEST_EQUALITY_CONST(pl.get<int>("my int"), 3); 00427 ECHO(my_int = 5); 00428 TEST_EQUALITY_CONST(pl.get<int>("my int"), 5); 00429 } 00430 00431 00432 TEUCHOS_UNIT_TEST( ParameterList, remove_1 ) 00433 { 00434 ParameterList pl; 00435 TEST_EQUALITY_CONST(pl.numParams(), 0); 00436 ECHO(pl.set("my int", 2)); 00437 TEST_EQUALITY_CONST(pl.numParams(), 1); 00438 TEST_EQUALITY_CONST(pl.get<int>("my int"), 2); 00439 ECHO(const bool param_was_removed_1 = pl.remove("my int")); 00440 TEST_EQUALITY_CONST(param_was_removed_1, true); 00441 TEST_EQUALITY_CONST(pl.numParams(), 0); 00442 TEST_THROW(pl.get<int>("my int"), Exceptions::InvalidParameterName); 00443 TEST_THROW(pl.remove("my int"), Exceptions::InvalidParameterName); 00444 ECHO(const bool param_was_removed_2 = pl.remove("my int", false)); 00445 TEST_EQUALITY_CONST(param_was_removed_2, false); 00446 } 00447 00448 00449 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_default ) 00450 { 00451 ParameterList pl("Base"); 00452 ECHO(pl.sublist("my sublist")); 00453 ECHO(const ParameterEntry &sublistParam = pl.getEntry("my sublist")); 00454 TEST_EQUALITY_CONST(sublistParam.isUsed(), false); 00455 TEST_EQUALITY_CONST(sublistParam.isList(), true); 00456 TEST_EQUALITY_CONST(sublistParam.isDefault(), false); 00457 TEST_EQUALITY_CONST(sublistParam.docString(), ""); 00458 TEST_EQUALITY_CONST(sublistParam.getValue<ParameterList>(0).name(), "Base->my sublist"); 00459 } 00460 00461 00462 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_docString ) 00463 { 00464 ParameterList pl("Base"); 00465 ECHO(pl.sublist("my sublist", false, "My great sublist")); 00466 ECHO(const ParameterEntry &sublistParam = pl.getEntry("my sublist")); 00467 TEST_EQUALITY_CONST(sublistParam.isUsed(), false); 00468 TEST_EQUALITY_CONST(sublistParam.isList(), true); 00469 TEST_EQUALITY_CONST(sublistParam.isDefault(), false); 00470 TEST_EQUALITY_CONST(sublistParam.docString(), "My great sublist"); 00471 TEST_EQUALITY_CONST(sublistParam.getValue<ParameterList>(0).name(), "Base->my sublist"); 00472 } 00473 00474 00475 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_mustAlreadyExist ) 00476 { 00477 ParameterList pl("Base"); 00478 TEST_THROW(pl.sublist("my sublist", true), Exceptions::InvalidParameterName); 00479 // ToDo: Examine the actual structure of the error message 00480 } 00481 00482 00483 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_nonsublist ) 00484 { 00485 ParameterList pl("Base"); 00486 ECHO(pl.set("my sublist", 1)); // Not a sublist! 00487 TEST_THROW(pl.sublist("my sublist"), Exceptions::InvalidParameterType); 00488 // ToDo: Examine the actual structure of the error message 00489 } 00490 00491 00492 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_nonconst ) 00493 { 00494 ParameterList pl("Base"); 00495 ECHO(pl.sublist("my sublist").set("my int", 2)); 00496 ECHO(const int my_int = pl.sublist("my sublist").get<int>("my int")); 00497 TEST_EQUALITY_CONST(my_int, 2); 00498 } 00499 00500 00501 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_const ) 00502 { 00503 ParameterList pl("Base"); 00504 ECHO(pl.sublist("my sublist").set("my int", 2)); 00505 ECHO(const int my_int = getConst(pl).sublist("my sublist").get<int>("my int")); 00506 TEST_EQUALITY_CONST(my_int, 2); 00507 } 00508 00509 00510 TEUCHOS_UNIT_TEST( ParameterList, get_nonexisting_sublist_const ) 00511 { 00512 ParameterList pl("Base"); 00513 TEST_THROW(getConst(pl).sublist("my sublist"), Exceptions::InvalidParameterName); 00514 // ToDo: Examine the actual structure of the error message 00515 } 00516 00517 00518 TEUCHOS_UNIT_TEST( ParameterList, get_existing_sublist_const_nonsublist ) 00519 { 00520 ParameterList pl("Base"); 00521 ECHO(pl.set("my sublist", 1)); // Not a sublist! 00522 TEST_THROW(getConst(pl).sublist("my sublist"), Exceptions::InvalidParameterType); 00523 // ToDo: Examine the actual structure of the error message 00524 } 00525 00526 00527 TEUCHOS_UNIT_TEST( ParameterList, sublist_add_2 ) 00528 { 00529 ParameterList PL_Main("PL_Main"); 00530 const std::string Direction_Doc = "This sublist controls how direction is computed."; 00531 ParameterList& PL_Direction = PL_Main.sublist("Direction", false, Direction_Doc); 00532 ParameterList& PL_LineSearch = PL_Main.sublist("Line Search"); 00533 out << "PL_Main=\n" << PL_Main << "\n"; 00534 TEST_EQUALITY_CONST(PL_Main.name(), "PL_Main"); 00535 TEST_EQUALITY_CONST(PL_Main.isSublist("Direction"), true); 00536 TEST_EQUALITY_CONST(PL_Main.isSublist("Line Search"), true); 00537 ECHO(const ParameterList& PL_Direction_2 = getConst(PL_Main).sublist("Direction")); 00538 TEST_EQUALITY(&PL_Direction, &PL_Direction_2); 00539 ECHO(const ParameterList& PL_LineSearch_2 = getConst(PL_Main).sublist("Line Search")); 00540 TEST_EQUALITY(&PL_LineSearch, &PL_LineSearch_2); 00541 TEST_EQUALITY_CONST(getConst(PL_Main).sublist("Direction").name(), "PL_Main->Direction"); 00542 TEST_EQUALITY_CONST(PL_Direction.name(), "PL_Main->Direction"); 00543 TEST_EQUALITY_CONST(PL_LineSearch.name(), "PL_Main->Line Search"); 00544 } 00545 00546 00547 TEUCHOS_UNIT_TEST( ParameterList, sublist_scenario_1 ) 00548 { 00549 // This is the scenario in the orginal testing program 00550 ParameterList PL_Main("PL_Main"); 00551 const std::string Direction_Doc = "This sublist controls how direction is computed."; 00552 ParameterList &PL_Direction = PL_Main.sublist("Direction", false, Direction_Doc); 00553 ParameterList &PL_Newton = PL_Direction.sublist("Newton"); 00554 ParameterList &PL_LinSol = PL_Newton.sublist("Linear Solver"); 00555 ParameterList &PL_LineSearch = PL_Main.sublist("Line Search"); 00556 out << "PL_Main=\n" << PL_Main << "\n"; 00557 TEST_EQUALITY_CONST(PL_Main.name(), "PL_Main"); 00558 TEST_EQUALITY_CONST(PL_Main.isSublist("Direction"), true); 00559 ECHO(const ParameterList& PL_Direction_2 = getConst(PL_Main).sublist("Direction")); 00560 TEST_EQUALITY(&PL_Direction, &PL_Direction_2); 00561 TEST_EQUALITY_CONST(getConst(PL_Main).sublist("Direction").name(), "PL_Main->Direction"); 00562 TEST_EQUALITY_CONST(PL_Direction.name(), "PL_Main->Direction"); 00563 TEST_EQUALITY_CONST(PL_Direction.isSublist("Newton"), true); 00564 TEST_EQUALITY_CONST(PL_Newton.isSublist("Linear Solver"), true); 00565 TEST_EQUALITY_CONST(PL_Newton.name(), "PL_Main->Direction->Newton"); 00566 TEST_EQUALITY_CONST(PL_Newton.isSublist("Linear Solver"), true); 00567 TEST_EQUALITY_CONST(PL_LinSol.name(), "PL_Main->Direction->Newton->Linear Solver"); 00568 TEST_EQUALITY_CONST(PL_Main.isSublist("Line Search"), true); 00569 TEST_EQUALITY_CONST(PL_LineSearch.name(), "PL_Main->Line Search"); 00570 } 00571 00572 00573 TEUCHOS_UNIT_TEST( ParameterList, copy_constructor ) 00574 { 00575 ECHO(ParameterList pl1("A")); 00576 ECHO(pl1.set("my int", 2)); 00577 ECHO(ParameterList pl2(pl1)); 00578 TEST_EQUALITY_CONST(pl2.name(), "A"); 00579 TEST_EQUALITY_CONST(pl2.get<int>("my int"), 2); 00580 } 00581 00582 00583 TEUCHOS_UNIT_TEST( ParameterList, assignment_operator ) 00584 { 00585 ECHO(ParameterList pl1("A")); 00586 ECHO(pl1.set("my int", 2)); 00587 ECHO(ParameterList pl2); 00588 ECHO(const ParameterList &pl2_ref = pl2 = pl1); 00589 TEST_EQUALITY_CONST(&pl2_ref, &pl2); 00590 TEST_EQUALITY_CONST(pl2.name(), "A"); 00591 TEST_EQUALITY_CONST(pl2.get<int>("my int"), 2); 00592 } 00593 00594 00595 TEUCHOS_UNIT_TEST( ParameterList, iterator_params ) 00596 { 00597 typedef ParameterList::ConstIterator ConstIter; 00598 ParameterList pl; 00599 pl.set("c", 1); 00600 pl.set("a", 2); 00601 pl.set("b", 3); 00602 ConstIter pl_itr = pl.begin(); 00603 TEST_EQUALITY_CONST(pl_itr->first, "c"); 00604 TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 1); 00605 ECHO(++pl_itr); 00606 TEST_EQUALITY_CONST(pl_itr->first, "a"); 00607 TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 2); 00608 ECHO(++pl_itr); 00609 TEST_EQUALITY_CONST(pl_itr->first, "b"); 00610 TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 3); 00611 ECHO(++pl_itr); 00612 TEST_ITER_EQUALITY(pl_itr, pl.end()); 00613 } 00614 00615 00616 TEUCHOS_UNIT_TEST( ParameterList, iterator_params_sublists ) 00617 { 00618 typedef ParameterList::ConstIterator ConstIter; 00619 ParameterList pl("base"); 00620 pl.set("c", 1); 00621 pl.sublist("a"); 00622 pl.set("b", 3); 00623 ConstIter pl_itr = pl.begin(); 00624 TEST_EQUALITY_CONST(pl_itr->first, "c"); 00625 TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 1); 00626 ECHO(++pl_itr); 00627 TEST_EQUALITY_CONST(pl_itr->first, "a"); 00628 TEST_EQUALITY_CONST(pl_itr->second.getValue<ParameterList>(0).name(), "base->a"); 00629 ECHO(++pl_itr); 00630 TEST_EQUALITY_CONST(pl_itr->first, "b"); 00631 TEST_EQUALITY_CONST(pl_itr->second.getValue<int>(0), 3); 00632 ECHO(++pl_itr); 00633 TEST_ITER_EQUALITY(pl_itr, pl.end()); 00634 } 00635 00636 // Test iterator access after removing params 00637 00638 // Test iterator access after removing sublists 00639 00640 00641 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityWithEmpty ) 00642 { 00643 // An empty list should not be equal to a full list 00644 ParameterList A; 00645 ParameterList B; 00646 TEST_ASSERT( A == B ); 00647 A.set("Hello","World"); 00648 TEST_ASSERT( A != B ); 00649 B.set("Hello","World"); 00650 TEST_ASSERT( A == B ); 00651 } 00652 00653 00654 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityDifferentSublistNames ) 00655 { 00656 // Sublists with different names should not be equal 00657 ParameterList A; 00658 ParameterList B; 00659 A.sublist("Bob"); 00660 B.sublist("Tom"); 00661 TEST_ASSERT( A != B ); 00662 } 00663 00664 00665 TEUCHOS_UNIT_TEST( ParameterList, operatorEqualityDifferentLengths ) 00666 { 00667 ParameterList A; 00668 ParameterList B; 00669 A.set("A","a"); 00670 A.set("B","b"); 00671 A.set("C","c"); 00672 A.print(out); 00673 00674 B.set("A","a"); 00675 B.set("B","b"); 00676 B.print(out); 00677 00678 TEST_ASSERT( A != B ); 00679 00680 B.set("C","c"); 00681 TEST_ASSERT( A == B ); 00682 } 00683 00684 00685 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesWithEmpty ) 00686 { 00687 ParameterList A; 00688 ParameterList B; 00689 TEST_ASSERT( haveSameValues(A,B) ); 00690 A.set("Hello","World"); 00691 TEST_ASSERT( !haveSameValues(A,B) ); 00692 B.set("Hello","World"); 00693 TEST_ASSERT( haveSameValues(A,B) ); 00694 } 00695 00696 00697 TEUCHOS_UNIT_TEST( ParameterList, haveSameValuesDifferentSublistNames ) 00698 { 00699 ParameterList A; 00700 ParameterList B; 00701 A.sublist("Smith").set("People",4); 00702 B.sublist("Jones").set("People",4); 00703 TEST_ASSERT( !haveSameValues(A,B) ); // sublist names matter 00704 } 00705 00706 00707 TEUCHOS_UNIT_TEST( ParameterList, validateAgainstSelf ) 00708 { 00709 ParameterList PL_Main = createMainPL(); 00710 ParameterList PL_Main_valid = createValidMainPL(); 00711 TEST_NOTHROW(PL_Main.validateParameters(PL_Main_valid)); 00712 } 00713 00714 00715 TEUCHOS_UNIT_TEST( ParameterList, validateParametersAndSetDefaults ) 00716 { 00717 ParameterList PL_Main = createMainPL(); 00718 ParameterList PL_Main_valid = createValidMainPL(); 00719 ECHO(PL_Main.validateParametersAndSetDefaults(PL_Main_valid)); 00720 TEST_NOTHROW( 00721 rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<int> >( 00722 PL_Main.getEntry("Nonlinear Solver").validator(), true ) ); 00723 } 00724 00725 00726 TEUCHOS_UNIT_TEST( ParameterList, getIntegralValue_int ) 00727 { 00728 ParameterList PL_Main = createMainPL(); 00729 ParameterList PL_Main_valid = createValidMainPL(); 00730 ECHO(PL_Main.set("Nonlinear Solver", "Line Search Based")); 00731 ECHO(PL_Main.validateParametersAndSetDefaults(PL_Main_valid)); 00732 ECHO(const int lineSearchValue = getIntegralValue<int>(PL_Main, "Nonlinear Solver")); 00733 TEST_EQUALITY_CONST(lineSearchValue, 0); 00734 ECHO(PL_Main.set("Nonlinear Solver", "Trust Region Based")); 00735 ECHO(const int trustRegionValue = getIntegralValue<int>(PL_Main, "Nonlinear Solver")); 00736 TEST_EQUALITY_CONST(trustRegionValue, 1); 00737 } 00738 00739 00740 } // namespace Teuchos 00741 00742 00743
1.7.6.1