Commit 0cf99d2e388c3d260269489c00b1637d3ccb8b0d
1 parent
0501adce
Added parameter access to GApplication and allow for direct parameter loop, adde…
…d corresponding unit tests
Showing
4 changed files
with
102 additions
and
4 deletions
include/GApplication.hpp
| @@ -59,6 +59,8 @@ public: | @@ -59,6 +59,8 @@ public: | ||
| 59 | 59 | ||
| 60 | // Operators | 60 | // Operators |
| 61 | GApplication& operator=(const GApplication& app); | 61 | GApplication& operator=(const GApplication& app); |
| 62 | + GApplicationPar& operator[](const int& index); | ||
| 63 | + const GApplicationPar& operator[](const int& index) const; | ||
| 62 | GApplicationPar& operator[](const std::string& name); | 64 | GApplicationPar& operator[](const std::string& name); |
| 63 | const GApplicationPar& operator[](const std::string& name) const; | 65 | const GApplicationPar& operator[](const std::string& name) const; |
| 64 | 66 | ||
| @@ -270,4 +272,32 @@ void GApplication::pars(const GApplicationPars& pars) | @@ -270,4 +272,32 @@ void GApplication::pars(const GApplicationPars& pars) | ||
| 270 | return; | 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 | #endif /* GAPPLICATION_HPP */ | 303 | #endif /* GAPPLICATION_HPP */ |
pyext/GApplication.i
| @@ -71,14 +71,12 @@ public: | @@ -71,14 +71,12 @@ public: | ||
| 71 | %rename(_logVerbose) logVerbose; | 71 | %rename(_logVerbose) logVerbose; |
| 72 | %rename(_logDebug) logDebug; | 72 | %rename(_logDebug) logDebug; |
| 73 | %rename(_clobber) clobber; | 73 | %rename(_clobber) clobber; |
| 74 | - %rename(_has_par) has_par; | ||
| 75 | %rename(_par_filename) par_filename; | 74 | %rename(_par_filename) par_filename; |
| 76 | %rename(_log_filename) log_filename; | 75 | %rename(_log_filename) log_filename; |
| 77 | %rename(_log_header) log_header; | 76 | %rename(_log_header) log_header; |
| 78 | %rename(_log_trailer) log_trailer; | 77 | %rename(_log_trailer) log_trailer; |
| 79 | %rename(_need_help) need_help; | 78 | %rename(_need_help) need_help; |
| 80 | %rename(_log) log; | 79 | %rename(_log) log; |
| 81 | - %rename(_pars) pars; | ||
| 82 | 80 | ||
| 83 | // Methods | 81 | // Methods |
| 84 | void clear(void); | 82 | void clear(void); |
| @@ -219,6 +217,25 @@ GApplication._log_value = _log_value | @@ -219,6 +217,25 @@ GApplication._log_value = _log_value | ||
| 219 | } | 217 | } |
| 220 | return; | 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 | GApplication copy() { | 239 | GApplication copy() { |
| 223 | return (*self); | 240 | return (*self); |
| 224 | } | 241 | } |
test/test_GApplication.py
| @@ -17,6 +17,7 @@ | @@ -17,6 +17,7 @@ | ||
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | # | 18 | # |
| 19 | # ========================================================================== | 19 | # ========================================================================== |
| 20 | +import os | ||
| 20 | import gammalib | 21 | import gammalib |
| 21 | 22 | ||
| 22 | 23 | ||
| @@ -85,6 +86,7 @@ class Test(gammalib.GPythonTestSuite): | @@ -85,6 +86,7 @@ class Test(gammalib.GPythonTestSuite): | ||
| 85 | # Append tests | 86 | # Append tests |
| 86 | self.append(self.test_log, 'Test GLog') | 87 | self.append(self.test_log, 'Test GLog') |
| 87 | self.append(self.test_pars, 'Test GApplicationPars') | 88 | self.append(self.test_pars, 'Test GApplicationPars') |
| 89 | + self.append(self.test_par_iteration, 'Test GApplication parameter iteration') | ||
| 88 | 90 | ||
| 89 | # Return | 91 | # Return |
| 90 | return | 92 | return |
| @@ -204,5 +206,33 @@ class Test(gammalib.GPythonTestSuite): | @@ -204,5 +206,33 @@ class Test(gammalib.GPythonTestSuite): | ||
| 204 | else: | 206 | else: |
| 205 | self.test_try_failure('This should never happen') | 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 | # Return | 237 | # Return |
| 208 | return | 238 | return |
test/test_GModel.py
| @@ -47,15 +47,36 @@ class Test(gammalib.GPythonTestSuite): | @@ -47,15 +47,36 @@ class Test(gammalib.GPythonTestSuite): | ||
| 47 | self.name("model") | 47 | self.name("model") |
| 48 | 48 | ||
| 49 | # Append tests | 49 | # Append tests |
| 50 | - self.append(self.test, "Model module dummy test") | 50 | + self.append(self.test_iterator, "Test Model iterator") |
| 51 | 51 | ||
| 52 | # Return | 52 | # Return |
| 53 | return | 53 | return |
| 54 | 54 | ||
| 55 | # Test function | 55 | # Test function |
| 56 | - def test(self): | 56 | + def test_iterator(self): |
| 57 | """ | 57 | """ |
| 58 | Test function. | 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 | # Return | 81 | # Return |
| 61 | return | 82 | return |