From 21b1b2f9dc0847c386d491ff24ca6e45d49bddd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Mon, 2 Sep 2013 23:33:13 +0200 Subject: [PATCH] Changed the syntax so that the difference between calling a member or a method is more explicit (dynDates) and added unitary tests. --- matlab/@dynDates/sort.m | 23 +++++++++++++++++++++++ matlab/@dynDates/subsref.m | 28 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/matlab/@dynDates/sort.m b/matlab/@dynDates/sort.m index 32b2abe71..62fba94e1 100644 --- a/matlab/@dynDates/sort.m +++ b/matlab/@dynDates/sort.m @@ -77,4 +77,27 @@ dd.time = sortrows(dd.time,[1,2]); %$ t(2) = dyn_assert(d.freq,e.freq); %$ t(3) = dyn_assert(d.ndat,e.ndat); %$ T = all(t); +%@eof:1 + +%@test:1 +%$ % Define some dates +%$ B1 = '1953Q4'; +%$ B2 = '1950Q2'; +%$ B3 = '1950Q1'; +%$ B4 = '1945Q3'; +%$ +%$ % Define expected results. +%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4]; +%$ e.freq = 4; +%$ e.ndat = 4; +%$ +%$ % Call the tested routine. +%$ d = dynDates(B1,B2,B3,B4); +%$ d = d.sort(); +%$ +%$ % Check the results. +%$ t(1) = dyn_assert(d.time,e.time); +%$ t(2) = dyn_assert(d.freq,e.freq); +%$ t(3) = dyn_assert(d.ndat,e.ndat); +%$ T = all(t); %@eof:1 \ No newline at end of file diff --git a/matlab/@dynDates/subsref.m b/matlab/@dynDates/subsref.m index 65cc3c354..e57bdd389 100644 --- a/matlab/@dynDates/subsref.m +++ b/matlab/@dynDates/subsref.m @@ -54,9 +54,15 @@ switch S(1).type case '.' switch S(1).subs case {'time','freq','ndat'}% Access public members. + if length(S)>1 && isequal(S(2).type,'()') && isempty(S(2).subs) + error(['dynDates::subsref: ' S(1).subs ' is not a method but a member!']) + end B = builtin('subsref', A, S(1)); case {'sort','unique'}% Public methods (without arguments) B = feval(S(1).subs,A); + if length(S)>1 && isequal(S(2).type,'()') && isempty(S(2).subs) + S = shiftS(S); + end case {'append','pop'}% Public methods (with arguments). if isequal(S(2).type,'()') B = feval(S(1).subs,A,S(2).subs{:}); @@ -134,4 +140,26 @@ end %$ t(4) = dyn_assert(d.ndat,2); %$ end %$ T = all(t); +%@eof:2 + +%@test:3 +%$ % Define a dynDates object +%$ B = dynDate('1950Q1'):dynDate('1960Q3'); +%$ +%$ % Try to extract a sub-dynDates object and apply a method +%$ +%$ d = B(2:3).sort() ; +%$ +%$ if isa(d,'dynDates') +%$ t(1) = 1; +%$ else +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dyn_assert(d.freq,B.freq); +%$ t(3) = dyn_assert(d.time,[1950 2; 1950 3]); +%$ t(4) = dyn_assert(d.ndat,2); +%$ end +%$ T = all(t); %@eof:2 \ No newline at end of file