Mercurial > hg > ltpda
comparison testing/utp_1.1/utps/ao/utp_ao_interp.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_INTERP a set of UTPs for the ao/interp method | |
2 % | |
3 % M Hewitson 06-08-08 | |
4 % | |
5 % $Id: utp_ao_interp.m,v 1.10 2009/08/07 10:28:36 hewitson Exp $ | |
6 % | |
7 | |
8 % <MethodDescription> | |
9 % | |
10 % The interp method of the ao class interpolates AOs. | |
11 % | |
12 % </MethodDescription> | |
13 | |
14 function results = utp_ao_interp(varargin) | |
15 | |
16 % Check the inputs | |
17 if nargin == 0 | |
18 | |
19 % Some keywords | |
20 class = 'ao'; | |
21 mthd = 'interp'; | |
22 | |
23 results = []; | |
24 disp('******************************************************'); | |
25 disp(['**** Running UTPs for ' class '/' mthd]); | |
26 disp('******************************************************'); | |
27 | |
28 % Test AOs | |
29 [at1,at2,at3,at4,at5,at6,atvec,atmat] = eval(['get_test_objects_' class]); | |
30 | |
31 % Exception list for the UTPs: | |
32 [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples(); | |
33 | |
34 % Run the tests | |
35 results = [results utp_01]; % getInfo call | |
36 results = [results utp_02]; % Vector input | |
37 results = [results utp_03]; % Matrix input | |
38 results = [results utp_04]; % List input | |
39 results = [results utp_05]; % Test with mixed input | |
40 results = [results utp_06]; % Test history is working | |
41 results = [results utp_07]; % Test the modify call works | |
42 results = [results utp_08]; % Test the data shape | |
43 results = [results utp_09]; % Test output of the data | |
44 results = [results utp_10]; % Test different interpolations | |
45 results = [results utp_11(mthd, at1, ple1, plist('vertices', 0:10))]; % Test plotinfo doesn't disappear | |
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 ~= 2, atest = false; end | |
114 % Check key | |
115 if ~io(3).plists.isparam('vertices'), atest = false; end | |
116 if ~io(3).plists.isparam('method'), atest = false; end | |
117 % Check default value | |
118 if ~isEmptyDouble(io(3).plists.find('vertices')), atest = false; end | |
119 if ~isequal(io(3).plists.find('method'), 'spline'), atest = false; end | |
120 % Check options | |
121 if ~isequal(io(3).plists.getOptionsForParam('vertices'), {[]}), atest = false; end | |
122 if ~isequal(io(3).plists.getOptionsForParam('method'), {'nearest', 'linear', 'spline', 'cubic'}), atest = false; end | |
123 end | |
124 % </AlgoCode> | |
125 else | |
126 atest = false; | |
127 end | |
128 | |
129 % Return a result structure | |
130 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
131 end % END UTP_01 | |
132 | |
133 %% UTP_02 | |
134 | |
135 % <TestDescription> | |
136 % | |
137 % Tests that the interp method works with a vector of AOs as input. | |
138 % | |
139 % </TestDescription> | |
140 function result = utp_02 | |
141 | |
142 % <SyntaxDescription> | |
143 % | |
144 % Test that the interp method works for a vector of AOs as input. | |
145 % | |
146 % </SyntaxDescription> | |
147 | |
148 try | |
149 % <SyntaxCode> | |
150 v = linspace(0,100).'; | |
151 out = interp(atvec, plist('vertices', v)); | |
152 % </SyntaxCode> | |
153 stest = true; | |
154 catch err | |
155 disp(err.message) | |
156 stest = false; | |
157 end | |
158 | |
159 % <AlgoDescription> | |
160 % | |
161 % 1) Check that the number of elements in 'out' is the square of the | |
162 % number in the input. | |
163 % 2) Check that each output AO contains the correct data. | |
164 % | |
165 % </AlgoDescription> | |
166 | |
167 atest = true; | |
168 TOL = 7e-14; | |
169 if stest | |
170 % <AlgoCode> | |
171 % Check we have the correct number of outputs | |
172 if numel(out) ~= numel(atvec), atest = false; end | |
173 % Check the data | |
174 for kk = 1:numel(atvec) | |
175 if isa(atvec(kk).data, 'cdata') | |
176 % for cdata object | |
177 y = interp1(atvec(kk).y, v, 'spline', 'extrap'); | |
178 if ~isequal(y, out(kk).y), atest = false; end | |
179 else | |
180 % for tsdata, fsdata and xydata objects | |
181 y = interp1(atvec(kk).x, atvec(kk).y, v, 'spline', 'extrap'); | |
182 if any(abs(v - out(kk).x) > TOL), atest = false; end | |
183 if ~isequal(y, out(kk).y), atest = false; end | |
184 end | |
185 end | |
186 % </AlgoCode> | |
187 else | |
188 atest = false; | |
189 end | |
190 | |
191 % Return a result structure | |
192 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
193 end % END UTP_02 | |
194 | |
195 %% UTP_03 | |
196 | |
197 % <TestDescription> | |
198 % | |
199 % Tests that the interp method works with a matrix of AOs as input. | |
200 % | |
201 % </TestDescription> | |
202 function result = utp_03 | |
203 | |
204 % <SyntaxDescription> | |
205 % | |
206 % Test that the interp method works for a matrix of AOs as input. | |
207 % | |
208 % </SyntaxDescription> | |
209 | |
210 try | |
211 % <SyntaxCode> | |
212 v = linspace(0,100).'; | |
213 out = interp(atmat, plist('vertices', v)); | |
214 % </SyntaxCode> | |
215 stest = true; | |
216 catch err | |
217 disp(err.message) | |
218 stest = false; | |
219 end | |
220 | |
221 % <AlgoDescription> | |
222 % | |
223 % 1) Check that the number of elements in 'out' is the square of the | |
224 % number in the input. | |
225 % 2) Check that each output AO contains the correct data. | |
226 % | |
227 % </AlgoDescription> | |
228 | |
229 atest = true; | |
230 TOL = 7e-14; | |
231 if stest | |
232 % <AlgoCode> | |
233 % Check we have the correct number of outputs | |
234 if numel(out) ~= numel(atmat), atest = false; end | |
235 % Check the data | |
236 for kk = 1:numel(atmat) | |
237 if isa(atmat(kk).data, 'cdata') | |
238 % for cdata object | |
239 y = interp1(atmat(kk).y, v, 'spline', 'extrap'); | |
240 if ~isequal(y, out(kk).y), atest = false; end | |
241 else | |
242 % for tsdata, fsdata and xydata objects | |
243 y = interp1(atmat(kk).x, atmat(kk).y, v, 'spline', 'extrap'); | |
244 if any(abs(v - out(kk).x) > TOL), atest = false; end | |
245 if ~isequal(y, out(kk).y), atest = false; end | |
246 end | |
247 end | |
248 % </AlgoCode> | |
249 else | |
250 atest = false; | |
251 end | |
252 | |
253 % Return a result structure | |
254 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
255 end % END UTP_03 | |
256 | |
257 %% UTP_04 | |
258 | |
259 % <TestDescription> | |
260 % | |
261 % Tests that the interp method works with a list of AOs as input. | |
262 % | |
263 % </TestDescription> | |
264 function result = utp_04 | |
265 | |
266 % <SyntaxDescription> | |
267 % | |
268 % Test that the interp method works for a list of AOs as input. | |
269 % | |
270 % </SyntaxDescription> | |
271 | |
272 try | |
273 % <SyntaxCode> | |
274 v = linspace(0,100).'; | |
275 out = interp(at1,at5,at6, plist('vertices', v)); | |
276 % </SyntaxCode> | |
277 stest = true; | |
278 catch err | |
279 disp(err.message) | |
280 stest = false; | |
281 end | |
282 | |
283 % <AlgoDescription> | |
284 % | |
285 % 1) Check that the number of elements in 'out' is the square of the | |
286 % number in the input. | |
287 % 2) Check that each output AO contains the correct data. | |
288 % | |
289 % </AlgoDescription> | |
290 | |
291 atest = true; | |
292 aoin = [at1, at5, at6]; | |
293 TOL = 7e-14; | |
294 if stest | |
295 % <AlgoCode> | |
296 % Check we have the correct number of outputs | |
297 if numel(out) ~= 3, atest = false; end | |
298 % Check the data | |
299 for kk = 1:numel(aoin) | |
300 if isa(aoin(kk).data, 'cdata') | |
301 % for cdata object | |
302 y = interp1(aoin(kk).y, v, 'spline', 'extrap'); | |
303 if ~isequal(y, out(kk).y), atest = false; end | |
304 else | |
305 % for tsdata, fsdata and xydata objects | |
306 y = interp1(aoin(kk).x, aoin(kk).y, v, 'spline', 'extrap'); | |
307 if any(abs(v - out(kk).x) > TOL), atest = false; end | |
308 if ~isequal(y, out(kk).y), atest = false; end | |
309 end | |
310 end | |
311 % </AlgoCode> | |
312 else | |
313 atest = false; | |
314 end | |
315 | |
316 % Return a result structure | |
317 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
318 end % END UTP_04 | |
319 | |
320 %% UTP_05 | |
321 | |
322 % <TestDescription> | |
323 % | |
324 % Tests that the interp method works with a mix of different shaped AOs as | |
325 % input. | |
326 % | |
327 % </TestDescription> | |
328 function result = utp_05 | |
329 | |
330 % <SyntaxDescription> | |
331 % | |
332 % Test that the interp method works with an input of matrices and vectors | |
333 % and single AOs. | |
334 % | |
335 % </SyntaxDescription> | |
336 | |
337 try | |
338 % <SyntaxCode> | |
339 v = linspace(0,100).'; | |
340 out = interp(at1,[at5 at6],at5,[at5 at1; at6 at1],at6, plist('vertices', v)); | |
341 % </SyntaxCode> | |
342 stest = true; | |
343 catch err | |
344 disp(err.message) | |
345 stest = false; | |
346 end | |
347 | |
348 % <AlgoDescription> | |
349 % | |
350 % 1) Check that the number of elements in 'out' is the same as in | |
351 % input. | |
352 % 2) Check that each output AO contains the correct data. | |
353 % | |
354 % </AlgoDescription> | |
355 | |
356 aoin = [at1, reshape([at5 at6], 1, []), at5, reshape([at5 at1; at6 at1], 1, []), at6]; | |
357 TOL = 7e-14; | |
358 atest = true; | |
359 if stest | |
360 % <AlgoCode> | |
361 % Check we have the correct number of outputs | |
362 if numel(out) ~= 9, atest = false; end | |
363 % Check the data | |
364 for kk = 1:numel(aoin) | |
365 if isa(aoin(kk).data, 'cdata') | |
366 % for cdata object | |
367 y = interp1(aoin(kk).y, v, 'spline', 'extrap'); | |
368 if ~isequal(y, out(kk).y), atest = false; end | |
369 else | |
370 % for tsdata, fsdata and xydata objects | |
371 y = interp1(aoin(kk).x, aoin(kk).y, v, 'spline', 'extrap'); | |
372 if any(abs(v - out(kk).x) > TOL), atest = false; end | |
373 if ~isequal(y, out(kk).y), atest = false; end | |
374 end | |
375 end | |
376 % </AlgoCode> | |
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 interp method properly applies history. | |
390 % | |
391 % </TestDescription> | |
392 function result = utp_06 | |
393 | |
394 % <SyntaxDescription> | |
395 % | |
396 % Test that the result of applying the interp method can be processed back | |
397 % to an m-file. | |
398 % | |
399 % </SyntaxDescription> | |
400 | |
401 try | |
402 % <SyntaxCode> | |
403 v = linspace(0,100); | |
404 out = interp(at5, plist('vertices', v)); | |
405 mout = rebuild(out); | |
406 % </SyntaxCode> | |
407 stest = true; | |
408 catch err | |
409 disp(err.message) | |
410 stest = false; | |
411 end | |
412 | |
413 % <AlgoDescription> | |
414 % | |
415 % 1) Check that the last entry in the history of 'out' corresponds to | |
416 % 'interp'. | |
417 % 2) Check that the re-built object is the same as 'out'. | |
418 % | |
419 % </AlgoDescription> | |
420 | |
421 atest = true; | |
422 if stest | |
423 % <AlgoCode> | |
424 % Check the last step in the history of 'out' | |
425 if ~strcmp(out.hist.methodInfo.mname, 'interp'), atest = false; end | |
426 % Check the re-built object | |
427 if ~eq(mout, out, ple2), atest = false; end | |
428 % </AlgoCode> | |
429 else | |
430 atest = false; | |
431 end | |
432 | |
433 % Return a result structure | |
434 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
435 end % END UTP_06 | |
436 | |
437 %% UTP_07 | |
438 | |
439 % <TestDescription> | |
440 % | |
441 % Tests that the interp method can modify the input AO. | |
442 % | |
443 % </TestDescription> | |
444 function result = utp_07 | |
445 | |
446 % <SyntaxDescription> | |
447 % | |
448 % Test that the interp method can modify the input AO by calling with no | |
449 % output. | |
450 % | |
451 % </SyntaxDescription> | |
452 | |
453 try | |
454 % <SyntaxCode> | |
455 % copy at1 to work with | |
456 ain = ao(at1); | |
457 % modify ain | |
458 v = linspace(0,100).'; | |
459 pl = plist('vertices', v); | |
460 aout = ain.interp(pl); | |
461 ain.interp(pl); | |
462 % </SyntaxCode> | |
463 stest = true; | |
464 catch err | |
465 disp(err.message) | |
466 stest = false; | |
467 end | |
468 | |
469 % <AlgoDescription> | |
470 % | |
471 % 1) Check that 'at1' and 'ain' are now different. | |
472 % 2) Check that 'ain' is interp(at1). | |
473 % | |
474 % </AlgoDescription> | |
475 | |
476 atest = true; | |
477 TOL = 7e-14; | |
478 if stest | |
479 % <AlgoCode> | |
480 % Check that interp modified the input by comparing to the copy | |
481 if eq(ao(at1), ain, ple1), atest = false; end | |
482 % Check that interp doesn't modified the input for the function notation | |
483 if ~eq(aout, ain, ple1), atest = false; end | |
484 % Check that the modified input is the interp of the copy | |
485 y = interp1(at1.x, at1.y, v, 'spline', 'extrap'); | |
486 if any(abs(v - ain.x) > TOL), atest = false; end | |
487 if ~isequal(y, ain.y), atest = false; end | |
488 % </AlgoCode> | |
489 else | |
490 atest = false; | |
491 end | |
492 | |
493 % Return a result structure | |
494 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
495 end % END UTP_07 | |
496 | |
497 %% UTP_08 | |
498 | |
499 % <TestDescription> | |
500 % | |
501 % Tests that the interp method keeps the data shape of the input object. | |
502 % | |
503 % </TestDescription> | |
504 function result = utp_08 | |
505 | |
506 % <SyntaxDescription> | |
507 % | |
508 % Test that the interp method keeps the data shape of the input object. The | |
509 % input AO must be an AO with row data and an AO with column data. | |
510 % | |
511 % </SyntaxDescription> | |
512 | |
513 try | |
514 % <SyntaxCode> | |
515 v = linspace(0,100); | |
516 pl = plist('vertices', v); | |
517 out1 = interp(at5, pl); | |
518 out2 = interp(at6, pl); | |
519 % </SyntaxCode> | |
520 stest = true; | |
521 catch err | |
522 disp(err.message) | |
523 stest = false; | |
524 end | |
525 | |
526 % <AlgoDescription> | |
527 % | |
528 % 1) Check that the shpe of the data doesn't change. | |
529 % | |
530 % </AlgoDescription> | |
531 | |
532 atest = true; | |
533 if stest | |
534 % <AlgoCode> | |
535 % Check the shape of the output data | |
536 if size(out1.data.y,2) ~= 1, atest = false; end | |
537 if size(out2.data.y,1) ~= 1, atest = false; end | |
538 % </AlgoCode> | |
539 else | |
540 atest = false; | |
541 end | |
542 | |
543 % Return a result structure | |
544 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
545 end % END UTP_08 | |
546 | |
547 %% UTP_09 | |
548 | |
549 % <TestDescription> | |
550 % | |
551 % Check that the interp method pass back the output objects to a list of | |
552 % output variables or to a single variable. | |
553 % | |
554 % </TestDescription> | |
555 function result = utp_09 | |
556 | |
557 % <SyntaxDescription> | |
558 % | |
559 % Call the method with a list of output variables and with a single output | |
560 % variable. Additionaly check that the rebuild method works on the output. | |
561 % | |
562 % </SyntaxDescription> | |
563 | |
564 try | |
565 % <SyntaxCode> | |
566 v = linspace(0,100); | |
567 pl = plist('vertices', v); | |
568 [o1, o2] = interp(at5, at6, pl); | |
569 o3 = interp(at5, at6, pl); | |
570 mout1 = rebuild(o1); | |
571 mout2 = rebuild(o2); | |
572 mout3 = rebuild(o3); | |
573 % </SyntaxCode> | |
574 stest = true; | |
575 catch err | |
576 disp(err.message) | |
577 stest = false; | |
578 end | |
579 | |
580 % <AlgoDescription> | |
581 % | |
582 % 1) Check that the output contains the right number of objects | |
583 % 2) Check that the 'rebuild' method produces the same object as 'out'. | |
584 % | |
585 % </AlgoDescription> | |
586 | |
587 atest = true; | |
588 if stest | |
589 % <AlgoCode> | |
590 % Check the number of outputs | |
591 if numel(o1) ~=1, atest = false; end | |
592 if numel(o2) ~=1, atest = false; end | |
593 if numel(o3) ~=2, atest = false; end | |
594 % Check the rebuilding of the object | |
595 if ~eq(o1, mout1, ple2), atest = false; end | |
596 if ~eq(o2, mout2, ple2), atest = false; end | |
597 if ~eq(o3, mout3, ple2), atest = false; end | |
598 % </AlgoCode> | |
599 else | |
600 atest = false; | |
601 end | |
602 | |
603 % Return a result structure | |
604 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
605 end % END UTP_09 | |
606 | |
607 %% UTP_10 | |
608 | |
609 % <TestDescription> | |
610 % | |
611 % Tests that the interp method can different interpolations. | |
612 % | |
613 % </TestDescription> | |
614 function result = utp_10 | |
615 | |
616 % <SyntaxDescription> | |
617 % | |
618 % Test that the interp method can all of MATLAB interpolates methods. | |
619 % 'nearest' - Nearest neighbor interpolation | |
620 % 'linear' - Linear interpolation | |
621 % 'spline' - Cubic spline interpolation (see UTPs above) | |
622 % 'pchip' - Piecewise cubic Hermite interpolation | |
623 % | |
624 % </SyntaxDescription> | |
625 | |
626 try | |
627 % <SyntaxCode> | |
628 v = linspace(0,100).'; | |
629 mtd1 = 'nearest'; | |
630 mtd2 = 'linear'; | |
631 mtd3 = 'pchip'; | |
632 pl1 = plist('vertices', v, 'method', mtd1); | |
633 pl2 = plist('vertices', v, 'method', mtd2); | |
634 pl3 = plist('vertices', v, 'method', mtd3); | |
635 out1 = at1.interp(pl1); | |
636 out2 = at1.interp(pl2); | |
637 out3 = at1.interp(pl3); | |
638 mout1 = rebuild(out1); | |
639 mout2 = rebuild(out2); | |
640 mout3 = rebuild(out3); | |
641 % </SyntaxCode> | |
642 stest = true; | |
643 catch err | |
644 disp(err.message) | |
645 stest = false; | |
646 end | |
647 | |
648 % <AlgoDescription> | |
649 % | |
650 % 1) Check the different interpolations | |
651 % 2) Check that the re-built objects are the same as 'out1..3'. | |
652 % | |
653 % </AlgoDescription> | |
654 | |
655 atest = true; | |
656 TOL = 7e-14; | |
657 if stest | |
658 % <AlgoCode> | |
659 % Check the data | |
660 y1 = interp1(at1.x, at1.y, v, mtd1, 'extrap'); | |
661 y2 = interp1(at1.x, at1.y, v, mtd2, 'extrap'); | |
662 y3 = interp1(at1.x, at1.y, v, mtd3, 'extrap'); | |
663 if any(abs(v - out1.x) > TOL), atest = false; end | |
664 if ~isequal(y1, out1.y), atest = false; end | |
665 if any(abs(v - out2.x) > TOL), atest = false; end | |
666 if ~isequal(y2, out2.y), atest = false; end | |
667 if any(abs(v - out3.x) > TOL), atest = false; end | |
668 if ~isequal(y3, out3.y), atest = false; end | |
669 % Check the re-built object | |
670 if ~eq(mout1, out1, ple2), atest = false; end | |
671 if ~eq(mout2, out2, ple2), atest = false; end | |
672 if ~eq(mout3, out3, ple2), atest = false; end | |
673 % </AlgoCode> | |
674 else | |
675 atest = false; | |
676 end | |
677 | |
678 % Return a result structure | |
679 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
680 end % END UTP_10 | |
681 | |
682 end |