diff --git a/ChangeLog b/ChangeLog index 62e95d7..682d69a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,9 @@ -2016-08-06 +2016-08-09 * Version 1.2.0 released ========================= + Add GApplication access by index operator (#1838) Add GApplication::pars() and GApplicationPar::current_value() methods (#1838) Add GLog is_open(), is_empty() and written_size() methods (#1837) Fix invalid pointer problem in GLog copies (#1823) diff --git a/NEWS b/NEWS index d1ff608..93f024a 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,6 @@ New Features and Important Changes in GammaLib 1.2.0 -6 August 2016 +9 August 2016 1. Introduction @@ -27,6 +27,7 @@ The following methods have been added: - GApplication::log_header1() - GApplication::log_header2() - GApplication::log_header3() +- GApplication::operator[](const int& index) - GApplication::pars() - GApplicationPar::current_value() - GLog::is_open() @@ -90,6 +91,9 @@ application parameters. The GApplicationPar::current_value() method has been added to retrieve the current parameter value without querying for the parameter (#1838). +Operators have been added to GApplication that allow the access of application +parameters by index (#1838). + 5. Base module -------------- diff --git a/README.md b/README.md index ce60b07..2006261 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ GammaLib information ==================== -* Version: 1.2.0.dev1 (6 August 2016) +* Version: 1.2.0.dev1 (9 August 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/include/GApplication.hpp b/include/GApplication.hpp index a0634a7..1e04ea8 100644 --- a/include/GApplication.hpp +++ b/include/GApplication.hpp @@ -149,6 +149,9 @@ std::string GApplication::classname(void) const * @brief Parameter access operator * * @param[in] name Parameter name. + * @return Reference to application parameter. + * + * Returns a reference to the application parameter with the given @p name. ***************************************************************************/ inline GApplicationPar& GApplication::operator[](const std::string& name) @@ -161,6 +164,10 @@ GApplicationPar& GApplication::operator[](const std::string& name) * @brief Parameter access operator (const version) * * @param[in] name Parameter name. + * @return Constant reference to application parameter + * + * Returns a const reference to the application parameter with the given + * @p name. ***************************************************************************/ inline const GApplicationPar& GApplication::operator[](const std::string& name) const @@ -170,6 +177,38 @@ const GApplicationPar& GApplication::operator[](const std::string& name) const /***********************************************************************//** + * @brief Parameter access operator + * + * @param[in] index Parameter index [0,...,pars().size()-1]. + * @return Reference to application parameter + * + * Returns a reference to the application parameter with the given @p index. + * No range checking is performed for the index. + ***************************************************************************/ +inline +GApplicationPar& GApplication::operator[](const int& index) +{ + return (m_pars[index]); +} + + +/***********************************************************************//** + * @brief Parameter access operator (const version) + * + * @param[in] index Parameter index [0,...,pars().size()-1]. + * @return Constant reference to application parameter + * + * Returns a const reference to the application parameter with the given + * @p index. No range checking is performed for the index. + ***************************************************************************/ +inline +const GApplicationPar& GApplication::operator[](const int& index) const +{ + return (m_pars[index]); +} + + +/***********************************************************************//** * @brief Return application name * * @return Application name. @@ -272,28 +311,4 @@ void GApplication::pars(const GApplicationPars& pars) return; } - -/***********************************************************************//** - * @brief Parameter access operator - * - * @param[in] name Parameter name. - ***************************************************************************/ -inline -GApplicationPar& GApplication::operator[](const int& index) -{ - return (m_pars[index]); -} - - -/***********************************************************************//** - * @brief Parameter access operator (const version) - * - * @param[in] name Parameter name. - ***************************************************************************/ -inline -const GApplicationPar& GApplication::operator[](const int& index) const -{ - return (m_pars[index]); -} - #endif /* GAPPLICATION_HPP */ diff --git a/pyext/GApplication.i b/pyext/GApplication.i index 6e6170e..ac8bf05 100644 --- a/pyext/GApplication.i +++ b/pyext/GApplication.i @@ -222,7 +222,8 @@ GApplication._log_value = _log_value return (*self)[index]; } else { - throw GException::out_of_range("__getitem__(int)", "Application parameter index", + throw GException::out_of_range("__getitem__(int)", "Application " + "parameter index", index, self->pars().size()); } } @@ -232,7 +233,8 @@ GApplication._log_value = _log_value return; } else { - throw GException::out_of_range("__setitem__(int)", "Application parameter index", + throw GException::out_of_range("__setitem__(int)", "Application " + "parameter index", index, self->pars().size()); } } diff --git a/pyext/GModelSky.i b/pyext/GModelSky.i index 261359e..9ec6947 100644 --- a/pyext/GModelSky.i +++ b/pyext/GModelSky.i @@ -91,4 +91,7 @@ public: * @brief GModelSky class extension ***************************************************************************/ %extend GModelSky { + GModelSky copy() { + return (*self); + } }; diff --git a/pyext/Makefile.am b/pyext/Makefile.am index f1e3d92..f19c22f 100755 --- a/pyext/Makefile.am +++ b/pyext/Makefile.am @@ -116,7 +116,9 @@ testscripts = gammalib/tests/__init__.py \ # Test data to be distributed in $(pyexecdir)/gammalib/tests/data testdata = $(top_srcdir)/test/data/initial_coords.txt \ $(top_srcdir)/test/data/fk5_to_galactic.txt \ - $(top_srcdir)/test/data/file.fits.gz + $(top_srcdir)/test/data/file.fits.gz \ + $(top_srcdir)/test/data/test_GApplication.par \ + $(top_srcdir)/test/data/model_point_plaw.xml # Distribute scripts testscriptsdir = $(pyexecdir)/gammalib/tests diff --git a/test/test_GApplication.py b/test/test_GApplication.py index c63a55f..40faf32 100755 --- a/test/test_GApplication.py +++ b/test/test_GApplication.py @@ -85,15 +85,15 @@ class Test(gammalib.GPythonTestSuite): # Append tests self.append(self.test_log, 'Test GLog') + self.append(self.test_app, 'Test GApplication') self.append(self.test_pars, 'Test GApplicationPars') - self.append(self.test_par_iteration, 'Test GApplication parameter iteration') # Return return def test_log(self): """ - Test GLog + Test GLog class """ # Allocate logger log = gammalib.GLog(self._logfile, True) @@ -192,47 +192,51 @@ class Test(gammalib.GPythonTestSuite): # Return return - def test_pars(self): + def test_app(self): """ - Test GApplicationPars + Test GApplication class """ - # Test GApplicationPars constructor with bad filename. - self.test_try('Test GApplicationPars constructor with bad filename') - try: - pars = gammalib.GApplicationPars('testme.par') - self.test_try_failure('Exception not thrown') - except: - self.test_try_success() - else: - self.test_try_failure('This should never happen') - - def test_par_iteration(self): - - - # Set PFILES environment temporary + # Set PFILES environment variable os.environ['PFILES'] = os.environ['PWD']+'/data' - # Test GApplicationPars from par file - app = gammalib.GApplication('test_GApplication','1.1.0') + # Allocate test application + app = gammalib.GApplication('test_GApplication', '1.1.0') - # Test for proper reading of par file - self.test_assert(app._version() == '1.1.0', 'Test for proper application version') - self.test_assert(app.pars().size() == 5, 'Test number of parameters') + # Check proper allocation of test application + self.test_value(app._name(), 'test_GApplication', 'Check application name') + self.test_value(app._version(), '1.1.0', 'Check application version') + self.test_value(app.pars().size(), 5, 'Check number of parameters') - # Loop over parameters + # Check that we properly can loop over all parameters. The npars + # variable counts the number of iterations. If the parameter "chatter" + # is encounter then set the chattiness to 4 npars = 0 for par in app: - - # Increment counter npars += 1 - - # Set chatter parameter to test persistence if par.name() == 'chatter': par.integer(4) - # Test outcome of iteration - self.test_assert(npars == 5, 'Test number of parameters in iterative loop') - self.test_assert(app['chatter'].integer() == 4, 'Test if parameter could be stored correctly') - + # Check outcome of loop + self.test_value(npars, 5, 'Check number of parameters in loop') + self.test_value(app['chatter'].integer(), 4, 'Check that "chatter" ' + 'parameter could be stored correctly') + + # Return + return + + def test_pars(self): + """ + Test GApplicationPars class + """ + # Test GApplicationPars constructor with bad filename + self.test_try('Test GApplicationPars constructor with bad filename') + try: + pars = gammalib.GApplicationPars('testme.par') + self.test_try_failure('Exception not thrown') + except: + self.test_try_success() + else: + self.test_try_failure('This should never happen') + # Return return diff --git a/test/test_GModel.py b/test/test_GModel.py index 2cc75c8..40b43ed 100755 --- a/test/test_GModel.py +++ b/test/test_GModel.py @@ -1,7 +1,7 @@ # ========================================================================== -# This module performs unit tests for the GammaLib model module. +# This module performs unit tests for the GammaLib model module # -# Copyright (C) 2012-2015 Juergen Knoedlseder +# Copyright (C) 2012-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 @@ -25,12 +25,12 @@ import gammalib # ==================================== # class Test(gammalib.GPythonTestSuite): """ - Test class for GammaLib model module. + Test class for GammaLib model module """ # Constructor def __init__(self): """ - Constructor. + Constructor """ # Call base class constructor gammalib.GPythonTestSuite.__init__(self) @@ -41,42 +41,46 @@ class Test(gammalib.GPythonTestSuite): # Set test functions def set(self): """ - Set all test functions. + Set all test functions """ # Set test name - self.name("model") + self.name('model') # Append tests - self.append(self.test_iterator, "Test Model iterator") + self.append(self.test_models, 'Test GModels') # Return return # Test function - def test_iterator(self): + def test_models(self): """ - Test function. + Test GModels class """ - - # Initialise model container + # Read model container and create a copy of its first model (the + # copy is needed since the model container will go out of scope + # later) + models = gammalib.GModels('data/model_point_plaw.xml') + model = models[0].copy() + name = model.name() + + # Create an empty model container models = gammalib.GModels() - - # Read model container - models_test = gammalib.GModels('data/model_point_plaw.xml') - model = models_test[0] - name = model.name() - + + # Append 10 identical models to model container for i in range(10): model.name(name+'_'+str(i)) models.append(model) - size = models.size() + # Loop over all models using the container iterator and count the + # number of iterations nmodels = 0 for model in models: nmodels += 1 - - self.test_assert(size == 10, 'Test model container size') - self.test_assert(nmodels == 10, 'Test model iterator') + + # Check that looping was successful + self.test_value(models.size(), 10, 'Check model container size') + self.test_value(nmodels, 10, 'Check model iterator') # Return return