Mercurial > hg > ltpda
view testing/utp_1.1/utps/ao/utp_ao_polyfit.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 |
line wrap: on
line source
% UTP_AO_POLYFIT a set of UTPs for the ao/polyfit method % % M Hewitson 06-08-08 % % $Id: utp_ao_polyfit.m,v 1.20 2010/05/05 04:20:10 mauro Exp $ % % <MethodDescription> % % The polyfit method of the ao class fits a polynomial to the input y % and/or x data. % % </MethodDescription> function results = utp_ao_polyfit(varargin) % Check the inputs if nargin == 0 % Some keywords class = 'ao'; mthd = 'polyfit'; results = []; disp('******************************************************'); disp(['**** Running UTPs for ' class '/' mthd]); disp('******************************************************'); % Test AOs [at1,at2,at3,at4,at5,at6,atvec,atmat] = eval(['get_test_objects_' class]); % Exception list for the UTPs: [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples(); % Run the tests results = [results utp_01]; % getInfo call results = [results utp_02]; % Vector input results = [results utp_03]; % Matrix input results = [results utp_04]; % List input results = [results utp_05]; % Test with mixed input results = [results utp_06]; % Test history is working results = [results utp_07]; % Test the modify call works results = [results utp_08]; % Test that units are properly handled results = [results utp_09]; % Test against Matlab polyfit disp('Done.'); disp('******************************************************'); elseif nargin == 1 % Check for UTP functions if strcmp(varargin{1}, 'isutp') results = 1; else results = 0; end else error('### Incorrect inputs') end %% UTP_01 % <TestDescription> % % Tests that the getInfo call works for this method. % % </TestDescription> function result = utp_01 % <SyntaxDescription> % % Test that the getInfo call works for no sets, all sets, and each set % individually. % % </SyntaxDescription> try % <SyntaxCode> % Call for no sets io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']); % Call for all sets io(2) = eval([class '.getInfo(''' mthd ''')']); % Call for each set for kk=1:numel(io(2).sets) io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']); end % </SyntaxCode> stest = true; catch err disp(err.message) stest = false; end % <AlgoDescription> % % 1) Check that getInfo call returned an minfo object in all cases. % 2) Check that all plists have the correct parameters. % % </AlgoDescription> atest = true; if stest % <AlgoCode> % check we have minfo objects if isa(io, 'minfo') %%% SET 'None' if ~isempty(io(1).sets), atest = false; end if ~isempty(io(1).plists), atest = false; end %%% Check all Sets if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end %%%%%%%%%% SET 'Default' if io(3).plists.nparams ~= 2, atest = false; end % Check key if ~io(3).plists.isparam('N'), atest = false; end if ~io(3).plists.isparam('rescale'), atest = false; end % Check default value if ~isequal(io(3).plists.find('N'), 1), atest = false; end if ~isequal(io(3).plists.find('rescale'), false), atest = false; end % Check options if ~isequal(io(3).plists.getOptionsForParam('N'), {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}), atest = false; end if ~isequal(io(3).plists.getOptionsForParam('rescale'), {false, true}), atest = false; end end % </AlgoCode> else atest = false; end % Return a result structure result = utp_prepare_result(atest, stest, dbstack, mfilename); end % END UTP_01 %% UTP_02 % <TestDescription> % % Tests that the polyfit method fails with with a vector of AOs as input. % % </TestDescription> function result = utp_02 % <SyntaxDescription> % % Test that the polyfit method works with a vector of AOs as input. % % </SyntaxDescription> try % <SyntaxCode> N = 2; % Call with single output out = polyfit(atvec, plist('N', N)); % Call with multiple output [out_1, out_2, out_3] = polyfit(atvec, plist('N', N)); % </SyntaxCode> stest = true; catch err disp(err.message) stest = false; end % <AlgoDescription> % % 1) Check we have the correct number of output objects in the version with % single output % 2) Check we have the correct number of elements in the output objects % % </AlgoDescription> atest = true; if stest % <AlgoDescription> % 1) Check we have the correct number of output objects in the version with % single output % 2) Check we have the correct number of elements in the output objects % % </AlgoDescription> % <AlgoCode> if ~isequal(size(out), [1 numel(atvec)]), atest = false; end for jj = 1:numel(out) if ~isequal(numel(out(jj).y), N+1), atest = false; end end if ~isequal(numel(out_1.y), N+1), atest = false; end if ~isequal(numel(out_2.y), N+1), atest = false; end if ~isequal(numel(out_3.y), N+1), atest = false; end % </AlgoCode> else atest = false; end % Return a result structure result = utp_prepare_result(atest, stest, dbstack, mfilename); end % END UTP_02 %% UTP_03 % <TestDescription> % % Tests that the polyfit method works with a matrix of AOs as input. % % </TestDescription> function result = utp_03 % <SyntaxDescription> % % Test that the polyfit method works for a matrix of AOs as input. % % </SyntaxDescription> try % <SyntaxCode> N = 3; amat = [at1 at2 at5; at6 at5 at2]; out = polyfit(amat, plist('N', N)); % </SyntaxCode> stest = true; catch err disp(err.message) stest = false; end % <AlgoDescription> % % 1) Check we have the correct number of output objects % % </AlgoDescription> atest = true; if stest % <AlgoCode> if ~isequal(numel(out), numel(amat)), atest = false; end for jj = 1:numel(out) if ~isequal(numel(out(jj).y), N+1), atest = false; end end % </AlgoCode> else atest = false; end % Return a result structure result = utp_prepare_result(atest, stest, dbstack, mfilename); end % END UTP_03 %% UTP_04 % <TestDescription> % % Tests that the polyfit method works with a list of AOs as input. % % </TestDescription> function result = utp_04 % <SyntaxDescription> % % Test that the polyfit method works for a list of AOs as input. % % </SyntaxDescription> try % <SyntaxCode> N = 1; % Call with single output out = polyfit(at5, at6, at5, plist('N', N)); % Call with multiple output [out_1, out_2, out_3] = polyfit(at5, at6, at5, plist('N', N)); % </SyntaxCode> stest = true; catch err disp(err.message) stest = false; end % <AlgoDescription> % % 1) Check we have the correct number of output objects in the version with % single output % 2) Check we have the correct number of elements in the output objects % % </AlgoDescription> atest = true; if stest % <AlgoDescription> % 1) Check we have the correct number of output objects in the version with % single output % 2) Check we have the correct number of elements in the output objects % % </AlgoDescription> % <AlgoCode> if ~isequal(size(out), [1 3]), atest = false; end for jj = 1:numel(out) if ~isequal(numel(out(jj).y), N+1), atest = false; end end if ~isequal(numel(out_1.y), N+1), atest = false; end if ~isequal(numel(out_2.y), N+1), atest = false; end if ~isequal(numel(out_3.y), N+1), atest = false; end % </AlgoCode> else atest = false; end % Return a result structure result = utp_prepare_result(atest, stest, dbstack, mfilename); end % END UTP_04 %% UTP_05 % <TestDescription> % % Tests that the polyfit method works with a mix of different shaped AOs as % input. % % </TestDescription> function result = utp_05 % <SyntaxDescription> % % Test that the polyfit method works with an input of matrices and vectors % and single AOs. % % </SyntaxDescription> try % <SyntaxCode> amat = [at1 at2 at5; at6 at5 at2]; avec = [at1 at2]; out = polyfit(at5, avec, at6, amat, at5); % </SyntaxCode> stest = true; catch err disp(err.message) stest = false; end % <AlgoDescription> % % 1) Check we have the correct number of output objects % % </AlgoDescription> atest = true; if stest % <AlgoCode> N = find(ao.getInfo('polyfit').plists, 'N'); if ~isequal(numel(out), numel(amat) + numel(avec) + 3), atest = false; end for jj = 1:numel(out) if ~isequal(numel(out(jj).y), N+1), atest = false; end end % </AlgoCode> else atest = false; end % Return a result structure result = utp_prepare_result(atest, stest, dbstack, mfilename); end % END UTP_05 %% UTP_06 % <TestDescription> % % Tests that the polyfit method properly applies history. % % </TestDescription> function result = utp_06 % <SyntaxDescription> % % Test that the result of applying the polyfit method can be processed back % to an m-file. % % </SyntaxDescription> try % <SyntaxCode> out1 = polyfit(at1, plist('rescale', 'true')); out2 = polyfit(at1, plist('vector_out', false)); mout1 = rebuild(out1); mout2 = rebuild(out2); % </SyntaxCode> stest = true; catch err disp(err.message) stest = false; end % <AlgoDescription> % % 1) Check that the last entry in the history of 'out1' corresponds to % 'polyfit'. % 2) Check that the re-built object is the same object as 'out'. % % </AlgoDescription> atest = true; if stest % <AlgoCode> % Check the last step in the history of 'out1' (matrix object) if ~strcmp(out1.hist.methodInfo.mname, 'polyfit'), atest = false; end if ~strcmp(out2.hist.methodInfo.mname, 'polyfit'), atest = false; end % Check the re-built object if ~eq(mout1, out1, ple2), atest = false; end if ~eq(mout2, out2, ple2), atest = false; end % </AlgoCode> else atest = false; end % Return a result structure result = utp_prepare_result(atest, stest, dbstack, mfilename); end % END UTP_06 %% UTP_07 % <TestDescription> % % Tests that the polyfit method cannot modify the input AO. % % </TestDescription> function result = utp_07 % <SyntaxDescription> % % Test that the polyfit method cannot modify the input AO by calling with no % output and that the method doesn't change the input of the function % notation (with a equal sign). % % </SyntaxDescription> try % <SyntaxCode> % copy at1 to work with ain = ao(at1); % modify ain aout = ain.polyfit(); ain.polyfit(); % </SyntaxCode> stest = true; catch err stest = true; end % <AlgoDescription> % % 1) Nothing to check % % </AlgoDescription> atest = true; if stest % <AlgoCode> % </AlgoCode> else atest = false; end % Return a result structure result = utp_prepare_result(atest, stest, dbstack, mfilename); end % END UTP_07 %% UTP_08 % <TestDescription> % % Tests that the polyfit method handles units correctly. % % </TestDescription> function result = utp_08 % <SyntaxDescription> % % Tests that the polyfit method handles units correctly. % % </SyntaxDescription> try % <SyntaxCode> % Input data nsecs = 100; fs = 10; u = get_random_unit(); x1 = ao(plist('tsfcn', 'randn(size(t)) + 0.02*t + 5', ... 'fs', fs, 'nsecs', nsecs, ... 'yunits', u)); x2 = ao(plist('tsfcn', 'randn(size(t)) + 0.02*t + 5', ... 'fs', fs, 'nsecs', nsecs, ... 'yunits', '')); % Settings for polyfit N = 2; out1 = polyfit(x1, plist('N', N)); out2 = polyfit(x2, plist('N', N)); % </SyntaxCode> stest = true; catch err disp(err.message) stest = false; end % <AlgoDescription> % % 1) Check that the units in both cases are yunits/(xunits)^(j) % % </AlgoDescription> atest = true; if stest % <AlgoCode> % 1) Check that the units in both cases are yunits/(xunits)^(j) for jj = 1:N+1 if ~isequal(out1.yunits(jj), x1.yunits./(x1.xunits).^(N+1-jj)), atest = false; end end for jj = 1:N+1 if ~isequal(out2.yunits(jj), x2.yunits./(x2.xunits).^(N+1-jj)), atest = false; end end % </AlgoCode> else atest = false; end % Return a result structure result = utp_prepare_result(atest, stest, dbstack, mfilename); end % END UTP_08 %% UTP_09 % <TestDescription> % % Tests that the ao/polyfit method agrees with MATLAB's polyfit when % configured to use the same parameters. % % </TestDescription> function result = utp_09 % <SyntaxDescription> % % Test that applying polyfit works on a single AO. % % </SyntaxDescription> % <SyntaxCode> try % Make test AO nsecs = 100; fs = 10; unit_list = unit.supportedUnits; u = unit(cell2mat(utils.math.randelement(unit_list,1))); a1 = ao(plist('nsecs', nsecs, 'fs', fs, ... 'tsfcn', 'polyval([3 2 1 ], t) + 1000*randn(size(t))', 'xunits', 's', 'yunits', u)); N = 4; pl = plist('N', N); p1 = a1.polyfit(pl.pset('rescale', true)); p2 = a1.polyfit(pl.pset('rescale', false)); stest = true; catch err disp(err.message) stest = false; end % </SyntaxCode> % <AlgoDescription> % % 1) Check that output agrees with the output of MATLAB's polyfit. % % </AlgoDescription> % <AlgoCode> atest = true; if stest % Call Matlab polyfit with rescale [p,s,mu] = polyfit(a1.x, a1.y, N); if ~isequal(p, p1.y') || ~isequal(s, find(p1.procinfo, 's')) || ~isequal(mu, find(p1.procinfo, 'mu')) atest = false; end % Call Matlab polyfit without rescale [p,s] = polyfit(a1.x, a1.y, N); if ~isequal(p, p2.y') || ~isequal(s, find(p2.procinfo, 's')) atest = false; end else atest = false; end % </AlgoCode> % Return a result structure result = utp_prepare_result(atest, stest, dbstack, mfilename); end % END UTP_09 end