Mercurial > hg > ltpda
comparison testing/utp_1.1/utps/timespan/utp_timespan_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_TIMESPAN_SAVE a set of UTPs for the timespan/save method | |
2 % | |
3 % M Hewitson 06-08-08 | |
4 % | |
5 % $Id: utp_timespan_save.m,v 1.6 2010/09/06 08:05:40 hewitson Exp $ | |
6 % | |
7 | |
8 % <MethodDescription> | |
9 % | |
10 % The save method of the timespan class saves a timespan 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_timespan_save(varargin) | |
17 | |
18 % Check the inputs | |
19 if nargin == 0 | |
20 | |
21 % Some keywords | |
22 class = 'timespan'; | |
23 mthd = 'save'; | |
24 | |
25 results = []; | |
26 disp('******************************************************'); | |
27 disp(['**** Running UTPs for ' class '/' mthd]); | |
28 disp('******************************************************'); | |
29 | |
30 % Test TIMESPAN objects | |
31 [ts1, ts2, ts3, ts4, ts5, ts6, tsv, tsm] = get_test_objects_timespan; | |
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 | |
46 disp('Done.'); | |
47 disp('******************************************************'); | |
48 | |
49 elseif nargin == 1 % Check for UTP functions | |
50 if strcmp(varargin{1}, 'isutp') | |
51 results = 1; | |
52 else | |
53 results = 0; | |
54 end | |
55 else | |
56 error('### Incorrect inputs') | |
57 end | |
58 | |
59 %% UTP_01 | |
60 | |
61 % <TestDescription> | |
62 % | |
63 % Tests that the getInfo call works for this method. | |
64 % | |
65 % </TestDescription> | |
66 function result = utp_01 | |
67 | |
68 | |
69 % <SyntaxDescription> | |
70 % | |
71 % Test that the getInfo call works for no sets, all sets, and each set | |
72 % individually. | |
73 % | |
74 % </SyntaxDescription> | |
75 | |
76 try | |
77 % <SyntaxCode> | |
78 % Call for no sets | |
79 io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']); | |
80 % Call for all sets | |
81 io(2) = eval([class '.getInfo(''' mthd ''')']); | |
82 % Call for each set | |
83 for kk=1:numel(io(2).sets) | |
84 io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']); | |
85 end | |
86 % </SyntaxCode> | |
87 stest = true; | |
88 catch err | |
89 disp(err.message) | |
90 stest = false; | |
91 end | |
92 | |
93 % <AlgoDescription> | |
94 % | |
95 % 1) Check that getInfo call returned an minfo object in all cases. | |
96 % 2) Check that all plists have the correct parameters. | |
97 % | |
98 % </AlgoDescription> | |
99 | |
100 atest = true; | |
101 if stest | |
102 % <AlgoCode> | |
103 % check we have minfo objects | |
104 if isa(io, 'minfo') | |
105 %%% SET 'None' | |
106 if ~isempty(io(1).sets), atest = false; end | |
107 if ~isempty(io(1).plists), atest = false; end | |
108 %%% Check all Sets | |
109 if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end | |
110 if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end | |
111 %%%%%%%%%% SET 'Default' | |
112 if io(3).plists.nparams ~= 4, atest = false; end | |
113 % Check key | |
114 if ~io(3).plists.isparam('filename'), atest = false; end | |
115 if ~io(3).plists.isparam('prefix'), atest = false; end | |
116 if ~io(3).plists.isparam('postfix'), atest = false; end | |
117 if ~io(3).plists.isparam('individual files'), atest = false; end | |
118 % Check default value | |
119 if ~isequal(io(3).plists.find('filename'), ''), atest = false; end | |
120 if ~isequal(io(3).plists.find('prefix'), ''), atest = false; end | |
121 if ~isequal(io(3).plists.find('postfix'), ''), atest = false; end | |
122 if ~isequal(io(3).plists.find('individual files'), false), atest = false; end | |
123 % Check options | |
124 if ~isequal(io(3).plists.getOptionsForParam('filename'), {[]}), atest = false; end | |
125 end | |
126 % </AlgoCode> | |
127 else | |
128 atest = false; | |
129 end | |
130 | |
131 % Return a result structure | |
132 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
133 end % END UTP_01 | |
134 | |
135 %% UTP_02 | |
136 | |
137 % <TestDescription> | |
138 % | |
139 % Tests that the save method works with a vector of TIMESPAN objects as input. | |
140 % | |
141 % </TestDescription> | |
142 function result = utp_02 | |
143 | |
144 % <SyntaxDescription> | |
145 % | |
146 % Test that the save method works for a vector of TIMESPAN objects as input. | |
147 % Test both formats 'xml' and 'mat'. | |
148 % | |
149 % </SyntaxDescription> | |
150 | |
151 try | |
152 % <SyntaxCode> | |
153 save(tsv, 'test.xml'); | |
154 save(tsv, 'test.mat'); | |
155 out1 = timespan('test.xml'); | |
156 out2 = timespan('test.mat'); | |
157 % </SyntaxCode> | |
158 stest = true; | |
159 catch err | |
160 disp(err.message) | |
161 stest = false; | |
162 end | |
163 | |
164 % <AlgoDescription> | |
165 % | |
166 % 1) Check that the number of elements in 'out1' and 'out2' are the same | |
167 % as in 'tsv' | |
168 % 2) Check that the loaded objects are the same as the saved objects. | |
169 % 3) The outputs 'out1' and 'out2' must be the same. | |
170 % | |
171 % </AlgoDescription> | |
172 | |
173 atest = true; | |
174 if stest | |
175 % <AlgoCode> | |
176 % Check we have the correct number of outputs | |
177 if ~isequal(size(out1), size(tsv)), atest = false; end | |
178 if ~isequal(size(out2), size(tsv)), atest = false; end | |
179 % Check each output against the input | |
180 for kk=1:numel(out1) | |
181 if ~eq(tsv(kk), out1(kk), ple1), atest = false; end | |
182 if ~eq(tsv(kk), out2(kk), ple1), atest = false; end | |
183 end | |
184 % Compare the outputs | |
185 if ~eq(out1, out2, ple2), atest = false; end | |
186 % </AlgoCode> | |
187 delete('test.xml'); | |
188 delete('test.mat'); | |
189 else | |
190 atest = false; | |
191 end | |
192 | |
193 % Return a result structure | |
194 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
195 end % END UTP_02 | |
196 | |
197 %% UTP_03 | |
198 | |
199 % <TestDescription> | |
200 % | |
201 % Tests that the save method works with a matrix of TIMESPAN objects as input. | |
202 % | |
203 % </TestDescription> | |
204 function result = utp_03 | |
205 | |
206 % <SyntaxDescription> | |
207 % | |
208 % Test that the save method works for a matrix of TIMESPAN objects as input. | |
209 % Test both formats 'xml' and 'mat'. | |
210 % | |
211 % </SyntaxDescription> | |
212 | |
213 try | |
214 % <SyntaxCode> | |
215 save(tsm, 'test.xml'); | |
216 save(tsm, 'test.mat'); | |
217 out1 = timespan('test.xml'); | |
218 out2 = timespan('test.mat'); | |
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 'out1' and 'out2' are the same | |
229 % as in 'tsm' | |
230 % 2) Check that the loaded objects are the same as the saved objects. | |
231 % 3) The outputs 'out1' and 'out2' must be the same. | |
232 % | |
233 % </AlgoDescription> | |
234 | |
235 atest = true; | |
236 if stest | |
237 % <AlgoCode> | |
238 % Check we have the correct number of outputs | |
239 if ~isequal(size(out1), size(tsm)), atest = false; end | |
240 if ~isequal(size(out2), size(tsm)), atest = false; end | |
241 % Check each output against the input | |
242 for kk=1:numel(out1) | |
243 if ~eq(tsm(kk), out1(kk), ple1), atest = false; end | |
244 if ~eq(tsm(kk), out2(kk), ple1), atest = false; end | |
245 end | |
246 % Compare the outputs | |
247 if ~eq(out1, out2, ple2), atest = false; end | |
248 % </AlgoCode> | |
249 delete('test.xml'); | |
250 delete('test.mat'); | |
251 else | |
252 atest = false; | |
253 end | |
254 | |
255 % Return a result structure | |
256 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
257 end % END UTP_03 | |
258 | |
259 %% UTP_04 | |
260 | |
261 % <TestDescription> | |
262 % | |
263 % Tests that the save method works with a list of TIMESPAN objects as input. | |
264 % | |
265 % </TestDescription> | |
266 function result = utp_04 | |
267 | |
268 % <SyntaxDescription> | |
269 % | |
270 % Test that the save method works for a list of TIMESPAN objects as input. | |
271 % Test both formats 'xml' and 'mat'. | |
272 % | |
273 % </SyntaxDescription> | |
274 | |
275 try | |
276 % <SyntaxCode> | |
277 save(ts5, ts4, ts3, 'test.xml'); | |
278 save(ts5, ts4, ts3, 'test.mat'); | |
279 out1 = timespan('test.xml'); | |
280 out2 = timespan('test.mat'); | |
281 % </SyntaxCode> | |
282 stest = true; | |
283 catch err | |
284 disp(err.message) | |
285 stest = false; | |
286 end | |
287 | |
288 % <AlgoDescription> | |
289 % | |
290 % 1) Check that the number of elements in 'out1' and 'out2' are the same | |
291 % as in the list | |
292 % 2) Check that the loaded objects are the same as the saved objects. | |
293 % 3) The outputs 'out1' and 'out2' must be the same except. | |
294 % | |
295 % </AlgoDescription> | |
296 | |
297 atest = true; | |
298 tsin = [ts5, ts4, ts3]; | |
299 if stest | |
300 % <AlgoCode> | |
301 % Check we have the correct number of outputs | |
302 if numel(out1) ~= 3, atest = false; end | |
303 if numel(out2) ~= 3, atest = false; end | |
304 % Check each output against the input | |
305 for kk=1:numel(out1) | |
306 if ~eq(tsin(kk), out1(kk), ple1), atest = false; end | |
307 if ~eq(tsin(kk), out2(kk), ple1), atest = false; end | |
308 end | |
309 % Compare the outputs | |
310 if ~eq(out1, out2, ple2), atest = false; end | |
311 % </AlgoCode> | |
312 delete('test.xml'); | |
313 delete('test.mat'); | |
314 else | |
315 atest = false; | |
316 end | |
317 | |
318 % Return a result structure | |
319 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
320 end % END UTP_04 | |
321 | |
322 %% UTP_05 | |
323 | |
324 % <TestDescription> | |
325 % | |
326 % Tests that the save method works with a mix of different shaped TIMESPAN objects | |
327 % as input. | |
328 % | |
329 % </TestDescription> | |
330 function result = utp_05 | |
331 | |
332 % <SyntaxDescription> | |
333 % | |
334 % Test that the save method works with an input of matrices and vectors | |
335 % and single TIMESPAN objects. Test both formats 'xml' and 'mat'. | |
336 % | |
337 % </SyntaxDescription> | |
338 | |
339 try | |
340 % <SyntaxCode> | |
341 save(ts5,tsv,ts4, 'test.xml'); | |
342 save(ts5,tsv,ts4, 'test.mat'); | |
343 out1 = timespan('test.xml'); | |
344 out2 = timespan('test.mat'); | |
345 % </SyntaxCode> | |
346 stest = true; | |
347 catch err | |
348 disp(err.message) | |
349 stest = false; | |
350 end | |
351 | |
352 % <AlgoDescription> | |
353 % | |
354 % 1) Check that the number of elements in 'out' is the same as in | |
355 % input. | |
356 % 2) Check that each output TIMESPAN object contains the correct data. | |
357 % | |
358 % </AlgoDescription> | |
359 | |
360 atest = true; | |
361 tsin = [ts5, reshape(tsv, 1, []), ts4]; | |
362 if stest | |
363 % <AlgoCode> | |
364 % Check we have the correct number of outputs | |
365 if numel(out1) ~= 2+numel(tsv), atest = false; end | |
366 if numel(out2) ~= 2+numel(tsv), atest = false; end | |
367 % Check each output against the input | |
368 for kk=1:numel(out1) | |
369 if ~eq(tsin(kk), out1(kk), ple1), atest = false; end | |
370 if ~eq(tsin(kk), out2(kk), ple1), atest = false; end | |
371 end | |
372 % Compare the outputs | |
373 if ~eq(out1, out2, ple2), atest = false; end | |
374 % </AlgoCode> | |
375 delete('test.xml'); | |
376 delete('test.mat'); | |
377 else | |
378 atest = false; | |
379 end | |
380 | |
381 % Return a result structure | |
382 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
383 end % END UTP_05 | |
384 | |
385 %% UTP_06 | |
386 | |
387 % <TestDescription> | |
388 % | |
389 % Tests that the save method properly applies history. | |
390 % | |
391 % </TestDescription> | |
392 function result = utp_06 | |
393 | |
394 % <SyntaxDescription> | |
395 % | |
396 % Test that the result of applying the save method can be processed back | |
397 % to an m-file. Do this for both extensions 'mat' and 'xml' | |
398 % | |
399 % </SyntaxDescription> | |
400 | |
401 try | |
402 % <SyntaxCode> | |
403 out1 = save(ts2, 'test.xml'); | |
404 out2 = save(ts4, 'test.mat'); | |
405 mout1 = rebuild(out1); | |
406 mout2 = rebuild(out2); | |
407 % </SyntaxCode> | |
408 stest = true; | |
409 catch err | |
410 disp(err.message) | |
411 stest = false; | |
412 end | |
413 | |
414 % <AlgoDescription> | |
415 % | |
416 % 1) Check that the history applies to the output object. Check that | |
417 % save doesn't add a history step to the input object. | |
418 % 2) Check that the read object doesn't contain the save + load history steps. | |
419 % 3) Check that the method rebuild produces the same object as 'out'. | |
420 % | |
421 % </AlgoDescription> | |
422 | |
423 atest = true; | |
424 if stest | |
425 % <AlgoCode> | |
426 % The last history step is not the save method | |
427 if ~eq(out1, ts2, ple1), atest = false; end | |
428 if ~eq(out2, ts4, ple1), atest = false; end | |
429 % Check the rebuilt object | |
430 if ~eq(mout1, out1, ple2), atest = false; end | |
431 if ~eq(mout2, out2, ple2), atest = false; end | |
432 % </AlgoCode> | |
433 % delete test file | |
434 delete('test.xml') | |
435 delete('test.mat') | |
436 else | |
437 atest = false; | |
438 end | |
439 | |
440 % Return a result structure | |
441 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
442 end % END UTP_06 | |
443 | |
444 %% UTP_07 | |
445 | |
446 % <TestDescription> | |
447 % | |
448 % Tests that the save method works with the modify command. | |
449 % | |
450 % </TestDescription> | |
451 function result = utp_07 | |
452 | |
453 % <SyntaxDescription> | |
454 % | |
455 % Use the save method with the modifier command. | |
456 % | |
457 % </SyntaxDescription> | |
458 | |
459 try | |
460 % <SyntaxCode> | |
461 % copy ts5 to work with | |
462 ts_mat = timespan(ts5); | |
463 ts_mat.save('test.mat'); | |
464 ts_xml = timespan(ts5); | |
465 ts_xml.save('test.xml'); | |
466 out1 = timespan('test.mat'); | |
467 out2 = timespan('test.xml'); | |
468 % </SyntaxCode> | |
469 stest = true; | |
470 catch err | |
471 disp(err.message) | |
472 stest = false; | |
473 end | |
474 | |
475 % <AlgoDescription> | |
476 % | |
477 % 1) Check that the save method doesn't apply the history. | |
478 % 2) Check the output against the input. | |
479 % 3) Check the history of the output against the input. | |
480 % | |
481 % </AlgoDescription> | |
482 | |
483 atest = true; | |
484 if stest | |
485 % <AlgoCode> | |
486 % Check the output | |
487 if ~eq(ts_mat, out1, ple1), atest = false; end | |
488 if ~eq(ts_xml, out2, ple1), atest = false; end | |
489 % Compare the outputs | |
490 if ~eq(out1, out2, ple2), atest = false; end | |
491 % </AlgoCode> | |
492 delete('test.xml'); | |
493 delete('test.mat'); | |
494 else | |
495 atest = false; | |
496 end | |
497 | |
498 % Return a result structure | |
499 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
500 end % END UTP_07 | |
501 | |
502 %% UTP_08 | |
503 | |
504 % <TestDescription> | |
505 % | |
506 % Control the method with a plist. | |
507 % | |
508 % </TestDescription> | |
509 function result = utp_08 | |
510 | |
511 % <SyntaxDescription> | |
512 % | |
513 % Test that the save method uses the filename which is stored in a plist. | |
514 % | |
515 % </SyntaxDescription> | |
516 | |
517 try | |
518 % <SyntaxCode> | |
519 pl1 = plist('filename', 'test.mat'); | |
520 pl2 = plist('filename', 'test.xml'); | |
521 save(ts5, pl1); | |
522 save(ts5, pl2); | |
523 out1 = timespan('test.mat'); | |
524 out2 = timespan('test.xml'); | |
525 % </SyntaxCode> | |
526 stest = true; | |
527 catch err | |
528 disp(err.message) | |
529 stest = false; | |
530 end | |
531 | |
532 % <AlgoDescription> | |
533 % | |
534 % 1) Check the output | |
535 % | |
536 % </AlgoDescription> | |
537 | |
538 atest = true; | |
539 if stest | |
540 % <AlgoCode> | |
541 % Check the output | |
542 if ~eq(ts5, out1, ple1), atest = false; end | |
543 if ~eq(ts5, out2, ple1), atest = false; end | |
544 % Compare the outputs | |
545 if ~eq(out1, out2, ple2), atest = false; end | |
546 % </AlgoCode> | |
547 delete('test.xml'); | |
548 delete('test.mat'); | |
549 else | |
550 atest = false; | |
551 end | |
552 | |
553 % Return a result structure | |
554 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
555 end % END UTP_08 | |
556 | |
557 end |