Nektar++
Loading...
Searching...
No Matches
Functions
Nektar::SessionNodeUnitTests Namespace Reference

Functions

static SessionNode BuildForcingSection ()
 Builds the tree that the XML in ParseXml() below describes, but by hand, so that the two construction routes can be compared.
 
static SessionNode ParseXml (const std::string &xml, const std::string &name="")
 Parses xml and converts the root element into a SessionNode.
 
 BOOST_AUTO_TEST_CASE (TestSessionNodeIdentity)
 
 BOOST_AUTO_TEST_CASE (TestSessionNodeAttributes)
 
 BOOST_AUTO_TEST_CASE (TestSessionNodeDescribe)
 
 BOOST_AUTO_TEST_CASE (TestSessionNodeChildren)
 
 BOOST_AUTO_TEST_CASE (TestSessionNodeValues)
 
 BOOST_AUTO_TEST_CASE (TestSessionNodePaths)
 
 BOOST_AUTO_TEST_CASE (TestSessionNodeConversions)
 
 BOOST_AUTO_TEST_CASE (TestSessionNodeBooleans)
 
 BOOST_AUTO_TEST_CASE (TestSessionNodeTimeLevel)
 
 BOOST_AUTO_TEST_CASE (TestSessionNodeFromXml)
 
 BOOST_AUTO_TEST_CASE (TestSessionNodeFromXmlEdgeCases)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/11]

Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE ( TestSessionNodeAttributes  )

Definition at line 96 of file TestSessionNode.cpp.

97{
98 SessionNode node("FORCE");
99 node.SetAttribute("TYPE", "Absorption");
100 node.SetAttribute("NVARS", "3");
101
102 BOOST_CHECK(node.DefinesAttribute("TYPE"));
103 BOOST_CHECK(!node.DefinesAttribute("MISSING"));
104 BOOST_CHECK_EQUAL(node.GetAttribute("TYPE"), "Absorption");
105 BOOST_CHECK_EQUAL(node.GetAttribute("MISSING", "fallback"), "fallback");
106 BOOST_CHECK_EQUAL(node.GetAttributeAs<int>("NVARS"), 3);
107 BOOST_CHECK_EQUAL(node.GetAttributeAs<int>("MISSING", 7), 7);
108
109 // Attributes are kept in the order they were supplied, and setting an
110 // existing attribute replaces it in place rather than appending.
111 node.SetAttribute("TYPE", "Noise");
112 BOOST_CHECK_EQUAL(node.GetAttributes().size(), 2u);
113 BOOST_CHECK_EQUAL(node.GetAttributes()[0].first, "TYPE");
114 BOOST_CHECK_EQUAL(node.GetAttributes()[0].second, "Noise");
115
116 BOOST_CHECK_THROW(node.GetAttribute("MISSING"), ErrorUtil::NekError);
117}
A single node of a format-neutral session configuration tree.

References Nektar::LibUtilities::SessionNode::DefinesAttribute(), Nektar::LibUtilities::SessionNode::GetAttribute(), Nektar::LibUtilities::SessionNode::GetAttributeAs(), Nektar::LibUtilities::SessionNode::GetAttributes(), and Nektar::LibUtilities::SessionNode::SetAttribute().

◆ BOOST_AUTO_TEST_CASE() [2/11]

Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE ( TestSessionNodeBooleans  )

Definition at line 217 of file TestSessionNode.cpp.

218{
219 SessionNode node("SETTINGS");
220 node.AddChild(SessionNode("A", "true"));
221 node.AddChild(SessionNode("B", "Yes"));
222 node.AddChild(SessionNode("C", "ON"));
223 node.AddChild(SessionNode("D", "1"));
224 node.AddChild(SessionNode("E", "false"));
225 node.AddChild(SessionNode("F", "No"));
226 node.AddChild(SessionNode("G", "0"));
227 node.AddChild(SessionNode("H", "maybe"));
228
229 for (const std::string name : {"A", "B", "C", "D"})
230 {
231 BOOST_CHECK(node.GetValueAs<bool>(name));
232 }
233 for (const std::string name : {"E", "F", "G"})
234 {
235 BOOST_CHECK(!node.GetValueAs<bool>(name));
236 }
237
238 BOOST_CHECK_THROW(node.GetValueAs<bool>("H"), ErrorUtil::NekError);
239 BOOST_CHECK(!node.GetValueAs<bool>("MISSING", false));
240}

References Nektar::LibUtilities::SessionNode::AddChild(), and Nektar::LibUtilities::SessionNode::GetValueAs().

