Commit b22066919e2fe03d69ea4cb4384c50e18e4d7b08
1 parent
926b69e7
Make compliant to coding standards.
Showing
2 changed files
with
87 additions
and
83 deletions
test/test_GXml.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | - * test_GXml.cpp - test GXml classes * | |
| 2 | + * test_GXml.hpp - Test xml module * | |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2010 by Jurgen Knodlseder * | |
| 4 | + * copyright (C) 2010-2012 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -20,41 +20,43 @@ |
| 20 | 20 | ***************************************************************************/ |
| 21 | 21 | /** |
| 22 | 22 | * @file test_GXml.cpp |
| 23 | - * @brief Testing of XML module. | |
| 24 | - * @author J. Knodlseder | |
| 23 | + * @brief Implementation of unit tests for XML module | |
| 24 | + * @author Juergen Knoedlseder | |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | 27 | /* __ Includes ___________________________________________________________ */ |
| 28 | 28 | #ifdef HAVE_CONFIG_H |
| 29 | 29 | #include <config.h> |
| 30 | 30 | #endif |
| 31 | -#include "GammaLib.hpp" | |
| 32 | 31 | #include "test_GXml.hpp" |
| 33 | -#include <iostream> | |
| 34 | -#include <stdexcept> | |
| 35 | -#include <stdlib.h> | |
| 36 | 32 | |
| 33 | + | |
| 34 | +/***********************************************************************//** | |
| 35 | + * @brief Set parameters and tests | |
| 36 | + **************************************************************************/ | |
| 37 | 37 | void TestGXml::set(void) |
| 38 | 38 | { |
| 39 | 39 | // Test name |
| 40 | 40 | name("GXml"); |
| 41 | 41 | |
| 42 | - // xml file | |
| 43 | - m_xml_file="data/test.xml"; | |
| 42 | + // Set XML filename | |
| 43 | + m_xml_file = "data/test.xml"; | |
| 44 | 44 | |
| 45 | - //Add Tests | |
| 46 | - add_test(static_cast<pfunction>(&TestGXml::test_GXml_attributes),"xml attributes"); | |
| 47 | - add_test(static_cast<pfunction>(&TestGXml::test_GXml_elements),"xml elements"); | |
| 48 | - add_test(static_cast<pfunction>(&TestGXml::test_GXml_construct),"xml constructors"); | |
| 49 | - add_test(static_cast<pfunction>(&TestGXml::test_GXml_load),"xml load"); | |
| 50 | - add_test(static_cast<pfunction>(&TestGXml::test_GXml_access),"xml access"); | |
| 45 | + // Append tests | |
| 46 | + append(static_cast<pfunction>(&TestGXml::test_GXml_attributes), "Test XML attributes"); | |
| 47 | + append(static_cast<pfunction>(&TestGXml::test_GXml_elements), "Test XML elements"); | |
| 48 | + append(static_cast<pfunction>(&TestGXml::test_GXml_construct),"Test XML constructors"); | |
| 49 | + append(static_cast<pfunction>(&TestGXml::test_GXml_load),"Test XML load"); | |
| 50 | + append(static_cast<pfunction>(&TestGXml::test_GXml_access), "Test XML access"); | |
| 51 | 51 | |
| 52 | + // Return | |
| 52 | 53 | return; |
| 53 | 54 | } |
| 54 | 55 | |
| 55 | -/*************************************************************************** | |
| 56 | - * Test: GXml element attributes * | |
| 57 | - ***************************************************************************/ | |
| 56 | + | |
| 57 | +/***********************************************************************//** | |
| 58 | + * @brief Test XML arrtibutes | |
| 59 | + **************************************************************************/ | |
| 58 | 60 | void TestGXml::test_GXml_attributes(void) |
| 59 | 61 | { |
| 60 | 62 | // Test valid attributes |
| ... | ... | @@ -142,16 +144,17 @@ void TestGXml::test_GXml_attributes(void) |
| 142 | 144 | test_try_failure(e); |
| 143 | 145 | } |
| 144 | 146 | |
| 145 | - | |
| 147 | + // Return | |
| 146 | 148 | return; |
| 147 | 149 | } |
| 148 | 150 | |
| 149 | 151 | |
| 150 | -/*************************************************************************** | |
| 151 | - * Test: GXml elements * | |
| 152 | - ***************************************************************************/ | |
| 152 | +/***********************************************************************//** | |
| 153 | + * @brief Test XML elements | |
| 154 | + **************************************************************************/ | |
| 153 | 155 | void TestGXml::test_GXml_elements(void) |
| 154 | 156 | { |
| 157 | + // Perform tests | |
| 155 | 158 | GXmlElement element; |
| 156 | 159 | element.attribute("test", "1.0"); |
| 157 | 160 | test_assert(element.attribute("test") == "1.0", |
| ... | ... | @@ -172,16 +175,14 @@ void TestGXml::test_GXml_elements(void) |
| 172 | 175 | "Test if element.attribute(\"test\")= 2.0 and if element.attribute(\"test2\")= 1.0", |
| 173 | 176 | "Unexpected attributes "+element.attribute("test")+" "+element.attribute("test2")); |
| 174 | 177 | |
| 175 | - | |
| 176 | - // Exit test | |
| 178 | + // Return | |
| 177 | 179 | return; |
| 178 | - | |
| 179 | 180 | } |
| 180 | 181 | |
| 181 | 182 | |
| 182 | -/*************************************************************************** | |
| 183 | - * Test: GXml constructors * | |
| 184 | - ***************************************************************************/ | |
| 183 | +/***********************************************************************//** | |
| 184 | + * @brief Test XML constructors | |
| 185 | + **************************************************************************/ | |
| 185 | 186 | void TestGXml::test_GXml_construct(void) |
| 186 | 187 | { |
| 187 | 188 | // Test void constructor |
| ... | ... | @@ -241,33 +242,27 @@ void TestGXml::test_GXml_construct(void) |
| 241 | 242 | GXmlNode* spat = src->element("spatialModel", 0); |
| 242 | 243 | spat->append(new GXmlElement("parameter free=\"0\" max=\"1000\" min=\"0.001\"" |
| 243 | 244 | " name=\"Prefactor\" scale=\"1\" value=\"1\"")); |
| 244 | - //xml.save("test2.xml"); | |
| 245 | - //std::cout << xml << std::endl; | |
| 246 | - | |
| 247 | 245 | test_try_success(); |
| 248 | 246 | } |
| 249 | 247 | catch (std::exception &e) { |
| 250 | 248 | test_try_failure(e); |
| 251 | 249 | } |
| 252 | 250 | |
| 253 | - // Exit test | |
| 251 | + // Return | |
| 254 | 252 | return; |
| 255 | - | |
| 256 | 253 | } |
| 257 | 254 | |
| 258 | 255 | |
| 259 | -/*************************************************************************** | |
| 260 | - * Test: GXml loading/saving * | |
| 261 | - ***************************************************************************/ | |
| 256 | +/***********************************************************************//** | |
| 257 | + * @brief Test XML loading/saving | |
| 258 | + **************************************************************************/ | |
| 262 | 259 | void TestGXml::test_GXml_load(void) |
| 263 | 260 | { |
| 264 | - | |
| 265 | 261 | // Test loading |
| 266 | 262 | test_try("Test loading"); |
| 267 | 263 | try { |
| 268 | 264 | GXml xml; |
| 269 | 265 | xml.load(m_xml_file); |
| 270 | - | |
| 271 | 266 | test_try_success(); |
| 272 | 267 | } |
| 273 | 268 | catch (std::exception &e) { |
| ... | ... | @@ -281,7 +276,6 @@ void TestGXml::test_GXml_load(void) |
| 281 | 276 | xml.load(m_xml_file); |
| 282 | 277 | xml.save("test.xml"); |
| 283 | 278 | xml.load("test.xml"); |
| 284 | - | |
| 285 | 279 | test_try_success(); |
| 286 | 280 | } |
| 287 | 281 | catch (std::exception &e) { |
| ... | ... | @@ -293,80 +287,80 @@ void TestGXml::test_GXml_load(void) |
| 293 | 287 | try { |
| 294 | 288 | GXml xml; |
| 295 | 289 | xml.load("test.xml"); |
| 296 | - | |
| 297 | 290 | test_try_success(); |
| 298 | 291 | } |
| 299 | 292 | catch (std::exception &e) { |
| 300 | 293 | test_try_failure(e); |
| 301 | 294 | } |
| 302 | 295 | |
| 303 | - // Exit test | |
| 296 | + // Return | |
| 304 | 297 | return; |
| 305 | - | |
| 306 | 298 | } |
| 307 | 299 | |
| 308 | 300 | |
| 309 | -/*************************************************************************** | |
| 310 | - * Test: GXml element access * | |
| 311 | - ***************************************************************************/ | |
| 301 | +/***********************************************************************//** | |
| 302 | + * @brief Test XML element access | |
| 303 | + **************************************************************************/ | |
| 312 | 304 | void TestGXml::test_GXml_access(void) |
| 313 | 305 | { |
| 314 | 306 | // Test root document access |
| 315 | 307 | GXml xml; |
| 316 | 308 | xml.load(m_xml_file); |
| 317 | - | |
| 318 | 309 | test_assert(xml.children() == 3, |
| 319 | 310 | "Test if xml.children()==3", |
| 320 | 311 | "Unexpected number of children in document "+str(xml.children())); |
| 321 | 312 | |
| 322 | - for (int i = 0; i < xml.children(); ++i) | |
| 313 | + // Test node access | |
| 314 | + for (int i = 0; i < xml.children(); ++i) { | |
| 323 | 315 | GXmlNode* ptr = xml.child(i); |
| 324 | - | |
| 316 | + } | |
| 325 | 317 | test_assert(xml.elements() == 1, |
| 326 | 318 | "Test if xml.elements()==1", |
| 327 | 319 | "Unexpected number of child elements in document "+str(xml.elements())); |
| 328 | 320 | |
| 329 | - for (int i = 0; i < xml.elements(); ++i) | |
| 321 | + // Test node access | |
| 322 | + for (int i = 0; i < xml.elements(); ++i) { | |
| 330 | 323 | GXmlNode* ptr = xml.element(i); |
| 331 | - | |
| 324 | + } | |
| 332 | 325 | test_assert( xml.elements("source_library") == 1, |
| 333 | 326 | "Test if the source_library = 1", |
| 334 | 327 | "Unexpected number of child elements in document "+str(xml.elements("source_library"))); |
| 335 | 328 | |
| 329 | + // Test element access | |
| 336 | 330 | for (int i = 0; i < xml.elements("source_library"); ++i) { |
| 337 | 331 | GXmlElement* ptr = xml.element("source_library", i); |
| 338 | 332 | test_assert(ptr->name() == "source_library","Test name","Unexpected element name "+ptr->name()); |
| 339 | 333 | } |
| 340 | - //std::cout << xml.elements("source_library") << std::endl; | |
| 341 | 334 | |
| 342 | 335 | |
| 343 | - // Exit test | |
| 336 | + // Return | |
| 344 | 337 | return; |
| 345 | - | |
| 346 | 338 | } |
| 347 | 339 | |
| 348 | 340 | |
| 349 | 341 | /*************************************************************************** |
| 350 | - * Main test function * | |
| 342 | + * @brief Main entry point for test executable | |
| 351 | 343 | ***************************************************************************/ |
| 352 | 344 | int main(void) |
| 353 | 345 | { |
| 354 | - GTestSuites testsuites("GXml"); | |
| 346 | + // Allocate test suit container | |
| 347 | + GTestSuites testsuites("XML module"); | |
| 355 | 348 | |
| 356 | - bool was_successful=true; | |
| 349 | + // Initially assume that we pass all tests | |
| 350 | + bool success = true; | |
| 357 | 351 | |
| 358 | - //Create a test suite | |
| 352 | + // Create a test suite | |
| 359 | 353 | TestGXml test; |
| 360 | 354 | |
| 361 | - //Append to the container | |
| 355 | + // Append test suite to the container | |
| 362 | 356 | testsuites.append(test); |
| 363 | 357 | |
| 364 | - //Run | |
| 365 | - was_successful=testsuites.run(); | |
| 358 | + // Run the testsuites | |
| 359 | + success = testsuites.run(); | |
| 366 | 360 | |
| 367 | - //save xml report | |
| 361 | + // Save test report | |
| 368 | 362 | testsuites.save("reports/GXml.xml"); |
| 369 | 363 | |
| 370 | - // Return | |
| 371 | - return was_successful ? 0:1; | |
| 364 | + // Return success status | |
| 365 | + return (success ? 0 : 1); | |
| 372 | 366 | } | ... | ... |
test/test_GXml.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | - * test_GXml.hpp - test xml class * | |
| 2 | + * test_GXml.hpp - Test xml module * | |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | 4 | * copyright (C) 2012 by Jean-Baptiste Cayrou * |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| ... | ... | @@ -18,32 +18,42 @@ |
| 18 | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 19 | 19 | * * |
| 20 | 20 | ***************************************************************************/ |
| 21 | +/** | |
| 22 | + * @file test_GXml.hpp | |
| 23 | + * @brief Definition of unit tests for XML module | |
| 24 | + * @author Jean-Baptiste Cayrou | |
| 25 | + */ | |
| 21 | 26 | |
| 22 | 27 | #ifndef TEST_GXML_HPP |
| 23 | 28 | #define TEST_GXML_HPP |
| 24 | 29 | |
| 25 | 30 | /* __ Includes ___________________________________________________________ */ |
| 26 | 31 | #include "GammaLib.hpp" |
| 27 | -#include <iostream> // cout, cerr | |
| 28 | - | |
| 29 | -class TestGXml : public GTestSuite | |
| 30 | -{ | |
| 31 | - public: | |
| 32 | - // Constructors and destructors | |
| 33 | - TestGXml(void) : GTestSuite(){ return; } | |
| 34 | - virtual ~TestGXml(void){ return; } | |
| 35 | - | |
| 36 | - // Methods | |
| 37 | - virtual void set(void); | |
| 38 | - void test_GXml_attributes(void); | |
| 39 | - void test_GXml_elements(); | |
| 40 | - void test_GXml_construct(); | |
| 41 | - void test_GXml_load(); | |
| 42 | - void test_GXml_access(); | |
| 43 | 32 | |
| 33 | + | |
| 34 | +/***********************************************************************//** | |
| 35 | + * @class TestGXml | |
| 36 | + * | |
| 37 | + * @brief Test suite for XML module | |
| 38 | + ***************************************************************************/ | |
| 39 | +class TestGXml : public GTestSuite { | |
| 40 | + | |
| 41 | +public: | |
| 42 | + // Constructors and destructors | |
| 43 | + TestGXml(void) : GTestSuite() {} | |
| 44 | + virtual ~TestGXml(void) {} | |
| 45 | + | |
| 46 | + // Methods | |
| 47 | + virtual void set(void); | |
| 48 | + void test_GXml_attributes(void); | |
| 49 | + void test_GXml_elements(void); | |
| 50 | + void test_GXml_construct(void); | |
| 51 | + void test_GXml_load(void); | |
| 52 | + void test_GXml_access(void); | |
| 53 | + | |
| 54 | +private: | |
| 44 | 55 | // Private members |
| 45 | - private: | |
| 46 | - std::string m_xml_file; | |
| 56 | + std::string m_xml_file; //!< XML filename | |
| 47 | 57 | }; |
| 48 | 58 | |
| 49 | 59 | #endif /* TEST_GXML_HPP */ | ... | ... |