libMesh
getpot_test.C
Go to the documentation of this file.
1 // Ignore unused parameter warnings coming from cppuint headers
2 #include <libmesh/ignore_warnings.h>
3 #include <cppunit/extensions/HelperMacros.h>
4 #include <cppunit/TestCase.h>
5 #include <libmesh/restore_warnings.h>
6 
7 #include <libmesh/getpot.h>
8 
9 // THE CPPUNIT_TEST_SUITE_END macro expands to code that involves
10 // std::auto_ptr, which in turn produces -Wdeprecated-declarations
11 // warnings. These can be ignored in GCC as long as we wrap the
12 // offending code in appropriate pragmas. We can't get away with a
13 // single ignore_warnings.h inclusion at the beginning of this file,
14 // since the libmesh headers pull in a restore_warnings.h at some
15 // point. We also don't bother restoring warnings at the end of this
16 // file since it's not a header.
17 #include <libmesh/ignore_warnings.h>
18 
19 using namespace libMesh;
20 
21 class GetPotTest : public CppUnit::TestCase {
22 
23 public:
24  CPPUNIT_TEST_SUITE( GetPotTest );
25 
26  CPPUNIT_TEST( testVariables );
27  CPPUNIT_TEST( testSections );
28  CPPUNIT_TEST( testSubSections );
29 
30  CPPUNIT_TEST_SUITE_END();
31 
32 protected:
33 
34  std::stringstream inputfile;
35 
36  GetPot input;
37 
38 public:
39 
41  {
42 
43  const char* text =
44  "[Section1]\n"
45  "\n"
46  " var1 = '5.0'\n"
47  "\n"
48  " [./SubSection1]\n"
49  "\n"
50  " var2 = 'blah'\n"
51  "\n"
52  "[]\n"
53  "\n"
54  "[Section2]\n"
55  "\n"
56  " #var3 = '5'\n"
57  "\n"
58  " [./Subsection2]\n"
59  "\n"
60  " var4 = 'false'\n"
61  "\n"
62  " [./Subsection3]\n"
63  "\n"
64  " var5 = '6.02e23'\n"
65  "\n"
66  " [../../Subsection4]\n"
67  "\n"
68  " var6 = '42'\n"
69  "\n"
70  "[]\n"
71  "\n"
72  "[Section3]\n"
73  "\n"
74  " unused_var = 'not_used'\n";
75 
76  inputfile << text;
77  }
78 
79  void setUp()
80  {
81  this->setup_inputfile();
82  input.parse_input_stream(inputfile);
83  }
84 
86  {
87  CPPUNIT_ASSERT( input.have_variable("Section1/var1") );
88  Real var1 = input("Section1/var1", 1.0);
89  CPPUNIT_ASSERT_EQUAL( var1, Real(5));
90 
91  CPPUNIT_ASSERT( input.have_variable("Section1/SubSection1/var2") );
92  std::string var2 = input("Section1/SubSection1/var2", "DIE!");
93  CPPUNIT_ASSERT_EQUAL( var2, std::string("blah") );
94 
95  // This variable is commented out in the input file so we shouldn't find it.
96  CPPUNIT_ASSERT( !input.have_variable("Section2/var3") );
97  int var3 = input("Section2/var3", 314);
98  CPPUNIT_ASSERT_EQUAL( var3, 314 );
99 
100  CPPUNIT_ASSERT( input.have_variable("Section2/Subsection2/var4") );
101  bool var4 = input("Section2/Subsection2/var4", true);
102  CPPUNIT_ASSERT_EQUAL( var4, false );
103 
104  CPPUNIT_ASSERT( input.have_variable("Section2/Subsection2/Subsection3/var5") );
105  Real var5 = input("Section2/Subsection2/Subsection3/var5", 3.1415);
106  CPPUNIT_ASSERT_EQUAL( var5, Real(6.02e23));
107 
108  CPPUNIT_ASSERT( input.have_variable("Section2/Subsection4/var6") );
109  unsigned int var6 = input("Section2/Subsection4/var6", 21);
110  CPPUNIT_ASSERT_EQUAL( var6, (unsigned int)42 );
111 
112  // We didn't use Section3/unused_var so it should be a UFO
113  std::vector<std::string> ufos = input.unidentified_variables();
114  CPPUNIT_ASSERT_EQUAL( ufos.size(), (std::vector<std::string>::size_type)1 );
115  CPPUNIT_ASSERT_EQUAL( ufos[0], std::string("Section3/unused_var") );
116  }
117 
119  {
120  // GetPot stores the '/' at the end of each section name
121  CPPUNIT_ASSERT(input.have_section("Section1/"));
122  CPPUNIT_ASSERT(input.have_section("Section1/SubSection1/"));
123  CPPUNIT_ASSERT(input.have_section("Section2/Subsection2/"));
124  CPPUNIT_ASSERT(input.have_section("Section2/Subsection2/Subsection3/"));
125  CPPUNIT_ASSERT(input.have_section("Section2/Subsection4/"));
126  CPPUNIT_ASSERT(input.have_section("Section3/"));
127 
128  // But we don't need to supply the trailing '/'
129  CPPUNIT_ASSERT(input.have_section("Section1"));
130  CPPUNIT_ASSERT(input.have_section("Section1/SubSection1"));
131  CPPUNIT_ASSERT(input.have_section("Section2/Subsection2/Subsection3"));
132 
133  // No such thing as this section
134  CPPUNIT_ASSERT(!input.have_section("ImNotASection/"));
135  }
136 
138  {
139  typedef std::vector<std::string>::size_type sz;
140  typedef std::string str;
141 
142  const std::vector<std::string> subsections1 =
143  input.get_subsection_names("Section1");
144 
145  CPPUNIT_ASSERT_EQUAL(subsections1.size(), sz(1));
146  CPPUNIT_ASSERT_EQUAL(subsections1[0], str("SubSection1"));
147 
148  const std::vector<std::string> subsections1_1 =
149  input.get_subsection_names("Section1/Subsection1");
150 
151  CPPUNIT_ASSERT(subsections1_1.empty());
152 
153  const std::vector<std::string> subsections2 =
154  input.get_subsection_names("Section2");
155 
156  CPPUNIT_ASSERT_EQUAL(subsections2.size(), sz(2));
157  CPPUNIT_ASSERT_EQUAL(subsections2[0], str("Subsection2"));
158  CPPUNIT_ASSERT_EQUAL(subsections2[1], str("Subsection4"));
159 
160  const std::vector<std::string> subsections2_2 =
161  input.get_subsection_names("Section2/Subsection2");
162 
163  CPPUNIT_ASSERT_EQUAL(subsections2_2.size(), sz(1));
164  CPPUNIT_ASSERT_EQUAL(subsections2_2[0], str("Subsection3"));
165 
166  const std::vector<std::string> subsections2_4 =
167  input.get_subsection_names("Section2/Subsection4");
168 
169  CPPUNIT_ASSERT(subsections2_4.empty());
170 
171  const std::vector<std::string> subsections3 =
172  input.get_subsection_names("Section3");
173 
174  CPPUNIT_ASSERT(subsections3.empty());
175  }
176 
177 };
178 
void testVariables()
Definition: getpot_test.C:85
std::stringstream inputfile
Definition: getpot_test.C:34
CPPUNIT_TEST_SUITE_REGISTRATION(GetPotTest)
The libMesh namespace provides an interface to certain functionality in the library.
void setUp()
Definition: getpot_test.C:79
void setup_inputfile()
Definition: getpot_test.C:40
void testSubSections()
Definition: getpot_test.C:137
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void testSections()
Definition: getpot_test.C:118
GetPot input
Definition: getpot_test.C:36