1 | """Automated testing for simulation |
---|
2 | |
---|
3 | To be compatible with testcase_generator, a simulus |
---|
4 | class must have: |
---|
5 | setup(self): to initialize the test-case |
---|
6 | getRandomStimulus(self): to get a random stimulus name |
---|
7 | stimuli that inherit from the Stimulus class |
---|
8 | |
---|
9 | @copyright: University of Tennessee, 2007 |
---|
10 | @license: This software is provided as part of the DANSE project. |
---|
11 | |
---|
12 | """ |
---|
13 | |
---|
14 | import testcase_generator as generator |
---|
15 | from testcase_generator import Stimulus |
---|
16 | try: |
---|
17 | import VolumeCanvas |
---|
18 | print "Testing local VolumeCanvas\n" |
---|
19 | except: |
---|
20 | import sans.realspace.VolumeCanvas as VolumeCanvas |
---|
21 | |
---|
22 | def randomModel(): |
---|
23 | """ Return a random model name """ |
---|
24 | from random import random |
---|
25 | from math import floor |
---|
26 | |
---|
27 | model_list = ['sphere', 'cylinder', 'ellipsoid', 'singlehelix'] |
---|
28 | rnd_id = int(floor(random()*len(model_list))) |
---|
29 | |
---|
30 | return model_list[rnd_id] |
---|
31 | |
---|
32 | |
---|
33 | class Simulation: |
---|
34 | """Simulation testing class""" |
---|
35 | |
---|
36 | # Probability of stopping a test case without |
---|
37 | # adding additional stimuli |
---|
38 | end_frequency = 0.1 |
---|
39 | |
---|
40 | def __init__(self): |
---|
41 | self.end_flag = False |
---|
42 | |
---|
43 | def reset(self): |
---|
44 | self.end_flag = False |
---|
45 | |
---|
46 | def setup(self): |
---|
47 | """Setup the canvas for stimuli application""" |
---|
48 | volume = VolumeCanvas.VolumeCanvas() |
---|
49 | volume.params['lores_density'] = 0.05 |
---|
50 | return volume |
---|
51 | |
---|
52 | def getRandomStimulus(self): |
---|
53 | """Return the name of a random stimulus""" |
---|
54 | |
---|
55 | from random import random |
---|
56 | |
---|
57 | # If the end flag is up, compute I(q) one last time |
---|
58 | if self.end_flag == True: |
---|
59 | return None |
---|
60 | |
---|
61 | # Get the list of stimuli |
---|
62 | attrs = dir(self) |
---|
63 | stim_list = [] |
---|
64 | |
---|
65 | # Get the list of simuli and |
---|
66 | # compute the total normalization |
---|
67 | sum = 0 |
---|
68 | for item in attrs: |
---|
69 | if item.endswith('Stimulus'): |
---|
70 | obj = getattr(self, item) |
---|
71 | if hasattr(obj, 'frequency'): |
---|
72 | stim_list.append(item) |
---|
73 | sum += getattr(self, item).frequency |
---|
74 | |
---|
75 | # Choose a stimulus |
---|
76 | rnd = random() |
---|
77 | |
---|
78 | # Check if we need to stop |
---|
79 | if rnd < self.end_frequency: |
---|
80 | self.end_flag = True |
---|
81 | return "GetIq" |
---|
82 | |
---|
83 | run_sum = 0 |
---|
84 | for item in stim_list: |
---|
85 | run_sum += getattr(self, item).frequency/sum |
---|
86 | if run_sum >= rnd: |
---|
87 | pos = item.index('Stimulus') |
---|
88 | |
---|
89 | return item[0:pos] |
---|
90 | |
---|
91 | class AddStimulus(Stimulus): |
---|
92 | """Add an object to the canvas""" |
---|
93 | |
---|
94 | frequency = 0.1 |
---|
95 | |
---|
96 | def __call__(self, canvas): |
---|
97 | """Apply stimulus""" |
---|
98 | report = generator.StimulusReport(tag=self.name) |
---|
99 | |
---|
100 | # Select random model |
---|
101 | add_model = randomModel() |
---|
102 | |
---|
103 | # Add the model |
---|
104 | handle = canvas.add(add_model) |
---|
105 | |
---|
106 | # Check that it is in the list of objects |
---|
107 | if handle in canvas.getShapeList(): |
---|
108 | report.passed = 1 |
---|
109 | else: |
---|
110 | report.log = "Add: tried to add %s" % add_model |
---|
111 | |
---|
112 | report.trace = "Added %s" % add_model |
---|
113 | |
---|
114 | return canvas, report |
---|
115 | |
---|
116 | class GetParamStimulus(Stimulus): |
---|
117 | """Get the value of a canvas or shape parameter""" |
---|
118 | |
---|
119 | frequency = 0.05 |
---|
120 | |
---|
121 | def __call__(self, canvas): |
---|
122 | """Apply stimulus""" |
---|
123 | from random import random |
---|
124 | import math |
---|
125 | |
---|
126 | report = generator.StimulusReport(tag=self.name) |
---|
127 | |
---|
128 | # Read a parameter |
---|
129 | #TODO: access shape parameters |
---|
130 | rnd = random() |
---|
131 | i_rnd = int(math.floor(rnd*len(canvas.params))) |
---|
132 | par_name = canvas.params.keys()[i_rnd] |
---|
133 | value = canvas.getParam(par_name) |
---|
134 | |
---|
135 | # Check that it is in the list of objects |
---|
136 | try: |
---|
137 | float(value) |
---|
138 | report.passed = 1 |
---|
139 | except: |
---|
140 | report.log = "get: bad value for [%s]" % par_name |
---|
141 | |
---|
142 | report.trace = "Read %s" % par_name |
---|
143 | |
---|
144 | return canvas, report |
---|
145 | |
---|
146 | class GetIqStimulus(Stimulus): |
---|
147 | """Calculate I(q)""" |
---|
148 | |
---|
149 | frequency = 0.25 |
---|
150 | |
---|
151 | def __call__(self, canvas): |
---|
152 | report = generator.StimulusReport(tag=self.name) |
---|
153 | |
---|
154 | # Check that a float is returned |
---|
155 | # Validation testing will be done elsewhere |
---|
156 | value = canvas.getIq(0.1) |
---|
157 | |
---|
158 | # Check that it is in the list of objects |
---|
159 | try: |
---|
160 | float(value) |
---|
161 | report.passed = 1 |
---|
162 | except: |
---|
163 | report.log = "GetIq: bad value for Iq "+str(value) |
---|
164 | |
---|
165 | report.trace = "I(q) = %g" % value |
---|
166 | return canvas, report |
---|
167 | |
---|
168 | class GetIq2DStimulus(Stimulus): |
---|
169 | """Calculate I(q)""" |
---|
170 | |
---|
171 | frequency = 0.25 |
---|
172 | |
---|
173 | def __call__(self, canvas): |
---|
174 | report = generator.StimulusReport(tag=self.name) |
---|
175 | |
---|
176 | # Check that a float is returned |
---|
177 | # Validation testing will be done elsewhere |
---|
178 | value = canvas.getIq2D(0.01,0.01) |
---|
179 | |
---|
180 | # Check that it is in the list of objects |
---|
181 | try: |
---|
182 | float(value) |
---|
183 | report.passed = 1 |
---|
184 | except: |
---|
185 | report.log = "GetIq2D: bad value for Iq "+str(value) |
---|
186 | |
---|
187 | report.trace = "I(q) = %g" % value |
---|
188 | return canvas, report |
---|
189 | |
---|
190 | class SetCanvasParamStimulus(Stimulus): |
---|
191 | """Set the value of a canvas parameter""" |
---|
192 | |
---|
193 | frequency = 0.05 |
---|
194 | |
---|
195 | def __call__(self, canvas): |
---|
196 | """Apply stimulus""" |
---|
197 | from random import random |
---|
198 | import math |
---|
199 | |
---|
200 | report = generator.StimulusReport(tag=self.name) |
---|
201 | |
---|
202 | # Read a parameter |
---|
203 | rnd = random() |
---|
204 | i_rnd = int(math.floor(rnd*len(canvas.params))) |
---|
205 | par_name = canvas.params.keys()[i_rnd] |
---|
206 | |
---|
207 | # Get current value |
---|
208 | current = canvas.getParam(par_name) |
---|
209 | |
---|
210 | # Set new value |
---|
211 | rnd2 = random() |
---|
212 | new_value = current*(1.0+rnd2) |
---|
213 | canvas.setParam(par_name, new_value) |
---|
214 | |
---|
215 | # Read it back |
---|
216 | current = canvas.getParam(par_name) |
---|
217 | |
---|
218 | # Check that the new value is correct |
---|
219 | if current == new_value: |
---|
220 | report.passed = 1 |
---|
221 | else: |
---|
222 | report.log = "get: bad value for [%s]" % par_name |
---|
223 | |
---|
224 | report.trace = "Read %s" % par_name |
---|
225 | |
---|
226 | return canvas, report |
---|
227 | |
---|
228 | class SetShapeParamStimulus(Stimulus): |
---|
229 | """Set the value of a canvas parameter""" |
---|
230 | |
---|
231 | frequency = 0.1 |
---|
232 | |
---|
233 | def __call__(self, canvas): |
---|
234 | """Apply stimulus""" |
---|
235 | from random import random |
---|
236 | import math |
---|
237 | |
---|
238 | report = generator.StimulusReport(tag=self.name) |
---|
239 | |
---|
240 | # Read a parameter |
---|
241 | rnd = random() |
---|
242 | shape_list = canvas.getShapeList() |
---|
243 | |
---|
244 | if len(shape_list)==0: |
---|
245 | # No object available, let's test an error condition |
---|
246 | try: |
---|
247 | canvas.setParam("shape0.test-radius", 1.0) |
---|
248 | report.log = "SetShapeParam: set didn't throw exception" |
---|
249 | except: |
---|
250 | report.passed = 1 |
---|
251 | report.trace = "SetShapeParam: testing failure behavior" |
---|
252 | |
---|
253 | else: |
---|
254 | i_rnd = int(math.floor(rnd*len(shape_list))) |
---|
255 | shape_name = shape_list[i_rnd] |
---|
256 | |
---|
257 | found = False |
---|
258 | while found==False: |
---|
259 | rnd2 = random() |
---|
260 | par_keys = canvas.shapes[shape_name].params.keys() |
---|
261 | i_rnd2 = int(math.floor(rnd2*(len(par_keys)))) |
---|
262 | |
---|
263 | short_name = par_keys[i_rnd2] |
---|
264 | par_name = "%s.%s" % (shape_name, short_name) |
---|
265 | if not short_name.lower() == "type": |
---|
266 | found= True |
---|
267 | |
---|
268 | # Get current value |
---|
269 | current = canvas.getParam(par_name) |
---|
270 | |
---|
271 | # Set new value |
---|
272 | if short_name in ['orientation', 'center']: |
---|
273 | new_value = [random(), random(), random()] |
---|
274 | else: |
---|
275 | new_value = current*(1.0+random()) |
---|
276 | |
---|
277 | canvas.setParam(par_name, new_value) |
---|
278 | |
---|
279 | # Read it back |
---|
280 | current = canvas.getParam(par_name) |
---|
281 | |
---|
282 | # Check that the new value is correct |
---|
283 | if current == new_value: |
---|
284 | report.passed = 1 |
---|
285 | else: |
---|
286 | report.log = "get: bad value for [%s]" % par_name |
---|
287 | |
---|
288 | report.trace = "Read %s" % par_name |
---|
289 | |
---|
290 | return canvas, report |
---|
291 | |
---|
292 | |
---|
293 | class RemoveStimulus(Stimulus): |
---|
294 | """Calculate I(q)""" |
---|
295 | |
---|
296 | frequency = 0.05 |
---|
297 | |
---|
298 | def __call__(self, canvas): |
---|
299 | from random import random |
---|
300 | import math |
---|
301 | |
---|
302 | report = generator.StimulusReport(tag=self.name) |
---|
303 | |
---|
304 | # Get list of shapes |
---|
305 | if len(canvas.shapes)>0: |
---|
306 | rnd = random() |
---|
307 | i_rnd = int(math.floor(rnd*len(canvas.shapes))) |
---|
308 | shape_name = canvas.shapes.keys()[i_rnd] |
---|
309 | |
---|
310 | canvas.delete(shape_name) |
---|
311 | if shape_name in canvas.shapes.keys() \ |
---|
312 | or shape_name in canvas.getShapeList(): |
---|
313 | report.log = "Remove: object was not removed" |
---|
314 | else: |
---|
315 | report.passed = 1 |
---|
316 | |
---|
317 | else: |
---|
318 | # No shape to remove: test bad remove call |
---|
319 | try: |
---|
320 | canvas.delete("test-shape") |
---|
321 | report.log = "Remove: delete didn't throw exception" |
---|
322 | except: |
---|
323 | report.passed = 1 |
---|
324 | report.trace = "Remove: testing failure behavior" |
---|
325 | |
---|
326 | return canvas, report |
---|
327 | |
---|
328 | |
---|
329 | |
---|
330 | if __name__ == '__main__': |
---|
331 | import sys |
---|
332 | |
---|
333 | stimuli = Simulation() |
---|
334 | |
---|
335 | if len(sys.argv)>1: |
---|
336 | #t = generator.TestCase(stimuli, filename = "test-case.xml") |
---|
337 | t = generator.TestCase(stimuli, filename = sys.argv[1]) |
---|
338 | print "Test passed =", t.run() |
---|
339 | else: |
---|
340 | g = generator.TestCaseGenerator(stimuli) |
---|
341 | g.generateAndRun(200) |
---|
342 | |
---|
343 | |
---|
344 | |
---|
345 | |
---|