◆ BOOST_AUTO_TEST_CASE() [3/11]

Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE ( TestSessionNodeChildren  )

Definition at line 131 of file TestSessionNode.cpp.

132{
134
135 BOOST_CHECK(forcing.DefinesChild("FORCE"));
136 BOOST_CHECK(!forcing.DefinesChild("MAPPING"));
137 BOOST_CHECK_EQUAL(forcing.GetChildren().size(), 2u);
138
139 // Repeated names are how repeated entries are represented, and document
140 // order is preserved.
141 std::vector<const SessionNode *> forces = forcing.GetChildren("FORCE");
142 BOOST_REQUIRE_EQUAL(forces.size(), 2u);
143 BOOST_CHECK_EQUAL(forces[0]->GetAttribute("TYPE"), "Absorption");
144 BOOST_CHECK_EQUAL(forces[1]->GetAttribute("TYPE"), "Noise");
145
146 // GetChild() and GetChildPtr() return the first match.
147 BOOST_CHECK_EQUAL(forcing.GetChild("FORCE").GetAttribute("TYPE"),
148 "Absorption");
149 BOOST_CHECK(forcing.GetChildPtr("MAPPING") == nullptr);
150 BOOST_CHECK_THROW(forcing.GetChild("MAPPING"), ErrorUtil::NekError);
151}
const SessionNode * GetChildPtr(const std::string &name) const
Returns the first child called name, or nullptr if there is none.
bool DefinesChild(const std::string &name) const
Returns true if this node has at least one child called name.
const std::string & GetAttribute(const std::string &name) const
Returns the value of attribute name; a fatal error is raised if the attribute is not defined.
const SessionNode & GetChild(const std::string &name) const
Returns the first child called name; a fatal error is raised if there is none.
const ChildList & GetChildren() const
Returns every child of this node, in document order.
static SessionNode BuildForcingSection()
Builds the tree that the XML in ParseXml() below describes, but by hand, so that the two construction...

References BuildForcingSection(), Nektar::LibUtilities::SessionNode::DefinesChild(), Nektar::LibUtilities::SessionNode::GetAttribute(), Nektar::LibUtilities::SessionNode::GetChild(), Nektar::LibUtilities::SessionNode::GetChildPtr(), and Nektar::LibUtilities::SessionNode::GetChildren().

◆ BOOST_AUTO_TEST_CASE() [4/11]

Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE ( TestSessionNodeConversions  )

Definition at line 187 of file TestSessionNode.cpp.

188{
189 SessionNode node("SETTINGS");
190 node.AddChild(SessionNode("PADDED", " 42 "));
191 node.AddChild(SessionNode("TRAILING", "3abc"));
192 node.AddChild(SessionNode("NEGATIVE", "-1"));
193 node.AddChild(SessionNode("HUGE", "999999999999999999999"));
194 node.AddChild(SessionNode("EMPTY", ""));
195 node.AddChild(SessionNode("REAL", "1.5e-3"));
196
197 // Surrounding whitespace is ignored, so that <NSTEPS> 42 </NSTEPS> reads
198 // as an integer for readers which do not strip it themselves.
199 BOOST_CHECK_EQUAL(node.GetValueAs<int>("PADDED"), 42);
200 BOOST_CHECK_CLOSE(node.GetValueAs<NekDouble>("REAL"), 1.5e-3, 1e-12);
201 BOOST_CHECK_EQUAL(node.GetValueAs<int>("NEGATIVE"), -1);
202
203 // Unlike std::stoi, trailing junk is rejected rather than ignored.
204 BOOST_CHECK_THROW(node.GetValueAs<int>("TRAILING"), ErrorUtil::NekError);
205 BOOST_CHECK_THROW(node.GetValueAs<int>("EMPTY"), ErrorUtil::NekError);
206
207 // Out-of-range values are rejected rather than wrapping around, and a
208 // negative value is never silently read as a large unsigned one.
209 BOOST_CHECK_THROW(node.GetValueAs<int>("HUGE"), ErrorUtil::NekError);
210 BOOST_CHECK_THROW(node.GetValueAs<size_t>("NEGATIVE"), ErrorUtil::NekError);
211
212 // A string "conversion" always succeeds and is verbatim, so that values
213 // which are later parsed by ParseUtils or the interpreter are untouched.
214 BOOST_CHECK_EQUAL(node.GetValueAs<std::string>("PADDED"), " 42 ");
215}

References Nektar::LibUtilities::SessionNode::AddChild(), and Nektar::LibUtilities::SessionNode::GetValueAs().

