Commit 91643221a94cb1fe7be3b31478aff9e05ec0ade6

Authored by Jürgen Knödlseder
1 parent e6e2c2cc

Support Python slicing of GApplicationPars and refactor unit tests (#2128)

pyext/GApplicationPars.i
1 1 /***************************************************************************
2 2 * GApplicationPars.i - Application parameter container *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2016 by Juergen Knoedlseder *
  4 + * copyright (C) 2010-2017 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -36,7 +36,7 @@
36 36 *
37 37 * @brief Application parameter container class
38 38 ***************************************************************************/
39   -class GApplicationPars : public GBase {
  39 +class GApplicationPars : public GContainer {
40 40  
41 41 public:
42 42 // Constructors and destructors
... ... @@ -76,9 +76,14 @@ public:
76 76 ***************************************************************************/
77 77 %extend GApplicationPars {
78 78 GApplicationPar& __getitem__(const int& index) {
  79 + // Counting from start, e.g. [2]
79 80 if (index >= 0 && index < self->size()) {
80 81 return (*self)[index];
81 82 }
  83 + // Counting from end, e.g. [-1]
  84 + else if (index < 0 && self->size()+index >= 0) {
  85 + return (*self)[self->size()+index];
  86 + }
82 87 else {
83 88 throw GException::out_of_range("__getitem__(int)", index, self->size());
84 89 }
... ... @@ -86,11 +91,45 @@ public:
86 91 GApplicationPar& __getitem__(const std::string& name) {
87 92 return (*self)[name];
88 93 }
  94 + GApplicationPars* __getitem__(PyObject *param) {
  95 + if (PySlice_Check(param)) {
  96 + Py_ssize_t start = 0;
  97 + Py_ssize_t stop = 0;
  98 + Py_ssize_t step = 0;
  99 + Py_ssize_t len = self->size();
  100 + if (PySlice_GetIndices((PySliceObject*)param, len, &start, &stop, &step) == 0) {
  101 + GApplicationPars* pars = new GApplicationPars;
  102 + if (step > 0) {
  103 + for (int i = (int)start; i < (int)stop; i += (int)step) {
  104 + pars->append((*self)[i]);
  105 + }
  106 + }
  107 + else {
  108 + for (int i = (int)start; i > (int)stop; i += (int)step) {
  109 + pars->append((*self)[i]);
  110 + }
  111 + }
  112 + return pars;
  113 + }
  114 + else {
  115 + throw GException::invalid_argument("__getitem__(PyObject)",
  116 + "Invalid slice indices");
  117 + }
  118 + }
  119 + else {
  120 + throw GException::invalid_argument("__getitem__(PyObject)","");
  121 + }
  122 + }
89 123 void __setitem__(const int& index, const GApplicationPar& par) {
90   - if (index>=0 && index < self->size()) {
  124 + // Counting from start, e.g. [2]
  125 + if (index >= 0 && index < self->size()) {
91 126 (*self)[index] = par;
92 127 return;
93 128 }
  129 + // Counting from end, e.g. [-1]
  130 + else if (index < 0 && self->size()+index >= 0) {
  131 + (*self)[self->size()+index] = par;
  132 + }
94 133 else {
95 134 throw GException::out_of_range("__setitem__(int)", index, self->size());
96 135 }
... ...
pyext/GModels.i
... ... @@ -125,9 +125,13 @@ public:
125 125 }
126 126 }
127 127 void __setitem__(const int& index, const GModel& val) {
  128 + // Counting from start, e.g. [2]
128 129 if (index >= 0 && index < self->size()) {
129 130 self->set(index, val);
130   - return;
  131 + }
  132 + // Counting from end, e.g. [-1]
  133 + else if (index < 0 && self->size()+index >= 0) {
  134 + self->set(self->size()+index, val);
131 135 }
132 136 else {
133 137 throw GException::out_of_range("__setitem__(int)", "Model index",
... ...
pyext/GObservations.i
... ... @@ -151,9 +151,13 @@ typedef GObservations::likelihood likelihood;
151 151 }
152 152 }
153 153 void __setitem__(const int& index, const GObservation& val) {
154   - if (index>=0 && index < self->size()) {
  154 + // Counting from start, e.g. [2]
  155 + if (index >= 0 && index < self->size()) {
155 156 self->set(index, val);
156   - return;
  157 + }
  158 + // Counting from end, e.g. [-1]
  159 + else if (index < 0 && self->size()+index >= 0) {
  160 + self->set(self->size()+index, val);
157 161 }
158 162 else {
159 163 throw GException::out_of_range("__setitem__(int)", index, self->size());
... ...
test/Makefile.am
... ... @@ -120,7 +120,8 @@ EXTRA_DIST = data reports testinst \
120 120 test_GXml.py \
121 121 test_GXspec.py \
122 122 test_examples.py \
123   - test_python.py
  123 + test_python.py \
  124 + test_support.py
124 125  
125 126 # Standard test sources and links
126 127 test_GSupport_SOURCES = test_GSupport.cpp test_GSupport.hpp
... ...
test/test_GApplication.py
1 1 # ==========================================================================
2 2 # This module performs unit tests for the GammaLib application module
3 3 #
4   -# Copyright (C) 2012-2016 Juergen Knoedlseder
  4 +# Copyright (C) 2012-2017 Juergen Knoedlseder
5 5 #
6 6 # This program is free software: you can redistribute it and/or modify
7 7 # it under the terms of the GNU General Public License as published by
... ... @@ -19,6 +19,7 @@
19 19 # ==========================================================================
20 20 import os
21 21 import gammalib
  22 +import test_support
22 23  
23 24  
24 25 # ========================================== #
... ... @@ -42,8 +43,7 @@ class Test(gammalib.GPythonTestSuite):
42 43 # Return
43 44 return
44 45  
45   -
46   - # Private methods
  46 + # Check log file versus a reference
47 47 def _check_log_file(self, ref, calls):
48 48 """
49 49 Check log file versus a reference
... ... @@ -74,24 +74,28 @@ class Test(gammalib.GPythonTestSuite):
74 74 # Return
75 75 return
76 76  
77   -
78   - # Public methods
79   - def set(self):
80   - """
81   - Set all test functions
  77 + # Setup GApplicationPars container
  78 + def _setup_pars(self):
82 79 """
83   - # Set test name
84   - self.name('app')
  80 + Setup GApplicationPars container
85 81  
86   - # Append tests
87   - self.append(self.test_log, 'Test GLog')
88   - self.append(self.test_app, 'Test GApplication')
89   - self.append(self.test_pars, 'Test GApplicationPars')
  82 + Returns
  83 + -------
  84 + pars : `~gammalib.GApplicationPars`
  85 + Parameter container
  86 + """
  87 + # Setup application parameter container
  88 + pars = gammalib.GApplicationPars()
  89 + for i in range(10):
  90 + name = '%s' % i
  91 + par = gammalib.GApplicationPar(name,'r','a','1.0','0.0','2.0','Dummy')
  92 + pars.append(par)
90 93  
91   - # Return
92   - return
  94 + # Return application parameter container
  95 + return pars
93 96  
94   - def test_log(self):
  97 + # Test GLog class
  98 + def _test_log(self):
95 99 """
96 100 Test GLog class
97 101 """
... ... @@ -192,7 +196,8 @@ class Test(gammalib.GPythonTestSuite):
192 196 # Return
193 197 return
194 198  
195   - def test_app(self):
  199 + # Test GApplication class
  200 + def _test_app(self):
196 201 """
197 202 Test GApplication class
198 203 """
... ... @@ -224,7 +229,8 @@ class Test(gammalib.GPythonTestSuite):
224 229 # Return
225 230 return
226 231  
227   - def test_pars(self):
  232 + # Test GApplicationPars class
  233 + def _test_pars(self):
228 234 """
229 235 Test GApplicationPars class
230 236 """
... ... @@ -240,3 +246,59 @@ class Test(gammalib.GPythonTestSuite):
240 246  
241 247 # Return
242 248 return
  249 +
  250 + # Test GApplicationPars class access operators
  251 + def _test_pars_access(self):
  252 + """
  253 + Test GApplicationPars class model access
  254 + """
  255 + # Setup application parameter container
  256 + pars = self._setup_pars()
  257 +
  258 + # Perform application parameter access tests
  259 + test_support._container_access_index(self, pars)
  260 +
  261 + # Check application parameter setting by index from start
  262 + par = gammalib.GApplicationPar('98','r','a','1.0','0.0','2.0','Dummy')
  263 + pars[3] = par
  264 + self.test_value(pars[3].name(), '98')
  265 +
  266 + # Check application parameter setting by index from end
  267 + par = gammalib.GApplicationPar('99','r','a','1.0','0.0','2.0','Dummy')
  268 + pars[-2] = par
  269 + self.test_value(pars[-2].name(), '99')
  270 +
  271 + # Return
  272 + return
  273 +
  274 + # Test GApplicationPars class slicing
  275 + def _test_pars_slicing(self):
  276 + """
  277 + Test GApplicationPars class slicing
  278 + """
  279 + # Setup application parameter container
  280 + pars = self._setup_pars()
  281 +
  282 + # Perform slicing tests
  283 + test_support._container_slicing(self, pars)
  284 +
  285 + # Return
  286 + return
  287 +
  288 + # Set test functions
  289 + def set(self):
  290 + """
  291 + Set all test functions
  292 + """
  293 + # Set test name
  294 + self.name('app')
  295 +
  296 + # Append tests
  297 + self.append(self._test_log, 'Test GLog')
  298 + self.append(self._test_app, 'Test GApplication')
  299 + self.append(self._test_pars, 'Test GApplicationPars')
  300 + self.append(self._test_pars_access, 'Test GApplicationPars class parameter access')
  301 + self.append(self._test_pars_access, 'Test GApplicationPars class slicing')
  302 +
  303 + # Return
  304 + return
... ...
test/test_GModel.py
... ... @@ -17,8 +17,8 @@
17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18 #
19 19 # ==========================================================================
20   -import os
21 20 import gammalib
  21 +import test_support
22 22  
23 23  
24 24 # ==================================== #
... ... @@ -36,134 +36,87 @@ class Test(gammalib.GPythonTestSuite):
36 36 # Call base class constructor
37 37 gammalib.GPythonTestSuite.__init__(self)
38 38  
39   - # Set test data directory
40   - self._datadir = os.environ['TEST_DATA']
41   -
42 39 # Return
43 40 return
44 41  
45   - # Set test functions
46   - def set(self):
  42 + # Setup GModels container
  43 + def _setup_models(self):
47 44 """
48   - Set all test functions
49   - """
50   - # Set test name
51   - self.name('model')
52   -
53   - # Append tests
54   - self.append(self.test_models_access, 'Test GModels class model access')
55   - self.append(self.test_models_slicing, 'Test GModels class slicing')
56   -
57   - # Return
58   - return
  45 + Setup GModels container
59 46  
60   - # Test function
61   - def test_models_access(self):
62   - """
63   - Test GModels class model access
  47 + Returns
  48 + -------
  49 + models : `~gammalib.GModels`
  50 + Models container
64 51 """
65   - # Set model filename
66   - filename = self._datadir + '/model_point_plaw.xml'
67   -
68   - # Read model container and create a copy of its first model (the
69   - # copy is needed since the model container will go out of scope
70   - # later)
71   - models = gammalib.GModels(filename)
72   - model = models[0].copy()
73   - name = model.name()
74   -
75   - # Create an empty model container
  52 + # Setup model container
76 53 models = gammalib.GModels()
77   -
78   - # Append 10 identical models to model container
  54 + model = gammalib.GModelSky()
79 55 for i in range(10):
80   - model.name(name+'_'+str(i))
  56 + model.name('%s' % i)
81 57 models.append(model)
82   -
83   - # Loop over all models using the container iterator and count the
84   - # number of iterations
85   - nmodels = 0
86   - for m in models:
87   - self.test_value(m.name(), name+'_'+str(nmodels))
88   - nmodels += 1
89 58  
90   - # Check that looping was successful
91   - self.test_value(models.size(), 10, 'Check model container size')
92   - self.test_value(nmodels, 10, 'Check model iterator')
  59 + # Return model container
  60 + return models
93 61  
94   - # Check model access from end
95   - self.test_value(models[-1].name(), name+'_9')
  62 + # Test GModels class access operators
  63 + def _test_models_access(self):
  64 + """
  65 + Test GModels class model access
  66 + """
  67 + # Setup model container and sky model
  68 + models = self._setup_models()
  69 + model = gammalib.GModelSky()
96 70  
97   - # Check model access by name
98   - self.test_value(models[name+'_7'].name(), name+'_7')
  71 + # Perform model access tests
  72 + test_support._container_access_index(self, models)
99 73  
100   - # Check model setting by index
101   - model.name('Test index')
102   - models[0] = model
103   - self.test_value(models[0].name(), 'Test index')
  74 + # Check model getting by name
  75 + self.test_value(models['7'].name(), '7')
104 76  
105 77 # Check model setting by name
106 78 model.name('Test name')
107   - models[name+'_3'] = model
  79 + models['3'] = model
108 80 self.test_value(models['Test name'].name(), 'Test name')
109 81  
  82 + # Check model setting by index from start
  83 + model.name('Test index from start')
  84 + models[1] = model
  85 + self.test_value(models[1].name(), 'Test index from start')
  86 +
  87 + # Check model setting by index from end
  88 + model.name('Test index from end')
  89 + models[-2] = model
  90 + self.test_value(models[-2].name(), 'Test index from end')
  91 +
110 92 # Return
111 93 return
112 94  
113   -
114 95 # Test GModels class slicing
115   - def test_models_slicing(self):
  96 + def _test_models_slicing(self):
116 97 """
117 98 Test GModels class slicing
118 99 """
119   - # Setup model container
120   - models = gammalib.GModels()
121   - model = gammalib.GModelSky()
122   - for i in range(10):
123   - model.name('%s' % i)
124   - models.append(model)
  100 + # Setup models container
  101 + models = self._setup_models()
125 102  
126   - # Test models[start:end]
127   - self.test_value(len(models[3:5]), 2)
128   - self.test_value(models[3:5][0].name(), '3')
129   - self.test_value(models[3:5][1].name(), '4')
  103 + # Perform slicing tests
  104 + test_support._container_slicing(self, models)
130 105  
131   - # Test models[start:]
132   - self.test_value(len(models[7:]), 3)
133   - self.test_value(models[7:][0].name(), '7')
134   - self.test_value(models[7:][1].name(), '8')
135   - self.test_value(models[7:][2].name(), '9')
  106 + # Return
  107 + return
136 108  
137   - # Test models[:end]
138   - self.test_value(len(models[:2]), 2)
139   - self.test_value(models[:2][0].name(), '0')
140   - self.test_value(models[:2][1].name(), '1')
  109 + # Set test functions
  110 + def set(self):
  111 + """
  112 + Set all test functions
  113 + """
  114 + # Set test name
  115 + self.name('model')
141 116  
142   - # Test models[:]
143   - self.test_value(len(models[:]), 10)
144   - for i in range(10):
145   - self.test_value(models[:][i].name(), '%s' % i)
146   -
147   - # Test models[start:end:step]
148   - self.test_value(len(models[3:7:2]), 2)
149   - self.test_value(models[3:7:2][0].name(), '3')
150   - self.test_value(models[3:7:2][1].name(), '5')
151   -
152   - # Test models[start:end:step]
153   - self.test_value(len(models[6:3:-2]), 2)
154   - self.test_value(models[6:3:-2][0].name(), '6')
155   - self.test_value(models[6:3:-2][1].name(), '4')
156   -
157   - # Test models[-start:]
158   - self.test_value(len(models[-2:]), 2)
159   - self.test_value(models[-2:][0].name(), '8')
160   - self.test_value(models[-2:][1].name(), '9')
161   -
162   - # Test models[:-end]
163   - self.test_value(len(models[:-7]), 3)
164   - self.test_value(models[:-7][0].name(), '0')
165   - self.test_value(models[:-7][1].name(), '1')
166   - self.test_value(models[:-7][2].name(), '2')
  117 + # Append tests
  118 + self.append(self._test_models_access, 'Test GModels class model access')
  119 + self.append(self._test_models_slicing, 'Test GModels class slicing')
167 120  
168 121 # Return
169 122 return
... ...
test/test_GObservation.py
... ... @@ -18,6 +18,7 @@
18 18 #
19 19 # ==========================================================================
20 20 import gammalib
  21 +import test_support
21 22  
22 23  
23 24 # ========================================== #
... ... @@ -38,26 +39,39 @@ class Test(gammalib.GPythonTestSuite):
38 39 # Return
39 40 return
40 41  
41   - # Set test functions
42   - def set(self):
  42 + # Setup GObservations container
  43 + def _setup_obs(self):
43 44 """
44   - Set all test functions
  45 + Setup GObservations container
  46 +
  47 + Returns
  48 + -------
  49 + obs : `~gammalib.GObservations`
  50 + Observation container
45 51 """
46   - # Set test name
47   - self.name('obs')
  52 + # Setup observation container
  53 + obs = gammalib.GObservations()
  54 + run = gammalib.GCTAObservation()
  55 + for i in range(10):
  56 + run.name('%s' % i)
  57 + run.id('%s' % i)
  58 + obs.append(run)
48 59  
49   - # Append tests
50   - self.append(self.test_energy, 'Test GEnergy class')
51   - self.append(self.test_energies, 'Test GEnergies class')
52   - self.append(self.test_time, 'Test GTime class')
53   - self.append(self.test_observations_slicing,
54   - 'Test GObservations class slicing')
  60 + # Setup model container
  61 + models = gammalib.GModels()
  62 + model = gammalib.GModelSky()
  63 + for i in range(10):
  64 + model.name('%s' % i)
  65 + models.append(model)
55 66  
56   - # Return
57   - return
  67 + # Set observation's model container
  68 + obs.models(models)
  69 +
  70 + # Return observations container
  71 + return obs
58 72  
59 73 # Test GEnergy class
60   - def test_energy(self):
  74 + def _test_energy(self):
61 75 """
62 76 Test GEnergy class
63 77 """
... ... @@ -131,7 +145,7 @@ class Test(gammalib.GPythonTestSuite):
131 145 return
132 146  
133 147 # Test GEnergies class
134   - def test_energies(self):
  148 + def _test_energies(self):
135 149 """
136 150 Test GEnergies class
137 151 """
... ... @@ -149,7 +163,7 @@ class Test(gammalib.GPythonTestSuite):
149 163 return
150 164  
151 165 # Test GTime class
152   - def test_time(self):
  166 + def _test_time(self):
153 167 """
154 168 Test GTime class
155 169 """
... ... @@ -217,78 +231,69 @@ class Test(gammalib.GPythonTestSuite):
217 231 # Return
218 232 return
219 233  
  234 + # Test GObservations class access operators
  235 + def _test_observations_access(self):
  236 + """
  237 + Test GObservations class observation access
  238 + """
  239 + # Setup observation container and CTA observation
  240 + obs = self._setup_obs()
  241 + run = gammalib.GCTAObservation()
  242 +
  243 + # Perform observation access tests
  244 + test_support._container_access_index(self, obs)
  245 +
  246 + # Check observation setting by index from start
  247 + run.id('98')
  248 + obs[3] = run
  249 + self.test_value(obs[3].id(), '98')
  250 +
  251 + # Check observation setting by index from end
  252 + run.id('99')
  253 + obs[-2] = run
  254 + self.test_value(obs[-2].id(), '99')
  255 +
  256 + # Return
  257 + return
220 258  
221 259 # Test GObservations class slicing
222   - def test_observations_slicing(self):
  260 + def _test_observations_slicing(self):
223 261 """
224 262 Test GObservations class slicing
225 263 """
226 264 # Setup observation container
227   - obs = gammalib.GObservations()
228   - run = gammalib.GCTAObservation()
229   - for i in range(10):
230   - run.id('%s' % i)
231   - obs.append(run)
  265 + obs = self._setup_obs()
232 266  
233   - # Setup model container
234   - models = gammalib.GModels()
235   - model = gammalib.GModelSky()
236   - for i in range(10):
237   - model.name('%s' % i)
238   - models.append(model)
239   -
240   - # Set observation's model container
241   - obs.models(models)
  267 + # Perform slicing tests
  268 + test_support._container_slicing(self, obs)
242 269  
243   - # Test obs[start:end]
244   - self.test_value(len(obs[3:5]), 2)
  270 + # Perform additional slicing tests
245 271 self.test_value(len(obs[3:5].models()), 10)
246   - self.test_value(obs[3:5][0].id(), '3')
247   - self.test_value(obs[3:5][1].id(), '4')
248   -
249   - # Test obs[start:]
250   - self.test_value(len(obs[7:]), 3)
251 272 self.test_value(len(obs[7:].models()), 10)
252   - self.test_value(obs[7:][0].id(), '7')
253   - self.test_value(obs[7:][1].id(), '8')
254   - self.test_value(obs[7:][2].id(), '9')
255   -
256   - # Test obs[:end]
257   - self.test_value(len(obs[:2]), 2)
258 273 self.test_value(len(obs[:2].models()), 10)
259   - self.test_value(obs[:2][0].id(), '0')
260   - self.test_value(obs[:2][1].id(), '1')
261   -
262   - # Test obs[:]
263   - self.test_value(len(obs[:]), 10)
264 274 self.test_value(len(obs[:].models()), 10)
265   - for i in range(10):
266   - self.test_value(obs[:][i].id(), '%s' % i)
267   -
268   - # Test obs[start:end:step]
269   - self.test_value(len(obs[3:7:2]), 2)
270 275 self.test_value(len(obs[3:7:2].models()), 10)
271   - self.test_value(obs[3:7:2][0].id(), '3')
272   - self.test_value(obs[3:7:2][1].id(), '5')
273   -
274   - # Test obs[start:end:step]
275   - self.test_value(len(obs[6:3:-2]), 2)
276 276 self.test_value(len(obs[6:3:-2].models()), 10)
277   - self.test_value(obs[6:3:-2][0].id(), '6')
278   - self.test_value(obs[6:3:-2][1].id(), '4')
279   -
280   - # Test obs[-start:]
281   - self.test_value(len(obs[-2:]), 2)
282 277 self.test_value(len(obs[-2:].models()), 10)
283   - self.test_value(obs[-2:][0].id(), '8')
284   - self.test_value(obs[-2:][1].id(), '9')
285   -
286   - # Test obs[:-end]
287   - self.test_value(len(obs[:-7]), 3)
288 278 self.test_value(len(obs[:-7].models()), 10)
289   - self.test_value(obs[:-7][0].id(), '0')
290   - self.test_value(obs[:-7][1].id(), '1')
291   - self.test_value(obs[:-7][2].id(), '2')
  279 +
  280 + # Return
  281 + return
  282 +
  283 + # Set test functions
  284 + def set(self):
  285 + """
  286 + Set all test functions
  287 + """
  288 + # Set test name
  289 + self.name('obs')
  290 +
  291 + # Append tests
  292 + self.append(self._test_energy, 'Test GEnergy class')
  293 + self.append(self._test_energies, 'Test GEnergies class')
  294 + self.append(self._test_time, 'Test GTime class')
  295 + self.append(self._test_observations_access, 'Test GObservations class observation access')
  296 + self.append(self._test_observations_slicing, 'Test GObservations class slicing')
292 297  
293 298 # Return
294 299 return
... ...
test/test_support.py 0 → 100755
  1 +# ==========================================================================
  2 +# This module provides support functions for the Python unit tests
  3 +#
  4 +# Copyright (C) 2017 Juergen Knoedlseder
  5 +#
  6 +# This program is free software: you can redistribute it and/or modify
  7 +# it under the terms of the GNU General Public License as published by
  8 +# the Free Software Foundation, either version 3 of the License, or
  9 +# (at your option) any later version.
  10 +#
  11 +# This program is distributed in the hope that it will be useful,
  12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 +# GNU General Public License for more details.
  15 +#
  16 +# You should have received a copy of the GNU General Public License
  17 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  18 +#
  19 +# ==========================================================================
  20 +
  21 +
  22 +# ============================== #
  23 +# Test container access by index #
  24 +# ============================== #
  25 +def _container_access_index(testsuite, container):
  26 + """
  27 + Test container access by index
  28 +
  29 + Parameters
  30 + ----------
  31 + testsuite : `~gammalib.GPythonTestSuite`
  32 + Test suite class
  33 + container : `~gammalib.GContainer`
  34 + Container class
  35 + """
  36 + # Loop over all elements using the container iterator and count the number
  37 + # of iterations
  38 + niter = 0
  39 + for element in container:
  40 + testsuite.test_value(element.name(), '%s' % niter)
  41 + niter += 1
  42 +
  43 + # Check that looping was successful
  44 + testsuite.test_value(container.size(), 10, 'Check container size')
  45 + testsuite.test_value(niter, 10, 'Check container iterator')
  46 +
  47 + # Test access from start
  48 + testsuite.test_value(container[3].name(), '3')
  49 +
  50 + # Check access from end
  51 + testsuite.test_value(container[-2].name(), '8')
  52 +
  53 + # Return
  54 + return
  55 +
  56 +
  57 +# ====================== #
  58 +# Test container slicing #
  59 +# ====================== #
  60 +def _container_slicing(testsuite, container):
  61 + """
  62 + Test container slicing
  63 +
  64 + Parameters
  65 + ----------
  66 + testsuite : `~gammalib.GPythonTestSuite`
  67 + Test suite class
  68 + container : `~gammalib.GContainer`
  69 + Container class
  70 + """
  71 + # Test container[start:end]
  72 + testsuite.test_value(len(container[3:5]), 2)
  73 + testsuite.test_value(container[3:5][0].name(), '3')
  74 + testsuite.test_value(container[3:5][1].name(), '4')
  75 +
  76 + # Test container[start:]
  77 + testsuite.test_value(len(container[7:]), 3)
  78 + testsuite.test_value(container[7:][0].name(), '7')
  79 + testsuite.test_value(container[7:][1].name(), '8')
  80 + testsuite.test_value(container[7:][2].name(), '9')
  81 +
  82 + # Test container[:end]
  83 + testsuite.test_value(len(container[:2]), 2)
  84 + testsuite.test_value(container[:2][0].name(), '0')
  85 + testsuite.test_value(container[:2][1].name(), '1')
  86 +
  87 + # Test container[:]
  88 + testsuite.test_value(len(container[:]), 10)
  89 + for i in range(10):
  90 + testsuite.test_value(container[:][i].name(), '%s' % i)
  91 +
  92 + # Test container[start:end:step]
  93 + testsuite.test_value(len(container[3:7:2]), 2)
  94 + testsuite.test_value(container[3:7:2][0].name(), '3')
  95 + testsuite.test_value(container[3:7:2][1].name(), '5')
  96 +
  97 + # Test container[start:end:step]
  98 + testsuite.test_value(len(container[6:3:-2]), 2)
  99 + testsuite.test_value(container[6:3:-2][0].name(), '6')
  100 + testsuite.test_value(container[6:3:-2][1].name(), '4')
  101 +
  102 + # Test container[-start:]
  103 + testsuite.test_value(len(container[-2:]), 2)
  104 + testsuite.test_value(container[-2:][0].name(), '8')
  105 + testsuite.test_value(container[-2:][1].name(), '9')
  106 +
  107 + # Test container[:-end]
  108 + testsuite.test_value(len(container[:-7]), 3)
  109 + testsuite.test_value(container[:-7][0].name(), '0')
  110 + testsuite.test_value(container[:-7][1].name(), '1')
  111 + testsuite.test_value(container[:-7][2].name(), '2')
  112 +
  113 + # Return
  114 + return
... ...