comparison testing/utp_1.1/utps/time/utp_time_time.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_TIME_TIME
2 %
3 % <MethodDescription>
4 %
5 % Tests time object constructor.
6 %
7 % </MethodDescription>
8 %
9 % NOTE: in this test we manipulate user preferences thus we should make
10 % sure we restore the original user preferences at the end of the testing
11 %
12 % $Id: utp_time_time.m,v 1.5 2011/04/28 07:06:27 hewitson Exp $
13
14 function results = utp_time_time(varargin)
15
16 global DEBUG
17 DEBUG = false;
18
19 % check inputs
20 if nargin == 0
21
22 % some keywords
23 class = 'time';
24 mthd = 'time';
25
26 results = [];
27 disp('******************************************************');
28 disp(['**** Running UTPs for ' class '/' mthd]);
29 disp('******************************************************');
30
31 % get preferences
32 prefs = getappdata(0, 'LTPDApreferences');
33 oldTimezone = char(prefs.getTimePrefs.getTimeTimezone);
34 oldTimeformat = char(prefs.getTimePrefs.getTimestringFormat);
35
36 % set timezone to UTC to simplify testing
37 prefs.getTimePrefs.setTimeTimezone('UTC');
38
39 % get preferences
40 try
41 results = [results utp_901];
42 results = [results utp_902];
43 results = [results utp_903];
44 results = [results utp_904];
45 results = [results utp_905];
46 results = [results utp_906];
47 results = [results utp_907];
48 results = [results utp_908];
49 results = [results utp_909];
50 catch ex
51 % restore preferences
52 prefs.getTimePrefs.setTimeTimezone(oldTimezone);
53 prefs.getTimePrefs.setTimestringFormat(oldTimeformat);
54 rethrow(ex);
55 end
56
57 % restore preferences
58 prefs.getTimePrefs.setTimeTimezone(oldTimezone);
59 prefs.getTimePrefs.setTimestringFormat(oldTimeformat);
60
61 disp('Done.');
62 disp('******************************************************');
63
64 elseif nargin == 1
65 % check for UTP functions
66 if strcmp(varargin{1}, 'isutp')
67 results = 1;
68 else
69 results = 0;
70 end
71 else
72 error('### Incorrect inputs')
73 end
74
75 %% UTP_901
76
77 % <TestDescription>
78 %
79 % Tests time object constructor without arguments. Should return the current time.
80 %
81 % </TestDescription>
82 function result = utp_901
83
84 % <SyntaxDescription>
85 % Call the time() constructor with no inputs.
86 % </SyntaxDescription>
87 stest = false;
88 try
89 % <SyntaxCode>
90 obj = time();
91 % </SyntaxCode>
92 stest = true;
93 end
94
95 atest = true;
96 try
97 % do not run algorithm tests if sintax tests failed
98 assert(stest);
99
100 % nothing to test here
101 % <AlgoDescription>
102 % No test can be done here since we don't know the time when the
103 % constructor is called. We could check that the resulting time is >
104 % 0, but that's not so useful.
105 % </AlgoDescription>
106 % <AlgoCode>
107 % </AlgoCode>
108 catch
109 atest = false;
110 end
111
112 % return a result structure
113 result = utp_prepare_result(atest, stest, dbstack, mfilename);
114 end
115
116 %% UTP_902
117
118 % <TestDescription>
119 %
120 % Tests time object constructor from numeric value.
121 %
122 % </TestDescription>
123 function result = utp_902
124
125 % <SyntaxDescription>
126 % Call the time() constructor with a numeric input (number of seconds
127 % since epoch).
128 % </SyntaxDescription>
129 stest = false;
130 try
131 % <SyntaxCode>
132 obj = time(1234.5);
133 % </SyntaxCode>
134 stest = true;
135 end
136
137 atest = true;
138 try
139 % do not run algorithm tests if sintax tests failed
140 assert(stest);
141
142 % nothing to test here
143 % <AlgoDescription>
144 % Check the time object has the expected millisecond value.
145 % </AlgoDescription>
146 % <AlgoCode>
147 assert(obj.utc_epoch_milli == 1234.5*1000.0);
148 % </AlgoCode>
149 catch
150 atest = false;
151 end
152
153 % return a result structure
154 result = utp_prepare_result(atest, stest, dbstack, mfilename);
155 end
156
157 %% UTP_903
158
159 % <TestDescription>
160 %
161 % Tests time object constructor from string.
162 %
163 % </TestDescription>
164 function result = utp_903
165
166 % <SyntaxDescription>
167 % Call the time() constructor with different time-string inputs.
168 % </SyntaxDescription>
169 stest = false;
170 try
171 % <SyntaxCode>
172 obj1 = time('1970-01-01 00:00:00');
173 obj2 = time('1970-01-01 00:01:00');
174 % </SyntaxCode>
175 stest = true;
176 end
177
178 atest = true;
179 try
180 % do not run algorithm tests if sintax tests failed
181 assert(stest);
182
183 % <AlgoDescription>
184 % Check the time objects have the expected millisecond values.
185 % </AlgoDescription>
186 % <AlgoCode>
187 assertEq(obj1.utc_epoch_milli, 0);
188 assertEq(obj2.utc_epoch_milli, 60*1000.0);
189 % </AlgoCode>
190 catch ex
191 atest = false;
192 end
193
194 % return a result structure
195 result = utp_prepare_result(atest, stest, dbstack, mfilename);
196 end
197
198
199 %% UTP_904
200
201 % <TestDescription>
202 %
203 % Tests time object constructor from a cell array of strings.
204 %
205 % </TestDescription>
206 function result = utp_904
207
208 % <SyntaxDescription>
209 % Call the time() constructor with cell-array of time-strings.
210 % </SyntaxDescription>
211 stest = false;
212 try
213 % <SyntaxCode>
214 obj = time({'1970-01-01 00:00:00', '1970-01-01 00:01:00', '1970-01-01 00:02:00'});
215 % </SyntaxCode>
216 stest = true;
217 end
218
219 atest = true;
220 try
221 % do not run algorithm tests if sintax tests failed
222 assert(stest);
223
224 % <AlgoDescription>
225 % Check the time objects have the expected millisecond values.
226 % </AlgoDescription>
227 % <AlgoCode>
228 assert(all(size(obj) == [1, 3]));
229 assert(all([obj.utc_epoch_milli] == [0 60 120]*1000.0));
230 % </AlgoCode>
231 catch
232 atest = false;
233 end
234
235 % return a result structure
236 result = utp_prepare_result(atest, stest, dbstack, mfilename);
237 end
238
239 %% UTP_905
240
241 % <TestDescription>
242 %
243 % Tests time object constructor from plist with 'time' parameter.
244 %
245 % </TestDescription>
246 function result = utp_905
247
248 % <SyntaxDescription>
249 % Call the time() constructor with plist input. Check a plist with a
250 % numeric value and a plist with a time-string.
251 % </SyntaxDescription>
252 stest = false;
253 try
254 % <SyntaxCode>
255 obj1 = time(plist('time', 1234.5));
256 obj2 = time(plist('time', '1970-01-01 00:01:00'));
257 % </SyntaxCode>
258 stest = true;
259 end
260
261 atest = true;
262 try
263 % do not run algorithm tests if sintax tests failed
264 assert(stest);
265
266 % <AlgoDescription>
267 % Check the time objects have the expected millisecond values.
268 % </AlgoDescription>
269 % <AlgoCode>
270 assert(obj1.utc_epoch_milli == 1234.5*1000.0);
271 assert(obj2.utc_epoch_milli == 60*1000.0);
272 % </AlgoCode>
273 catch
274 atest = false;
275 end
276
277 % return a result structure
278 result = utp_prepare_result(atest, stest, dbstack, mfilename);
279 end
280
281 %% UTP_906
282
283 % <TestDescription>
284 %
285 % Tests time object constructor from plist with 'milliseconds' parameter
286 %
287 % </TestDescription>
288 function result = utp_906
289
290 % <SyntaxDescription>
291 % Call the time() constructor with plist input using the 'millisecond'
292 % key.
293 % </SyntaxDescription>
294 stest = false;
295 try
296 % <SyntaxCode>
297 obj = time(plist('milliseconds', 1234.5));
298 % </SyntaxCode>
299 stest = true;
300 end
301
302 atest = true;
303 try
304 % do not run algorithm tests if sintax tests failed
305 assert(stest);
306
307 % <AlgoDescription>
308 % Check the time object has the expected millisecond value.
309 % </AlgoDescription>
310 % <AlgoCode>
311 assertEq(obj.utc_epoch_milli, 1234.5);
312 % </AlgoCode>
313 catch
314 atest = false;
315 end
316
317 % return a result structure
318 result = utp_prepare_result(atest, stest, dbstack, mfilename);
319 end
320
321
322 %% UTP_907
323
324 % <TestDescription>
325 %
326 % Tests time object constructor from plist with more parameters.
327 %
328 % </TestDescription>
329 function result = utp_907
330 % <SyntaxDescription>
331 % Call the time() constructor with plist input containing the time
332 % string and the time-zone.
333 % key.
334 % </SyntaxDescription>
335 stest = false;
336 try
337 % <SyntaxCode>
338 obj1 = time(plist('time', '1970-01-01 01:01:00', 'timezone', 'UTC'));
339 obj2 = time(plist('time', '1970-01-01 01:01:00', 'timezone', 'CET'));
340 % </SyntaxCode>
341 stest = true;
342 end
343
344 atest = true;
345 try
346 % do not run algorithm tests if sintax tests failed
347 assert(stest);
348
349 % <AlgoDescription>
350 % Check the time objects have the expected millisecond values.
351 % </AlgoDescription>
352 % <AlgoCode>
353 % time string is interpreted in the given timezone
354 assertEq(obj1.utc_epoch_milli, 61*60*1000.0);
355 assertEq(obj2.utc_epoch_milli, 60*1000.0);
356 % </AlgoCode>
357 catch ex
358 atest = false;
359 end
360
361 % return a result structure
362 result = utp_prepare_result(atest, stest, dbstack, mfilename);
363 end
364
365
366 %% UTP_908
367
368 % <TestDescription>
369 %
370 % Tests time object constructor from structure.
371 %
372 % </TestDescription>
373 function result = utp_908
374 % <SyntaxDescription>
375 % Call the time() constructor with an input structure obtained from
376 % calling struct() on a time object.
377 % key.
378 % </SyntaxDescription>
379 stest = false;
380 try
381 % <SyntaxCode>
382 t = time(plist('time', '1970-01-01 00:01:00'));
383 w = warning('off', 'MATLAB:structOnObject');
384 s = struct(t);
385 warning(w);
386 obj = time(s);
387 % </SyntaxCode>
388 stest = true;
389 end
390
391 atest = true;
392 try
393 % do not run algorithm tests if sintax tests failed
394 assert(stest);
395
396 % <AlgoDescription>
397 % Check the original and reconstructed time objects have the same
398 % millisecond value.
399 % </AlgoDescription>
400 % <AlgoCode>
401 assert(obj.utc_epoch_milli == t.utc_epoch_milli);
402 % </AlgoCode>
403 catch
404 atest = false;
405 end
406
407 % return a result structure
408 result = utp_prepare_result(atest, stest, dbstack, mfilename);
409 end
410
411
412 %% UTP_909
413
414 % <TestDescription>
415 %
416 % Tests time object constructor from string and time format.
417 %
418 % </TestDescription>
419 function result = utp_909
420 % <SyntaxDescription>
421 % Call the time() constructor with an input time string and time format
422 % string.
423 % </SyntaxDescription>
424 stest = false;
425 try
426 % <SyntaxCode>
427 obj = time('1970-01-01 00:01:00', 'yyyy-mm-dd HH:MM:SS');
428 % </SyntaxCode>
429 stest = true;
430 end
431
432 atest = true;
433 try
434 % do not run algorithm tests if sintax tests failed
435 assert(stest);
436
437 % <AlgoDescription>
438 % Check the resulting time object has the expected millisecond value.
439 % </AlgoDescription>
440 % <AlgoCode>
441 assertEq(obj.utc_epoch_milli, 60*1000.0);
442 % </AlgoCode>
443 catch
444 atest = false;
445 end
446
447 % return a result structure
448 result = utp_prepare_result(atest, stest, dbstack, mfilename);
449 end
450
451 end
452
453 function rv = assertEq(v1, v2)
454 global DEBUG
455 if v1 == v2
456 rv = true;
457 else
458 msg = sprintf('ERROR: assertEq: expected %e got %e\n', v2, v1);
459 if DEBUG
460 fprintf([msg '\n']);
461 end
462 error(msg);
463 end
464 end