Commit 0cf99d2e388c3d260269489c00b1637d3ccb8b0d

Authored by Michael Mayer
1 parent 0501adce

Added parameter access to GApplication and allow for direct parameter loop, adde…

…d corresponding  unit tests
include/GApplication.hpp
... ... @@ -59,6 +59,8 @@ public:
59 59  
60 60 // Operators
61 61 GApplication& operator=(const GApplication& app);
  62 + GApplicationPar& operator[](const int& index);
  63 + const GApplicationPar& operator[](const int& index) const;
62 64 GApplicationPar& operator[](const std::string& name);
63 65 const GApplicationPar& operator[](const std::string& name) const;
64 66  
... ... @@ -270,4 +272,32 @@ void GApplication::pars(const GApplicationPars& pars)
270 272 return;
271 273 }
272 274  
  275 +
  276 +/***********************************************************************//**
  277 + * @brief Return pointer to application parameter
  278 + *
  279 + * @param[in] index Parameter index [0,...,m_pars.size()-1].
  280 + *
  281 + * Returns a pointer to the application parameter with the specified @p index.
  282 + ***************************************************************************/
  283 +inline
  284 +GApplicationPar& GApplication::operator[](const int& index)
  285 +{
  286 + return (m_pars[index]);
  287 +}
  288 +
  289 +
  290 +/***********************************************************************//**
  291 + * @brief Return pointer to application parameter (const version)
  292 + *
  293 + * @param[in] index Model index [0,...,m_pars.size()-1].
  294 + *
  295 + * Returns a const pointer to the parameter with the specified @p index.
  296 + ***************************************************************************/
  297 +inline
  298 +const GApplicationPar& GApplication::operator[](const int& index) const
  299 +{
  300 + return (m_pars[index]);
  301 +}
  302 +
273 303 #endif /* GAPPLICATION_HPP */
... ...
pyext/GApplication.i
... ... @@ -71,14 +71,12 @@ public:
71 71 %rename(_logVerbose) logVerbose;
72 72 %rename(_logDebug) logDebug;
73 73 %rename(_clobber) clobber;
74   - %rename(_has_par) has_par;
75 74 %rename(_par_filename) par_filename;
76 75 %rename(_log_filename) log_filename;
77 76 %rename(_log_header) log_header;
78 77 %rename(_log_trailer) log_trailer;
79 78 %rename(_need_help) need_help;
80 79 %rename(_log) log;
81   - %rename(_pars) pars;
82 80  
83 81 // Methods
84 82 void clear(void);
... ... @@ -219,6 +217,25 @@ GApplication._log_value = _log_value
219 217 }
220 218 return;
221 219 }
  220 + GApplicationPar& __getitem__(const int& index) {
  221 + if (index >= 0 && index < self->pars().size()) {
  222 + return (*self)[index];
  223 + }
  224 + else {
  225 + throw GException::out_of_range("__getitem__(int)", "Application parameter index",
  226 + index, self->pars().size());
  227 + }
  228 + }
  229 + void __setitem__(const int& index, const GApplicationPar& val) {
  230 + if (index >= 0 && index < self->pars().size()) {
  231 + (*self)[index] = val;
  232 + return;
  233 + }
  234 + else {
  235 + throw GException::out_of_range("__setitem__(int)", "Application parameter index",
  236 + index, self->pars().size());
  237 + }
  238 + }
222 239 GApplication copy() {
223 240 return (*self);
224 241 }
... ...
test/test_GApplication.py
... ... @@ -17,6 +17,7 @@
17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18 #
19 19 # ==========================================================================
  20 +import os
20 21 import gammalib
21 22  
22 23  
... ... @@ -85,6 +86,7 @@ class Test(gammalib.GPythonTestSuite):
85 86 # Append tests
86 87 self.append(self.test_log, 'Test GLog')
87 88 self.append(self.test_pars, 'Test GApplicationPars')
  89 + self.append(self.test_par_iteration, 'Test GApplication parameter iteration')
88 90  
89 91 # Return
90 92 return
... ... @@ -204,5 +206,33 @@ class Test(gammalib.GPythonTestSuite):
204 206 else:
205 207 self.test_try_failure('This should never happen')
206 208  
  209 + def test_par_iteration(self):
  210 +
  211 +
  212 + # Set PFILES environment temporary
  213 + os.environ['PFILES'] = os.environ['PWD']+'/data'
  214 +
  215 + # Test GApplicationPars from par file
  216 + app = gammalib.GApplication('test_GApplication','1.1.0')
  217 +
  218 + # Test for proper reading of par file
  219 + self.test_assert(app._version() == '1.1.0', 'Test for proper application version')
  220 + self.test_assert(app.pars().size() == 5, 'Test number of parameters')
  221 +
  222 + # Loop over parameters
  223 + npars = 0
  224 + for par in app:
  225 +
  226 + # Increment counter
  227 + npars += 1
  228 +
  229 + # Set chatter parameter to test persistence
  230 + if par.name() == 'chatter':
  231 + par.integer(4)
  232 +
  233 + # Test outcome of iteration
  234 + self.test_assert(npars == 5, 'Test number of parameters in iterative loop')
  235 + self.test_assert(app['chatter'].integer() == 4, 'Test if parameter could be stored correctly')
  236 +
207 237 # Return
208 238 return
... ...
test/test_GModel.py
... ... @@ -47,15 +47,36 @@ class Test(gammalib.GPythonTestSuite):
47 47 self.name("model")
48 48  
49 49 # Append tests
50   - self.append(self.test, "Model module dummy test")
  50 + self.append(self.test_iterator, "Test Model iterator")
51 51  
52 52 # Return
53 53 return
54 54  
55 55 # Test function
56   - def test(self):
  56 + def test_iterator(self):
57 57 """
58 58 Test function.
59 59 """
  60 +
  61 + # Initialise model container
  62 + models = gammalib.GModels()
  63 +
  64 + # Read model container
  65 + models_test = gammalib.GModels('data/model_point_plaw.xml')
  66 + model = models_test[0]
  67 + name = model.name()
  68 +
  69 + for i in range(10):
  70 + model.name(name+'_'+str(i))
  71 + models.append(model)
  72 +
  73 + size = models.size()
  74 + nmodels = 0
  75 + for model in models:
  76 + nmodels += 1
  77 +
  78 + self.test_assert(size == 10, 'Test model container size')
  79 + self.test_assert(nmodels == 10, 'Test model iterator')
  80 +
60 81 # Return
61 82 return
... ...