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