void testVariables() { CPPUNIT_ASSERT( input.have_variable("Section1/var1") ); Real var1 = input("Section1/var1", 1.0); CPPUNIT_ASSERT_EQUAL( var1, Real(5)); CPPUNIT_ASSERT( input.have_variable("Section1/SubSection1/var2") ); std::string var2 = input("Section1/SubSection1/var2", "DIE!"); CPPUNIT_ASSERT_EQUAL( var2, std::string("blah") ); // This variable is commented out in the input file so we shouldn't find it. CPPUNIT_ASSERT( !input.have_variable("Section2/var3") ); int var3 = input("Section2/var3", 314); CPPUNIT_ASSERT_EQUAL( var3, 314 ); CPPUNIT_ASSERT( input.have_variable("Section2/Subsection2/var4") ); bool var4 = input("Section2/Subsection2/var4", true); CPPUNIT_ASSERT_EQUAL( var4, false ); CPPUNIT_ASSERT( input.have_variable("Section2/Subsection2/Subsection3/var5") ); Real var5 = input("Section2/Subsection2/Subsection3/var5", 3.1415); CPPUNIT_ASSERT_EQUAL( var5, Real(6.02e23)); CPPUNIT_ASSERT( input.have_variable("Section2/Subsection4/var6") ); unsigned int var6 = input("Section2/Subsection4/var6", 21); CPPUNIT_ASSERT_EQUAL( var6, (unsigned int)42 ); // We didn't use Section3/unused_var so it should be a UFO std::vector<std::string> ufos = input.unidentified_variables(); CPPUNIT_ASSERT_EQUAL( ufos.size(), (std::vector<std::string>::size_type)1 ); CPPUNIT_ASSERT_EQUAL( ufos[0], std::string("Section3/unused_var") ); }
void Runner::check_for_unused_vars( const GetPot& input, bool warning_only ) { /* Everything should be set up now, so check if there's any unused variables in the input file. If so, then tell the user what they were and error out. */ std::vector<std::string> unused_vars = input.unidentified_variables(); bool unused_vars_detected = false; // String we use to check if there is a variable in the Materials section std::string mat_string = "Materials/"; // If we do have unused variables, make sure they are in the Materials // section since we're allowing those to be present and not used. if( !unused_vars.empty() ) { int n_total = unused_vars.size(); int n_mats = 0; for( int v = 0; v < n_total; v++ ) if( (unused_vars[v]).find(mat_string) != std::string::npos ) n_mats += 1; libmesh_assert_greater_equal( n_total, n_mats ); if( n_mats < n_total ) unused_vars_detected = true; } if( unused_vars_detected ) { libMesh::err << "==========================================================" << std::endl; if( warning_only ) libMesh::err << "Warning: "; else libMesh::err << "Error: "; libMesh::err << "Found unused variables!" << std::endl; for( std::vector<std::string>::const_iterator it = unused_vars.begin(); it != unused_vars.end(); ++it ) { // Don't print out any unused Material variables, since we allow these if( (*it).find(mat_string) != std::string::npos ) continue; libMesh::err << *it << std::endl; } libMesh::err << "==========================================================" << std::endl; if( !warning_only ) libmesh_error(); } }