comparison testing/utp_1.1/utps/smodel/utp_smodel_setParams.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_SMODEL_SETPARAMS a set of UTPs for the smodel/setParams method
2 %
3 % M Hewitson 06-08-08
4 %
5 % $Id: utp_smodel_setParams.m,v 1.2 2011/05/10 04:53:37 mauro Exp $
6 %
7
8 % <MethodDescription>
9 %
10 % The setParams method of the smodel class sets the 'params' property and
11 % in some cases also the 'values' property
12 %
13 % </MethodDescription>
14
15 function results = utp_smodel_setParams(varargin)
16
17 % Check the inputs
18 if nargin == 0
19
20 % Some keywords
21 cl = 'smodel';
22 mthd = 'setParams';
23
24 disp('******************************************************');
25 disp(['**** Running UTPs for ' cl '/' mthd]);
26 disp('******************************************************');
27
28 s0 = smodel();
29
30 s1 = smodel('a*x1 + b*x2 + c*x3');
31 s1.setParams({'a', 'b'});
32 s1.setName();
33
34 s2 = smodel('a*x1 + b*x2 + c*x3');
35 s2.setParams({'a', 'b'}, [1 2]);
36 s2.setName();
37
38 s3 = smodel('a*x1 + b*x2 + c*x3');
39 s3.setParams({'a', 'b'}, [1 2]);
40 s3.setName();
41
42 sv = [s1, s2, s1];
43 sm = [s1, s2, s1; s1, s2, s1];
44
45 % Exception list for the UTPs:
46 [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();
47
48 % The setter method have different possibilities to set a property
49 args1 = {{'a', 'b'}}; % obj.setParams({'a', 'b'})
50 args2 = {{'a', 'b'}, [1 2]}; % obj.setParams({'a', 'b'}, [1 2])
51 args3 = {{'a', 'b'}, {1 2}}; % obj.setParams({'a', 'b'}, {1 2})
52 args4 = {'a', 3}; % obj.setParams('a', 3)
53 args5 = {'a1', 'a4'}; % obj.setParams('a1', 'a4')
54 args6 = {plist('params', 'a')};
55 args7 = {plist('params', {'a', 'b'})};
56 args8 = {plist('params', 'a', 'values', 1)};
57 args9 = {plist('params', {'a', 'b'}, 'values', [1 2])};
58
59 % Run the general tests
60 results(1) = utp_01();
61 results(2) = utp_genericAnyShape(mthd, s0, args1, @algoTests);
62 results(3) = utp_genericAnyShape(mthd, s0, args2, @algoTests);
63 results(4) = utp_genericAnyShape(mthd, s1, args3, @algoTests);
64 results(5) = utp_genericAnyShape(mthd, s2, args4, @algoTests);
65 results(6) = utp_genericAnyShape(mthd, s3, args5, @algoTests);
66 results(7) = utp_genericAnyShape(mthd, s0, args6, @algoTests);
67 results(8) = utp_genericAnyShape(mthd, s0, args7, @algoTests);
68 results(9) = utp_genericAnyShape(mthd, s1, args8, @algoTests);
69 results(10) = utp_genericAnyShape(mthd, s1, args9, @algoTests);
70 results(11) = utp_genericAnyShape(mthd, sv, args3, @algoTests);
71 results(12) = utp_genericAnyShape(mthd, sm, args8, @algoTests);
72 results(13) = utp_genericList(mthd, s1, s2, s3, args7, @algoTests);
73 results(14) = utp_genericHistory(mthd, s1, args2, ple2);
74 results(15) = utp_genericModify(mthd, s1, args5, @algoTests, ple1);
75 results(16) = utp_genericOutput(mthd, s1, s2, args2, @algoTests, ple2); % test the outputs
76
77 disp('Done.');
78 disp('******************************************************');
79
80 elseif nargin == 1 % Check for UTP functions
81 if strcmp(varargin{1}, 'isutp')
82 results = 1;
83 else
84 results = 0;
85 end
86 else
87 error('### Incorrect inputs')
88 end
89
90 % Check that the property have the correct value
91 function atest = algoTests(in, out, idx, argsin)
92 atest = true;
93 % Check that the input and output objects are the same except the
94 % property 'params' and the history
95 ple = plist('EXCEPTIONS', {'created', 'proctime', 'UUID', 'hist', 'params', 'values'});
96 if ~eq(in(idx), out(idx), ple), atest = false; end
97 if ~eq(in(idx).hist, out(idx).hist.inhists), atest = false; end
98 % Check that the parameter gets the correct value.
99 values = {};
100 if isa(argsin{1}, 'plist') && argsin{1}.isparam('params')
101 argsin = argsin{1};
102 % The value was set with a plist.
103 params = cellstr(argsin.find('params'));
104 if argsin.isparam('values')
105 values = argsin.find('values');
106 end
107 elseif numel(argsin) == 1 && iscell(argsin{1})
108 params = argsin{1};
109 elseif numel(argsin) == 2 && iscell(argsin{1}) && ( iscell(argsin{2}) || isnumeric(argsin{2}))
110 params = argsin{1};
111 if iscell(argsin{2})
112 values = argsin{2};
113 else
114 values = argsin{2};
115 end
116 elseif numel(argsin) == 2 && ischar(argsin{1}) && isnumeric(argsin{2})
117 params = argsin(1);
118 values = argsin(2);
119 elseif iscellstr(argsin)
120 params = argsin;
121 end
122
123 if isnumeric(values)
124 values = num2cell(values);
125 end
126
127 p = in(idx).params;
128 v = in(idx).values;
129
130 if ~isempty(values) && isempty(v)
131 v = cell(size(p));
132 end
133
134 for ll = 1:numel(params)
135 paramsIdx = strcmp(p, params{ll});
136 if any(paramsIdx)
137 if ~isempty(values)
138 v{paramsIdx} = values{ll};
139 end
140 else
141 % Append the alias key-value pair
142 p = [p params(ll)];
143 if ~isempty(values)
144 v = [v values(ll)];
145 elseif ~isempty(v)
146 v = [v cell(1)];
147 end
148 end
149 end
150
151 % Check 'params' and 'values'
152 if ~isequal(out(idx).params, p), atest = false; end
153 if ~isequal(out(idx).values, v), atest = false; end
154
155 % Check if we have specified some values that the number of values ans
156 % params are the same
157 if ~isempty(values)
158 if numel(out(idx).params) ~= numel(out(idx).values), atest = false; end
159 end
160 end
161
162 %% UTP_01
163
164 % <TestDescription>
165 %
166 % Tests that the getInfo call works for this method.
167 %
168 % </TestDescription>
169 function result = utp_01
170
171
172 % <SyntaxDescription>
173 %
174 % Test that the getInfo call works for no sets, all sets, and each set
175 % individually.
176 %
177 % </SyntaxDescription>
178
179 try
180 % <SyntaxCode>
181 % Call for no sets
182 io(1) = feval('smodel.getInfo', mthd, 'none');
183 % Call for all sets
184 io(2) = feval('smodel.getInfo', mthd);
185 % Call for each set
186 for kk=1:numel(io(2).sets)
187 io(kk+2) = feval('smodel.getInfo', mthd, io(2).sets{kk});
188 end
189 % </SyntaxCode>
190 stest = true;
191 catch err
192 disp(err.message)
193 stest = false;
194 end
195
196 % <AlgoDescription>
197 %
198 % 1) Check that getInfo call returned an minfo object in all cases.
199 % 2) Check that all plists have the correct parameters.
200 %
201 % </AlgoDescription>
202
203 atest = true;
204 if stest
205 % <AlgoCode>
206 % check we have minfo objects
207 if isa(io, 'minfo')
208 % SET 'None'
209 if ~isempty(io(1).sets), atest = false; end
210 if ~isempty(io(1).plists), atest = false; end
211 % Check all Sets
212 if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end
213 if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
214 % SET 'Default'
215 if io(3).plists.nparams ~= 2, atest = false; end
216 % Check key
217 if ~io(3).plists.isparam('params'), atest = false; end
218 if ~io(3).plists.isparam('values'), atest = false; end
219 % Check default value
220 if ~isEmptyCell(io(3).plists.find('params')), atest = false; end
221 if ~isEmptyCell(io(3).plists.find('values')), atest = false; end
222 % Check options
223 if ~isequal(io(3).plists.getOptionsForParam('params'), {{}}), atest = false; end
224 if ~isequal(io(3).plists.getOptionsForParam('values'), {{}}), atest = false; end
225 end
226 % </AlgoCode>
227 else
228 atest = false;
229 end
230
231 % Return a result structure
232 result = utp_prepare_result(atest, stest, dbstack, mfilename);
233 end % END UTP_01
234
235 end