Mercurial > hg > ltpda
comparison testing/utp_1.1/utps/rational/utp_rational_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_RATIONAL_SAVE a set of UTPs for the rational/save method | |
2 % | |
3 % M Hewitson 06-08-08 | |
4 % | |
5 % $Id: utp_rational_save.m,v 1.5 2010/08/31 09:43:48 hewitson Exp $ | |
6 % | |
7 | |
8 % <MethodDescription> | |
9 % | |
10 % The save method of the rational class saves a rational 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_rational_save(varargin) | |
17 | |
18 % Check the inputs | |
19 if nargin == 0 | |
20 | |
21 % Some keywords | |
22 class = 'rational'; | |
23 mthd = 'save'; | |
24 | |
25 results = []; | |
26 disp('******************************************************'); | |
27 disp(['**** Running UTPs for ' class '/' mthd]); | |
28 disp('******************************************************'); | |
29 | |
30 % Test RATIONAL objects | |
31 [ra1,ra2,ra3,rav,ram] = get_test_objects_rational; | |
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 RATIONAL 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 RATIONAL 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 RATIONAL objects as input. | |
148 % Test both formats 'xml' and 'mat'. | |
149 % | |
150 % </SyntaxDescription> | |
151 | |
152 try | |
153 % <SyntaxCode> | |
154 save(rav, 'test.xml'); | |
155 save(rav, 'test.mat'); | |
156 out1 = rational('test.xml'); | |
157 out2 = rational('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 'rav' | |
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(rav)), atest = false; end | |
179 if ~isequal(size(out2), size(rav)), atest = false; end | |
180 % Check each output against the input | |
181 for kk=1:numel(out1) | |
182 if ~eq(rav(kk), out1(kk), ple1), atest = false; end | |
183 if ~eq(rav(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 RATIONAL 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 RATIONAL objects as | |
211 % input. Test both formats 'xml' and 'mat'. | |
212 % | |
213 % </SyntaxDescription> | |
214 | |
215 try | |
216 % <SyntaxCode> | |
217 save(ram, 'test.xml'); | |
218 save(ram, 'test.mat'); | |
219 out1 = rational('test.xml'); | |
220 out2 = rational('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 'ram' | |
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(ram)), atest = false; end | |
242 if ~isequal(size(out2), size(ram)), atest = false; end | |
243 % Check each output against the input | |
244 for kk=1:numel(out1) | |
245 if ~eq(ram(kk), out1(kk), ple1), atest = false; end | |
246 if ~eq(ram(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 RATIONAL 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 RATIONAL objects as input. | |
273 % Test both formats 'xml' and 'mat'. | |
274 % | |
275 % </SyntaxDescription> | |
276 | |
277 try | |
278 % <SyntaxCode> | |
279 save(ra1, ra2, ra3, 'test.xml'); | |
280 save(ra1, ra2, ra3, 'test.mat'); | |
281 out1 = rational('test.xml'); | |
282 out2 = rational('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 rain = [ra1, ra2, ra3]; | |
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(rain(kk), out1(kk), ple1), atest = false; end | |
309 if ~eq(rain(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 RATIONAL | |
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 RATIONAL objects. Test both formats 'xml' and 'mat'. | |
338 % | |
339 % </SyntaxDescription> | |
340 | |
341 try | |
342 % <SyntaxCode> | |
343 save(ra1,rav,ra2, 'test.xml'); | |
344 save(ra1,rav,ra2, 'test.mat'); | |
345 out1 = rational('test.xml'); | |
346 out2 = rational('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 RATIONAL object contains the correct data. | |
359 % | |
360 % </AlgoDescription> | |
361 | |
362 atest = true; | |
363 rain = [ra1, reshape(rav, 1, []), ra2]; | |
364 if stest | |
365 % <AlgoCode> | |
366 % Check we have the correct number of outputs | |
367 if numel(out1) ~= 2+numel(rav), atest = false; end | |
368 if numel(out2) ~= 2+numel(rav), atest = false; end | |
369 % Check each output against the input | |
370 for kk=1:numel(out1) | |
371 if ~eq(rain(kk), out1(kk), ple1), atest = false; end | |
372 if ~eq(rain(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(ra3, 'test.xml'); | |
406 out2 = save(ra2, '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 if ~eq(out1.hist, ra3.hist), atest = false; end | |
432 if ~eq(out2.hist, ra2.hist), atest = false; end | |
433 % check the history steps of the read object (load + save) | |
434 % The steps (save + load) shouldn't add history | |
435 outr1 = rational('test.xml'); | |
436 outr2 = rational('test.mat'); | |
437 if strcmp(outr1.hist, ra3.hist), atest = false; end | |
438 if strcmp(outr2.hist, ra2.hist), atest = false; end | |
439 % Check the rebuilt object | |
440 if ~eq(mout1, out1, ple2), atest = false; end | |
441 if ~eq(mout2, out2, ple2), atest = false; end | |
442 % </AlgoCode> | |
443 % delete test file | |
444 delete('test.xml') | |
445 delete('test.mat') | |
446 else | |
447 atest = false; | |
448 end | |
449 | |
450 % Return a result structure | |
451 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
452 end % END UTP_06 | |
453 | |
454 %% UTP_07 | |
455 | |
456 % <TestDescription> | |
457 % | |
458 % Tests that the save method works with the modify command. | |
459 % | |
460 % </TestDescription> | |
461 function result = utp_07 | |
462 | |
463 % <SyntaxDescription> | |
464 % | |
465 % Use the save method with the modifier command. | |
466 % | |
467 % </SyntaxDescription> | |
468 | |
469 try | |
470 % <SyntaxCode> | |
471 % copy ra1 to work with | |
472 iir_mat = rational(ra1); | |
473 iir_mat.save('test.mat'); | |
474 iir_xml = rational(ra1); | |
475 iir_xml.save('test.xml'); | |
476 out1 = rational('test.mat'); | |
477 out2 = rational('test.xml'); | |
478 % </SyntaxCode> | |
479 stest = true; | |
480 catch err | |
481 disp(err.message) | |
482 stest = false; | |
483 end | |
484 | |
485 % <AlgoDescription> | |
486 % | |
487 % 1) Check that the save method doesn't apply the history. | |
488 % 2) Check the output against the input. | |
489 % | |
490 % </AlgoDescription> | |
491 | |
492 atest = true; | |
493 if stest | |
494 % <AlgoCode> | |
495 % Check that the modified object have the 'save' method as the last | |
496 % history step | |
497 if strcmp(iir_xml.hist.methodInfo.mname, 'save'), atest = false; end | |
498 if strcmp(iir_mat.hist.methodInfo.mname, 'save'), atest = false; end | |
499 % Check the output without the history | |
500 if ~eq(iir_mat, out1, ple1), atest = false; end | |
501 if ~eq(iir_xml, out2, ple1), atest = false; end | |
502 % Compare the outputs | |
503 if ~eq(out1, out2, ple2), atest = false; end | |
504 % </AlgoCode> | |
505 delete('test.xml'); | |
506 delete('test.mat'); | |
507 else | |
508 atest = false; | |
509 end | |
510 | |
511 % Return a result structure | |
512 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
513 end % END UTP_07 | |
514 | |
515 %% UTP_08 | |
516 | |
517 % <TestDescription> | |
518 % | |
519 % Control the method with a plist. | |
520 % | |
521 % </TestDescription> | |
522 function result = utp_08 | |
523 | |
524 % <SyntaxDescription> | |
525 % | |
526 % Test that the save method uses the filename which is stored in a plist. | |
527 % | |
528 % </SyntaxDescription> | |
529 | |
530 try | |
531 % <SyntaxCode> | |
532 pl1 = plist('filename', 'test.mat'); | |
533 pl2 = plist('filename', 'test.xml'); | |
534 save(ra3, pl1); | |
535 save(ra3, pl2); | |
536 out1 = rational('test.mat'); | |
537 out2 = rational('test.xml'); | |
538 % </SyntaxCode> | |
539 stest = true; | |
540 catch err | |
541 disp(err.message) | |
542 stest = false; | |
543 end | |
544 | |
545 % <AlgoDescription> | |
546 % | |
547 % 1) Check the output | |
548 % | |
549 % </AlgoDescription> | |
550 | |
551 atest = true; | |
552 if stest | |
553 % <AlgoCode> | |
554 % Check the output | |
555 if ~eq(ra3, out1, ple1), atest = false; end | |
556 if ~eq(ra3, out2, ple1), atest = false; end | |
557 % Compare the outputs | |
558 if ~eq(out1, out2, ple2), atest = false; end | |
559 % </AlgoCode> | |
560 delete('test.xml'); | |
561 delete('test.mat'); | |
562 else | |
563 atest = false; | |
564 end | |
565 | |
566 % Return a result structure | |
567 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
568 end % END UTP_08 | |
569 | |
570 %% UTP_09 | |
571 | |
572 % <TestDescription> | |
573 % | |
574 % Test the save method with different complex RATIONAL objects | |
575 % | |
576 % </TestDescription> | |
577 function result = utp_09 | |
578 | |
579 % <SyntaxDescription> | |
580 % | |
581 % Test the save method with different complex RATIONAL objects | |
582 % | |
583 % </SyntaxDescription> | |
584 | |
585 try | |
586 % <SyntaxCode> | |
587 pzm = pzmodel(1, [pz(1,1), pz(1+3i), pz(10)], [{[2 1],1, 5-2i}, 4, {3+3i}]); | |
588 p = parfrac([1 2+1i 2-1i], [6 1+3i 1-3i], [1 2 3 4], 'my par frac', unit('V'), unit('Hz')); | |
589 r1 = rational(pzm); | |
590 r2 = rational(p); | |
591 r3 = rational([1 2+1i 2-1i], [6 1+3i 1-3i]); | |
592 | |
593 save(r1, 'test_r1.mat'); % highpass | |
594 save(r1, 'test_r1.xml'); % highpass | |
595 save(r2, 'test_r2.mat'); % lowpass | |
596 save(r2, 'test_r2.xml'); % lowpass | |
597 save(r3, 'test_r3.mat'); % bandpass | |
598 save(r3, 'test_r3.xml'); % bandpass | |
599 out1 = rational('test_r1.mat'); | |
600 out2 = rational('test_r1.xml'); | |
601 out3 = rational('test_r2.mat'); | |
602 out4 = rational('test_r2.xml'); | |
603 out5 = rational('test_r3.mat'); | |
604 out6 = rational('test_r3.xml'); | |
605 % </SyntaxCode> | |
606 stest = true; | |
607 catch err | |
608 disp(err.message) | |
609 stest = false; | |
610 end | |
611 | |
612 % <AlgoDescription> | |
613 % | |
614 % 1) Check the output | |
615 % | |
616 % </AlgoDescription> | |
617 | |
618 atest = true; | |
619 if stest | |
620 % <AlgoCode> | |
621 % Check highpass | |
622 if ~eq(r1, out1, ple1), atest = false; end | |
623 if ~eq(r1, out2, ple1), atest = false; end | |
624 if ~eq(r2, out3, ple1), atest = false; end | |
625 if ~eq(r2, out4, ple1), atest = false; end | |
626 if ~eq(r3, out5, ple1), atest = false; end | |
627 if ~eq(r3, out6, ple1), atest = false; end | |
628 % Compare the outputs | |
629 if ~eq(out1, out2, ple2), atest = false; end | |
630 if ~eq(out3, out4, ple2), atest = false; end | |
631 if ~eq(out5, out6, ple2), atest = false; end | |
632 % </AlgoCode> | |
633 delete('test_r1.mat'); | |
634 delete('test_r1.xml'); | |
635 delete('test_r2.mat'); | |
636 delete('test_r2.xml'); | |
637 delete('test_r3.mat'); | |
638 delete('test_r3.xml'); | |
639 else | |
640 atest = false; | |
641 end | |
642 | |
643 % Return a result structure | |
644 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
645 end % END UTP_09 | |
646 | |
647 end |