Mercurial > hg > ltpda
comparison testing/utp_1.1/utps/ao/utp_ao_hist.m @ 44:409a22968d5e default
Add unit tests
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Tue, 06 Dec 2011 18:42:11 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
43:bc767aaa99a8 | 44:409a22968d5e |
---|---|
1 % UTP_AO_HIST a set of UTPs for the ao/hist method | |
2 % | |
3 % M Hewitson 06-08-08 | |
4 % | |
5 % $Id: utp_ao_hist.m,v 1.12 2010/06/07 16:43:06 ingo Exp $ | |
6 % | |
7 | |
8 % <MethodDescription> | |
9 % | |
10 % The hist method of the ao class histograms the y | |
11 % and/or x data. | |
12 % | |
13 % </MethodDescription> | |
14 | |
15 function results = utp_ao_hist(varargin) | |
16 | |
17 % Check the inputs | |
18 if nargin == 0 | |
19 | |
20 % Some keywords | |
21 class = 'ao'; | |
22 mthd = 'hist'; | |
23 | |
24 results = []; | |
25 disp('******************************************************'); | |
26 disp(['**** Running UTPs for ' class '/' mthd]); | |
27 disp('******************************************************'); | |
28 | |
29 % Test AOs | |
30 [at1,at2,at3,at4,at5,at6,atvec,atmat] = eval(['get_test_objects_' class]); | |
31 | |
32 % Exception list for the UTPs: | |
33 [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples(); | |
34 | |
35 % Run the tests | |
36 results = [results utp_01]; % getInfo call | |
37 results = [results utp_02]; % Vector input | |
38 results = [results utp_03]; % Matrix input | |
39 results = [results utp_04]; % List input | |
40 results = [results utp_05]; % Test with mixed input | |
41 results = [results utp_06]; % Test history is working | |
42 results = [results utp_07]; % Test the modify call works | |
43 results = [results utp_08]; % Test the data shape | |
44 results = [results utp_09]; % Test output of the data | |
45 results = [results utp_10]; % Test with plist: 'N' | |
46 | |
47 results = [results utp_11(mthd, at1, ple1)]; % Test plotinfo doesn't disappear | |
48 | |
49 results = [results utp_12]; % Test with plist: 'X' | |
50 | |
51 disp('Done.'); | |
52 disp('******************************************************'); | |
53 | |
54 elseif nargin == 1 % Check for UTP functions | |
55 if strcmp(varargin{1}, 'isutp') | |
56 results = 1; | |
57 else | |
58 results = 0; | |
59 end | |
60 else | |
61 error('### Incorrect inputs') | |
62 end | |
63 | |
64 %% UTP_01 | |
65 | |
66 % <TestDescription> | |
67 % | |
68 % Tests that the getInfo call works for this method. | |
69 % | |
70 % </TestDescription> | |
71 function result = utp_01 | |
72 | |
73 | |
74 % <SyntaxDescription> | |
75 % | |
76 % Test that the getInfo call works for no sets, all sets, and each set | |
77 % individually. | |
78 % | |
79 % </SyntaxDescription> | |
80 | |
81 try | |
82 % <SyntaxCode> | |
83 % Call for no sets | |
84 io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']); | |
85 % Call for all sets | |
86 io(2) = eval([class '.getInfo(''' mthd ''')']); | |
87 % Call for each set | |
88 for kk=1:numel(io(2).sets) | |
89 io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']); | |
90 end | |
91 % </SyntaxCode> | |
92 stest = true; | |
93 catch err | |
94 disp(err.message) | |
95 stest = false; | |
96 end | |
97 | |
98 % <AlgoDescription> | |
99 % | |
100 % 1) Check that getInfo call returned an minfo object in all cases. | |
101 % 2) Check that all plists have the correct parameters. | |
102 % | |
103 % </AlgoDescription> | |
104 | |
105 atest = true; | |
106 if stest | |
107 % <AlgoCode> | |
108 % check we have minfo objects | |
109 if isa(io, 'minfo') | |
110 %%% SET 'None' | |
111 if ~isempty(io(1).sets), atest = false; end | |
112 if ~isempty(io(1).plists), atest = false; end | |
113 %%% Check all Sets | |
114 if ~any(strcmpi(io(2).sets, 'Number of bins')), atest = false; end | |
115 if ~any(strcmpi(io(2).sets, 'Bin centres')), atest = false; end | |
116 if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end | |
117 %%%%%%%%%% SET 'Number of bins' | |
118 if io(3).plists.nparams ~= 2, atest = false; end | |
119 % Check key | |
120 if ~io(3).plists.isparam('N'), atest = false; end | |
121 if ~io(3).plists.isparam('Norm'), atest = false; end | |
122 % Check default value | |
123 if ~isequal(io(3).plists.find('N'), 10), atest = false; end | |
124 if ~isequal(io(3).plists.find('Norm'), false), atest = false; end | |
125 % Check options | |
126 if ~isequal(io(3).plists.getOptionsForParam('N'), {10}), atest = false; end | |
127 if ~isequal(io(3).plists.getOptionsForParam('Norm'), {false, true}), atest = false; end | |
128 %%%%%%%%%% SET 'Bin centres' | |
129 if io(4).plists.nparams ~= 1, atest = false; end | |
130 % Check key | |
131 if ~io(4).plists.isparam('X'), atest = false; end | |
132 % Check default value | |
133 if ~isEmptyDouble(io(3).plists.find('X')), atest = false; end | |
134 % Check options | |
135 if ~isequal(io(4).plists.getOptionsForParam('X'), {[]}), atest = false; end | |
136 end | |
137 % </AlgoCode> | |
138 else | |
139 atest = false; | |
140 end | |
141 | |
142 % Return a result structure | |
143 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
144 end % END UTP_01 | |
145 | |
146 %% UTP_02 | |
147 | |
148 % <TestDescription> | |
149 % | |
150 % Tests that the hist method works with a vector of AOs as input. | |
151 % | |
152 % </TestDescription> | |
153 function result = utp_02 | |
154 | |
155 % <SyntaxDescription> | |
156 % | |
157 % Test that the hist method works for a vector of AOs as input. | |
158 % | |
159 % </SyntaxDescription> | |
160 | |
161 try | |
162 % <SyntaxCode> | |
163 out = hist(atvec); | |
164 % </SyntaxCode> | |
165 stest = true; | |
166 catch err | |
167 disp(err.message) | |
168 stest = false; | |
169 end | |
170 | |
171 % <AlgoDescription> | |
172 % | |
173 % 1) Check that the number of elements in 'out' is the same as in 'atvec' | |
174 % 2) Check that each output AO contains the correct data. | |
175 % | |
176 % </AlgoDescription> | |
177 | |
178 atest = true; | |
179 if stest | |
180 % <AlgoCode> | |
181 % Check we have the correct number of outputs | |
182 if ~isequal(size(out), size(atvec)), atest = false; end | |
183 % Check each output against the histogram of the input | |
184 for kk=1:numel(out) | |
185 if ~isequal(hist(atvec(kk).data.getY).', out(kk).data.getY) | |
186 atest = false; | |
187 break; | |
188 end | |
189 end | |
190 % </AlgoCode> | |
191 else | |
192 atest = false; | |
193 end | |
194 | |
195 % Return a result structure | |
196 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
197 end % END UTP_02 | |
198 | |
199 %% UTP_03 | |
200 | |
201 % <TestDescription> | |
202 % | |
203 % Tests that the hist method works with a matrix of AOs as input. | |
204 % | |
205 % </TestDescription> | |
206 function result = utp_03 | |
207 | |
208 % <SyntaxDescription> | |
209 % | |
210 % Test that the hist method works for a matrix of AOs as input. | |
211 % | |
212 % </SyntaxDescription> | |
213 | |
214 try | |
215 % <SyntaxCode> | |
216 % We need a matrix without matrix cdata AOs in it | |
217 amat = [at1 at5 at6; at6 at1 at2]; | |
218 out = hist(amat); | |
219 % </SyntaxCode> | |
220 stest = true; | |
221 catch err | |
222 disp(err.message) | |
223 stest = false; | |
224 end | |
225 | |
226 % <AlgoDescription> | |
227 % | |
228 % 1) Check that the number of elements in 'out' is the same as in 'atmat' | |
229 % 2) Check that each output AO contains the correct data. | |
230 % | |
231 % </AlgoDescription> | |
232 | |
233 atest = true; | |
234 if stest | |
235 % <AlgoCode> | |
236 % Check we have the correct number of outputs | |
237 if ~isequal(size(out), size(amat)), atest = false; end | |
238 % Check each output against the histogram value of the input | |
239 for kk=1:numel(out) | |
240 if ~isequal(hist(amat(kk).data.getY).', out(kk).data.getY) | |
241 atest = false; | |
242 break; | |
243 end | |
244 end | |
245 % </AlgoCode> | |
246 else | |
247 atest = false; | |
248 end | |
249 | |
250 % Return a result structure | |
251 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
252 end % END UTP_03 | |
253 | |
254 %% UTP_04 | |
255 | |
256 % <TestDescription> | |
257 % | |
258 % Tests that the hist method works with a list of AOs as input. | |
259 % | |
260 % </TestDescription> | |
261 function result = utp_04 | |
262 | |
263 % <SyntaxDescription> | |
264 % | |
265 % Test that the hist method works for a list of AOs as input. | |
266 % | |
267 % </SyntaxDescription> | |
268 | |
269 try | |
270 % <SyntaxCode> | |
271 out = hist(at1,at2,at3); | |
272 % </SyntaxCode> | |
273 stest = true; | |
274 catch err | |
275 disp(err.message) | |
276 stest = false; | |
277 end | |
278 | |
279 % <AlgoDescription> | |
280 % | |
281 % 1) Check that the number of elements in 'out' is the same as in | |
282 % input. | |
283 % 2) Check that each output AO contains the correct data. | |
284 % | |
285 % </AlgoDescription> | |
286 | |
287 atest = true; | |
288 if stest | |
289 % <AlgoCode> | |
290 % Check we have the correct number of outputs | |
291 if numel(out) ~= 3, atest = false; end | |
292 % Check each output against the histogram value of the input | |
293 if ~isequal(hist(at1.data.getY).', out(1).data.getY), atest = false; end | |
294 if ~isequal(hist(at2.data.getY).', out(2).data.getY), atest = false; end | |
295 if ~isequal(hist(at3.data.getY).', out(3).data.getY), atest = false; end | |
296 % </AlgoCode> | |
297 else | |
298 atest = false; | |
299 end | |
300 | |
301 % Return a result structure | |
302 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
303 end % END UTP_04 | |
304 | |
305 %% UTP_05 | |
306 | |
307 % <TestDescription> | |
308 % | |
309 % Tests that the hist method works with a mix of different shaped AOs as | |
310 % input. | |
311 % | |
312 % </TestDescription> | |
313 function result = utp_05 | |
314 | |
315 % <SyntaxDescription> | |
316 % | |
317 % Test that the hist method works with an input of matrices and vectors | |
318 % and single AOs. | |
319 % | |
320 % </SyntaxDescription> | |
321 | |
322 try | |
323 % <SyntaxCode> | |
324 % We need a matrix without matrix cdata AOs in it | |
325 amat = [at1 at5 at6; at6 at1 at2]; | |
326 out = hist(at1,atvec,at2,amat,at3); | |
327 % </SyntaxCode> | |
328 stest = true; | |
329 catch err | |
330 disp(err.message) | |
331 stest = false; | |
332 end | |
333 | |
334 % <AlgoDescription> | |
335 % | |
336 % 1) Check that the number of elements in 'out' is the same as in | |
337 % input. | |
338 % 2) Check that each output AO contains the correct data. | |
339 % | |
340 % </AlgoDescription> | |
341 | |
342 atest = true; | |
343 if stest | |
344 % <AlgoCode> | |
345 % Check we have the correct number of outputs | |
346 if numel(out) ~= (3+numel(amat)+numel(atvec)), atest = false; end | |
347 % Check the first input | |
348 nout = 1; | |
349 if ~isequal(hist(at1.data.getY).', out(nout).data.getY), atest = false; end | |
350 nout = nout+1; | |
351 % Check the elements of the input vector | |
352 for jj=1:numel(atvec) | |
353 if ~isequal(hist(atvec(jj).data.getY).', out(nout).data.getY), atest = false; end | |
354 nout = nout+1; | |
355 end | |
356 % Check the 3rd input | |
357 if ~isequal(hist(at2.data.getY).', out(nout).data.getY), atest = false; end | |
358 nout = nout+1; | |
359 % Check the elements of the input matrix | |
360 for jj=1:numel(amat) | |
361 if ~isequal(hist(amat(jj).data.getY).', out(nout).data.getY), atest = false; end | |
362 nout = nout+1; | |
363 end | |
364 % Check the last input | |
365 if ~isequal(hist(at3.data.getY).', out(nout).data.getY), atest = false; end | |
366 % </AlgoCode> | |
367 else | |
368 atest = false; | |
369 end | |
370 | |
371 % Return a result structure | |
372 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
373 end % END UTP_05 | |
374 | |
375 %% UTP_06 | |
376 | |
377 % <TestDescription> | |
378 % | |
379 % Tests that the hist method properly applies history. | |
380 % | |
381 % </TestDescription> | |
382 function result = utp_06 | |
383 | |
384 % <SyntaxDescription> | |
385 % | |
386 % Test that the result of applying the hist method can be processed back | |
387 % to an m-file. | |
388 % | |
389 % </SyntaxDescription> | |
390 | |
391 try | |
392 % <SyntaxCode> | |
393 out = hist(at1); | |
394 mout = rebuild(out); | |
395 % </SyntaxCode> | |
396 stest = true; | |
397 catch err | |
398 disp(err.message) | |
399 stest = false; | |
400 end | |
401 | |
402 % <AlgoDescription> | |
403 % | |
404 % 1) Check that the last entry in the history of 'out' corresponds to | |
405 % 'hist'. | |
406 % 2) Check that the re-built object is the same object as 'out'. | |
407 % | |
408 % </AlgoDescription> | |
409 | |
410 atest = true; | |
411 if stest | |
412 % <AlgoCode> | |
413 % Check the last step in the history of 'out' | |
414 if ~strcmp(out.hist.methodInfo.mname, 'hist'), atest = false; end | |
415 % Check the re-built object | |
416 if ~eq(mout, out, ple4), atest = false; end | |
417 % </AlgoCode> | |
418 else | |
419 atest = false; | |
420 end | |
421 | |
422 % Return a result structure | |
423 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
424 end % END UTP_06 | |
425 | |
426 %% UTP_07 | |
427 | |
428 % <TestDescription> | |
429 % | |
430 % Tests that the hist method can modify the input AO. | |
431 % | |
432 % </TestDescription> | |
433 function result = utp_07 | |
434 | |
435 % <SyntaxDescription> | |
436 % | |
437 % Test that the hist method can modify the input AO by calling with no | |
438 % output. Remark that the command at1.hist() doesn't call the histogram | |
439 % method because an AO have a property with the name 'hist'. Thus the | |
440 % command at1.hist returns the value in the property hist. | |
441 % | |
442 % </SyntaxDescription> | |
443 | |
444 try | |
445 % <SyntaxCode> | |
446 % copy at1 to work with | |
447 ain = ao(at1); | |
448 % modify ain | |
449 aout = hist(ain); | |
450 hist(ain); | |
451 % </SyntaxCode> | |
452 stest = true; | |
453 catch err | |
454 disp(err.message) | |
455 stest = false; | |
456 end | |
457 | |
458 % <AlgoDescription> | |
459 % | |
460 % 1) Check that 'at1' and 'ain' are now different. | |
461 % 2) Check that 'ain' is hist(at1). | |
462 % | |
463 % </AlgoDescription> | |
464 | |
465 atest = true; | |
466 if stest | |
467 % <AlgoCode> | |
468 % Check that hist modified the input by comparing to the copy | |
469 if eq(ao(at1), ain, ple1), atest = false; end | |
470 % Check that hist doesn't modified the input for the function notation | |
471 if ~eq(aout, ain, ple1), atest = false; end | |
472 % Check that the modified input is the hist of the copy | |
473 [n, x] = hist(at1.y, 10); | |
474 if ~isequal(n.', ain.y), atest = false; end | |
475 if ~isequal(x.', ain.x), atest = false; end | |
476 % </AlgoCode> | |
477 else | |
478 atest = false; | |
479 end | |
480 | |
481 % Return a result structure | |
482 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
483 end % END UTP_07 | |
484 | |
485 %% UTP_08 | |
486 | |
487 % <TestDescription> | |
488 % | |
489 % Tests that the hist method keeps the data shape of the input object. | |
490 % | |
491 % </TestDescription> | |
492 function result = utp_08 | |
493 | |
494 % <SyntaxDescription> | |
495 % | |
496 % Test that the hist method keeps the data shape of the input object. The | |
497 % input AO must be an AO with row data and an AO with column data. | |
498 % | |
499 % </SyntaxDescription> | |
500 | |
501 try | |
502 % <SyntaxCode> | |
503 out1 = hist(at5); | |
504 out2 = hist(at6); | |
505 % </SyntaxCode> | |
506 stest = true; | |
507 catch err | |
508 disp(err.message) | |
509 stest = false; | |
510 end | |
511 | |
512 % <AlgoDescription> | |
513 % | |
514 % 1) Check that the shpe of the data doesn't change. | |
515 % | |
516 % </AlgoDescription> | |
517 | |
518 atest = true; | |
519 if stest | |
520 % <AlgoCode> | |
521 % Check the shape of the output data | |
522 if size(out1.data.y) ~= size(at5.data.y), atest = false; end | |
523 if size(out2.data.y) ~= size(at6.data.y), atest = false; end | |
524 % </AlgoCode> | |
525 else | |
526 atest = false; | |
527 end | |
528 | |
529 % Return a result structure | |
530 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
531 end % END UTP_08 | |
532 | |
533 %% UTP_09 | |
534 | |
535 % <TestDescription> | |
536 % | |
537 % Check that the hist method pass back the output objects to a list of | |
538 % output variables or to a single variable. | |
539 % | |
540 % </TestDescription> | |
541 function result = utp_09 | |
542 | |
543 % <SyntaxDescription> | |
544 % | |
545 % Call the method with a list of output variables and with a single output | |
546 % variable. Additionaly check that the rebuild method works on the output. | |
547 % | |
548 % </SyntaxDescription> | |
549 | |
550 try | |
551 % <SyntaxCode> | |
552 [o1, o2] = hist(at5, at6); | |
553 o3 = hist(at5, at6); | |
554 mout1 = rebuild(o1); | |
555 mout2 = rebuild(o2); | |
556 mout3 = rebuild(o3); | |
557 % </SyntaxCode> | |
558 stest = true; | |
559 catch err | |
560 disp(err.message) | |
561 stest = false; | |
562 end | |
563 | |
564 % <AlgoDescription> | |
565 % | |
566 % 1) Check that the output contains the right number of objects | |
567 % 2) Check that the 'rebuild' method produces the same object as 'out'. | |
568 % | |
569 % </AlgoDescription> | |
570 | |
571 atest = true; | |
572 if stest | |
573 % <AlgoCode> | |
574 % Check the number of outputs | |
575 if numel(o1) ~=1, atest = false; end | |
576 if numel(o2) ~=1, atest = false; end | |
577 if numel(o3) ~=2, atest = false; end | |
578 % Check the rebuilding of the object | |
579 if ~eq(o1, mout1, ple2), atest = false; end | |
580 if ~eq(o2, mout2, ple2), atest = false; end | |
581 if ~eq(o3, mout3, ple2), atest = false; end | |
582 % </AlgoCode> | |
583 else | |
584 atest = false; | |
585 end | |
586 | |
587 % Return a result structure | |
588 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
589 end % END UTP_09 | |
590 | |
591 %% UTP_10 | |
592 | |
593 % <TestDescription> | |
594 % | |
595 % Control the method with a plist. | |
596 % | |
597 % </TestDescription> | |
598 function result = utp_10 | |
599 | |
600 % <SyntaxDescription> | |
601 % | |
602 % Test the hist method with a factor and an offset. | |
603 % | |
604 % </SyntaxDescription> | |
605 | |
606 try | |
607 % <SyntaxCode> | |
608 N = 10; | |
609 pl = plist('N', N); | |
610 out = hist(at5, pl); | |
611 mout = rebuild(out); | |
612 % </SyntaxCode> | |
613 stest = true; | |
614 catch err | |
615 disp(err.message) | |
616 stest = false; | |
617 end | |
618 | |
619 % <AlgoDescription> | |
620 % | |
621 % 1) Check that the hist method with defined number of bins | |
622 % 2) Check that the re-built object is the same object as 'out'. | |
623 % | |
624 % </AlgoDescription> | |
625 | |
626 atest = true; | |
627 if stest | |
628 % <AlgoCode> | |
629 [n, x] = hist(at5.y, N); | |
630 if ~isequal(n.', out.y), atest = false; end | |
631 if ~isequal(x.', out.x), atest = false; end | |
632 if ~isequal(length(out.y), N), atest = false; end | |
633 % Check the re-built object | |
634 if ~eq(mout, out, ple2), atest = false; end | |
635 % </AlgoCode> | |
636 else | |
637 atest = false; | |
638 end | |
639 | |
640 % Return a result structure | |
641 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
642 end % END UTP_10 | |
643 | |
644 %% UTP_12 | |
645 | |
646 % <TestDescription> | |
647 % | |
648 % Control the method with a plist. | |
649 % | |
650 % </TestDescription> | |
651 function result = utp_12 | |
652 | |
653 % <SyntaxDescription> | |
654 % | |
655 % Test the hist method with a factor and an offset. | |
656 % | |
657 % </SyntaxDescription> | |
658 | |
659 try | |
660 % <SyntaxCode> | |
661 X = [-1.5:.1:1.5]; | |
662 pl = plist('X', X); | |
663 out = hist(at5, pl); | |
664 mout = rebuild(out); | |
665 % </SyntaxCode> | |
666 stest = true; | |
667 catch err | |
668 disp(err.message) | |
669 stest = false; | |
670 end | |
671 | |
672 % <AlgoDescription> | |
673 % | |
674 % 1) Check that the hist method with set of bin centers | |
675 % 2) Check that the re-built object is the same object as 'out'. | |
676 % | |
677 % </AlgoDescription> | |
678 | |
679 atest = true; | |
680 if stest | |
681 % <AlgoCode> | |
682 [n, x] = hist(at5.y, X); | |
683 if ~isequal(n.', out.y), atest = false; end | |
684 if ~isequal(x.', out.x), atest = false; end | |
685 if ~isequal(X.', out.x), atest = false; end | |
686 if ~isequal(length(out.y), length(X)), atest = false; end | |
687 % Check the re-built object | |
688 if ~eq(mout, out, ple2), atest = false; end | |
689 % </AlgoCode> | |
690 else | |
691 atest = false; | |
692 end | |
693 | |
694 % Return a result structure | |
695 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
696 end % END UTP_12 | |
697 | |
698 end |