comparison testing/utp_1.1/utps/ao/utp_ao_save.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_SAVE a set of UTPs for the ao/save method
2 %
3 % M Hewitson 06-08-08
4 %
5 % $Id: utp_ao_save.m,v 1.10 2010/08/31 09:36:46 hewitson Exp $
6 %
7
8 % <MethodDescription>
9 %
10 % The save method of the ao class saves an analysis object to disk. Save stores
11 % the variables in a MATLAB formatted file (MAT-file) named filename.mat or in a
12 % XML fromat named filename.xml
13 %
14 % </MethodDescription>
15
16 function results = utp_ao_save(varargin)
17
18 % Check the inputs
19 if nargin == 0
20
21 % Some keywords
22 class = 'ao';
23 mthd = 'save';
24
25 results = [];
26 disp('******************************************************');
27 disp(['**** Running UTPs for ' class '/' mthd]);
28 disp('******************************************************');
29
30 % Test AOs
31 [at1,at2,at3,at4,at5,at6,atvec,atmat] = eval(['get_test_objects_' class]);
32
33 % Exception list for the UTPs:
34 [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();
35
36 % Run the tests
37 results = [results utp_01]; % getInfo call
38 results = [results utp_02]; % Vector input
39 results = [results utp_03]; % Matrix input
40 results = [results utp_04]; % List input
41 results = [results utp_05]; % Test with mixed input
42 results = [results utp_06]; % Test history is working
43 results = [results utp_07]; % Test the modify call works
44 results = [results utp_08]; % Test plist contains the filename
45 results = [results utp_09]; % Test object with complex data
46 results = [results utp_10]; % Test AO which is build from a pzmodel
47 results = [results utp_11]; % Test AO which is build from complex plist
48
49 disp('Done.');
50 disp('******************************************************');
51
52 elseif nargin == 1 % Check for UTP functions
53 if strcmp(varargin{1}, 'isutp')
54 results = 1;
55 else
56 results = 0;
57 end
58 else
59 error('### Incorrect inputs')
60 end
61
62 %% UTP_01
63
64 % <TestDescription>
65 %
66 % Tests that the getInfo call works for this method.
67 %
68 % </TestDescription>
69 function result = utp_01
70
71
72 % <SyntaxDescription>
73 %
74 % Test that the getInfo call works for no sets, all sets, and each set
75 % individually.
76 %
77 % </SyntaxDescription>
78
79 try
80 % <SyntaxCode>
81 % Call for no sets
82 io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']);
83 % Call for all sets
84 io(2) = eval([class '.getInfo(''' mthd ''')']);
85 % Call for each set
86 for kk=1:numel(io(2).sets)
87 io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']);
88 end
89 % </SyntaxCode>
90 stest = true;
91 catch err
92 disp(err.message)
93 stest = false;
94 end
95
96 % <AlgoDescription>
97 %
98 % 1) Check that getInfo call returned an minfo object in all cases.
99 % 2) Check that all plists have the correct parameters.
100 %
101 % </AlgoDescription>
102
103 atest = true;
104 if stest
105 % <AlgoCode>
106 % check we have minfo objects
107 if isa(io, 'minfo')
108 %%% SET 'None'
109 if ~isempty(io(1).sets), atest = false; end
110 if ~isempty(io(1).plists), atest = false; end
111 %%% Check all Sets
112 if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end
113 if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
114 %%%%%%%%%% SET 'Default'
115 if io(3).plists.nparams ~= 4, atest = false; end
116 % Check key
117 if ~io(3).plists.isparam('filename'), atest = false; end
118 if ~io(3).plists.isparam('prefix'), atest = false; end
119 if ~io(3).plists.isparam('postfix'), atest = false; end
120 if ~io(3).plists.isparam('individual files'), atest = false; end
121 % Check default value
122 if ~isequal(io(3).plists.find('filename'), ''), atest = false; end
123 if ~isequal(io(3).plists.find('prefix'), ''), atest = false; end
124 if ~isequal(io(3).plists.find('postfix'), ''), atest = false; end
125 if ~isequal(io(3).plists.find('individual files'), false), atest = false; end
126 % Check options
127 if ~isequal(io(3).plists.getOptionsForParam('filename'), {[]}), atest = false; end
128 end
129 % </AlgoCode>
130 else
131 atest = false;
132 end
133
134 % Return a result structure
135 result = utp_prepare_result(atest, stest, dbstack, mfilename);
136 end % END UTP_01
137
138 %% UTP_02
139
140 % <TestDescription>
141 %
142 % Tests that the save method works with a vector of AOs as input.
143 %
144 % </TestDescription>
145 function result = utp_02
146
147 % <SyntaxDescription>
148 %
149 % Test that the save method works for a vector of AOs as input. Test both
150 % formats 'xml' and 'mat'.
151 %
152 % </SyntaxDescription>
153
154 try
155 % <SyntaxCode>
156 save(atvec, 'test.xml');
157 save(atvec, 'test.mat');
158 out1 = ao('test.xml');
159 out2 = ao('test.mat');
160 % </SyntaxCode>
161 stest = true;
162 catch err
163 disp(err.message)
164 stest = false;
165 end
166
167 % <AlgoDescription>
168 %
169 % 1) Check that the number of elements in 'out1' and 'out2' are the same
170 % as in 'atvec'
171 % 2) Check that the loaded objects are the same as the saved objects except
172 % the the history because the load-constructor adds one history step.
173 % 3) The outputs 'out1' and 'out2' must be the same except the history
174 % properties 'methodInfo', 'plistUsed' and 'proctime'.
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(out1), size(atvec)), atest = false; end
183 if ~isequal(size(out2), size(atvec)), atest = false; end
184 % Check each output against the input
185 for kk=1:numel(out1)
186 if ~eq(atvec(kk), out1(kk), ple1), atest = false; end
187 if ~eq(atvec(kk), out2(kk), ple1), atest = false; end
188 end
189 % Compare the outputs
190 if ~eq(out1, out2, ple1), atest = false; end
191 % </AlgoCode>
192 delete('test.xml');
193 delete('test.mat');
194 else
195 atest = false;
196 end
197
198 % Return a result structure
199 result = utp_prepare_result(atest, stest, dbstack, mfilename);
200 end % END UTP_02
201
202 %% UTP_03
203
204 % <TestDescription>
205 %
206 % Tests that the save method works with a matrix of AOs as input.
207 %
208 % </TestDescription>
209 function result = utp_03
210
211 % <SyntaxDescription>
212 %
213 % Test that the save method works for a matrix of AOs as input. Test both
214 % formats 'xml' and 'mat'.
215 %
216 % </SyntaxDescription>
217
218 try
219 % <SyntaxCode>
220 save(atmat, 'test.xml');
221 save(atmat, 'test.mat');
222 out1 = ao('test.xml');
223 out2 = ao('test.mat');
224 % </SyntaxCode>
225 stest = true;
226 catch err
227 disp(err.message)
228 stest = false;
229 end
230
231 % <AlgoDescription>
232 %
233 % 1) Check that the number of elements in 'out1' and 'out2' are the same
234 % as in 'atmat'
235 % 2) Check that the loaded objects are the same as the saved objects except
236 % the the history because the load-constructor adds one history step.
237 % 3) The outputs 'out1' and 'out2' must be the same except the history
238 % properties 'methodInfo', 'plistUsed' and 'proctime'.
239 %
240 % </AlgoDescription>
241
242 atest = true;
243 if stest
244 % <AlgoCode>
245 % Check we have the correct number of outputs
246 if ~isequal(size(out1), size(atmat)), atest = false; end
247 if ~isequal(size(out2), size(atmat)), atest = false; end
248 % Check each output against the input
249 for kk=1:numel(out1)
250 if ~eq(atmat(kk), out1(kk), ple1), atest = false; end
251 if ~eq(atmat(kk), out2(kk), ple1), atest = false; end
252 end
253 % Compare the outputs
254 if ~eq(out1, out2, ple1), atest = false; end
255 % </AlgoCode>
256 delete('test.xml');
257 delete('test.mat');
258 else
259 atest = false;
260 end
261
262 % Return a result structure
263 result = utp_prepare_result(atest, stest, dbstack, mfilename);
264 end % END UTP_03
265
266 %% UTP_04
267
268 % <TestDescription>
269 %
270 % Tests that the save method works with a list of AOs as input.
271 %
272 % </TestDescription>
273 function result = utp_04
274
275 % <SyntaxDescription>
276 %
277 % Test that the save method works for a list of AOs as input. Test both
278 % formats 'xml' and 'mat'.
279 %
280 % </SyntaxDescription>
281
282 try
283 % <SyntaxCode>
284 save(at1, at2, at3, 'test.xml');
285 save(at1, at2, at3, 'test.mat');
286 out1 = ao('test.xml');
287 out2 = ao('test.mat');
288 % </SyntaxCode>
289 stest = true;
290 catch err
291 disp(err.message)
292 stest = false;
293 end
294
295 % <AlgoDescription>
296 %
297 % 1) Check that the number of elements in 'out1' and 'out2' are the same
298 % as in the list
299 % 2) Check that the loaded objects are the same as the saved objects except
300 % the the history because the load-constructor adds one history step.
301 % 3) The outputs 'out1' and 'out2' must be the same except the history
302 % properties 'methodInfo', 'plistUsed' and 'proctime'.
303 %
304 % </AlgoDescription>
305
306 atest = true;
307 aoin = [at1, at2, at3];
308 if stest
309 % <AlgoCode>
310 % Check we have the correct number of outputs
311 if numel(out1) ~= 3, atest = false; end
312 if numel(out2) ~= 3, atest = false; end
313 % Check each output against the input
314 for kk=1:numel(out1)
315 if ~eq(aoin(kk), out1(kk), ple1), atest = false; end
316 if ~eq(aoin(kk), out2(kk), ple1), atest = false; end
317 end
318 % Compare the outputs
319 if ~eq(out1, out2, ple1), atest = false; end
320 % </AlgoCode>
321 delete('test.xml');
322 delete('test.mat');
323 else
324 atest = false;
325 end
326
327 % Return a result structure
328 result = utp_prepare_result(atest, stest, dbstack, mfilename);
329 end % END UTP_04
330
331 %% UTP_05
332
333 % <TestDescription>
334 %
335 % Tests that the save method works with a mix of different shaped AOs as
336 % input.
337 %
338 % </TestDescription>
339 function result = utp_05
340
341 % <SyntaxDescription>
342 %
343 % Test that the save method works with an input of matrices and vectors
344 % and single AOs. Test both formats 'xml' and 'mat'.
345 %
346 % </SyntaxDescription>
347
348 try
349 % <SyntaxCode>
350 save(at1,atvec,at2, 'test.xml');
351 save(at1,atvec,at2, 'test.mat');
352 out1 = ao('test.xml');
353 out2 = ao('test.mat');
354 % </SyntaxCode>
355 stest = true;
356 catch err
357 disp(err.message)
358 stest = false;
359 end
360
361 % <AlgoDescription>
362 %
363 % 1) Check that the number of elements in 'out' is the same as in
364 % input.
365 % 2) Check that each output AO contains the correct data.
366 %
367 % </AlgoDescription>
368
369 atest = true;
370 aoin = [at1, reshape(atvec, 1, []), at2];
371 if stest
372 % <AlgoCode>
373 % Check we have the correct number of outputs
374 if numel(out1) ~= 2+numel(atvec), atest = false; end
375 if numel(out2) ~= 2+numel(atvec), atest = false; end
376 % Check each output against the input
377 for kk=1:numel(out1)
378 if ~eq(aoin(kk), out1(kk), ple1), atest = false; end
379 if ~eq(aoin(kk), out2(kk), ple1), atest = false; end
380 end
381 % Compare the outputs
382 if ~eq(out1, out2, ple1), atest = false; end
383 % </AlgoCode>
384 delete('test.xml');
385 delete('test.mat');
386 else
387 atest = false;
388 end
389
390 % Return a result structure
391 result = utp_prepare_result(atest, stest, dbstack, mfilename);
392 end % END UTP_05
393
394 %% UTP_06
395
396 % <TestDescription>
397 %
398 % Tests that the save method properly applies history.
399 %
400 % </TestDescription>
401 function result = utp_06
402
403 % <SyntaxDescription>
404 %
405 % Test that the result of applying the save method can be processed back
406 % to an m-file. Do this for both extensions 'mat' and 'xml'
407 %
408 % </SyntaxDescription>
409
410 try
411 % <SyntaxCode>
412 out1 = save(at4, 'test.xml');
413 out2 = save(at2, 'test.mat');
414 mout1 = rebuild(out1);
415 mout2 = rebuild(out2);
416 % </SyntaxCode>
417 stest = true;
418 catch err
419 disp(err.message)
420 stest = false;
421 end
422
423 % <AlgoDescription>
424 %
425 % 1) Check that save doesn't add a history step.
426 % 2) Check that the read object is the same as the stored object.
427 % save + load doesn't add history.
428 % 3) Check that the re-built object is the same object as 'out'.
429 %
430 % </AlgoDescription>
431
432 atest = true;
433 if stest
434 % <AlgoCode>
435 % The last history step is the save method
436 if strcmp(out1.hist.methodInfo.mname, 'save'), atest = false; end
437 if strcmp(out2.hist.methodInfo.mname, 'save'), atest = false; end
438 % check the history steps of the read object (load + save)
439 outr1 = ao('test.xml');
440 outr2 = ao('test.mat');
441 if strcmp(outr1.hist.methodInfo.mname, 'ao'), atest = false; end
442 if strcmp(outr2.hist.methodInfo.mname, 'ao'), atest = false; end
443 % Check the re-built object
444 if ~eq(mout1, out1, ple2), atest = false; end
445 if ~eq(mout2, out2, ple2), atest = false; end
446 % </AlgoCode>
447 % delete test file
448 delete('test.xml')
449 delete('test.mat')
450 else
451 atest = false;
452 end
453
454 % Return a result structure
455 result = utp_prepare_result(atest, stest, dbstack, mfilename);
456 end % END UTP_06
457
458 %% UTP_07
459
460 % <TestDescription>
461 %
462 % Tests that the save method works with the modify command.
463 %
464 % </TestDescription>
465 function result = utp_07
466
467 % <SyntaxDescription>
468 %
469 % Test that the modify command.
470 %
471 % </SyntaxDescription>
472
473 try
474 % <SyntaxCode>
475 % copy at1 to work with
476 aa_mat = ao(at1);
477 aa_mat.save('test.mat');
478 aa_xml = ao(at1);
479 aa_xml.save('test.xml');
480 out1 = ao('test.mat');
481 out2 = ao('test.xml');
482 % </SyntaxCode>
483 stest = true;
484 catch err
485 disp(err.message)
486 stest = false;
487 end
488
489 % <AlgoDescription>
490 %
491 % 1) Check that the save method applies the history.
492 % 2) Check the output against the input except the history.
493 % 3) Check the history of the output against the input.
494 %
495 % </AlgoDescription>
496
497 atest = true;
498 if stest
499 % <AlgoCode>
500 % Check that the modified object have the 'save' method as the last
501 % history step
502 if strcmp(aa_xml.hist.methodInfo.mname, 'save'), atest = false; end
503 if strcmp(aa_mat.hist.methodInfo.mname, 'save'), atest = false; end
504 % Check the output without the history
505 if ~eq(aa_mat, out1, ple1), atest = false; end
506 if ~eq(aa_xml, out2, ple1), atest = false; end
507 % Compare the outputs
508 if ~eq(out1, out2, ple1), atest = false; end
509 % </AlgoCode>
510 delete('test.xml');
511 delete('test.mat');
512 else
513 atest = false;
514 end
515
516 % Return a result structure
517 result = utp_prepare_result(atest, stest, dbstack, mfilename);
518 end % END UTP_07
519
520 %% UTP_08
521
522 % <TestDescription>
523 %
524 % Control the method with a plist.
525 %
526 % </TestDescription>
527 function result = utp_08
528
529 % <SyntaxDescription>
530 %
531 % Test that the save method uses the filename which is stored in a plist.
532 %
533 % </SyntaxDescription>
534
535 try
536 % <SyntaxCode>
537 pl1 = plist('filename', 'test.mat');
538 pl2 = plist('filename', 'test.xml');
539 save(at5, pl1);
540 save(at5, pl2);
541 out1 = ao('test.mat');
542 out2 = ao('test.xml');
543 % </SyntaxCode>
544 stest = true;
545 catch err
546 disp(err.message)
547 stest = false;
548 end
549
550 % <AlgoDescription>
551 %
552 % 1) Check the output
553 %
554 % </AlgoDescription>
555
556 atest = true;
557 if stest
558 % <AlgoCode>
559 % Check the output
560 if ~eq(at5, out1, ple1), atest = false; end
561 if ~eq(at5, out2, ple1), atest = false; end
562 % Compare the outputs
563 if ~eq(out1, out2, ple1), atest = false; end
564 % </AlgoCode>
565 delete('test.xml');
566 delete('test.mat');
567 else
568 atest = false;
569 end
570
571 % Return a result structure
572 result = utp_prepare_result(atest, stest, dbstack, mfilename);
573 end % END UTP_08
574
575 %% UTP_09
576
577 % <TestDescription>
578 %
579 % Test the save method with an AO with complex data.
580 %
581 % </TestDescription>
582 function result = utp_09
583
584 % <SyntaxDescription>
585 %
586 % Save an AO with complex fsdata object.
587 %
588 % </SyntaxDescription>
589
590 try
591 % <SyntaxCode>
592 % Create an AO with complex fsdata
593 aa = ao(1:123, randn(123,1)+randn(123,1)*i, 1);
594 aa.setT0(12345);
595 save(aa, 'test.mat');
596 save(aa, 'test.xml');
597 out1 = ao('test.mat');
598 out2 = ao('test.xml');
599 % </SyntaxCode>
600 stest = true;
601 catch err
602 disp(err.message)
603 stest = false;
604 end
605
606 % <AlgoDescription>
607 %
608 % 1) Check the output
609 %
610 % </AlgoDescription>
611
612 atest = true;
613 if stest
614 % <AlgoCode>
615 % Check the output
616 if ~eq(aa, out1, ple1), atest = false; end
617 if ~eq(aa, out2, ple1), atest = false; end
618 % Compare the outputs
619 if ~eq(out1, out2, ple1), atest = false; end
620 % </AlgoCode>
621 delete('test.xml');
622 delete('test.mat');
623 else
624 atest = false;
625 end
626
627 % Return a result structure
628 result = utp_prepare_result(atest, stest, dbstack, mfilename);
629 end % END UTP_09
630
631 %% UTP_10
632
633 % <TestDescription>
634 %
635 % Test the save method with an AO which is created from a pole/zero model
636 %
637 % </TestDescription>
638 function result = utp_10
639
640 % <SyntaxDescription>
641 %
642 % Save an AO which is created from a pzmodel.
643 %
644 % </SyntaxDescription>
645
646 try
647 % <SyntaxCode>
648 % Create an AO with complex fsdata
649 aa = ao(plist('pzmodel', pzmodel(1,{[1 4], 2}, 3), 'Nsecs', 10, 'fs', 37));
650 save(aa, 'test.mat');
651 save(aa, 'test.xml');
652 out1 = ao('test.mat');
653 out2 = ao('test.xml');
654 % </SyntaxCode>
655 stest = true;
656 catch err
657 disp(err.message)
658 stest = false;
659 end
660
661 % <AlgoDescription>
662 %
663 % 1) Check the output
664 %
665 % </AlgoDescription>
666
667 atest = true;
668 if stest
669 % <AlgoCode>
670 % Check the output
671 if ~eq(aa, out1, ple1), atest = false; end
672 if ~eq(aa, out2, ple1), atest = false; end
673 % Compare the outputs
674 if ~eq(out1, out2, ple1), atest = false; end
675 % </AlgoCode>
676 delete('test.xml');
677 delete('test.mat');
678 else
679 atest = false;
680 end
681
682 % Return a result structure
683 result = utp_prepare_result(atest, stest, dbstack, mfilename);
684 end % END UTP_10
685
686 %% UTP_11
687
688 % <TestDescription>
689 %
690 % Test the save method with a complex plist which builds the AO
691 %
692 % </TestDescription>
693 function result = utp_11
694
695 % <SyntaxDescription>
696 %
697 % Save an AO which is created from a complex plist.
698 %
699 % </SyntaxDescription>
700
701 try
702 % <SyntaxCode>
703 % Create an AO with complex fsdata
704 win = specwin('Hanning');
705 pl = plist('A', {'1', 2, sym('t')}, 'B', sym([1 2; 3 4]));
706 pli = plist('vals', randn(123)+randn(123)*i, 'pl', pl, 'win', win);
707 aa = ao(pli);
708 save(aa, 'test.mat');
709 save(aa, 'test.xml');
710 out1 = ao('test.mat');
711 out2 = ao('test.xml');
712 % </SyntaxCode>
713 stest = true;
714 catch err
715 disp(err.message)
716 stest = false;
717 end
718
719 % <AlgoDescription>
720 %
721 % 1) Check the output
722 %
723 % </AlgoDescription>
724
725 atest = true;
726 if stest
727 % <AlgoCode>
728 % Check the output
729 if ~eq(aa, out1, ple1), atest = false; end
730 if ~eq(aa, out2, ple1), atest = false; end
731 % Compare the outputs
732 if ~eq(out1, out2, ple1), atest = false; end
733 % </AlgoCode>
734 delete('test.xml');
735 delete('test.mat');
736 else
737 atest = false;
738 end
739
740 % Return a result structure
741 result = utp_prepare_result(atest, stest, dbstack, mfilename);
742 end % END UTP_11
743
744 end