◆ BOOST_AUTO_TEST_CASE() [5/11]

Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE ( TestSessionNodeDescribe  )

Definition at line 119 of file TestSessionNode.cpp.

120{
122
123 BOOST_CHECK_EQUAL(forcing.GetChild("FORCE").Describe(),
124 "FORCING/FORCE [TYPE = \"Absorption\"]");
125
126 // A node with no attributes describes as just its path.
127 BOOST_CHECK_EQUAL(forcing.GetChild("FORCE").GetChild("COEFF").Describe(),
128 "FORCING/FORCE/COEFF");
129}
std::string Describe() const
Returns a short human-readable rendering of this node, naming its path and its attributes,...

References BuildForcingSection(), Nektar::LibUtilities::SessionNode::Describe(), and Nektar::LibUtilities::SessionNode::GetChild().

◆ BOOST_AUTO_TEST_CASE() [6/11]

Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE ( TestSessionNodeFromXml  )

Definition at line 269 of file TestSessionNode.cpp.

270{
271 const std::string xml = "<FORCING>"
272 " <FORCE TYPE=\"Absorption\">"
273 " <COEFF> SpongeCoefficient </COEFF>"
274 " <BOUNDARYREGIONS> 0 </BOUNDARYREGIONS>"
275 " </FORCE>"
276 " <FORCE TYPE=\"Noise\">"
277 " <WHITENOISE> 0.01 </WHITENOISE>"
278 " <UPDATEFREQ> 10 </UPDATEFREQ>"
279 " </FORCE>"
280 "</FORCING>";
281
282 SessionNode parsed = ParseXml(xml);
284
285 // TinyXML condenses whitespace, so the text of an element arrives here
286 // already stripped: the parsed tree should match the hand-built one
287 // exactly.
288 BOOST_CHECK_EQUAL(parsed.GetName(), built.GetName());
289 BOOST_REQUIRE_EQUAL(parsed.GetChildren("FORCE").size(),
290 built.GetChildren("FORCE").size());
291
292 for (size_t i = 0; i < parsed.GetChildren("FORCE").size(); ++i)
293 {
294 const SessionNode *p = parsed.GetChildren("FORCE")[i];
295 const SessionNode *b = built.GetChildren("FORCE")[i];
296
297 BOOST_CHECK_EQUAL(p->GetAttribute("TYPE"), b->GetAttribute("TYPE"));
298 BOOST_REQUIRE_EQUAL(p->GetChildren().size(), b->GetChildren().size());
299
300 for (size_t j = 0; j < p->GetChildren().size(); ++j)
301 {
302 BOOST_CHECK_EQUAL(p->GetChildren()[j].GetName(),
303 b->GetChildren()[j].GetName());
304 BOOST_CHECK_EQUAL(p->GetChildren()[j].GetText(),
305 b->GetChildren()[j].GetText());
306 BOOST_CHECK_EQUAL(p->GetChildren()[j].GetPath(),
307 b->GetChildren()[j].GetPath());
308 }
309 }
310}
const std::string & GetName() const
Returns the name of this node, e.g. "FORCE".
static SessionNode ParseXml(const std::string &xml, const std::string &name="")
Parses xml and converts the root element into a SessionNode.

References BuildForcingSection(), Nektar::LibUtilities::SessionNode::GetChildren(), Nektar::LibUtilities::SessionNode::GetName(), and ParseXml().

◆ BOOST_AUTO_TEST_CASE() [7/11]

Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE ( TestSessionNodeFromXmlEdgeCases  )

Definition at line 312 of file TestSessionNode.cpp.

