QMCPACK
test_ParameterSet.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2022 QMCPACK developers.
6 //
7 // File developed by: Mark Dewing, markdewing@gmail.com, University of Illinois at Urbana-Champaign
8 // Ye Luo, yeluo@anl.gov, Argonne National Laboratory
9 //
10 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 #include "catch.hpp"
15 
16 #include "OhmmsData/Libxml2Doc.h"
17 #include "OhmmsData/ParameterSet.h"
18 
19 TEST_CASE("ParameterSet", "[xml]")
20 {
21  const char* content = R"(
22 <simulation>
23  <parameter name="p1">1</parameter>
24  <parameter name="p4"> -2 -3 </parameter>
25  <p2>2</p2>
26 </simulation>)";
28  bool okay = doc.parseFromString(content);
29  REQUIRE(okay == true);
30 
31  xmlNodePtr root = doc.getRoot();
32  ParameterSet param;
33  int p1_val = 0;
34  int p2_val = 0;
35  int p3_val = 0;
36  qmcplusplus::astring p4_str;
37  param.add(p1_val, "p1");
38  param.add(p2_val, "p2");
39  param.add(p3_val, "p3");
40  param.add(p4_str, "p4");
41  param.put(root);
42 
43  CHECK(p1_val == 1);
44  CHECK(p2_val == 2);
45  CHECK(p3_val == 0);
46 
47  auto p4_vals = qmcplusplus::convertStrToVec<int>(p4_str.s);
48  REQUIRE(p4_vals.size() == 2);
49  CHECK(p4_vals[0] == -2);
50  CHECK(p4_vals[1] == -3);
51 }
52 
53 TEST_CASE("ParameterSet_bool", "[xml]")
54 {
55  {
56  const char* content = R"(
57  <simulation>
58  <parameter name="p1"> yes </parameter>
59  <parameter name="p2"> no </parameter>
60  </simulation>)";
62  bool okay = doc.parseFromString(content);
63  REQUIRE(okay == true);
64 
65  ParameterSet param;
66  bool p1_val = false;
67  bool p2_val = true;
68  bool p3_val = false;
69  qmcplusplus::astring p4_str;
70  param.add(p1_val, "p1");
71  param.add(p2_val, "p2");
72  param.add(p3_val, "p3", {true, false});
73  param.put(doc.getRoot());
74 
75  CHECK(p1_val == true);
76  CHECK(p2_val == false);
77  CHECK(p3_val == true);
78  }
79 
80  {
81  const char* content = R"(
82  <simulation>
83  <parameter name="p1"> </parameter>
84  </simulation>)";
86  bool okay = doc.parseFromString(content);
87  REQUIRE(okay == true);
88 
89  ParameterSet param;
90  bool p1_val = false;
91  qmcplusplus::astring p4_str;
92  param.add(p1_val, "p1");
93  CHECK_THROWS_WITH(param.put(doc.getRoot()), "p1 requires a single value input.");
94  }
95 
96  {
97  const char* content = R"(
98  <simulation>
99  <parameter name="p1"> yes no </parameter>
100  </simulation>)";
102  bool okay = doc.parseFromString(content);
103  REQUIRE(okay == true);
104 
105  ParameterSet param;
106  bool p1_val = false;
107  qmcplusplus::astring p4_str;
108  param.add(p1_val, "p1");
109  CHECK_THROWS_WITH(param.put(doc.getRoot()), "p1 only accepts a single value input.");
110  }
111 
112  {
113  const char* content = R"(
114  <simulation>
115  <parameter name="p1"> here </parameter>
116  </simulation>)";
118  bool okay = doc.parseFromString(content);
119  REQUIRE(okay == true);
120 
121  ParameterSet param;
122  bool p1_val = false;
123  qmcplusplus::astring p4_str;
124  param.add(p1_val, "p1");
125  CHECK_THROWS_WITH(param.put(doc.getRoot()), "p1 only accepts 'yes'/'no'/'true'/'false' but the input value is 'here'.");
126  }
127 }
class that handles xmlDoc
Definition: Libxml2Doc.h:76
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
bool put(std::istream &is) override
read from std::istream
Definition: ParameterSet.h:42
class to handle a set of parameters
Definition: ParameterSet.h:27
REQUIRE(std::filesystem::exists(filename))
TEST_CASE("ParameterSet", "[xml]")
void add(PDT &aparam, const std::string &aname_in, std::vector< PDT > candidate_values={}, TagStatus status=TagStatus::OPTIONAL)
add a new parameter corresponding to an xmlNode <parameter>
bool parseFromString(const std::string_view data)
Definition: Libxml2Doc.cpp:204
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))