comparison testing/utp_1.1/utps/plist/utp_plist_pset.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_PLIST_PSET a set of UTPs for the plist/pset method
2 %
3 % M Hewitson 06-08-08
4 %
5 % $Id: utp_plist_pset.m,v 1.4 2010/09/02 17:50:44 nicolodi Exp $
6 %
7
8 % <MethodDescription>
9 %
10 % The pset method of the plist class set or add a key/value pair a
11 % param-object into the parameter list. Exist the key in the parameter list
12 % then becomes the value the new value.
13 %
14 % </MethodDescription>
15
16 function results = utp_plist_pset(varargin)
17
18 % Check the inputs
19 if nargin == 0
20
21 % Some keywords
22 class = 'plist';
23 mthd = 'pset';
24
25 results = [];
26 disp('******************************************************');
27 disp(['**** Running UTPs for ' class '/' mthd]);
28 disp('******************************************************');
29
30 % Test PLIST objects
31 pl1 = plist('a', 1);
32 pl2 = plist('b', 2);
33 pl3 = plist('c', 3);
34 pl4 = plist('d', 4, 'e', 5);
35 plv = [pl1, pl2, pl3];
36 plm = [pl1, pl2; pl3 pl4];
37
38 % Exception list for the UTPs:
39 [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();
40
41 % Run the tests
42 results = [results utp_01]; % getInfo call
43 results = [results utp_02]; % Vector input
44 results = [results utp_03]; % Matrix input
45 results = [results utp_04]; % List input
46 results = [results utp_05]; % Test with mixed input
47 results = [results utp_06]; % Test the modify call works
48 results = [results utp_07]; % Test with different inputs for the key/value pair
49 results = [results utp_08]; % Test setting and appending case.
50 results = [results utp_09]; % Test capital letter of the key.
51
52 disp('Done.');
53 disp('******************************************************');
54
55 elseif nargin == 1 % Check for UTP functions
56 if strcmp(varargin{1}, 'isutp')
57 results = 1;
58 else
59 results = 0;
60 end
61 else
62 error('### Incorrect inputs')
63 end
64
65 %% UTP_01
66
67 % <TestDescription>
68 %
69 % Tests that the getInfo call works for this method.
70 %
71 % </TestDescription>
72 function result = utp_01
73
74
75 % <SyntaxDescription>
76 %
77 % Test that the getInfo call works for no sets, all sets, and each set
78 % individually.
79 %
80 % </SyntaxDescription>
81
82 try
83 % <SyntaxCode>
84 % Call for no sets
85 io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']);
86 % Call for all sets
87 io(2) = eval([class '.getInfo(''' mthd ''')']);
88 % Call for each set
89 for kk=1:numel(io(2).sets)
90 io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']);
91 end
92 % </SyntaxCode>
93 stest = true;
94 catch err
95 disp(err.message)
96 stest = false;
97 end
98
99 % <AlgoDescription>
100 %
101 % 1) Check that getInfo call returned an minfo object in all cases.
102 % 2) Check that all plists have the correct parameters.
103 %
104 % </AlgoDescription>
105
106 atest = true;
107 if stest
108 % <AlgoCode>
109 % check we have minfo objects
110 if isa(io, 'minfo')
111 % SET 'None'
112 if ~isempty(io(1).sets), atest = false; end
113 if ~isempty(io(1).plists), atest = false; end
114 % Check all Sets
115 if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end
116 if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
117 % SET 'Default'
118 if io(3).plists.nparams ~= 0, atest = false; end
119 % Check key
120 % Check default value
121 % Check options
122 end
123 % </AlgoCode>
124 else
125 atest = false;
126 end
127
128 % Return a result structure
129 result = utp_prepare_result(atest, stest, dbstack, mfilename);
130 end % END UTP_01
131
132 %% UTP_02
133
134 % <TestDescription>
135 %
136 % Tests that the pset method works with a vector of PLIST objects as input.
137 %
138 % </TestDescription>
139 function result = utp_02
140
141 % <SyntaxDescription>
142 %
143 % Test that the pset method set or add a key/value pair to all PLIST
144 % objects in the vector.
145 %
146 % </SyntaxDescription>
147
148 try
149 % <SyntaxCode>
150 out = pset(plv, 'new key', 'val');
151 % </SyntaxCode>
152 stest = true;
153 catch err
154 disp(err.message)
155 stest = false;
156 end
157
158 % <AlgoDescription>
159 %
160 % 1) Check that the shape of the output is the same as the shape of the input
161 % 2) Check that the output PLIST contains the new key/value pair.
162 %
163 % </AlgoDescription>
164
165 atest = true;
166 if stest
167 % <AlgoCode>
168 % Check that the shape of the output is the same as the shape of the input
169 if ~isa(out, 'plist') && size(out) == size(plv), atest = false; end
170 % Check that the output have all key/value pairs
171 for ii = 1:numel(plv)
172 % The method plist/find is tested in the UTP utp_plist_find
173 if ~isequal(out(ii).find('new key'), 'val'), atest = false; end
174 end
175 % </AlgoCode>
176 else
177 atest = false;
178 end
179
180 % Return a result structure
181 result = utp_prepare_result(atest, stest, dbstack, mfilename);
182 end % END UTP_02
183
184 %% UTP_03
185
186 % <TestDescription>
187 %
188 % Tests that the pset method works with a matrix of PLIST objects as input.
189 %
190 % </TestDescription>
191 function result = utp_03
192
193 % <SyntaxDescription>
194 %
195 % Test that the pset method set or add a key/value pair to all PLIST
196 % objects in the matrix.
197 %
198 % </SyntaxDescription>
199
200 try
201 % <SyntaxCode>
202 out = pset(plm, 'new key', 'val');
203 % </SyntaxCode>
204 stest = true;
205 catch err
206 disp(err.message)
207 stest = false;
208 end
209
210 % <AlgoDescription>
211 %
212 % 1) Check that the shape of the output is the same as the shape of the input
213 % 2) Check that the output PLIST contains the new key/value pair.
214 %
215 % </AlgoDescription>
216
217 atest = true;
218 if stest
219 % <AlgoCode>
220 % Check that the shape of the output is the same as the shape of the input
221 if ~isa(out, 'plist') && size(out) == size(plm), atest = false; end
222 % Check that the output have all key/value pairs
223 for ii = 1:numel(plm)
224 % The method plist/find is tested in the UTP utp_plist_find
225 if ~isequal(out(ii).find('new key'), 'val'), atest = false; end
226 end
227 % </AlgoCode>
228 else
229 atest = false;
230 end
231
232 % Return a result structure
233 result = utp_prepare_result(atest, stest, dbstack, mfilename);
234 end % END UTP_03
235
236 %% UTP_04
237
238 % <TestDescription>
239 %
240 % Tests that the pset method works with a list of PLIST objects as input.
241 %
242 % </TestDescription>
243 function result = utp_04
244
245 % <SyntaxDescription>
246 %
247 % Test that the pset method set or add a key/value pair to all PLIST
248 % objects in the input.
249 %
250 % </SyntaxDescription>
251
252 try
253 % <SyntaxCode>
254 out = pset(pl1, pl2, pl3, 'new key', 'val');
255 % </SyntaxCode>
256 stest = true;
257 catch err
258 disp(err.message)
259 stest = false;
260 end
261
262 % <AlgoDescription>
263 %
264 % 1) Check that the shape of the output is the same as the shape of the input
265 % 2) Check that the output PLIST contains the new key/value pair.
266 %
267 % </AlgoDescription>
268
269 atest = true;
270 plin = [pl1, pl2, pl3];
271 if stest
272 % <AlgoCode>
273 % Check that the shape of the output is the same as the shape of the input
274 if ~isa(out, 'plist') && size(out) == size(plin), atest = false; end
275 % Check that the output have all key/value pairs
276 for ii = 1:numel(plin)
277 % The method plist/find is tested in the UTP utp_plist_find
278 if ~isequal(out(ii).find('new key'), 'val'), atest = false; end
279 end
280 % </AlgoCode>
281 else
282 atest = false;
283 end
284
285 % Return a result structure
286 result = utp_prepare_result(atest, stest, dbstack, mfilename);
287 end % END UTP_04
288
289 %% UTP_05
290
291 % <TestDescription>
292 %
293 % Tests that the pset method works with a mix of different shaped PLIST
294 % objects as input.
295 %
296 % </TestDescription>
297 function result = utp_05
298
299 % <SyntaxDescription>
300 %
301 % Test that the pset method set or add a key/value pair to all PLIST
302 % objects in the input.
303 %
304 % </SyntaxDescription>
305
306 try
307 % <SyntaxCode>
308 out = pset(plm, pl2, plv, 'new key', 'val');
309 % </SyntaxCode>
310 stest = true;
311 catch err
312 disp(err.message)
313 stest = false;
314 end
315
316 % <AlgoDescription>
317 %
318 % 1) Check that the shape of the output is the same as the shape of the input
319 % 2) Check that the output PLIST contains the new key/value pair.
320 %
321 % </AlgoDescription>
322
323 atest = true;
324 plin = [reshape(plm, 1, []), pl2, reshape(plv, 1, [])];
325 if stest
326 % <AlgoCode>
327 % Check that the shape of the output is the same as the shape of the input
328 if ~isa(out, 'plist') && size(out) == size(plin), atest = false; end
329 % Check that the output have all key/value pairs
330 for ii = 1:numel(plin)
331 % The method plist/find is tested in the UTP utp_plist_find
332 if ~isequal(out(ii).find('new key'), 'val'), atest = false; end
333 end
334 % </AlgoCode>
335 else
336 atest = false;
337 end
338
339 % Return a result structure
340 result = utp_prepare_result(atest, stest, dbstack, mfilename);
341 end % END UTP_05
342
343 %% UTP_06
344
345 % <TestDescription>
346 %
347 % Tests that the pset method applies the modify command
348 %
349 % </TestDescription>
350 function result = utp_06
351
352 % <SyntaxDescription>
353 %
354 % Test that the pset method can modify the input PLIST by calling with no
355 % output and that the method doesn't change the input of the function
356 % notation (with a equal sign).
357 %
358 % </SyntaxDescription>
359
360 try
361 % <SyntaxCode>
362 % copy at1 to work with
363 pleq = plist(pl1);
364 plmo = plist(pl1);
365 % modify ain
366 out = pleq.pset('new key', 'val');
367 plmo.pset('new key', 'val');
368 % </SyntaxCode>
369 stest = true;
370 catch err
371 disp(err.message)
372 stest = false;
373 end
374
375 % <AlgoDescription>
376 %
377 % 1) Check that 'out' and 'pleq' are now different.
378 % 2) Check that 'plmo' and 'out' are the same.
379 % 3) Check that 'out' and 'plmo' have the new key/value pair
380 % 4) Check that pleq don't have the new key/value pair
381 %
382 % </AlgoDescription>
383
384 atest = true;
385 if stest
386 % <AlgoCode>
387 % Check that 'out' and 'pleq' are not the same
388 if eq(out, pleq, ple1), atest = false; end
389 % Check that 'out' have the new key/value pair and 'pleq' not
390 if ~isequal(out.find('new key'), 'val'), atest = false; end
391 if isequal(pleq.find('new key'), 'val'), atest = false; end
392 % Check 'plmo' have the new key/value pair
393 if ~isequal(plmo.find('new key'), 'val'), atest = false; end
394 % Check that 'out' and plmo are the same
395 if ~eq(out, plmo, ple1), atest = false; end
396 % </AlgoCode>
397 else
398 atest = false;
399 end
400
401 % Return a result structure
402 result = utp_prepare_result(atest, stest, dbstack, mfilename);
403 end % END UTP_06
404
405 %% UTP_07
406
407 % <TestDescription>
408 %
409 % Test the pset method works for different input variants for the key/value
410 % pair.
411 %
412 % </TestDescription>
413 function result = utp_07
414
415 % <SyntaxDescription>
416 %
417 % Test that the pset method accepts param-objects or direct key/value
418 % pairs as an input.
419 %
420 % </SyntaxDescription>
421
422 try
423 % <SyntaxCode>
424 p1 = param('p1', 1);
425 p2 = param('p2', 2);
426 p3 = param('p3', 3);
427 out1 = pl1.pset([p1, p2], p3);
428 out2 = pl1.pset('aa', 1, 'bb', 2);
429 % In this case overwirtes the key 'p3' the value of the param p3
430 out3 = pl1.pset(p1, 'aa', 1, [p2 p3], 'p3', 13);
431 % </SyntaxCode>
432 stest = true;
433 catch err
434 disp(err.message)
435 stest = false;
436 end
437
438 % <AlgoDescription>
439 %
440 % 1) Check that the output have all key/value pairs.
441 %
442 % </AlgoDescription>
443
444 atest = true;
445 try
446 % do not run algorithm tests if sintax tests failed
447 assert(stest);
448
449 % <AlgoCode>
450 % Check the key 'p1'
451 assert(isequal(out1.find('p1'), 1));
452 assert(isequal(out3.find('p1'), 1));
453 % Check the key 'p2'
454 assert(isequal(out1.find('p2'), 2));
455 assert(isequal(out3.find('p2'), 2));
456 % Check the key 'p3'
457 assert(isequal(out1.find('p3'), 3));
458 assert(isequal(out3.find('p3'), 13));
459 % Check the key 'aa'
460 assert(isequal(out2.find('aa'), 1));
461 assert(isequal(out3.find('aa'), 1));
462 % Check the key 'bb'
463 assert(isequal(out2.find('bb'), 2));
464 % </AlgoCode>
465 catch ex
466 atest = false;
467 end
468
469 % Return a result structure
470 result = utp_prepare_result(atest, stest, dbstack, mfilename);
471 end % END UTP_07
472
473 %% UTP_08
474
475 % <TestDescription>
476 %
477 % Test the pset method in the setting and appending case.
478 %
479 % </TestDescription>
480 function result = utp_08
481
482 % <SyntaxDescription>
483 %
484 % Create an example where the pset method set a key to a new value and
485 % an example where pset append the new key/value pair
486 %
487 % </SyntaxDescription>
488
489 try
490 % <SyntaxCode>
491 pl_set = plist('a', 1, 'b', 2, 'c', 3);
492 pl_app = plist('a', 1, 'b', 2, 'c', 3);
493 pl_set.pset('a', 13);
494 pl_app.pset('d', 13);
495 % </SyntaxCode>
496 stest = true;
497 catch err
498 disp(err.message)
499 stest = false;
500 end
501
502 % <AlgoDescription>
503 %
504 % 1) Check the number of parametes in the output.
505 % 2) Check the new key/value pair
506 %
507 % </AlgoDescription>
508
509 atest = true;
510 if stest
511 % <AlgoCode>
512 % Check the correct number of paramter in the output
513 if pl_set.nparams ~= 3, atest = false; end
514 if pl_app.nparams ~= 4, atest = false; end
515 % Check the new key/value pair
516 if ~isequal(pl_set.find('a'), 13), atest = false; end
517 if ~isequal(pl_app.find('d'), 13), atest = false; end
518 % </AlgoCode>
519 else
520 atest = false;
521 end
522
523 % Return a result structure
524 result = utp_prepare_result(atest, stest, dbstack, mfilename);
525 end % END UTP_08
526
527 %% UTP_09
528
529 % <TestDescription>
530 %
531 % Test that the pset method sets the key always in capital letter
532 %
533 % </TestDescription>
534 function result = utp_09
535
536 % <SyntaxDescription>
537 %
538 % Test that the pset method sets the key always in capital letter
539 %
540 % </SyntaxDescription>
541
542 try
543 % <SyntaxCode>
544 out = pl2.pset('This_Id_A_NeW_Key', 1);
545 % </SyntaxCode>
546 stest = true;
547 catch err
548 disp(err.message)
549 stest = false;
550 end
551
552 % <AlgoDescription>
553 %
554 % 1) Check that the key is in capital letters
555 %
556 % </AlgoDescription>
557
558 atest = true;
559 if stest
560 % <AlgoCode>
561 for ii = 1:out.nparams
562 found = false;
563 if strcmp(out.params(ii).key, 'THIS_ID_A_NEW_KEY')
564 found = true;
565 end
566 end
567 if ~found, atest = false; end
568 % </AlgoCode>
569 else
570 atest = false;
571 end
572
573 % Return a result structure
574 result = utp_prepare_result(atest, stest, dbstack, mfilename);
575 end % END UTP_09
576
577 end