313{
314 // An element whose children are all elements carries no text, and an
315 // element with neither text nor children is still a valid, empty node.
316 SessionNode parsed = ParseXml("<FORCE><EMPTY/></FORCE>");
317 BOOST_CHECK(!parsed.DefinesText());
318 BOOST_REQUIRE(parsed.DefinesChild("EMPTY"));
319 BOOST_CHECK(!parsed.GetChild("EMPTY").DefinesText());
320 BOOST_CHECK_EQUAL(parsed.GetChild("EMPTY").GetChildren().size(), 0u);
321
322 // Comments carry no configuration and are dropped.
323 parsed = ParseXml("<FORCE><!-- a comment --><A>1</A></FORCE>");
324 BOOST_CHECK_EQUAL(parsed.GetChildren().size(), 1u);
325 BOOST_CHECK_EQUAL(parsed.GetValue("A"), "1");
326
327 // The root can be renamed, so that a section can be given its logical
328 // name rather than whatever the document happened to call it.
329 parsed = ParseXml("<Forcing><A>1</A></Forcing>", "FORCING");
330 BOOST_CHECK_EQUAL(parsed.GetName(), "FORCING");
331 BOOST_CHECK_EQUAL(parsed.GetChild("A").GetPath(), "FORCING/A");
332
333 // A null element yields an empty node rather than a crash, so callers
334 // may convert an optional section unconditionally.
335 SessionNode none = LibUtilities::SessionNodeFromXml(nullptr, "FORCING");
336 BOOST_CHECK_EQUAL(none.GetName(), "FORCING");
337 BOOST_CHECK_EQUAL(none.GetChildren().size(), 0u);
338}
bool DefinesText() const
Returns true if this node carries a non-empty block of text.
const std::string & GetPath() const
Returns the path of this node from the root of the tree it belongs to, e.g. "FORCING/FORCE/COEFF".
const std::string & GetValue(const std::string &name) const
Returns the text of the first child called name.

References Nektar::LibUtilities::SessionNode::DefinesChild(), Nektar::LibUtilities::SessionNode::DefinesText(), Nektar::LibUtilities::SessionNode::GetChild(), Nektar::LibUtilities::SessionNode::GetChildren(), Nektar::LibUtilities::SessionNode::GetName(), Nektar::LibUtilities::SessionNode::GetPath(), Nektar::LibUtilities::SessionNode::GetValue(), ParseXml(), and Nektar::LibUtilities::SessionNodeFromXml().

◆ BOOST_AUTO_TEST_CASE() [8/11]

Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE ( TestSessionNodeIdentity  )

Definition at line 82 of file TestSessionNode.cpp.

83{
84 SessionNode node("COEFF", "SpongeCoefficient");
85
86 BOOST_CHECK_EQUAL(node.GetName(), "COEFF");
87 BOOST_CHECK_EQUAL(node.GetText(), "SpongeCoefficient");
88 BOOST_CHECK_EQUAL(node.GetPath(), "COEFF");
89 BOOST_CHECK(node.DefinesText());
90
91 SessionNode empty("EMPTY");
92 BOOST_CHECK(!empty.DefinesText());
93 BOOST_CHECK_EQUAL(empty.GetText(), "");
94}

References Nektar::LibUtilities::SessionNode::DefinesText(), Nektar::LibUtilities::SessionNode::GetName(), Nektar::LibUtilities::SessionNode::GetPath(), and Nektar::LibUtilities::SessionNode::GetText().

◆ BOOST_AUTO_TEST_CASE() [9/11]

Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE ( TestSessionNodePaths  )

Definition at line 169 of file TestSessionNode.cpp.

170{
172
173 BOOST_CHECK_EQUAL(forcing.GetPath(), "FORCING");
174 BOOST_CHECK_EQUAL(forcing.GetChild("FORCE").GetPath(), "FORCING/FORCE");
175 BOOST_CHECK_EQUAL(forcing.GetChild("FORCE").GetChild("COEFF").GetPath(),
176 "FORCING/FORCE/COEFF");
177
178 // Paths of an existing subtree are rewritten when it is grafted into a
179 // parent, so they always describe the tree the node now belongs to.
180 SessionNode root("NEKTAR");
181 root.AddChild(forcing);
182 BOOST_CHECK_EQUAL(
183 root.GetChild("FORCING").GetChild("FORCE").GetChild("COEFF").GetPath(),
184 "NEKTAR/FORCING/FORCE/COEFF");
185}

References Nektar::LibUtilities::SessionNode::AddChild(), BuildForcingSection(), Nektar::LibUtilities::SessionNode::GetChild(), and Nektar::LibUtilities::SessionNode::GetPath().

◆ BOOST_AUTO_TEST_CASE() [10/11]

Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE ( TestSessionNodeTimeLevel  )

Definition at line 242 of file TestSessionNode.cpp.

