libMesh
getpot_test.C
Go to the documentation of this file.
1 
2 #include <libmesh/getpot.h>
3 
4 // For command_line shim methods
5 #include <libmesh/libmesh.h>
6 
7 #include "libmesh_cppunit.h"
8 
9 using namespace libMesh;
10 
11 class GetPotTest : public CppUnit::TestCase {
12 
13 public:
14  CPPUNIT_TEST_SUITE( GetPotTest );
15 
16  CPPUNIT_TEST( testVariables );
17  CPPUNIT_TEST( testSections );
18  CPPUNIT_TEST( testSubSections );
19  CPPUNIT_TEST( testCommandLine );
20 
21  CPPUNIT_TEST_SUITE_END();
22 
23 protected:
24 
25  std::stringstream inputfile;
26 
27  GetPot input;
28 
29 public:
30 
32  {
33 
34  const char* text =
35  "[Section1]\n"
36  "\n"
37  " var1 = '5.0'\n"
38  "\n"
39  " [./SubSection1]\n"
40  "\n"
41  " var2 = 'blah'\n"
42  "\n"
43  "[]\n"
44  "\n"
45  "[Section2]\n"
46  "\n"
47  " #var3 = '5'\n"
48  "\n"
49  " [./Subsection2]\n"
50  "\n"
51  " var4 = 'false'\n"
52  "\n"
53  " [./Subsection3]\n"
54  "\n"
55  " var5 = '6.02e23'\n"
56  "\n"
57  " [../../Subsection4]\n"
58  "\n"
59  " var6 = '42'\n"
60  "\n"
61  "[]\n"
62  "\n"
63  "[Section3]\n"
64  "\n"
65  " unused_var = 'not_used'\n";
66 
67  inputfile << text;
68  }
69 
70  void setUp()
71  {
72  this->setup_inputfile();
73  input.parse_input_stream(inputfile);
74  }
75 
77  {
78  CPPUNIT_ASSERT( input.have_variable("Section1/var1") );
79  Real var1 = input("Section1/var1", 1.0);
80  CPPUNIT_ASSERT_EQUAL( var1, Real(5));
81 
82  CPPUNIT_ASSERT( input.have_variable("Section1/SubSection1/var2") );
83  std::string var2 = input("Section1/SubSection1/var2", "DIE!");
84  CPPUNIT_ASSERT_EQUAL( var2, std::string("blah") );
85 
86  // This variable is commented out in the input file so we shouldn't find it.
87  CPPUNIT_ASSERT( !input.have_variable("Section2/var3") );
88  int var3 = input("Section2/var3", 314);
89  CPPUNIT_ASSERT_EQUAL( var3, 314 );
90 
91  CPPUNIT_ASSERT( input.have_variable("Section2/Subsection2/var4") );
92  bool var4 = input("Section2/Subsection2/var4", true);
93  CPPUNIT_ASSERT_EQUAL( var4, false );
94 
95  CPPUNIT_ASSERT( input.have_variable("Section2/Subsection2/Subsection3/var5") );
96  Real var5 = input("Section2/Subsection2/Subsection3/var5", 3.1415);
97  CPPUNIT_ASSERT_EQUAL( var5, Real(6.02e23));
98 
99  CPPUNIT_ASSERT( input.have_variable("Section2/Subsection4/var6") );
100  unsigned int var6 = input("Section2/Subsection4/var6", 21);
101  CPPUNIT_ASSERT_EQUAL( var6, (unsigned int)42 );
102 
103  // We didn't use Section3/unused_var so it should be a UFO
104  std::vector<std::string> ufos = input.unidentified_variables();
105  CPPUNIT_ASSERT_EQUAL( ufos.size(), (std::vector<std::string>::size_type)1 );
106  CPPUNIT_ASSERT_EQUAL( ufos[0], std::string("Section3/unused_var") );
107  }
108 
110  {
111  // GetPot stores the '/' at the end of each section name
112  CPPUNIT_ASSERT(input.have_section("Section1/"));
113  CPPUNIT_ASSERT(input.have_section("Section1/SubSection1/"));
114  CPPUNIT_ASSERT(input.have_section("Section2/Subsection2/"));
115  CPPUNIT_ASSERT(input.have_section("Section2/Subsection2/Subsection3/"));
116  CPPUNIT_ASSERT(input.have_section("Section2/Subsection4/"));
117  CPPUNIT_ASSERT(input.have_section("Section3/"));
118 
119  // But we don't need to supply the trailing '/'
120  CPPUNIT_ASSERT(input.have_section("Section1"));
121  CPPUNIT_ASSERT(input.have_section("Section1/SubSection1"));
122  CPPUNIT_ASSERT(input.have_section("Section2/Subsection2/Subsection3"));
123 
124  // No such thing as this section
125  CPPUNIT_ASSERT(!input.have_section("ImNotASection/"));
126  }
127 
129  {
130  typedef std::vector<std::string>::size_type sz;
131  typedef std::string str;
132 
133  const std::vector<std::string> subsections1 =
134  input.get_subsection_names("Section1");
135 
136  CPPUNIT_ASSERT_EQUAL(subsections1.size(), sz(1));
137  CPPUNIT_ASSERT_EQUAL(subsections1[0], str("SubSection1"));
138 
139  const std::vector<std::string> subsections1_1 =
140  input.get_subsection_names("Section1/Subsection1");
141 
142  CPPUNIT_ASSERT(subsections1_1.empty());
143 
144  const std::vector<std::string> subsections2 =
145  input.get_subsection_names("Section2");
146 
147  CPPUNIT_ASSERT_EQUAL(subsections2.size(), sz(2));
148  CPPUNIT_ASSERT_EQUAL(subsections2[0], str("Subsection2"));
149  CPPUNIT_ASSERT_EQUAL(subsections2[1], str("Subsection4"));
150 
151  const std::vector<std::string> subsections2_2 =
152  input.get_subsection_names("Section2/Subsection2");
153 
154  CPPUNIT_ASSERT_EQUAL(subsections2_2.size(), sz(1));
155  CPPUNIT_ASSERT_EQUAL(subsections2_2[0], str("Subsection3"));
156 
157  const std::vector<std::string> subsections2_4 =
158  input.get_subsection_names("Section2/Subsection4");
159 
160  CPPUNIT_ASSERT(subsections2_4.empty());
161 
162  const std::vector<std::string> subsections3 =
163  input.get_subsection_names("Section3");
164 
165  CPPUNIT_ASSERT(subsections3.empty());
166  }
167 
169  {
170  // Test whether the new dash/underscore agnosticism works
171  //
172  // This means the unit_tests-foo executable *must* be run with
173  // options equivalent to the asserted ones and without options
174  // equivalent to the anti-asserted ones.
175  CPPUNIT_ASSERT(libMesh::on_command_line("--option-with-underscores"));
176  CPPUNIT_ASSERT(libMesh::on_command_line("--option_with_underscores"));
177  CPPUNIT_ASSERT(libMesh::on_command_line("--option-with-dashes"));
178  CPPUNIT_ASSERT(libMesh::on_command_line("--option_with_dashes"));
179  CPPUNIT_ASSERT(!libMesh::on_command_line("--option-with-lies"));
180  CPPUNIT_ASSERT(!libMesh::on_command_line("--option_with_lies"));
181  CPPUNIT_ASSERT_EQUAL(libMesh::command_line_next("--option-with-underscores", 2), 3);
182  CPPUNIT_ASSERT_EQUAL(libMesh::command_line_next("--option_with_underscores", 2), 3);
183  }
184 
185 };
186 
GetPotTest::inputfile
std::stringstream inputfile
Definition: getpot_test.C:25
GetPotTest::testSections
void testSections()
Definition: getpot_test.C:109
libMesh::command_line_next
T command_line_next(std::string name, T default_value)
Use GetPot's search()/next() functions to get following arguments from the command line.
Definition: libmesh.C:963
GetPotTest::testCommandLine
void testCommandLine()
Definition: getpot_test.C:168
GetPotTest
Definition: getpot_test.C:11
GetPotTest::testSubSections
void testSubSections()
Definition: getpot_test.C:128
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
GetPotTest::input
GetPot input
Definition: getpot_test.C:27
GetPotTest::setUp
void setUp()
Definition: getpot_test.C:70
libmesh_cppunit.h
libMesh::on_command_line
bool on_command_line(std::string arg)
Definition: libmesh.C:898
GetPotTest::testVariables
void testVariables()
Definition: getpot_test.C:76
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(GetPotTest)
GetPotTest::setup_inputfile
void setup_inputfile()
Definition: getpot_test.C:31