Mercurial > hg > ltpda
comparison testing/utp_1.1/utps/parfrac/utp_parfrac_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_PARFRAC_SAVE a set of UTPs for the parfrac/save method | |
2 % | |
3 % M Hewitson 06-08-08 | |
4 % | |
5 % $Id: utp_parfrac_save.m,v 1.7 2010/08/31 09:43:48 hewitson Exp $ | |
6 % | |
7 | |
8 % <MethodDescription> | |
9 % | |
10 % The save method of the parfrac class saves a parfrac object to disk. Save | |
11 % stores the variables in a MATLAB formatted file (MAT-file) named | |
12 % filename.mat or in a XML fromat named filename.xml | |
13 % | |
14 % </MethodDescription> | |
15 | |
16 function results = utp_parfrac_save(varargin) | |
17 | |
18 % Check the inputs | |
19 if nargin == 0 | |
20 | |
21 % Some keywords | |
22 class = 'parfrac'; | |
23 mthd = 'save'; | |
24 | |
25 results = []; | |
26 disp('******************************************************'); | |
27 disp(['**** Running UTPs for ' class '/' mthd]); | |
28 disp('******************************************************'); | |
29 | |
30 % Test PARFRAC objects | |
31 [pf1,pf2,pf3,pfv,pfm] = get_test_objects_parfrac; | |
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 different complex PARFRAC objects | |
46 | |
47 disp('Done.'); | |
48 disp('******************************************************'); | |
49 | |
50 elseif nargin == 1 % Check for UTP functions | |
51 if strcmp(varargin{1}, 'isutp') | |
52 results = 1; | |
53 else | |
54 results = 0; | |
55 end | |
56 else | |
57 error('### Incorrect inputs') | |
58 end | |
59 | |
60 %% UTP_01 | |
61 | |
62 % <TestDescription> | |
63 % | |
64 % Tests that the getInfo call works for this method. | |
65 % | |
66 % </TestDescription> | |
67 function result = utp_01 | |
68 | |
69 | |
70 % <SyntaxDescription> | |
71 % | |
72 % Test that the getInfo call works for no sets, all sets, and each set | |
73 % individually. | |
74 % | |
75 % </SyntaxDescription> | |
76 | |
77 try | |
78 % <SyntaxCode> | |
79 % Call for no sets | |
80 io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']); | |
81 % Call for all sets | |
82 io(2) = eval([class '.getInfo(''' mthd ''')']); | |
83 % Call for each set | |
84 for kk=1:numel(io(2).sets) | |
85 io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']); | |
86 end | |
87 % </SyntaxCode> | |
88 stest = true; | |
89 catch err | |
90 disp(err.message) | |
91 stest = false; | |
92 end | |
93 | |
94 % <AlgoDescription> | |
95 % | |
96 % 1) Check that getInfo call returned an minfo object in all cases. | |
97 % 2) Check that all plists have the correct parameters. | |
98 % | |
99 % </AlgoDescription> | |
100 | |
101 atest = true; | |
102 if stest | |
103 % <AlgoCode> | |
104 % check we have minfo objects | |
105 if isa(io, 'minfo') | |
106 %%% SET 'None' | |
107 if ~isempty(io(1).sets), atest = false; end | |
108 if ~isempty(io(1).plists), atest = false; end | |
109 %%% Check all Sets | |
110 if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end | |
111 if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end | |
112 %%%%%%%%%% SET 'Default' | |
113 if io(3).plists.nparams ~= 4, atest = false; end | |
114 % Check key | |
115 if ~io(3).plists.isparam('filename'), atest = false; end | |
116 if ~io(3).plists.isparam('prefix'), atest = false; end | |
117 if ~io(3).plists.isparam('postfix'), atest = false; end | |
118 if ~io(3).plists.isparam('individual files'), atest = false; end | |
119 % Check default value | |
120 if ~isequal(io(3).plists.find('filename'), ''), atest = false; end | |
121 if ~isequal(io(3).plists.find('prefix'), ''), atest = false; end | |
122 if ~isequal(io(3).plists.find('postfix'), ''), atest = false; end | |
123 if ~isequal(io(3).plists.find('individual files'), false), atest = false; end | |
124 % Check options | |
125 if ~isequal(io(3).plists.getOptionsForParam('filename'), {[]}), atest = false; end | |
126 end | |
127 % </AlgoCode> | |
128 else | |
129 atest = false; | |
130 end | |
131 | |
132 % Return a result structure | |
133 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
134 end % END UTP_01 | |
135 | |
136 %% UTP_02 | |
137 | |
138 % <TestDescription> | |
139 % | |
140 % Tests that the save method works with a vector of PARFRAC objects as input. | |
141 % | |
142 % </TestDescription> | |
143 function result = utp_02 | |
144 | |
145 % <SyntaxDescription> | |
146 % | |
147 % Test that the save method works for a vector of PARFRAC objects as input. | |
148 % Test both formats 'xml' and 'mat'. | |
149 % | |
150 % </SyntaxDescription> | |
151 | |
152 try | |
153 % <SyntaxCode> | |
154 save(pfv, 'test.xml'); | |
155 save(pfv, 'test.mat'); | |
156 out1 = parfrac('test.xml'); | |
157 out2 = parfrac('test.mat'); | |
158 % </SyntaxCode> | |
159 stest = true; | |
160 catch err | |
161 disp(err.message) | |
162 stest = false; | |
163 end | |
164 | |
165 % <AlgoDescription> | |
166 % | |
167 % 1) Check that the number of elements in 'out1' and 'out2' are the | |
168 % same as in 'pfv' | |
169 % 2) Check that the loaded objects are the same as the saved objects. | |
170 % 3) The outputs 'out1' and 'out2' must be the same. | |
171 % | |
172 % </AlgoDescription> | |
173 | |
174 atest = true; | |
175 if stest | |
176 % <AlgoCode> | |
177 % Check we have the correct number of outputs | |
178 if ~isequal(size(out1), size(pfv)), atest = false; end | |
179 if ~isequal(size(out2), size(pfv)), atest = false; end | |
180 % Check each output against the input | |
181 for kk=1:numel(out1) | |
182 if ~eq(pfv(kk), out1(kk), ple1), atest = false; end | |
183 if ~eq(pfv(kk), out2(kk), ple1), atest = false; end | |
184 end | |
185 % Compare the outputs | |
186 if ~eq(out1, out2, ple2), atest = false; end | |
187 % </AlgoCode> | |
188 delete('test.xml'); | |
189 delete('test.mat'); | |
190 else | |
191 atest = false; | |
192 end | |
193 | |
194 % Return a result structure | |
195 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
196 end % END UTP_02 | |
197 | |
198 %% UTP_03 | |
199 | |
200 % <TestDescription> | |
201 % | |
202 % Tests that the save method works with a matrix of PARFRAC objects as | |
203 % input. | |
204 % | |
205 % </TestDescription> | |
206 function result = utp_03 | |
207 | |
208 % <SyntaxDescription> | |
209 % | |
210 % Test that the save method works for a matrix of PARFRAC objects as | |
211 % input. Test both formats 'xml' and 'mat'. | |
212 % | |
213 % </SyntaxDescription> | |
214 | |
215 try | |
216 % <SyntaxCode> | |
217 save(pfm, 'test.xml'); | |
218 save(pfm, 'test.mat'); | |
219 out1 = parfrac('test.xml'); | |
220 out2 = parfrac('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 | |
231 % same as in 'pfm' | |
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(pfm)), atest = false; end | |
242 if ~isequal(size(out2), size(pfm)), atest = false; end | |
243 % Check each output against the input | |
244 for kk=1:numel(out1) | |
245 if ~eq(pfm(kk), out1(kk), ple1), atest = false; end | |
246 if ~eq(pfm(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 PARFRAC 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 PARFRAC objects as input. | |
273 % Test both formats 'xml' and 'mat'. | |
274 % | |
275 % </SyntaxDescription> | |
276 | |
277 try | |
278 % <SyntaxCode> | |
279 save(pf1, pf2, pf3, 'test.xml'); | |
280 save(pf1, pf2, pf3, 'test.mat'); | |
281 out1 = parfrac('test.xml'); | |
282 out2 = parfrac('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 | |
293 % same 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 pfin = [pf1, pf2, pf3]; | |
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(pfin(kk), out1(kk), ple1), atest = false; end | |
309 if ~eq(pfin(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 PARFRAC | |
329 % objects 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 PARFRAC objects. Test both formats 'xml' and 'mat'. | |
338 % | |
339 % </SyntaxDescription> | |
340 | |
341 try | |
342 % <SyntaxCode> | |
343 save(pf1,pfv,pf2, 'test.xml'); | |
344 save(pf1,pfv,pf2, 'test.mat'); | |
345 out1 = parfrac('test.xml'); | |
346 out2 = parfrac('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 PARFRAC object contains the correct data. | |
359 % | |
360 % </AlgoDescription> | |
361 | |
362 atest = true; | |
363 pfin = [pf1, reshape(pfv, 1, []), pf2]; | |
364 if stest | |
365 % <AlgoCode> | |
366 % Check we have the correct number of outputs | |
367 if numel(out1) ~= 2+numel(pfv), atest = false; end | |
368 if numel(out2) ~= 2+numel(pfv), atest = false; end | |
369 % Check each output against the input | |
370 for kk=1:numel(out1) | |
371 if ~eq(pfin(kk), out1(kk), ple1), atest = false; end | |
372 if ~eq(pfin(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(pf3, 'test.xml'); | |
406 out2 = save(pf2, '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, pf3.hist), atest = false; end | |
433 if ~eq(out2.hist, pf2.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 = parfrac('test.xml'); | |
437 outr2 = parfrac('test.mat'); | |
438 if strcmp(outr1.hist, pf3.hist), atest = false; end | |
439 if strcmp(outr2.hist, pf2.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 pf1 to work with | |
473 iir_mat = parfrac(pf1); | |
474 iir_mat.save('test.mat'); | |
475 iir_xml = parfrac(pf1); | |
476 iir_xml.save('test.xml'); | |
477 out1 = parfrac('test.mat'); | |
478 out2 = parfrac('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 % 3) Check the history of the output against the input. | |
491 % | |
492 % </AlgoDescription> | |
493 | |
494 atest = true; | |
495 if stest | |
496 % <AlgoCode> | |
497 % Check that the modified object doesn't have the 'save' method as | |
498 % the last history step | |
499 if strcmp(iir_xml.hist.methodInfo.mname, 'save'), atest = false; end | |
500 if strcmp(iir_mat.hist.methodInfo.mname, 'save'), atest = false; end | |
501 % Check the output | |
502 if ~eq(iir_mat, out1, ple1), atest = false; end | |
503 if ~eq(iir_xml, out2, ple1), atest = false; end | |
504 % Compare the outputs | |
505 if ~eq(out1, out2, ple2), atest = false; end | |
506 % </AlgoCode> | |
507 delete('test.xml'); | |
508 delete('test.mat'); | |
509 else | |
510 atest = false; | |
511 end | |
512 | |
513 % Return a result structure | |
514 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
515 end % END UTP_07 | |
516 | |
517 %% UTP_08 | |
518 | |
519 % <TestDescription> | |
520 % | |
521 % Control the method with a plist. | |
522 % | |
523 % </TestDescription> | |
524 function result = utp_08 | |
525 | |
526 % <SyntaxDescription> | |
527 % | |
528 % Test that the save method uses the filename which is stored in a plist. | |
529 % | |
530 % </SyntaxDescription> | |
531 | |
532 try | |
533 % <SyntaxCode> | |
534 pl1 = plist('filename', 'test.mat'); | |
535 pl2 = plist('filename', 'test.xml'); | |
536 save(pf3, pl1); | |
537 save(pf3, pl2); | |
538 out1 = parfrac('test.mat'); | |
539 out2 = parfrac('test.xml'); | |
540 % </SyntaxCode> | |
541 stest = true; | |
542 catch err | |
543 disp(err.message) | |
544 stest = false; | |
545 end | |
546 | |
547 % <AlgoDescription> | |
548 % | |
549 % 1) Check the output | |
550 % | |
551 % </AlgoDescription> | |
552 | |
553 atest = true; | |
554 if stest | |
555 % <AlgoCode> | |
556 % Check the output | |
557 if ~eq(pf3, out1, ple1), atest = false; end | |
558 if ~eq(pf3, out2, ple1), atest = false; end | |
559 % Compare the outputs | |
560 if ~eq(out1, out2, ple2), atest = false; end | |
561 % </AlgoCode> | |
562 delete('test.xml'); | |
563 delete('test.mat'); | |
564 else | |
565 atest = false; | |
566 end | |
567 | |
568 % Return a result structure | |
569 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
570 end % END UTP_08 | |
571 | |
572 %% UTP_09 | |
573 | |
574 % <TestDescription> | |
575 % | |
576 % Test the save method with different complex PARFRAC objects | |
577 % | |
578 % </TestDescription> | |
579 function result = utp_09 | |
580 | |
581 % <SyntaxDescription> | |
582 % | |
583 % Test the save method with different complex PARFRAC objects | |
584 % | |
585 % </SyntaxDescription> | |
586 | |
587 try | |
588 % <SyntaxCode> | |
589 pzm = pzmodel(1, [pz(1,1), pz(1+3i), pz(10)], [{[2 1],1, 5-2i}, 4, {3+3i}]); | |
590 r = rational([ 1 2 3], [2 -4 5]); | |
591 p1 = parfrac(pzm); | |
592 p2 = parfrac(r); | |
593 p3 = parfrac([1 2+1i 2-1i], [6 1+3i 1-3i], [1 2]); | |
594 | |
595 save(p1, 'test_p1.mat'); % highpass | |
596 save(p1, 'test_p1.xml'); % highpass | |
597 save(p2, 'test_p2.mat'); % lowpass | |
598 save(p2, 'test_p2.xml'); % lowpass | |
599 save(p3, 'test_p3.mat'); % bandpass | |
600 save(p3, 'test_p3.xml'); % bandpass | |
601 out1 = parfrac('test_p1.mat'); | |
602 out2 = parfrac('test_p1.xml'); | |
603 out3 = parfrac('test_p2.mat'); | |
604 out4 = parfrac('test_p2.xml'); | |
605 out5 = parfrac('test_p3.mat'); | |
606 out6 = parfrac('test_p3.xml'); | |
607 % </SyntaxCode> | |
608 stest = true; | |
609 catch err | |
610 disp(err.message) | |
611 stest = false; | |
612 end | |
613 | |
614 % <AlgoDescription> | |
615 % | |
616 % 1) Check the output | |
617 % | |
618 % </AlgoDescription> | |
619 | |
620 atest = true; | |
621 if stest | |
622 % <AlgoCode> | |
623 % Check highpass | |
624 if ~eq(p1, out1, ple1), atest = false; end | |
625 if ~eq(p1, out2, ple1), atest = false; end | |
626 if ~eq(p2, out3, ple1), atest = false; end | |
627 if ~eq(p2, out4, ple1), atest = false; end | |
628 if ~eq(p3, out5, ple1), atest = false; end | |
629 if ~eq(p3, out6, ple1), atest = false; end | |
630 % Compare the outputs | |
631 if ~eq(out1, out2, ple2), atest = false; end | |
632 if ~eq(out3, out4, ple2), atest = false; end | |
633 if ~eq(out5, out6, ple2), atest = false; end | |
634 % </AlgoCode> | |
635 delete('test_p1.mat'); | |
636 delete('test_p1.xml'); | |
637 delete('test_p2.mat'); | |
638 delete('test_p2.xml'); | |
639 delete('test_p3.mat'); | |
640 delete('test_p3.xml'); | |
641 else | |
642 atest = false; | |
643 end | |
644 | |
645 % Return a result structure | |
646 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
647 end % END UTP_09 | |
648 | |
649 end |