243{
244 // A section with no TIMELEVEL children applies at every time level.
246 BOOST_CHECK(plain.GetTimeLevel(0) == &plain);
247 BOOST_CHECK(plain.GetTimeLevel(3) == &plain);
248
249 SessionNode levelled("FORCE");
250 SessionNode zero("TIMELEVEL");
251 zero.SetAttribute("VALUE", "0");
252 zero.AddChild(SessionNode("WHITENOISE", "0.01"));
253 SessionNode one("TIMELEVEL");
254 one.SetAttribute("VALUE", "1");
255 one.AddChild(SessionNode("WHITENOISE", "0.02"));
256 levelled.AddChild(zero);
257 levelled.AddChild(one);
258
259 const SessionNode *selected = levelled.GetTimeLevel(1);
260 BOOST_REQUIRE(selected != nullptr);
261 BOOST_CHECK_EQUAL(selected->GetValue("WHITENOISE"), "0.02");
262
263 // A section which defines time levels but not this one simply does not
264 // apply here, which the caller is expected to handle.
265 BOOST_CHECK(levelled.GetTimeLevel(2, false) == nullptr);
266 BOOST_CHECK_THROW(levelled.GetTimeLevel(2, true), ErrorUtil::NekError);
267}
const SessionNode * GetTimeLevel(const size_t timeLevel, const bool enableCheck=true) const
Resolves the TIMELEVEL indirection used by parallel-in-time runs.

References Nektar::LibUtilities::SessionNode::AddChild(), BuildForcingSection(), Nektar::LibUtilities::SessionNode::GetTimeLevel(), Nektar::LibUtilities::SessionNode::GetValue(), and Nektar::LibUtilities::SessionNode::SetAttribute().

◆ BOOST_AUTO_TEST_CASE() [11/11]

Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE ( TestSessionNodeValues  )

Definition at line 153 of file TestSessionNode.cpp.

154{
155 SessionNode force = *BuildForcingSection().GetChildren("FORCE")[1];
156
157 BOOST_CHECK_EQUAL(force.GetValue("WHITENOISE"), "0.01");
158 BOOST_CHECK_CLOSE(force.GetValueAs<NekDouble>("WHITENOISE"), 0.01, 1e-12);
159 BOOST_CHECK_EQUAL(force.GetValueAs<int>("UPDATEFREQ"), 10);
160
161 // The defaulted forms are used when the entry is absent.
162 BOOST_CHECK_EQUAL(force.GetValue("NSTEPS", "none"), "none");
163 BOOST_CHECK_EQUAL(force.GetValueAs<int>("NSTEPS", 0), 0);
164
165 BOOST_CHECK_THROW(force.GetValue("NSTEPS"), ErrorUtil::NekError);
166 BOOST_CHECK_THROW(force.GetValueAs<int>("WHITENOISE"), ErrorUtil::NekError);
167}
T GetValueAs(const std::string &name) const
Returns the text of the first child called name converted to a T; a fatal error is raised if there is...

References BuildForcingSection(), Nektar::LibUtilities::SessionNode::GetChildren(), Nektar::LibUtilities::SessionNode::GetValue(), and Nektar::LibUtilities::SessionNode::GetValueAs().

◆ BuildForcingSection()

static SessionNode Nektar::SessionNodeUnitTests::BuildForcingSection ( )
static

Builds the tree that the XML in ParseXml() below describes, but by hand, so that the two construction routes can be compared.

Definition at line 50 of file TestSessionNode.cpp.

51{
52 SessionNode coeff("COEFF", "SpongeCoefficient");
53 SessionNode regions("BOUNDARYREGIONS", "0");
54
55 SessionNode absorption("FORCE");
56 absorption.SetAttribute("TYPE", "Absorption");
57 absorption.AddChild(coeff);
58 absorption.AddChild(regions);
59
60 SessionNode noise("FORCE");
61 noise.SetAttribute("TYPE", "Noise");
62 noise.AddChild(SessionNode("WHITENOISE", "0.01"));
63 noise.AddChild(SessionNode("UPDATEFREQ", "10"));
64
65 SessionNode forcing("FORCING");
66 forcing.AddChild(absorption);
67 forcing.AddChild(noise);
68
69 return forcing;
70}

References Nektar::LibUtilities::SessionNode::AddChild(), and Nektar::LibUtilities::SessionNode::SetAttribute().

Referenced by BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), and BOOST_AUTO_TEST_CASE().

◆ ParseXml()

static SessionNode Nektar::SessionNodeUnitTests::ParseXml ( const std::string &  xml,
const std::string &  name = "" 
)
static

Parses xml and converts the root element into a SessionNode.

Definition at line 73 of file TestSessionNode.cpp.

75{
76 TiXmlDocument doc;
77 doc.Parse(xml.c_str());
78 BOOST_REQUIRE_MESSAGE(!doc.Error(), doc.ErrorDesc());
79 return LibUtilities::SessionNodeFromXml(doc.RootElement(), name);
80}

References Nektar::LibUtilities::SessionNodeFromXml().

Referenced by BOOST_AUTO_TEST_CASE(), and BOOST_AUTO_TEST_CASE().