preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ParserTest.cpp
Go to the documentation of this file.
1#include <Eigen/Core>
2#include <string>
3#include <tuple>
4#include "math/constants.hpp"
6#include "testing/Testing.hpp"
8#include "xml/ValueParser.hpp"
10#include "xml/XMLTag.hpp"
11
12using namespace precice;
13using namespace precice::xml;
15
17
18struct CallbackHostAttr : public XMLTag::Listener {
19 Eigen::VectorXd eigenValue1;
20 Eigen::VectorXd eigenValue2;
21 Eigen::VectorXd eigenValue3;
26
27 void xmlTagCallback(const ConfigurationContext &context, XMLTag &callingTag) override
28 {
29 if (callingTag.getName() == "test-double") {
30 doubleValue = callingTag.getDoubleAttributeValue("attribute");
31 }
32
33 if (callingTag.getName() == "test-eigen-1") {
34 eigenValue1 = callingTag.getEigenVectorXdAttributeValue("attribute");
35 }
36
37 if (callingTag.getName() == "test-eigen-2") {
38 eigenValue2 = callingTag.getEigenVectorXdAttributeValue("attribute");
39 }
40
41 if (callingTag.getName() == "test-eigen-3") {
42 eigenValue3 = callingTag.getEigenVectorXdAttributeValue("attribute");
43 }
44
45 if (callingTag.getName() == "test-int") {
46 intValue = callingTag.getIntAttributeValue("attribute");
47 }
48
49 if (callingTag.getName() == "test-bool") {
50 boolValue = callingTag.getBooleanAttributeValue("attribute");
51 }
52
53 if (callingTag.getName() == "test-string") {
54 stringValue = callingTag.getStringAttributeValue("text");
55 }
56 }
57
58 void xmlEndTagCallback(const ConfigurationContext &context, XMLTag &callingTag) override
59 {
60 std::ignore = callingTag;
61 }
62};
63
65BOOST_AUTO_TEST_CASE(AttributeTypeTest)
66{
68 std::string filename(getPathToSources() + "/xml/tests/xmlparser_test.xml");
69
71 XMLTag rootTag(cb, "configuration", XMLTag::OCCUR_ONCE);
72 XMLTag testcaseTag(cb, "test-config", XMLTag::OCCUR_ONCE);
73
74 XMLTag doubleTag(cb, "test-double", XMLTag::OCCUR_ONCE_OR_MORE);
75 XMLTag eigen1Tag(cb, "test-eigen-1", XMLTag::OCCUR_ONCE_OR_MORE);
76 XMLTag eigen2Tag(cb, "test-eigen-2", XMLTag::OCCUR_ONCE_OR_MORE);
77 XMLTag eigen3Tag(cb, "test-eigen-3", XMLTag::OCCUR_ONCE_OR_MORE);
78 XMLTag intTag(cb, "test-int", XMLTag::OCCUR_ONCE_OR_MORE);
79 XMLTag stringTag(cb, "test-string", XMLTag::OCCUR_ONCE_OR_MORE);
80 XMLTag boolTag(cb, "test-bool", XMLTag::OCCUR_ONCE_OR_MORE);
81
82 XMLAttribute<double> doubleAttr("attribute");
83 XMLAttribute<Eigen::VectorXd> eigenAttr("attribute");
84 XMLAttribute<int> intAttr("attribute");
85 XMLAttribute<bool> boolAttr("attribute");
86 XMLAttribute<std::string> stringAttr("text");
87
88 doubleTag.addAttribute(doubleAttr);
89 eigen1Tag.addAttribute(eigenAttr);
90 eigen2Tag.addAttribute(eigenAttr);
91 eigen3Tag.addAttribute(eigenAttr);
92 intTag.addAttribute(intAttr);
93 boolTag.addAttribute(boolAttr);
94 stringTag.addAttribute(stringAttr);
95
96 testcaseTag.addSubtag(doubleTag);
97 testcaseTag.addSubtag(eigen1Tag);
98 testcaseTag.addSubtag(eigen2Tag);
99 testcaseTag.addSubtag(eigen3Tag);
100 testcaseTag.addSubtag(intTag);
101 testcaseTag.addSubtag(boolTag);
102 testcaseTag.addSubtag(stringTag);
103
104 rootTag.addSubtag(testcaseTag);
105
106 auto hash = configure(rootTag, ConfigurationContext{}, filename);
107 BOOST_TEST(hash == "60a732f93a3a52eea0b33582a0ce0e92");
108
109 BOOST_TEST(cb.boolValue == true);
110 BOOST_TEST(cb.doubleValue == 3.1);
111 BOOST_TEST(cb.intValue == 4);
112 BOOST_TEST(cb.stringValue == "Hello World");
113
114 BOOST_TEST(cb.eigenValue1.size() == 1);
115 BOOST_TEST(cb.eigenValue1(0) == 3.0);
116
117 BOOST_TEST(cb.eigenValue2.size() == 2);
118 BOOST_TEST(cb.eigenValue2(0) == 3.0);
119 BOOST_TEST(cb.eigenValue2(1) == 2.0);
120
121 BOOST_TEST(cb.eigenValue3.size() == 3);
122 BOOST_TEST(cb.eigenValue3(0) == 3.0);
123 BOOST_TEST(cb.eigenValue3(1) == 2.0);
124 BOOST_TEST(cb.eigenValue3(2) == 1.0);
125}
126
127PRECICE_TEST_SETUP(1_rank)
129{
130 PRECICE_TEST();
131 std::string filename(getPathToSources() + "/xml/tests/xmlparser_occtest.xml");
132
134 XMLTag rootTag(cb, "configuration", XMLTag::OCCUR_ONCE);
135 XMLTag testcaseTag(cb, "test-config", XMLTag::OCCUR_ONCE);
136
137 XMLTag occ2(cb, "test-occ_once_or_more-once", XMLTag::OCCUR_ONCE_OR_MORE);
138 XMLTag occ3(cb, "test-occ_arbitrary", XMLTag::OCCUR_ARBITRARY);
139 XMLTag occ4(cb, "test-occ_not_or_once", XMLTag::OCCUR_NOT_OR_ONCE);
140
141 XMLTag occ5(cb, "test-occ_once_or_more-more", XMLTag::OCCUR_ONCE_OR_MORE);
142 XMLTag occ6(cb, "test-occ_arbitrary-opt", XMLTag::OCCUR_ARBITRARY);
143 XMLTag occ7(cb, "test-occ_not_or_once-opt", XMLTag::OCCUR_NOT_OR_ONCE);
144
145 testcaseTag.addSubtag(occ2);
146 testcaseTag.addSubtag(occ3);
147 testcaseTag.addSubtag(occ4);
148
149 testcaseTag.addSubtag(occ5);
150 testcaseTag.addSubtag(occ6);
151 testcaseTag.addSubtag(occ7);
152
153 rootTag.addSubtag(testcaseTag);
154
155 auto hash = configure(rootTag, ConfigurationContext{}, filename);
156 BOOST_TEST(hash == "b2b2b03b807f5d0d86d77fdb2f07b42f");
157}
158
159PRECICE_TEST_SETUP(1_rank)
161{
162 PRECICE_TEST();
163 std::string filename(getPathToSources() + "/xml/tests/xmlparser_nstest.xml");
164
166 XMLTag rootTag(cb, "configuration", XMLTag::OCCUR_ONCE);
167 XMLTag testcaseTag(cb, "test-config", XMLTag::OCCUR_ONCE);
168
169 XMLTag intTag(cb, "test-no_ns", XMLTag::OCCUR_ONCE_OR_MORE);
170 XMLTag stringTag(cb, "test-ns", XMLTag::OCCUR_ONCE_OR_MORE, "ns");
171
172 testcaseTag.addNamespace("ns");
173
174 testcaseTag.addSubtag(intTag);
175 testcaseTag.addSubtag(stringTag);
176
177 rootTag.addSubtag(testcaseTag);
178
179 auto hash = configure(rootTag, ConfigurationContext{}, filename);
180 BOOST_TEST(hash == "758805e1fc385a73b929cc75ade90c8c");
181}
182
186
187 void xmlTagCallback(const ConfigurationContext &context, XMLTag &callingTag) override
188 {
189 startContext = context;
190 }
191
192 void xmlEndTagCallback(const ConfigurationContext &context, XMLTag &callingTag) override
193 {
194 endContext = context;
195 }
196};
197
198PRECICE_TEST_SETUP(1_rank)
200{
201 PRECICE_TEST();
202 std::string filename(getPathToSources() + "/xml/tests/config_xmltest_context.xml");
203
205 XMLTag rootTag(cl, "configuration", XMLTag::OCCUR_ONCE);
206 ConfigurationContext ccontext{"test", 12, 32};
207 auto hash = configure(rootTag, ccontext, filename);
208 BOOST_TEST(hash == "c63ea663514b5150ae9415d7adbb84cb");
209 BOOST_TEST(cl.startContext.name == "test");
210 BOOST_TEST(cl.startContext.rank == 12);
211 BOOST_TEST(cl.startContext.size == 32);
212 BOOST_TEST(cl.endContext.name == "test");
213 BOOST_TEST(cl.endContext.rank == 12);
214 BOOST_TEST(cl.endContext.size == 32);
215}
216
217PRECICE_TEST_SETUP(1_rank)
219{
220 PRECICE_TEST();
221
222 BOOST_TEST(decodeXML("Less than &lt; test") == "Less than < test");
223 BOOST_TEST(decodeXML("Greater than &gt; test") == "Greater than > test");
224 BOOST_TEST(decodeXML("Ampersand &amp; test") == "Ampersand & test");
225 BOOST_TEST(decodeXML("Quotation &quot; test") == "Quotation \" test");
226 BOOST_TEST(decodeXML("Apostrophe &apos; test") == "Apostrophe ' test");
227
228 BOOST_TEST(decodeXML("&quot; &lt; &gt; &gt; &lt; &amp; &quot; &amp; &apos;") == "\" < > > < & \" & '");
229}
230
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(AttributeTypeTest)
#define PRECICE_TEST()
Definition Testing.hpp:39
#define PRECICE_TEST_SETUP(...)
Creates and attaches a TestSetup to a Boost test case.
Definition Testing.hpp:29
Represents an XML tag to be configured automatically.
Definition XMLTag.hpp:28
Eigen::VectorXd getEigenVectorXdAttributeValue(const std::string &name) const
Definition XMLTag.cpp:173
std::string getStringAttributeValue(const std::string &name, std::optional< std::string > default_value=std::nullopt) const
Definition XMLTag.cpp:145
bool getBooleanAttributeValue(const std::string &name, std::optional< bool > default_value=std::nullopt) const
Definition XMLTag.cpp:159
const std::string & getName() const
Returns name (without namespace).
Definition XMLTag.hpp:153
int getIntAttributeValue(const std::string &name, std::optional< int > default_value=std::nullopt) const
Definition XMLTag.cpp:131
XMLTag & addNamespace(const std::string &namespaceName)
Adds a namespace to the tag.
Definition XMLTag.cpp:35
double getDoubleAttributeValue(const std::string &name, std::optional< double > default_value=std::nullopt) const
Definition XMLTag.cpp:117
XMLTag & addAttribute(const XMLAttribute< double > &attribute)
Adds a XML attribute by making a copy of the given attribute.
Definition XMLTag.cpp:53
XMLTag & addSubtag(const XMLTag &tag)
Adds an XML tag as subtag by making a copy of the given tag.
Definition XMLTag.cpp:41
std::string getPathToSources()
Returns the base path to the sources.
Definition Testing.cpp:33
contains the XML configuration parser.
std::string decodeXML(std::string_view xml)
Decodes escape sequences of a given xml.
std::string configure(XMLTag &tag, const precice::xml::ConfigurationContext &context, std::string_view configurationFilename)
Configures the given configuration from file configurationFilename.
Definition XMLTag.cpp:284
Main namespace of the precice library.
std::string stringValue
void xmlTagCallback(const ConfigurationContext &context, XMLTag &callingTag) override
Callback at begin of XML tag.
Eigen::VectorXd eigenValue2
void xmlEndTagCallback(const ConfigurationContext &context, XMLTag &callingTag) override
Callback at end of XML tag and at end of subtag.
Eigen::VectorXd eigenValue1
Eigen::VectorXd eigenValue3
void xmlTagCallback(const ConfigurationContext &context, XMLTag &callingTag) override
Callback at begin of XML tag.
void xmlEndTagCallback(const ConfigurationContext &context, XMLTag &callingTag) override
Callback at end of XML tag and at end of subtag.
ConfigurationContext endContext
ConfigurationContext startContext
Tightly coupled to the parameters of Participant()
Definition XMLTag.hpp:21
Callback interface for configuration classes using XMLTag.
Definition XMLTag.hpp:46