comparison testing/utp_1.1/utps/ao/utp_ao_lincom.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_LINCOM a set of UTPs for the ao/lincom method
2 %
3 % M Hewitson 06-08-08
4 %
5 % $Id: utp_ao_lincom.m,v 1.17 2011/04/17 15:46:21 ingo Exp $
6 %
7
8 % <MethodDescription>
9 %
10 % The lincom method of the ao class computes the combination of the input y
11 % and/or x data.
12 %
13 % </MethodDescription>
14
15 function results = utp_ao_lincom(varargin)
16
17 % Check the inputs
18 if nargin == 0
19
20 % Some keywords
21 class = 'ao';
22 mthd = 'lincom';
23
24 results = [];
25 disp('******************************************************');
26 disp(['**** Running UTPs for ' class '/' mthd]);
27 disp('******************************************************');
28
29 % Test AOs
30 [at1,at2,at3,at4,at5,at6,atvec,atmat] = eval(['get_test_objects_' class]);
31
32 % Exception list for the UTPs:
33 [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();
34
35 unit_list = unit.supportedUnits;
36
37 % Run the tests
38 results = [results utp_01]; % getInfo call
39 results = [results utp_02]; % Vector input
40 results = [results utp_04]; % List input
41 results = [results utp_06]; % Test history is working
42 results = [results utp_07]; % Test the modify call works
43 results = [results utp_08]; % Test input data shape == output data shape
44 results = [results utp_11(mthd, [at1 at1], ple1, plist('COEFFS', [1 2]))]; % Test plotinfo doesn't disappear
45 results = [results utp_12]; % Vector + pest input
46 results = [results utp_14]; % List + pest input
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 ~= 1, atest = false; end
115 % Check key
116 if ~io(3).plists.isparam('coeffs'), atest = false; end
117 % Check default value
118 if ~isEmptyDouble(io(3).plists.find('coeffs')), atest = false; end
119 % Check options
120 if ~isequal(io(3).plists.getOptionsForParam('coeffs'), {[]}), atest = false; end
121 end
122 % </AlgoCode>
123 else
124 atest = false;
125 end
126
127 % Return a result structure
128 result = utp_prepare_result(atest, stest, dbstack, mfilename);
129 end % END UTP_01
130
131 %% UTP_02
132
133 % <TestDescription>
134 %
135 % Tests that the lincom method works with a vector of AOs as input.
136 %
137 % </TestDescription>
138 function result = utp_02
139
140 % <SyntaxDescription>
141 %
142 % Test that the lincom method works for a vector of AOs as input.
143 %
144 % </SyntaxDescription>
145
146 try
147 % <SyntaxCode>
148 % Coefficients for combination
149 u_d = unit(cell2mat(utils.math.randelement(unit_list,1)));
150 u_n = unit(cell2mat(utils.math.randelement(unit_list,1)));
151 coeffs = ao([0.34 -0.4], plist('yunits', u_n / u_d));
152 % we need a vector of AOs that are all the same length
153 avec = [at5.setYunits(u_d) at6.setYunits(u_d) coeffs];
154 out = lincom(avec);
155 % </SyntaxCode>
156 stest = true;
157 catch err
158 disp(err.message)
159 stest = false;
160 end
161
162 % <AlgoDescription>
163 %
164 % 1) Check that the number of elements in 'out' is 1
165 % 2) Check that the output AO contains the correct units
166 % 3) Check that the output AO contains the correct data.
167 %
168 % </AlgoDescription>
169
170 atest = true;
171 if stest
172 % <AlgoCode>
173 % Check we have the correct number of outputs
174 if numel(out) ~= 1, atest = false; end
175 % Check we have the correct units
176 if ~isequal(out.yunits, u_n), atest = false; end
177 % Check each output against the linear combination of the inputs
178 com = zeros(size(avec(1).y));
179 for kk = 1:(numel(avec)-1)
180 com = com + avec(numel(avec)).y(kk).*avec(kk).y;
181 end
182 if ~isequal(com, out.data.getY), atest = false; end
183 % </AlgoCode>
184 else
185 atest = false;
186 end
187
188 % Return a result structure
189 result = utp_prepare_result(atest, stest, dbstack, mfilename);
190 end % END UTP_02
191
192
193 %% UTP_04
194
195 % <TestDescription>
196 %
197 % Tests that the lincom method works with a list of AOs as input.
198 %
199 % </TestDescription>
200 function result = utp_04
201
202 % <SyntaxDescription>
203 %
204 % Test that the lincom method works for a list of AOs as input.
205 %
206 % </SyntaxDescription>
207
208 try
209 % <SyntaxCode>
210 % Coefficients for combination
211 u_d = unit(cell2mat(utils.math.randelement(unit_list,1)));
212 u_n = unit(cell2mat(utils.math.randelement(unit_list,1)));
213 coeffs = ao([0.34 -0.4 0.1], plist('yunits', u_n / u_d));
214 out = lincom(at5.setYunits(u_d), at6.setYunits(u_d), at5.setYunits(u_d), coeffs);
215 % </SyntaxCode>
216 stest = true;
217 catch err
218 disp(err.message)
219 stest = false;
220 end
221
222 % <AlgoDescription>
223 %
224 % 1) Check that the number of elements in 'out' is 1.
225 % 2) Check that the output AO contains the correct units
226 % 3) Check that the output AO contains the correct data.
227 %
228 % </AlgoDescription>
229
230 atest = true;
231 if stest
232 % <AlgoCode>
233 % Check we have the correct number of outputs
234 if numel(out) ~= 1, atest = false; end
235 % Check we have the correct units
236 if ~isequal(out.yunits, u_n), atest = false; end
237 % Check each output against the linear combination of the inputs
238 com = coeffs.y(1).*at5.y + coeffs.y(2).*at6.y + coeffs.y(3).*at5.y;
239 if ~isequal(com, out.y), atest = false; end
240 % </AlgoCode>
241 else
242 atest = false;
243 end
244
245 % Return a result structure
246 result = utp_prepare_result(atest, stest, dbstack, mfilename);
247 end % END UTP_04
248
249
250 %% UTP_06
251
252 % <TestDescription>
253 %
254 % Tests that the lincom method properly applies history.
255 %
256 % </TestDescription>
257 function result = utp_06
258
259 % <SyntaxDescription>
260 %
261 % Test that the result of applying the lincom method can be processed back
262 % to an m-file.
263 %
264 % </SyntaxDescription>
265
266 try
267 % <SyntaxCode>
268 a = at1.setYunits('Hz');
269 out = lincom(a, a, ao([1 -1]));
270 mout = rebuild(out);
271 % </SyntaxCode>
272 stest = true;
273 catch err
274 disp(err.message)
275 stest = false;
276 end
277
278 % <AlgoDescription>
279 %
280 % 1) Check that the last entry in the history of 'out' corresponds to
281 % 'lincom'.
282 % 2) Check that the re-built object is the same object as 'out'.
283 %
284 % </AlgoDescription>
285
286 atest = true;
287 if stest
288 % <AlgoCode>
289 % Check the last step in the history of 'out'
290 if ~strcmp(out.hist.methodInfo.mname, 'lincom'), atest = false; end
291 % Check the re-built object
292 if ~eq(mout, out, ple2), atest = false; end
293 % </AlgoCode>
294 else
295 atest = false;
296 end
297
298 % Return a result structure
299 result = utp_prepare_result(atest, stest, dbstack, mfilename);
300 end % END UTP_06
301
302 %% UTP_07
303
304 % <TestDescription>
305 %
306 % Tests that the lincom method can not modify the input AO.
307 %
308 % </TestDescription>
309 function result = utp_07
310
311 % <SyntaxDescription>
312 %
313 % Test that the lincom method can not modify the input AO.
314 % The method must throw an error for the modifier call.
315 %
316 % </SyntaxDescription>
317
318 try
319 % <SyntaxCode>
320 % copy at1 to work with
321 ain = ao(at5);
322 % modify ain
323 ain.lincom(at6, plist('Coeffs', [1 -1]));
324 % </SyntaxCode>
325 stest = false;
326 catch err
327 stest = true;
328 end
329
330 % <AlgoDescription>
331 %
332 % 1) Nothing to check.
333 %
334 % </AlgoDescription>
335
336 atest = true;
337 if stest
338 % <AlgoCode>
339 % </AlgoCode>
340 else
341 atest = false;
342 end
343
344 % Return a result structure
345 result = utp_prepare_result(atest, stest, dbstack, mfilename);
346 end % END UTP_07
347
348 %% UTP_08
349
350 % <TestDescription>
351 %
352 % Test the shape of the output.
353 %
354 % </TestDescription>
355 function result = utp_08
356
357 % <SyntaxDescription>
358 %
359 % Test that the plus method keeps the data shape of the input object. The
360 % input AO must be an AO with row data and an AO with column data.
361 %
362 % </SyntaxDescription>
363
364 try
365 % <SyntaxCode>
366 a = at5.setYunits('Hz');
367 b = at6.setYunits('Hz');
368 out1 = lincom(a, b, ao([1 -1]));
369 out2 = lincom(b, a, ao([1 -1]));
370 % </SyntaxCode>
371 stest = true;
372 catch err
373 disp(err.message)
374 stest = false;
375 end
376
377 % <AlgoDescription>
378 %
379 % 1) Check that the shpe of the output data doesn't change.
380 %
381 % </AlgoDescription>
382
383 atest = true;
384 if stest
385 % <AlgoCode>
386 % Check the shape of the output data
387 if size(out1.data.y, 2) ~= 1, atest = false; end
388 if size(out2.data.y, 1) ~= 1, atest = false; end
389 % </AlgoCode>
390 else
391 atest = false;
392 end
393
394 % Return a result structure
395 result = utp_prepare_result(atest, stest, dbstack, mfilename);
396 end % END UTP_08
397
398 %% UTP_12
399
400 % <TestDescription>
401 %
402 % Tests that the lincom method works with a vector of AOs as input + a pest object.
403 %
404 % </TestDescription>
405 function result = utp_12
406
407 % <SyntaxDescription>
408 %
409 % Test that the lincom method works for a vector of AOs as input.
410 %
411 % </SyntaxDescription>
412
413 try
414 % <SyntaxCode>
415 % Coefficients for combination
416 u_1 = unit(cell2mat(utils.math.randelement(unit_list,1)));
417 u_2 = unit(cell2mat(utils.math.randelement(unit_list,1)));
418 u_c1 = unit(cell2mat(utils.math.randelement(unit_list,1)));
419 coeffs = pest(plist('y', [0.34 -0.4], 'paramnames', {'i','ii'}, 'yunits', {u_c1, u_c1 * u_1 / u_2}));
420 % we need a vector of AOs that are all the same length
421 avec = [at5.setYunits(u_1) at6.setYunits(u_2)];
422 out = lincom(avec, coeffs);
423 % </SyntaxCode>
424 stest = true;
425 catch err
426 disp(err.message)
427 stest = false;
428 end
429
430 % <AlgoDescription>
431 %
432 % 1) Check that the number of elements in 'out' is 1
433 % 2) Check that the output AO contains the correct units
434 % 3) Check that the output AO contains the correct data.
435 %
436 % </AlgoDescription>
437
438 atest = true;
439 if stest
440 % <AlgoCode>
441 % Check we have the correct number of outputs
442 if numel(out) ~= 1, atest = false; end
443 % Check we have the correct units
444 if ~eq(out.yunits, u_c1 * u_1), atest = false; end
445 % Check each output against the linear combination of the inputs
446 com = zeros(size(avec(1).y));
447 for kk = 1:numel(avec)
448 com = com + avec(kk).y.*coeffs.y(kk);
449 end
450 if ~isequal(com, out.data.getY), atest = false; end
451 % </AlgoCode>
452 else
453 atest = false;
454 end
455
456 % Return a result structure
457 result = utp_prepare_result(atest, stest, dbstack, mfilename);
458 end % END UTP_12
459
460
461 %% UTP_14
462
463 % <TestDescription>
464 %
465 % Tests that the lincom method works with a list of AOs as input + a pest object.
466 %
467 % </TestDescription>
468 function result = utp_14
469
470 % <SyntaxDescription>
471 %
472 % Test that the lincom method works for a list of AOs as input.
473 %
474 % </SyntaxDescription>
475
476 try
477 % <SyntaxCode>
478 % Coefficients for combination
479 u_d = unit(cell2mat(utils.math.randelement(unit_list,1)));
480 u_n = unit(cell2mat(utils.math.randelement(unit_list,1)));
481 coeffs = ao([0.34 -0.4 0.1], plist('yunits', u_n / u_d)); % TODO: Use a pest with different units!!
482 out = lincom(at5.setYunits(u_d), at6.setYunits(u_d), at5.setYunits(u_d), coeffs);
483 % </SyntaxCode>
484 stest = true;
485 catch err
486 disp(err.message)
487 stest = false;
488 end
489
490 % <AlgoDescription>
491 %
492 % 1) Check that the number of elements in 'out' is 1.
493 % 2) Check that the output AO contains the correct units
494 % 3) Check that the output AO contains the correct data.
495 %
496 % </AlgoDescription>
497
498 atest = true;
499 if stest
500 % <AlgoCode>
501 % Check we have the correct number of outputs
502 if numel(out) ~= 1, atest = false; end
503 % Check we have the correct units
504 if ~isequal(out.yunits, u_n), atest = false; end
505 % Check each output against the linear combination of the inputs
506 com = coeffs.y(1).*at5.y + coeffs.y(2).*at6.y + coeffs.y(3).*at5.y;
507 if ~isequal(com, out.y), atest = false; end
508 % </AlgoCode>
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_14
516
517 end