diff --git a/src/@dates/sort.m b/src/@dates/sort.m index 882decb4d6a411e679516c2c4d4256488e34cb39..2b2bda7e0443c0aaeaccfa468ba95346f25f74bf 100644 --- a/src/@dates/sort.m +++ b/src/@dates/sort.m @@ -1,6 +1,6 @@ function o = sort(o) % --*-- Unitary tests --*-- -% Sort method for dates class. +% Sort method for dates class (with copy). % % INPUTS % - o [dates] @@ -23,11 +23,8 @@ function o = sort(o) % --*-- Unitary tests --*-- % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -if isequal(o.ndat(),1) - return -end - -o.time = sortrows(o.time,[1,2]); +o = copy(o); +o.sort_(); %@test:1 %$ % Define some dates @@ -39,36 +36,16 @@ o.time = sortrows(o.time,[1,2]); %$ % Define expected results. %$ e.time = [1945 3; 1950 1; 1950 2; 1953 4]; %$ e.freq = 4; +%$ f.time = [1953 4; 1950 2; 1950 1; 1945 3]; %$ %$ % Call the tested routine. %$ d = dates(B1,B2,B3,B4); -%$ d = d.sort; +%$ c = d.sort(); %$ %$ % Check the results. -%$ t(1) = dassert(d.time,e.time); -%$ t(2) = dassert(d.freq,e.freq); -%$ t(3) = size(e.time,1) == d.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; -%$ -%$ % Call the tested routine. -%$ d = dates(B1,B2,B3,B4); -%$ d = d.sort(); -%$ -%$ % Check the results. -%$ t(1) = dassert(d.time,e.time); -%$ t(2) = dassert(d.freq,e.freq); -%$ t(3) = size(e.time,1) == d.ndat(); +%$ t(1) = ~dassert(d.time,e.time); +%$ t(2) = dassert(c.time,e.time); +%$ t(3) = dassert(d.freq,e.freq); +%$ t(4) = dassert(c.freq,e.freq); %$ T = all(t); -%@eof:1 +%@eof:1 \ No newline at end of file diff --git a/src/@dates/sort_.m b/src/@dates/sort_.m new file mode 100644 index 0000000000000000000000000000000000000000..7b3f97e5d86646b6c53b7a149b8605dc00fe81bc --- /dev/null +++ b/src/@dates/sort_.m @@ -0,0 +1,72 @@ +function o = sort_(o) % --*-- Unitary tests --*-- + +% Sort method for dates class (in place modification). +% +% INPUTS +% - o [dates] +% +% OUTPUTS +% - o [dates] with dates sorted by increasing order. + +% Copyright (C) 2015 Dynare Team +% +% This code is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% Dynare dates submodule is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with Dynare. If not, see <http://www.gnu.org/licenses/>. + +if isequal(o.ndat(),1) + return +end + +o.time = sortrows(o.time,[1,2]); + +%@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; +%$ +%$ % Call the tested routine. +%$ d = dates(B1,B2,B3,B4); +%$ d.sort_(); +%$ +%$ % Check the results. +%$ t(1) = dassert(d.time,e.time); +%$ t(2) = dassert(d.freq,e.freq); +%$ T = all(t); +%@eof:1 + +%@test:2 +%$ % 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; +%$ +%$ % Call the tested routine. +%$ d = dates(B1,B2,B3,B4); +%$ c = sort_(d); +%$ +%$ % Check the results. +%$ t(1) = dassert(d.time,e.time); +%$ t(2) = dassert(d.freq,e.freq); +%$ T = all(t); +%@eof:2 \ No newline at end of file diff --git a/src/@dates/subsref.m b/src/@dates/subsref.m index 9f14322b1400f2e49cbbc3e845dca4c1f347955e..1af52a1d6d917863273859b18c76f848e8aa8b8e 100644 --- a/src/@dates/subsref.m +++ b/src/@dates/subsref.m @@ -36,7 +36,7 @@ switch S(1).type error(['dates::subsref: ' S(1).subs ' is not a method but a member!']) end B = builtin('subsref', A, S(1)); - case {'sort','unique','double','isempty','length','char','ndat'}% Public methods (without input arguments) + case {'sort','sort_','unique','double','isempty','length','char','ndat'}% Public methods (without input arguments) B = feval(S(1).subs,A); if length(S)>1 && isequal(S(2).type,'()') && isempty(S(2).subs) S = shiftS(S,1);