QMCPACK
test_xml.cpp File Reference
+ Include dependency graph for test_xml.cpp:

Go to the source code of this file.

Functions

 TEST_CASE ("read_file", "[xml]")
 
 TEST_CASE ("parseString", "[xml]")
 
 TEST_CASE ("XMLParsingString", "[xml]")
 
 TEST_CASE ("putContent", "[xml]")
 
 TEST_CASE ("write_file", "[xml]")
 
 TEST_CASE ("XPathObject", "[xml]")
 

Function Documentation

◆ TEST_CASE() [1/6]

TEST_CASE ( "read_file"  ,
""  [xml] 
)

Definition at line 22 of file test_xml.cpp.

References qmcplusplus::doc, qmcplusplus::okay, Libxml2Document::parse(), and qmcplusplus::REQUIRE().

23 {
25  bool okay = doc.parse("bad.xml");
26  REQUIRE(okay == false);
27 
28  okay = doc.parse("simple.xml");
29  REQUIRE(okay == true);
30 }
class that handles xmlDoc
Definition: Libxml2Doc.h:76
REQUIRE(std::filesystem::exists(filename))
bool parse(const std::string &fname)
Definition: Libxml2Doc.cpp:180

◆ TEST_CASE() [2/6]

TEST_CASE ( "parseString"  ,
""  [xml] 
)

Definition at line 32 of file test_xml.cpp.

References qmcplusplus::doc, getNodeName(), Libxml2Document::getRoot(), qmcplusplus::okay, Libxml2Document::parseFromString(), and qmcplusplus::REQUIRE().

33 {
34  string s1(R"(<?xml version="1.0"?>
35  <simulation></simulation>
36  )");
37 
39  bool okay = doc.parseFromString(s1);
40  REQUIRE(okay == true);
41 
42  xmlNodePtr root = doc.getRoot();
43  REQUIRE(root != NULL);
44 
45  REQUIRE((char*)root->name == string("simulation"));
46 
47  std::string root_name(getNodeName(root));
48 
49  REQUIRE(root_name == "simulation");
50 }
class that handles xmlDoc
Definition: Libxml2Doc.h:76
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
REQUIRE(std::filesystem::exists(filename))
bool parseFromString(const std::string_view data)
Definition: Libxml2Doc.cpp:204
std::string getNodeName(xmlNodePtr cur)
Definition: libxmldefs.cpp:15

◆ TEST_CASE() [3/6]

TEST_CASE ( "XMLParsingString"  ,
""  [xml] 
)

Definition at line 52 of file test_xml.cpp.

References qmcplusplus::doc, Libxml2Document::getRoot(), getXMLAttributeValue(), qmcplusplus::okay, Libxml2Document::parseFromString(), and qmcplusplus::REQUIRE().

53 {
54  string s1(R"(<?xml version="1.0"?>
55  <simulation name="qmc"> aa </simulation>
56  )");
57 
59  bool okay = doc.parseFromString(s1);
60  REQUIRE(okay == true);
61 
62  xmlNodePtr root = doc.getRoot();
63  REQUIRE(root != NULL);
64 
65  const XMLNodeString node_string(root);
66  REQUIRE(node_string == " aa ");
67 
68  const std::string attr_string(getXMLAttributeValue(root, "name"));
69  REQUIRE(attr_string == "qmc");
70 
71  const std::string attr_string_missing(getXMLAttributeValue(root, "not_here"));
72  REQUIRE(attr_string_missing.empty());
73 }
class that handles xmlDoc
Definition: Libxml2Doc.h:76
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
REQUIRE(std::filesystem::exists(filename))
std::string getXMLAttributeValue(const xmlNodePtr cur, const std::string_view name)
get the value string for attribute name if name is unfound in cur you get an empty string back this i...
convert xmlNode contents into a std::string
bool parseFromString(const std::string_view data)
Definition: Libxml2Doc.cpp:204

◆ TEST_CASE() [4/6]

TEST_CASE ( "putContent"  ,
""  [xml] 
)

Definition at line 75 of file test_xml.cpp.

References qmcplusplus::CHECK(), qmcplusplus::doc, qmcplusplus::Units::charge::e, Libxml2Document::getRoot(), qmcplusplus::okay, Libxml2Document::parseFromString(), putContent(), and qmcplusplus::REQUIRE().

