test_GXml.cpp
13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/***************************************************************************
* test_GXml.cpp - Test xml module *
* ----------------------------------------------------------------------- *
* copyright (C) 2010-2014 by Juergen Knoedlseder *
* ----------------------------------------------------------------------- *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
***************************************************************************/
/**
* @file test_GXml.cpp
* @brief Implementation of unit tests for XML module
* @author Juergen Knoedlseder
*/
/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "test_GXml.hpp"
#include "GTools.hpp"
/***********************************************************************//**
* @brief Set parameters and tests
**************************************************************************/
void TestGXml::set(void)
{
// Test name
name("GXml");
// Set XML filename
m_xml_file = "data/test.xml";
// Append tests
append(static_cast<pfunction>(&TestGXml::test_GXml_attributes), "Test XML attributes");
append(static_cast<pfunction>(&TestGXml::test_GXml_elements), "Test XML elements");
append(static_cast<pfunction>(&TestGXml::test_GXml_construct),"Test XML constructors");
append(static_cast<pfunction>(&TestGXml::test_GXml_load),"Test XML load");
append(static_cast<pfunction>(&TestGXml::test_GXml_access), "Test XML access");
// Return
return;
}
/***********************************************************************//**
* @brief Clone test suite
*
* @return Pointer to deep copy of test suite.
***************************************************************************/
TestGXml* TestGXml::clone(void) const
{
// Clone test suite
return new TestGXml(*this);
}
/***********************************************************************//**
* @brief Test XML arrtibutes
**************************************************************************/
void TestGXml::test_GXml_attributes(void)
{
// Test valid attributes
test_try("Test valid attributes");
try {
GXmlAttribute attr("test", "1.0");
test_assert(attr.value() == "1.0","Test if value()= 1.0","Unexpected attribute "+attr.value());
attr.value("\"1.0\"");
test_assert(attr.value() == "1.0","Test if value()= 1.0","Unexpected attribute "+attr.value());
attr.value("'1.0'");
test_assert(attr.value() == "1.0","Test if value()= 1.0","Unexpected attribute "+attr.value());
attr.value("''1.0'");
test_assert(attr.value() == "''1.0'",
"Test if value()= ''1.0'",
"Unexpected attribute "+attr.value());
attr.value("'1.0");
test_assert(attr.value() == "'1.0",
"Test if value()= '1.0",
"Unexpected attribute "+attr.value());
attr.value("1.0'");
test_assert(attr.value() == "1.0'",
"Test if value()= 1.0'",
"Unexpected attribute "+attr.value());
attr.value("\"1.0");
test_assert(attr.value() == "\"1.0",
"Test if value()= \"1.0",
"Unexpected attribute "+attr.value());
attr.value("1.0\"");
test_assert(attr.value() == "1.0\"",
"Test if value()= 1.0\"",
"Unexpected attribute "+attr.value());
attr.value("\"\"1.0\"");
test_assert(attr.value() == "\"\"1.0\"",
"Test if value()= \"\"1.0\"",
"Unexpected attribute "+attr.value());
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test invalid attributes
test_try("Test invalid attributes 1");
try {
GXmlAttribute attr("test", "\"\"1.0'\"");
test_try_failure();
}
catch (GException::xml_attribute_value &e) {
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
test_try("Test invalid attributes 2");
try {
GXmlAttribute attr("test", "''1.0\"");
test_try_failure();
}
catch (GException::xml_attribute_value &e) {
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
test_try("Test invalid attributes 3");
try {
GXmlAttribute attr("test", "\"1.0'");
test_try_failure();
}
catch (GException::xml_attribute_value &e) {
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Return
return;
}
/***********************************************************************//**
* @brief Test XML elements
**************************************************************************/
void TestGXml::test_GXml_elements(void)
{
// Perform tests
GXmlElement element;
element.attribute("test", "1.0");
test_assert(element.attribute("test") == "1.0",
"Test if element.attribute(\"test\")= 1.0",
"Unexpected attribute "+element.attribute("test"));
element.attribute("test", "2.0");
test_assert(element.attribute("test") == "2.0",
"Test if element.attribute(\"test\")= 2.0",
"Unexpected attribute "+element.attribute("test"));
element.attribute("test2", "1.0");
test_assert(element.attribute("test2") == "1.0",
"Test if element.attribute(\"test2\")= 1.0",
"Unexpected attribute "+element.attribute("test2"));
test_assert(element.attribute("test") == "2.0"&& element.attribute("test2") == "1.0",
"Test if element.attribute(\"test\")= 2.0 and if element.attribute(\"test2\")= 1.0",
"Unexpected attributes "+element.attribute("test")+" "+element.attribute("test2"));
// Return
return;
}
/***********************************************************************//**
* @brief Test XML constructors
**************************************************************************/
void TestGXml::test_GXml_construct(void)
{
// Test void constructor
test_try("Test void constructor");
try {
GXml xml;
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test load constructor
test_try("Test load constructor");
try {
GXml xml(m_xml_file);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test copy constructor
test_try("Test copy constructor");
try {
GXml xml1(m_xml_file);
GXml xml2 = xml1;
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test XML file creation
test_try("Test XML file creation");
try {
GXml xml;
xml.append(GXmlComment("This is a comment."));
xml.append(GXmlElement("source_library"));
GXmlElement* lib = xml.element("source_library", 0);
lib->append(GXmlElement("source name=\"LMC\" type=\"DiffuseSource\""));
GXmlNode* src = lib->element("source", 0);
src->append(GXmlElement("spectrum type=\"PLSuperExpCutoff\""));
GXmlNode* spec = src->element("spectrum", 0);
spec->append(GXmlElement("parameter free=\"1\" max=\"1000\" min=\"1e-07\""
" name=\"Prefactor\" scale=\"1e-07\""
" value=\"0.02754520844\""));
spec->append(GXmlElement("parameter free=\"1\" max=\"5\" min=\"-5\""
" name=\"Index1\" scale=\"1\" value=\"-2.0458781\""));
GXmlElement* par = spec->element("parameter", 0);
par->attribute("value", "1.01");
par->attribute("error", "3.145");
par = spec->element("parameter", 1);
par->attribute("value", "-2.100");
par->attribute("error", "9.876");
src->append(GXmlElement("spatialModel file=\"LMC.fits\" type=\"SpatialMap\""));
GXmlNode* spat = src->element("spatialModel", 0);
spat->append(GXmlElement("parameter free=\"0\" max=\"1000\" min=\"0.001\""
" name=\"Prefactor\" scale=\"1\" value=\"1\""));
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Return
return;
}
/***********************************************************************//**
* @brief Test XML loading/saving
**************************************************************************/
void TestGXml::test_GXml_load(void)
{
// Test loading
test_try("Test loading");
try {
GXml xml;
xml.load(m_xml_file);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test saving
test_try("Test saving");
try {
GXml xml;
xml.load(m_xml_file);
xml.save("test.xml");
xml.load("test.xml");
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test loading of saved XML document
test_try("Test loading of saved XML document");
try {
GXml xml;
xml.load("test.xml");
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Return
return;
}
/***********************************************************************//**
* @brief Test XML element access
**************************************************************************/
void TestGXml::test_GXml_access(void)
{
// Test root document access
GXml xml;
xml.load(m_xml_file);
test_assert(xml.size() == 3,
"Test if xml.children()==3",
"Unexpected number of children in document "+
gammalib::str(xml.size()));
// Test node access
for (int i = 0; i < xml.size(); ++i) {
GXmlNode* ptr = xml[i];
test_assert(ptr != 0, "Test node access");
}
test_assert(xml.elements() == 1,
"Test if xml.elements()==1",
"Unexpected number of child elements in document "+
gammalib::str(xml.elements()));
// Test node access
for (int i = 0; i < xml.elements(); ++i) {
GXmlNode* ptr = xml.element(i);
test_assert(ptr != 0, "Test xml element access");
}
test_assert(xml.elements("source_library") == 1,
"Test if the source_library = 1",
"Unexpected number of child elements in document "+
gammalib::str(xml.elements("source_library")));
// Test element access
for (int i = 0; i < xml.elements("source_library"); ++i) {
GXmlElement* ptr = xml.element("source_library", i);
test_assert(ptr->name() == "source_library",
"Test name",
"Unexpected element name "+ptr->name());
}
// Test hierarchy access
GXmlElement* ptr = NULL;
ptr = xml.element("source_library");
test_assert(ptr->name() == "source_library", "Test hierarchy access",
"Unexpected element name "+ptr->name());
ptr = xml.element("source_library > source");
test_assert(ptr->name() == "source", "Test hierarchy access",
"Unexpected element name "+ptr->name());
ptr = xml.element("source_library > source > spectrum");
test_assert(ptr->name() == "spectrum", "Test hierarchy access",
"Unexpected element name "+ptr->name());
ptr = xml.element("source_library > source > spectrum > parameter[2]");
test_assert(ptr->name() == "parameter", "Test hierarchy access",
"Unexpected element name "+ptr->name());
test_assert(ptr->attribute("name") == "Scale", "Test hierarchy access",
"Unexpected element attribute "+ptr->attribute("name"));
// Return
return;
}
/***************************************************************************
* @brief Main entry point for test executable
***************************************************************************/
int main(void)
{
// Allocate test suit container
GTestSuites testsuites("XML module");
// Initially assume that we pass all tests
bool success = true;
// Create a test suite
TestGXml test;
// Append test suite to the container
testsuites.append(test);
// Run the testsuites
success = testsuites.run();
// Save test report
testsuites.save("reports/GXml.xml");
// Return success status
return (success ? 0 : 1);
}