From bd79c7e15cfb25a9245bc39494cb5ed646748314 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Thu, 23 Jun 2016 22:49:32 +0200 Subject: [PATCH] Add testing of example C++ executables to unit test (#1806) --- .gitignore | 3 +-- ChangeLog | 3 ++- NEWS | 2 +- README.md | 2 +- configure.ac | 2 ++ examples/cpp/Makefile.am | 5 +++++ examples/cpp/optimize.cpp | 15 +++++++-------- examples/cpp/readmodel.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- examples/cpp/source.xml | 14 -------------- test/Makefile.am | 2 +- test/test_examples.py | 210 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_examples.sh.in | 25 +++++++++++++++++++++++++ 12 files changed, 312 insertions(+), 32 deletions(-) delete mode 100644 examples/cpp/source.xml create mode 100755 test/test_examples.py create mode 100755 test/test_examples.sh.in diff --git a/.gitignore b/.gitignore index bb47dfe..f468a5a 100644 --- a/.gitignore +++ b/.gitignore @@ -86,15 +86,14 @@ test/reports/*.xml test/*.trs test/region.reg test/test_python.sh +test/test_examples.sh test/test_csv*.dat examples/python/*.xml examples/python/*.html -examples/cpp/*.fits examples/cpp/createfits examples/cpp/createskymap examples/cpp/createxml -examples/cpp/my_xml_file.xml examples/cpp/readmodel examples/cpp/numerics examples/cpp/optimize diff --git a/ChangeLog b/ChangeLog index 0409ba0..b831fc1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,9 @@ -2016-06-22 Juergen Knoedlseder +2016-06-23 Juergen Knoedlseder * Version 1.1.0 released ========================= + Correct Makefile for example executables (#1806) Handle additional columns in GArf class GCTAAeff2D no longer extrapolates beyond response table (#1784) Add GWcsSIN projection class (#1798) diff --git a/NEWS b/NEWS index a463462..636deba 100644 --- a/NEWS +++ b/NEWS @@ -2,7 +2,7 @@ New Features and Important Changes in GammaLib 1.1.0 Author(s) : Juergen Knoedlseder -22 June 2016 +23 June 2016 1. Introduction diff --git a/README.md b/README.md index 01f3b7e..36f4a5e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ GammaLib information ==================== -* Version: 1.1.0.dev1 (22 June 2016) +* Version: 1.1.0.dev1 (23 June 2016) * Author: Juergen Knoedlseder (jurgen.knodlseder@irap.omp.eu) [![Build Status](https://cta-jenkins.irap.omp.eu/buildStatus/icon?job=gammalib-integrate-os)](https://cta-jenkins.irap.omp.eu/job/gammalib-integrate-os/) diff --git a/configure.ac b/configure.ac index a5e6b66..cc29503 100644 --- a/configure.ac +++ b/configure.ac @@ -1396,9 +1396,11 @@ AC_CONFIG_FILES([Makefile pyext/setup.py examples/Makefile examples/cpp/Makefile + test/test_examples.sh gammalib.pc testconf.sh], [if test -f test/test_python.sh; then chmod +x test/test_python.sh; fi; + if test -f test/test_examples.sh; then chmod +x test/test_examples.sh; fi; if test -f testconf.sh; then chmod +x testconf.sh; fi]) if test "x$with_inst" = "xyes"; then diff --git a/examples/cpp/Makefile.am b/examples/cpp/Makefile.am index 16180b5..2b9c34f 100644 --- a/examples/cpp/Makefile.am +++ b/examples/cpp/Makefile.am @@ -1,5 +1,10 @@ ## Process this file with automake to produce Makefile.in +# Darwin support +MACOSX_DEPLOYMENT_TARGET=@CONFIGURE_MACOSX_DEPLOYMENT_TARGET@ +@EXPORT_MACOSX_DEPLOYMENT_TARGET@export MACOSX_DEPLOYMENT_TARGET + +# Include files INCLUDES = -I$(top_srcdir)/include # Program name diff --git a/examples/cpp/optimize.cpp b/examples/cpp/optimize.cpp index 7a432a9..bc64843 100644 --- a/examples/cpp/optimize.cpp +++ b/examples/cpp/optimize.cpp @@ -47,13 +47,13 @@ protected: }; void parabola::eval(const GOptimizerPars& pars) { - const double a = 2.0; - const double b = -4.0; - const double c = 2.0; - double x = pars[0]->value(); - m_value = a*x*x + b*x + c; - m_gradient[0] = 2.0*a*x + b; - m_curvature(0,0) = m_gradient[0] * m_gradient[0]; + const double a = 2.0; + const double b = -4.0; + const double c = 2.0; + double x = pars[0]->value(); + m_value = a*x*x + b*x + c; + m_gradient[0] = 2.0*a*x + b; + m_curvature(0,0) = m_gradient[0] * m_gradient[0]; }; @@ -69,7 +69,6 @@ int main(void) { log.cout(true); // Allocate optimizer - //GOptimizerLM opt(log); GOptimizerLM opt; // Allocate function diff --git a/examples/cpp/readmodel.cpp b/examples/cpp/readmodel.cpp index 98d6fbe..732e821 100644 --- a/examples/cpp/readmodel.cpp +++ b/examples/cpp/readmodel.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * readmodel.cpp - Illustrates how to read a model * * ----------------------------------------------------------------------- * - * copyright (C) 2013 by Juergen Knoedlseder * + * copyright (C) 2013-2016 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -26,15 +26,68 @@ /* __ Includes ___________________________________________________________ */ #include +#include #include "GammaLib.hpp" /***********************************************************************//** - * @brief Dummy read model test + * @brief Create XML file + * + * Creates XML file that should be read back to avoid shipping of a test + * XML file. ***************************************************************************/ -int main(void) { - GModels models("source.xml"); +void create_xml_file(void) +{ + // Open ASCII file + std::ofstream file("example_source.xml"); + + // Write file if file is open + if (file.is_open()) { + + // Write XML file + file << "\n"; + file << "\n"; + file << " \n"; + file << " \n"; + file << " \n"; + file << " \n"; + file << " \n"; + file << " \n"; + file << " \n"; + file << " \n"; + file << " \n"; + file << " \n"; + file << " \n"; + file << "\n"; + + // Close file + file.close(); + + } // endif: file was open + + // Return + return; +} + + +/***********************************************************************//** + * @brief Read XML model + ***************************************************************************/ +int main(void) +{ + // Create XML file + create_xml_file(); + + // Read XML model + GModels models("example_source.xml"); std::cout << models << std::endl; + + // Return return 0; } diff --git a/examples/cpp/source.xml b/examples/cpp/source.xml deleted file mode 100644 index 1b2e7a3..0000000 --- a/examples/cpp/source.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - diff --git a/test/Makefile.am b/test/Makefile.am index 55fc549..faafb3b 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -44,7 +44,7 @@ endif # Optional python bindings if WITH_PYTHON - TEST_PYTHON_SCRIPT = test_python.sh + TEST_PYTHON_SCRIPT = test_python.sh test_examples.sh TEST_PYTHON_ENV = PYTHONPATH=$(top_builddir)/pyext:$(top_builddir)/pyext/build/gammalib$(TEST_MWL)$(TEST_CTA)$(TEST_LAT)$(TEST_COM):$(PYTHONPATH) endif diff --git a/test/test_examples.py b/test/test_examples.py new file mode 100755 index 0000000..3e95d22 --- /dev/null +++ b/test/test_examples.py @@ -0,0 +1,210 @@ +#! /usr/bin/env python +# ========================================================================== +# This scripts performs unit tests for the example executables +# +# Copyright (C) 2016 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 . +# +# ========================================================================== +import gammalib +import sys +import os + + +# =============================================== # +# Test class for example executables unit testing # +# =============================================== # +class Test(gammalib.GPythonTestSuite): + """ + Test class for GammaLib example executables + """ + # Constructor + def __init__(self): + """ + Constructor. + """ + # Call base class constructor + gammalib.GPythonTestSuite.__init__(self) + + # Examples directory + self._dir = '../examples/cpp/' + + # Return + return + + # Execute binary + def _execute_binary(self, name): + """ + Execute binary + + Parameters + ---------- + name : str + Binary executable name. + """ + # Setup command + cmd = self._dir + name + + # Execute binary, make sure we catch any exception + try: + rc = os.system(cmd+' >/dev/null 2>&1') + except: + pass + + # Check if execution was successful + self.test_assert(rc == 0, 'Check "'+name+'" execution on command line') + + # Return + return + + # Set test functions + def set(self): + """ + Set all test functions + """ + # Set test name + self.name("examples") + + # Append tests + self.append(self.test_createfits, "Test createfits") + self.append(self.test_createskymap, "Test createskymap") + self.append(self.test_createxml, "Test createxml") + self.append(self.test_interpolate, "Test interpolate") + self.append(self.test_numerics, "Test numerics") + self.append(self.test_optimize, "Test optimize") + self.append(self.test_readmodel, "Test readmodel") + + # Return + return + + # Test createfits + def test_createfits(self): + """ + Test createfits + """ + # Execute binary + self._execute_binary('createfits') + + #TODO: Do any testing + + # Return + return + + # Test createskymap + def test_createskymap(self): + """ + Test createskymap + """ + # Execute binary + self._execute_binary('createskymap') + + #TODO: Do any testing + + # Return + return + + # Test createxml + def test_createxml(self): + """ + Test createxml + """ + # Execute binary + self._execute_binary('createxml') + + #TODO: Do any testing + + # Return + return + + # Test interpolate + def test_interpolate(self): + """ + Test interpolate + """ + # Execute binary + self._execute_binary('interpolate') + + #TODO: Do any testing + + # Return + return + + # Test numerics + def test_numerics(self): + """ + Test numerics + """ + # Execute binary + self._execute_binary('numerics') + + #TODO: Do any testing + + # Return + return + + # Test optimize + def test_optimize(self): + """ + Test optimize + """ + # Execute binary + self._execute_binary('optimize') + + #TODO: Do any testing + + # Return + return + + # Test readmodel + def test_readmodel(self): + """ + Test readmodel + """ + # Execute binary + self._execute_binary('readmodel') + + #TODO: Do any testing + + # Return + return + + +# ======================== # +# Main routine entry point # +# ======================== # +if __name__ == '__main__': + + # Allocate test suites + suites = gammalib.GTestSuites('Examples testing') + + # Allocate test suite, setup tests and append them to the container + suite = Test() + suite.set() + suites.append(suite) + + # Run test suite + success = suites.run() + + # Save test results + suites.save('reports/examples.xml') + + # Set return code + if success: + rc = 0 + else: + rc = 1 + + # Exit with return code + sys.exit(rc) diff --git a/test/test_examples.sh.in b/test/test_examples.sh.in new file mode 100755 index 0000000..fc5d689 --- /dev/null +++ b/test/test_examples.sh.in @@ -0,0 +1,25 @@ +#! /bin/sh +# ========================================================================== +# This script launches the examples unit tests +# +# Copyright (C) 2016 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 . +# +# ========================================================================== + +# +# Run the examples test +# ===================== +@PYTHON_COVERAGE@./test_examples.py -- libgit2 0.21.4