diff --git a/src/@dates/intersect.m b/src/@dates/intersect.m index df48770ed039ade43d04e899f6d63afdfeb52fcf..9cc2ee662c27a04bb27e29d537a6f8e14f6e031c 100644 --- a/src/@dates/intersect.m +++ b/src/@dates/intersect.m @@ -9,7 +9,7 @@ function q = intersect(o, p) % --*-- Unitary tests --*-- % OUTPUTS % - q [dates] All the common elements in o and p. -% Copyright (C) 2013-2020 Dynare Team +% Copyright © 2013-2020 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 @@ -39,9 +39,19 @@ if ~isequal(o.freq,p.freq) end if isoctave - time = intersect(o.time,p.time,'rows'); + if o.freq==365 + time = intersect(o.time(:,1), p.time(:,1)); + time = [time, NaN(length(time), 1)]; + else + time = intersect(o.time,p.time,'rows'); + end else - time = intersect(o.time,p.time,'rows','legacy'); + if o.freq==365 + time = intersect(o.time(:,1), p.time(:,1), 'legacy'); + time = [time, NaN(length(time), 1)]; + else + time = intersect(o.time,p.time,'rows','legacy'); + end end q = dates(); @@ -52,31 +62,46 @@ end q.freq = o.freq; q.time = time; +return + %@test:1 -%$ % Define some dates objects -%$ d1 = dates('1950Q1'):dates('1969Q4') ; -%$ d2 = dates('1960Q1'):dates('1969Q4') ; -%$ d3 = dates('1970Q1'):dates('1979Q4') ; -%$ -%$ % Call the tested routine. -%$ c1 = intersect(d1,d2); -%$ c2 = intersect(d1,d3); -%$ -%$ % Check the results. -%$ t(1) = dassert(c1,d2); -%$ t(2) = dassert(isempty(c2),true); -%$ T = all(t); +% Define some dates objects +d1 = dates('1950Q1'):dates('1969Q4') ; +d2 = dates('1960Q1'):dates('1969Q4') ; +d3 = dates('1970Q1'):dates('1979Q4') ; + +% Call the tested routine. +c1 = intersect(d1,d2); +c2 = intersect(d1,d3); + +% Check the results. +t(1) = isequal(c1,d2); +t(2) = isequal(isempty(c2),true); +T = all(t); %@eof:1 %@test:2 -%$ % Define some dates objects -%$ d1 = dates('1950Q1'):dates('1969Q4') ; -%$ d2 = dates('1950Q1'):dates('1969Q4') ; -%$ -%$ % Call the tested routine. -%$ c1 = intersect(d1,d2); -%$ -%$ % Check the results. -%$ t(1) = dassert(c1,d1); -%$ T = all(t); +% Define some dates objects +d1 = dates('1950Q1'):dates('1969Q4') ; +d2 = dates('1950Q1'):dates('1969Q4') ; + +% Call the tested routine. +c1 = intersect(d1,d2); + +% Check the results. +t(1) = isequal(c1,d1); +T = all(t); %@eof:2 + +%@test:3 +% Define some dates objects +d1 = dates('2000-01-01'):dates('2000-01-10'); +d2 = dates('2000-01-05'):dates('2000-01-10'); + +% Call the tested routine. +c1 = intersect(d1,d2); + +% Check the results. +t(1) = isequal(c1,d2); +T = all(t); +%@eof:3 diff --git a/src/@dates/setdiff.m b/src/@dates/setdiff.m index c3681868945def7479c387e94b287c16f553eeef..cfd9edb0da4c39bfab679d2bc382f034b69a8652 100644 --- a/src/@dates/setdiff.m +++ b/src/@dates/setdiff.m @@ -12,7 +12,7 @@ function [q, io] = setdiff(o,p) % --*-- Unitary tests --*-- % % See also pop, remove. -% Copyright (C) 2013-2020 Dynare Team +% Copyright © 2013-2020 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 @@ -49,115 +49,148 @@ if isequal(o.length(),p.length()) && isequal(o, p) end if isoctave - if nargout<2 - time = setdiff(o.time,p.time,'rows'); + if o.freq==2 + [~, io] = setdiff(o.time(:,1), p.time(:,1)); + time = o.time(io,:); else - [time, io] = setdiff(o.time,p.time,'rows'); + if nargout<2 + time = setdiff(o.time, p.time, 'rows'); + else + [time, io] = setdiff(o.time, p.time, 'rows'); + end end else - if nargout<2 - time = setdiff(o.time,p.time,'rows','legacy'); + if o.freq==365 + [~, io] = setdiff(o.time(:,1), p.time(:,1), 'legacy'); + time = o.time(io,:); else - [time, io] = setdiff(o.time,p.time,'rows','legacy'); + if nargout<2 + time = setdiff(o.time, p.time, 'rows', 'legacy'); + else + [time, io] = setdiff(o.time, p.time, 'rows', 'legacy'); + end end end q = dates(o.freq); q.time = time; +return + %@test:1 -%$ % Define some dates objects -%$ d1 = dates('1950Q1'):dates('1950Q4') ; -%$ d2 = dates('1950Q3'):dates('1950Q4') ; -%$ -%$ % Call the tested routine. -%$ try -%$ c1 = setdiff(d1,d2); -%$ [c2, i] = setdiff(d1,d2); -%$ t(1) = true; -%$ catch -%$ t(1) = false; -%$ end -%$ -%$ % Check the results. -%$ if t(1) -%$ t(2) = dassert(c1,c2); -%$ t(3) = dassert(d1(i),c2); -%$ end -%$ T = all(t); +% Define some dates objects +d1 = dates('1950Q1'):dates('1950Q4') ; +d2 = dates('1950Q3'):dates('1950Q4') ; + +% Call the tested routine. +try + c1 = setdiff(d1,d2); + [c2, i] = setdiff(d1,d2); + t(1) = true; +catch + t(1) = false; +end + +% Check the results. +if t(1) + t(2) = isequal(c1,c2); + t(3) = isequal(d1(i),c2); +end +T = all(t); %@eof:1 %@test:2 -%$ % Define some dates objects -%$ d1 = dates('1950Q1'):dates('1950Q4') ; -%$ d2 = dates('1950M3'):dates('1950M4') ; -%$ -%$ % Call the tested routine. -%$ try -%$ c1 = setdiff(d1,d2); -%$ t(1) = false; -%$ catch -%$ t(1) = true; -%$ end -%$ -%$ T = all(t); +% Define some dates objects +d1 = dates('1950Q1'):dates('1950Q4') ; +d2 = dates('1950M3'):dates('1950M4') ; + +% Call the tested routine. +try + c1 = setdiff(d1,d2); + t(1) = false; +catch + t(1) = true; +end + +T = all(t); %@eof:2 %@test:3 -%$ % Define some dates objects -%$ d = dates('1950Q1'):dates('1950Q4') ; -%$ -%$ % Call the tested routine. -%$ try -%$ c1 = setdiff(d,1); -%$ t(1) = false; -%$ catch -%$ t(1) = true; -%$ end -%$ -%$ T = all(t); +% Define some dates objects +d = dates('1950Q1'):dates('1950Q4') ; + +% Call the tested routine. +try + c1 = setdiff(d,1); + t(1) = false; +catch + t(1) = true; +end + +T = all(t); %@eof:3 %@test:4 -%$ % Define some dates objects -%$ d1 = dates('1950Q1'):dates('1950Q4') ; -%$ d2 = dates('1951Q3'):dates('1951Q4') ; -%$ -%$ % Call the tested routine. -%$ try -%$ c1 = setdiff(d1,d2); -%$ [c2, i] = setdiff(d1,d2); -%$ t(1) = true; -%$ catch -%$ t(1) = false; -%$ end -%$ -%$ % Check the results. -%$ if t(1) -%$ t(2) = dassert(isequal(c1,d1),true); -%$ t(3) = dassert(isequal(c1,d1(i)),true); -%$ end -%$ T = all(t); +% Define some dates objects +d1 = dates('1950Q1'):dates('1950Q4') ; +d2 = dates('1951Q3'):dates('1951Q4') ; + +% Call the tested routine. +try + c1 = setdiff(d1,d2); + [c2, i] = setdiff(d1,d2); + t(1) = true; +catch + t(1) = false; +end + +% Check the results. +if t(1) + t(2) = isequal(isequal(c1,d1),true); + t(3) = isequal(isequal(c1,d1(i)),true); +end +T = all(t); %@eof:4 %@test:5 -%$ % Define some dates objects -%$ d1 = dates('1950Q1'):dates('1950Q4') ; -%$ -%$ % Call the tested routine. -%$ try -%$ c1 = setdiff(d1,d1); -%$ [c2, i] = setdiff(d1,d1); -%$ t(1) = true; -%$ catch -%$ t(1) = false; -%$ end -%$ -%$ % Check the results. -%$ if t(1) -%$ t(2) = dassert(isempty(c1),true); -%$ t(3) = dassert(isempty(c2),true); -%$ t(4) = dassert(isempty(i),true); -%$ end -%$ T = all(t); +% Define some dates objects +d1 = dates('1950Q1'):dates('1950Q4') ; + +% Call the tested routine. +try + c1 = setdiff(d1,d1); + [c2, i] = setdiff(d1,d1); + t(1) = true; +catch + t(1) = false; +end + +% Check the results. +if t(1) + t(2) = isequal(isempty(c1),true); + t(3) = isequal(isempty(c2),true); + t(4) = isequal(isempty(i),true); +end +T = all(t); %@eof:5 + +%@test:6 +% Define some dates objects +d1 = dates('2000-01-01'):dates('2000-01-10'); +d2 = dates('2000-01-05'):dates('2000-01-10'); +d3 = dates('2000-01-01'):dates('2000-01-04'); +% Call the tested routine. +try + c1 = setdiff(d1, d2); + t(1) = true; +catch + t(1) = false; +end + +% Check the results. +if t(1) + t(2) = isequal(c1, d3); +end +T = all(t); +%@eof:6 + diff --git a/src/@dates/union.m b/src/@dates/union.m index 3d56a20fb15f3be28296b628e69f776b7c171754..59868ad0ce6f3f5618928bdd9254b3dc3cab9e03 100644 --- a/src/@dates/union.m +++ b/src/@dates/union.m @@ -6,12 +6,12 @@ function o = union(varargin) % --*-- Unitary tests --*-- % - varargin [dates] % % OUPUTS -% - o [dates] +% - o [dates] % % REMARKS % 1. Elements in o are sorted by increasing order. -% Copyright (C) 2013-2017 Dynare Team +% Copyright © 2013-2020 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 @@ -37,24 +37,42 @@ end o = sort(unique(horzcat(varargin{:}))); +return + %@test:1 -%$ % Define some dates objects -%$ d1 = dates('1950Q1'):dates('1959Q4') ; -%$ d2 = dates('1960Q1'):dates('1969Q4') ; -%$ d3 = dates('1970Q1'):dates('1979Q4') ; -%$ -%$ % Call the tested routine. -%$ e1 = union(d1); -%$ e2 = union(d1,d2); -%$ e3 = union(d1,d2,d3); -%$ e4 = union(d1,d2,d3,d2+d3); -%$ e5 = union(d1,d2,d3,d2); -%$ -%$ % Check the results. -%$ t(1) = dassert(e1,d1); -%$ t(2) = dassert(e2,d1+d2); -%$ t(3) = dassert(e3,d1+d2+d3); -%$ t(4) = dassert(e4,d1+d2+d3); -%$ t(5) = dassert(e5,d1+d2+d3); -%$ T = all(t); +% Define some dates objects +d1 = dates('1950Q1'):dates('1959Q4') ; +d2 = dates('1960Q1'):dates('1969Q4') ; +d3 = dates('1970Q1'):dates('1979Q4') ; + +% Call the tested routine. +e1 = union(d1); +e2 = union(d1,d2); +e3 = union(d1,d2,d3); +e4 = union(d1,d2,d3,d2+d3); +e5 = union(d1,d2,d3,d2); + +% Check the results. +t(1) = dassert(e1,d1); +t(2) = dassert(e2,d1+d2); +t(3) = dassert(e3,d1+d2+d3); +t(4) = dassert(e4,d1+d2+d3); +t(5) = dassert(e5,d1+d2+d3); +T = all(t); +%@eof:1 + +%@test:1 +% Define some dates objects +d1 = dates('2000-01-01'):dates('2000-01-05'); +d2 = dates('2000-01-05'):dates('2000-01-10'); +d3 = dates('2000-01-01'):dates('2000-01-10'); + +% Call the tested routine. +e1 = union(d1); +e2 = union(d1,d2); + +% Check the results. +t(1) = isequal(e1, d1); +t(2) = isequal(e2, d3); +T = all(t); %@eof:1 \ No newline at end of file diff --git a/src/@dates/unique.m b/src/@dates/unique.m index 86c6963db4ced270e51a344c3efd82175d18ad16..a5630cdc0a7978c46b36779bf60de4da6943173d 100644 --- a/src/@dates/unique.m +++ b/src/@dates/unique.m @@ -11,7 +11,7 @@ function o = unique(o) % --*-- Unitary tests --*-- % REMARKS % 1. Only the last occurence of a date is kept. -% Copyright (C) 2013-2017 Dynare Team +% Copyright © 2013-2020 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 @@ -33,91 +33,113 @@ end o = copy(o); o.unique_(); +return + %@test:1 -%$ % Define some dates -%$ B1 = '1953Q4'; -%$ B2 = '1950Q2'; -%$ B3 = '1950q1'; -%$ B4 = '1945Q3'; -%$ B5 = '1950Q2'; -%$ -%$ % Define expected results. -%$ e.time = [1953 4; 1950 1; 1945 3; 1950 2]; -%$ e.freq = 4; -%$ f.time = [1953 4; 1950 2; 1950 1; 1945 3; 1950 2]; -%$ -%$ % Call the tested routine. -%$ d = dates(B1,B2,B3,B4,B5); -%$ try -%$ c = d.unique(); -%$ t(1) = true; -%$ catch -%$ t(1) = false; -%$ end -%$ -%$ % Check the results. -%$ if t(1) -%$ t(2) = dassert(d.time,f.time); -%$ t(3) = dassert(c.time,e.time); -%$ t(4) = dassert(d.freq,e.freq); -%$ end -%$ T = all(t); +% Define some dates +B1 = '1953Q4'; +B2 = '1950Q2'; +B3 = '1950q1'; +B4 = '1945Q3'; +B5 = '1950Q2'; + +% Define expected results. +e.time = [1953 4; 1950 1; 1945 3; 1950 2]; +e.freq = 4; +f.time = [1953 4; 1950 2; 1950 1; 1945 3; 1950 2]; + +% Call the tested routine. +d = dates(B1,B2,B3,B4,B5); +try + c = d.unique(); + t(1) = true; +catch + t(1) = false; +end + +% Check the results. +if t(1) + t(2) = dassert(d.time,f.time); + t(3) = dassert(c.time,e.time); + t(4) = dassert(d.freq,e.freq); +end +T = all(t); %@eof:1 %@test:2 -%$ % Define some dates -%$ B1 = '1953Q4'; -%$ B2 = '1950Q2'; -%$ B3 = '1950q1'; -%$ B4 = '1945Q3'; -%$ B5 = '1950Q2'; -%$ -%$ % Define expected results. -%$ e.time = [1953 4; 1950 1; 1945 3; 1950 2]; -%$ e.freq = 4; -%$ f.time = [1953 4; 1950 2; 1950 1; 1945 3; 1950 2]; -%$ -%$ % Call the tested routine. -%$ d = dates(B1,B2,B3,B4,B5); -%$ try -%$ c = unique(d); -%$ t(1) = true; -%$ catch -%$ t(1) = false; -%$ end -%$ -%$ % Check the results. -%$ if t(1) -%$ t(2) = dassert(d.time,f.time); -%$ t(3) = dassert(c.time,e.time); -%$ t(4) = dassert(d.freq,e.freq); -%$ end -%$ T = all(t); +% Define some dates +B1 = '1953Q4'; +B2 = '1950Q2'; +B3 = '1950q1'; +B4 = '1945Q3'; +B5 = '1950Q2'; + +% Define expected results. +e.time = [1953 4; 1950 1; 1945 3; 1950 2]; +e.freq = 4; +f.time = [1953 4; 1950 2; 1950 1; 1945 3; 1950 2]; + +% Call the tested routine. +d = dates(B1,B2,B3,B4,B5); +try + c = unique(d); + t(1) = true; +catch + t(1) = false; +end + +% Check the results. +if t(1) + t(2) = dassert(d.time,f.time); + t(3) = dassert(c.time,e.time); + t(4) = dassert(d.freq,e.freq); +end +T = all(t); %@eof:2 %@test:3 -%$ % Define some dates -%$ B1 = '1953Q4'; -%$ -%$ % Define expected results. -%$ e.time = [1953 4]; -%$ e.freq = 4; -%$ f.time = e.time; -%$ -%$ % Call the tested routine. -%$ d = dates(B1); -%$ try -%$ c = d.unique(); -%$ t(1) = true; -%$ catch -%$ t(1) = false; -%$ end -%$ -%$ % Check the results. -%$ if t(1) -%$ t(2) = dassert(d.time,f.time); -%$ t(3) = dassert(c.time,e.time); -%$ t(4) = dassert(d.freq,e.freq); -%$ end -%$ T = all(t); +% Define some dates +B1 = '1953Q4'; + +% Define expected results. +e.time = [1953 4]; +e.freq = 4; +f.time = e.time; + +% Call the tested routine. +d = dates(B1); +try + c = d.unique(); + t(1) = true; +catch + t(1) = false; +end + +% Check the results. +if t(1) + t(2) = dassert(d.time,f.time); + t(3) = dassert(c.time,e.time); + t(4) = dassert(d.freq,e.freq); +end +T = all(t); %@eof:3 + +%@test:4 +% Define some dates +d1 = [dates('2000-01-01'), dates('2000-01-02'), dates('2000-01-01')]; + +% Call the tested routine. +try + d2 = d1.unique(); + t(1) = true; +catch + t(1) = false; +end + +% Check the results. +if t(1) + t(2) = isequal(d1, dates('2000-01-01', '2000-01-02')); +end +T = all(t); +%@eof:4 + diff --git a/src/@dates/unique_.m b/src/@dates/unique_.m index fec7bd46b15259746c68e2959d6fc169e4b064d9..a4cc2cd47a519a440cc16cad5cabd3b8eb48f168 100644 --- a/src/@dates/unique_.m +++ b/src/@dates/unique_.m @@ -11,7 +11,7 @@ function o = unique_(o) % --*-- Unitary tests --*-- % REMARKS % 1. Only the last occurence of a date is kept. -% Copyright (C) 2013-2020 Dynare Team +% Copyright © 2013-2020 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 @@ -31,67 +31,77 @@ if o.ndat()<=1 end if isoctave - [tmp, id, jd] = unique(o.time,'rows'); + if o.freq==365 + [~, id, jd] = unique(o.time(:,1)); + else + [~, id, jd] = unique(o.time,'rows'); + end else - [tmp, id, jd] = unique(o.time,'rows','legacy'); + if o.freq==365 + [~, id, jd] = unique(o.time(:,1), 'legacy'); + else + [~, id, jd] = unique(o.time, 'rows', 'legacy'); + end end o.time = o.time(sort(id),:); -%@test:1 -%$ % Define some dates -%$ B1 = '1953Q4'; -%$ B2 = '1950Q2'; -%$ B3 = '1950q1'; -%$ B4 = '1945Q3'; -%$ B5 = '1950Q2'; -%$ -%$ % Define expected results. -%$ e.time = [1953 4; 1950 1; 1945 3; 1950 2]; -%$ e.freq = 4; -%$ -%$ % Call the tested routine. -%$ d = dates(B1,B2,B3,B4,B5); -%$ try -%$ d.unique_(); -%$ t(1) = true; -%$ catch -%$ t(1) = false; -%$ end -%$ -%$ % Check the results. -%$ if t(1) -%$ t(2) = dassert(d.time,e.time); -%$ t(3) = dassert(d.freq,e.freq); -%$ end -%$ T = all(t); -%@eof:1 +return %@test:1 -%$ % Define some dates -%$ B1 = '1953Q4'; -%$ B2 = '1950Q2'; -%$ B3 = '1950q1'; -%$ B4 = '1945Q3'; -%$ B5 = '1950Q2'; -%$ -%$ % Define expected results. -%$ e.time = [1953 4; 1950 1; 1945 3; 1950 2]; -%$ e.freq = 4; -%$ -%$ % Call the tested routine. -%$ d = dates(B1,B2,B3,B4,B5); -%$ try -%$ unique_(d); -%$ t(1) = true; -%$ catch -%$ t(1) = false; -%$ end -%$ -%$ % Check the results. -%$ if t(1) -%$ t(2) = dassert(d.time,e.time); -%$ t(3) = dassert(d.freq,e.freq); -%$ end -%$ T = all(t); +% Define some dates +B1 = '1953Q4'; +B2 = '1950Q2'; +B3 = '1950q1'; +B4 = '1945Q3'; +B5 = '1950Q2'; + +% Define expected results. +e.time = [1953 4; 1950 1; 1945 3; 1950 2]; +e.freq = 4; + +% Call the tested routine. +d = dates(B1,B2,B3,B4,B5); +try + d.unique_(); + t(1) = true; +catch + t(1) = false; +end + +% Check the results. +if t(1) + t(2) = isequal(d.time,e.time); + t(3) = isequal(d.freq,e.freq); +end +T = all(t); %@eof:1 + +%@test:2 +% Define some dates +B1 = '1953Q4'; +B2 = '1950Q2'; +B3 = '1950q1'; +B4 = '1945Q3'; +B5 = '1950Q2'; + +% Define expected results. +e.time = [1953 4; 1950 1; 1945 3; 1950 2]; +e.freq = 4; + +% Call the tested routine. +d = dates(B1,B2,B3,B4,B5); +try + unique_(d); + t(1) = true; +catch + t(1) = false; +end + +% Check the results. +if t(1) + t(2) = isequal(d.time,e.time); + t(3) = isequal(d.freq,e.freq); +end +T = all(t); +%@eof:2