libMesh
parameters_test.C
Go to the documentation of this file.
1 #include <libmesh/parameters.h>
2 
3 #include "libmesh_cppunit.h"
4 
5 #include <map>
6 #include <string>
7 
8 using namespace libMesh;
9 
10 class ParametersTest : public CppUnit::TestCase {
11 public:
12  LIBMESH_CPPUNIT_TEST_SUITE( ParametersTest );
13 
14  CPPUNIT_TEST( testInt );
15  CPPUNIT_TEST( testFloat );
16  CPPUNIT_TEST( testDouble );
17 
18  CPPUNIT_TEST( testMap );
19 
20  CPPUNIT_TEST_SUITE_END();
21 
22 private:
23 
24 public:
25  void setUp()
26  {}
27 
28  void tearDown()
29  {}
30 
31  template <typename T>
32  void testScalar ()
33  {
34  [[maybe_unused]] // nvc++ has some issues...
35  Parameters param;
36 
37  T t = 10, t_orig = 10;
38 
39  param.set<T>("first") = t;
40 
41  ++t;
42 
43  param.set<T>("second") = t;
44 
45  CPPUNIT_ASSERT_EQUAL(t_orig, param.get<T>("first"));
46  CPPUNIT_ASSERT_EQUAL(t, param.get<T>("second"));
47  }
48 
49  void testMap ()
50  {
51  LOG_UNIT_TEST;
52 
53  Parameters param;
54 
55  std::map<int, std::string> m;
56  m[2] = "two";
57  m[4] = "four";
58  param.set<std::map<int, std::string>>("mymap") = m;
59  const std::map<int, std::string> & gotten =
60  param.get<std::map<int, std::string>>("mymap");
61 
62  CPPUNIT_ASSERT_EQUAL(gotten.size(), std::size_t(2));
63  CPPUNIT_ASSERT_EQUAL(gotten.at(2), std::string("two"));
64  CPPUNIT_ASSERT_EQUAL(gotten.at(4), std::string("four"));
65  }
66 
67  void testInt () { LOG_UNIT_TEST; testScalar<int>(); }
68  void testFloat () { LOG_UNIT_TEST; testScalar<float>(); }
69  void testDouble () { LOG_UNIT_TEST; testScalar<double>(); }
70 };
71 
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:67
The libMesh namespace provides an interface to certain functionality in the library.
const T & get(std::string_view) const
Definition: parameters.h:426
CPPUNIT_TEST_SUITE_REGISTRATION(ParametersTest)
T & set(const std::string &)
Definition: parameters.h:469