76 {
77  string s1(R"(<?xml version="1.0"?>
78  <simulation>
79  <item1>2</item1>
80  <item2>3.5</item2>
81  <item3>4.0</item3>
82  <item4>4.0 5.1</item4>
83  <item5>4.0 5.1,</item5>
84  </simulation>
85  )");
86 
88  bool okay = doc.parseFromString(s1);
89  REQUIRE(okay == true);
90 
91  xmlNodePtr root = doc.getRoot();
92 
93  int a;
94  xmlNodePtr item = xmlFirstElementChild(root);
95  REQUIRE(string((char*)item->name) == "item1");
96  putContent(a, item);
97  REQUIRE(a == 2);
98 
99  double b;
100  item = xmlNextElementSibling(item);
101  REQUIRE(string((char*)item->name) == "item2");
102  putContent(b, item);
103  REQUIRE(b == 3.5);
104 
105  float c;
106  putContent(c, item);
107  CHECK(c == Approx(3.5));
108 
109  vector<double> d;
110  item = xmlNextElementSibling(item);
111  REQUIRE(string((char*)item->name) == "item3");
112  putContent(d, item);
113  REQUIRE(d.size() == 1);
114  vector<double> e;
115  item = xmlNextElementSibling(item);
116  REQUIRE(string((char*)item->name) == "item4");
117  putContent(e, item);
118  REQUIRE(e.size() == 2);
119 
120  vector<double> f;
121  item = xmlNextElementSibling(item);
122  REQUIRE(string((char*)item->name) == "item5");
123  // Will hang, don't test for now
124  //putContent(f, item);
125  //REQUIRE(f.size() == 2);
126 }
class that handles xmlDoc
Definition: Libxml2Doc.h:76
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
REQUIRE(std::filesystem::exists(filename))
bool putContent(T &a, xmlNodePtr cur)
replaces a&#39;s value with the first "element" in the "string" returned by XMLNodeString{cur}.
Definition: libxmldefs.h:88
bool parseFromString(const std::string_view data)
Definition: Libxml2Doc.cpp:204
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))

◆ TEST_CASE() [5/6]

TEST_CASE ( "write_file"  ,
""  [xml] 
)

Definition at line 128 of file test_xml.cpp.

References Libxml2Document::addChild(), qmcplusplus::doc, Libxml2Document::dump(), Libxml2Document::getRoot(), and Libxml2Document::newDoc().

129 {
131  doc.newDoc("root");
132 
133  xmlNodePtr node1 = doc.addChild(doc.getRoot(), "node1");
134  doc.addChild(node1, "value1", 1);
135  doc.addChild(node1, "value2", 3.2);
136  doc.addChild(node1, "boolvalue3", true);
137  doc.addChild(node1, "boolvalue4", false);
138  doc.dump("tmp.out.xml");
139 }
class that handles xmlDoc
Definition: Libxml2Doc.h:76
void newDoc(const std::string &rootName)
Definition: Libxml2Doc.cpp:93
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
void addChild(xmlNodePtr newnode)
Definition: Libxml2Doc.cpp:111
void dump(const std::string &newxml)
Definition: Libxml2Doc.cpp:109

◆ TEST_CASE() [6/6]

TEST_CASE ( "XPathObject"  ,
""  [xml] 
)

Definition at line 141 of file test_xml.cpp.

References qmcplusplus::doc, OhmmsXPathObject::empty(), Libxml2Document::getXPathContext(), qmcplusplus::okay, Libxml2Document::parseFromString(), qmcplusplus::REQUIRE(), and OhmmsXPathObject::size().

142 {
143  const char* content = R"(
144  <simulation>
145  <parameter name="p1">1</parameter>
146  <p2>2</p2>
147  </simulation>
148  )";
150  bool okay = doc.parseFromString(content);
151  REQUIRE(okay == true);
152 
153  xmlXPathContextPtr path_ctx = doc.getXPathContext();
154  OhmmsXPathObject xpath("//parameter", path_ctx);
155  REQUIRE(xpath.empty() == false);
156  REQUIRE(xpath.size() == 1);
157  REQUIRE(xpath[0] != NULL);
158 }
class that handles xmlDoc
Definition: Libxml2Doc.h:76
xmlXPathContextPtr getXPathContext()
Definition: Libxml2Doc.cpp:100
class to handle xmlXPathObject
Definition: Libxml2Doc.h:26
REQUIRE(std::filesystem::exists(filename))
bool parseFromString(const std::string_view data)
Definition: Libxml2Doc.cpp:204