QMCPACK
test_xml.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) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Mark Dewing, markdewing@gmail.com, University of Illinois at Urbana-Champaign
8 //
9 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "catch.hpp"
14 
15 #include "OhmmsData/Libxml2Doc.h"
16 #include <string>
17 #include <vector>
18 
19 using std::string;
20 using std::vector;
21 
22 TEST_CASE("read_file", "[xml]")
23 {
25  bool okay = doc.parse("bad.xml");
26  REQUIRE(okay == false);
27 
28  okay = doc.parse("simple.xml");
29  REQUIRE(okay == true);
30 }
31 
32 TEST_CASE("parseString", "[xml]")
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 }
51 
52 TEST_CASE("XMLParsingString", "[xml]")
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 }
74 
75 TEST_CASE("putContent", "[xml]")
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 }
127 
128 TEST_CASE("write_file", "[xml]")
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 }
140 
141 TEST_CASE("XPathObject", "[xml]")
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
TEST_CASE("read_file", "[xml]")
Definition: test_xml.cpp:22
xmlXPathContextPtr getXPathContext()
Definition: Libxml2Doc.cpp:100
void newDoc(const std::string &rootName)
Definition: Libxml2Doc.cpp:93
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
class to handle xmlXPathObject
Definition: Libxml2Doc.h:26
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...
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
convert xmlNode contents into a std::string
bool parseFromString(const std::string_view data)
Definition: Libxml2Doc.cpp:204
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
void addChild(xmlNodePtr newnode)
Definition: Libxml2Doc.cpp:111
bool parse(const std::string &fname)
Definition: Libxml2Doc.cpp:180
std::string getNodeName(xmlNodePtr cur)
Definition: libxmldefs.cpp:15
void dump(const std::string &newxml)
Definition: Libxml2Doc.cpp:109