Skip to content
Snippets Groups Projects
Verified Commit 0a50b59d authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Fixed subsref overloaded method.

parent 1a0a4898
Branches
Tags
No related merge requests found
...@@ -13,7 +13,7 @@ function B = subsref(A,S) % --*-- Unitary tests --*-- ...@@ -13,7 +13,7 @@ function B = subsref(A,S) % --*-- Unitary tests --*--
% 1. The type of the returned argument depends on the content of S. % 1. The type of the returned argument depends on the content of S.
% 2. See the matlab's documentation about the subsref method. % 2. See the matlab's documentation about the subsref method.
% Copyright (C) 2011-2020 Dynare Team % Copyright © 2011-2020 Dynare Team
% %
% This code is free software: you can redistribute it and/or modify % 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 % it under the terms of the GNU General Public License as published by
...@@ -126,7 +126,13 @@ switch S(1).type ...@@ -126,7 +126,13 @@ switch S(1).type
if ~isequal(n1,n2,n3) if ~isequal(n1,n2,n3)
error('dates::subsref: Second, third and fourth input arguments must have the same number of elements!') error('dates::subsref: Second, third and fourth input arguments must have the same number of elements!')
end end
B.time = [S(1).subs{2}, S(1).subs{3}, S(1).subs{4}]; % Check that the provided inputs form a valid date
for i=1:length(S(1).subs{1})
if ~isvalidday([S(1).subs{1}(i), S(1).subs{2}(i), S(1).subs{3}(i)])
error('Triplet (%u, %u, %u) is not a valid date for a day.', S(1).subs{1}(i), S(1).subs{2}(i), S(1).subs{3}(i))
end
end
B.time = [datenum([S(1).subs{1}, S(1).subs{2}, S(1).subs{3}]), NaN(length(S(1).subs{1}), 1)];
else else
error('dates::subsref: Wrong calling sequence!') error('dates::subsref: Wrong calling sequence!')
end end
...@@ -181,7 +187,13 @@ switch S(1).type ...@@ -181,7 +187,13 @@ switch S(1).type
if ~isequal(n3,n2,n1) if ~isequal(n3,n2,n1)
error('dates::subsref: All arguments must have the same number of rows!') error('dates::subsref: All arguments must have the same number of rows!')
end end
B.time = [S(1).subs{1}, S(1).subs{2}, S(1).subs{3}]; % Check that the provided inputs form a valid date
for i=1:length(S(1).subs{1})
if ~isvalidday([S(1).subs{1}(i), S(1).subs{2}(i), S(1).subs{3}(i)])
error('Triplet (%u, %u, %u) is not a valid date for a day.', S(1).subs{1}(i), S(1).subs{2}(i), S(1).subs{3}(i))
end
end
B.time = [datenum([S(1).subs{1}, S(1).subs{2}, S(1).subs{3}]), NaN(length(S(1).subs{1}), 1)];
else else
error('dates::subsref: Wrong number of inputs!') error('dates::subsref: Wrong number of inputs!')
end end
...@@ -207,217 +219,213 @@ if ~isempty(S) ...@@ -207,217 +219,213 @@ if ~isempty(S)
B = subsref(B, S); B = subsref(B, S);
end end
return
%@test:1 %@test:1
%$ % Define a dates object % Define a dates object
%$ B = dates('1950Q1','1950Q2','1950Q3','1950Q4','1951Q1'); B = dates('1950Q1','1950Q2','1950Q3','1950Q4','1951Q1');
%$
%$ % Try to extract a sub-dates object. % Try to extract a sub-dates object.
%$ d = B(2:3); try
%$ d = B(2:3);
%$ if isa(d,'dates') t(1) = true;
%$ t(1) = 1; catch
%$ else t(1) = false;
%$ t(1) = 0; end
%$ end
%$ if t(1)
%$ if t(1) t(2) = isequal(d.freq, B.freq);
%$ t(2) = dassert(d.freq,B.freq); t(3) = isequal(d.time, [1950 2; 1950 3]);
%$ t(3) = dassert(d.time,[1950 2; 1950 3]); t(4) = isequal(d.ndat(), 2);
%$ t(4) = dassert(d.ndat(),2); end
%$ end T = all(t);
%$ T = all(t);
%@eof:1 %@eof:1
%@test:2 %@test:2
%$ % Define a dates object % Define a dates object
%$ B = dates('1950Q1'):dates('1960Q3'); B = dates('1950Q1'):dates('1960Q3');
%$
%$ % Try to extract a sub-dates object and apply a method % Try to extract a sub-dates object and apply a method
%$ try
%$ d = B(2:3).sort ; d = B(2:3).sort;
%$ t(1) = true;
%$ if isa(d,'dates') catch
%$ t(1) = 1; t(1) = false;
%$ else end
%$ t(1) = 0;
%$ end if t(1)
%$ t(2) = isequal(d.freq,B.freq);
%$ if t(1) t(3) = isequal(d.time,[1950 2; 1950 3]);
%$ t(2) = dassert(d.freq,B.freq); t(4) = isequal(d.ndat(),2);
%$ t(3) = dassert(d.time,[1950 2; 1950 3]); end
%$ t(4) = dassert(d.ndat(),2); T = all(t);
%$ end
%$ T = all(t);
%@eof:2 %@eof:2
%@test:3 %@test:3
%$ % Define a dates object % Define a dates object
%$ B = dates('1950Q1'):dates('1960Q3'); B = dates('1950Q1'):dates('1960Q3');
%$
%$ % Try to extract a sub-dates object and apply a method % Try to extract a sub-dates object and apply a method
%$ try
%$ d = B(2:3).sort() ; d = B(2:3).sort();
%$ t(1) = true;
%$ if isa(d,'dates') catch
%$ t(1) = 1; t(1) = false;
%$ else end
%$ t(1) = 0;
%$ end if t(1)
%$ t(2) = isequal(d.freq,B.freq);
%$ if t(1) t(3) = isequal(d.time,[1950 2; 1950 3]);
%$ t(2) = dassert(d.freq,B.freq); t(4) = isequal(d.ndat(),2);
%$ t(3) = dassert(d.time,[1950 2; 1950 3]); end
%$ t(4) = dassert(d.ndat(),2); T = all(t);
%$ end
%$ T = all(t);
%@eof:3 %@eof:3
%@test:4 %@test:4
%$ % Define a dates object % Define a dates object
%$ B = dates('1950Q1','1950Q2','1950Q3','1950Q4','1951Q1'); B = dates('1950Q1','1950Q2','1950Q3','1950Q4','1951Q1');
%$
%$ % Try to extract a sub-dates object. % Try to extract a sub-dates object.
%$ d = B(2); try
%$ d = B(2);
%$ if isa(d,'dates') t(1) = true;
%$ t(1) = 1; catch
%$ else t(1) = false;
%$ t(1) = 0; end
%$ end
%$ if t(1)
%$ if t(1) t(2) = isequal(d.freq,B.freq);
%$ t(2) = dassert(d.freq,B.freq); t(3) = isequal(d.time,[1950 2]);
%$ t(3) = dassert(d.time,[1950 2]); t(4) = isequal(d.ndat(),1);
%$ t(4) = dassert(d.ndat(),1); end
%$ end T = all(t);
%$ T = all(t);
%@eof:4 %@eof:4
%@test:5 %@test:5
%$ % Define an empty dates object with quaterly frequency. % Define an empty dates object with quaterly frequency.
%$ qq = dates('Q'); qq = dates('Q');
%$
%$ % Define a ranges of dates using qq. % Define a ranges of dates using qq.
%$ try try
%$ r1 = qq(1950,1):qq(1950,3);%qq([1950, 3]); r1 = qq(1950,1):qq(1950,3);
%$ t(1) = 1; t(1) = true;
%$ catch catch
%$ t(1) = 0; t(1) = false;
%$ end end
%$ if t(1)
%$ try if t(1)
%$ r2 = qq([1950, 1; 1950, 2; 1950, 3]); try
%$ t(2) = 1; r2 = qq([1950, 1; 1950, 2; 1950, 3]);
%$ catch t(2) = true;
%$ t(2) = 0; catch
%$ end t(2) = false;
%$ end end
%$ if t(1) && t(2) end
%$ try if t(1) && t(2)
%$ r3 = qq(1950*ones(3,1), transpose(1:3)); try
%$ t(3) = 1; r3 = qq(1950*ones(3,1), transpose(1:3));
%$ catch t(3) = true;
%$ t(3) = 0; catch
%$ end t(3) = false;
%$ end end
%$ end
%$ if t(1) && t(2) && t(3)
%$ t(4) = dassert(r1,r2); if t(1) && t(2) && t(3)
%$ t(5) = dassert(r1,r3); t(4) = isequal(r1,r2);
%$ end t(5) = isequal(r1,r3);
%$ T = all(t); end
T = all(t);
%@eof:5 %@eof:5
%@test:6 %@test:6
%$ % Define an empty dates object with quaterly frequency. % Define an empty dates object with quaterly frequency.
%$ date = dates(); date = dates();
%$
%$ % Define a ranges of dates using qq. % Define a ranges of dates using qq.
%$ try try
%$ r1 = date(4,1950,1):date(4,[1950, 3]); r1 = date(4,1950,1):date(4,[1950, 3]);
%$ t(1) = 1; t(1) = true;
%$ catch catch
%$ t(1) = 0; t(1) = false;
%$ end end
%$ if t(1) if t(1)
%$ try try
%$ r2 = date(4,[1950, 1; 1950, 2; 1950, 3]); r2 = date(4,[1950, 1; 1950, 2; 1950, 3]);
%$ t(2) = 1; t(2) = true;
%$ catch catch
%$ t(2) = 0; t(2) = false;
%$ end end
%$ end end
%$ if t(1) && t(2) if t(1) && t(2)
%$ try try
%$ r3 = date(4,1950*ones(3,1), transpose(1:3)); r3 = date(4,1950*ones(3,1), transpose(1:3));
%$ t(3) = 1; t(3) = true;
%$ catch catch
%$ t(3) = 0; t(3) = false;
%$ end end
%$ end end
%$
%$ if t(1) && t(2) && t(3) if t(1) && t(2) && t(3)
%$ t(4) = dassert(r1,r2); t(4) = isequal(r1, r2);
%$ t(5) = dassert(r1,r3); t(5) = isequal(r1, r3);
%$ end end
%$ T = all(t); T = all(t);
%@eof:6 %@eof:6
%@test:7 %@test:7
%$ % Define a dates object % Define a dates object
%$ B = dates('1950Q1','1950Q2','1950Q3','1950Q4','1951Q1'); B = dates('1950Q1','1950Q2','1950Q3','1950Q4','1951Q1');
%$
%$ % Try to extract a sub-dates object. % Try to extract a sub-dates object.
%$ try try
%$ d = B([]); d = B([]);
%$ t(1) = true; t(1) = true;
%$ catch catch
%$ t(1) = false; t(1) = false;
%$ end end
%$
%$ if t(1) if t(1)
%$ t(2) = dassert(isa(d,'dates'), true); t(2) = isequal(isa(d,'dates'), true);
%$ t(3) = dassert(isempty(d), true); t(3) = isequal(isempty(d), true);
%$ end end
%$ T = all(t); T = all(t);
%@eof:7 %@eof:7
%@test:8 %@test:8
%$ % Define a dates object % Define a dates object
%$ B = dates('1950-11-15','1950-11-16','1950-11-17','1950-11-18'); B = dates('1950-11-15','1950-11-16','1950-11-17','1950-11-18');
%$
%$ % Try to extract a sub-dates object. % Try to extract a sub-dates object.
%$ try try
%$ d = B([]); d = B([]);
%$ t(1) = true; t(1) = true;
%$ catch catch
%$ t(1) = false; t(1) = false;
%$ end end
%$
%$ if t(1) if t(1)
%$ t(2) = dassert(isa(d,'dates'), true); t(2) = isequal(isa(d,'dates'), true);
%$ t(3) = dassert(isempty(d), true); t(3) = isequal(isempty(d), true);
%$ end end
%$ T = all(t); T = all(t);
%@eof:8 %@eof:8
%@test:9 %@test:9
%$ % Define a dates object % Define a dates object
%$ B = dates('1950-11-15','1950-11-16','1950-11-17','1950-11-18'); B = dates('1950-11-15','1950-11-16','1950-11-17','1950-11-18');
%$
%$ % Try to extract a sub-dates object. % Try to extract a sub-dates object.
%$ d = B(2); try
%$ d = B(2);
%$ if isa(d,'dates') t(1) = true;
%$ t(1) = 1; catch
%$ else t(1) = false;
%$ t(1) = 0; end
%$ end
%$ if t(1)
%$ if t(1) t(2) = isequal(d.freq,B.freq);
%$ t(2) = dassert(d.freq,B.freq); t(3) = isequal(d.time(1), 712543) && isnan(d.time(2));
%$ t(3) = dassert(d.time,[1950 11 16]); t(4) = isequal(d.ndat(),1);
%$ t(4) = dassert(d.ndat(),1); end
%$ end T = all(t);
%$ T = all(t);
%@eof:9 %@eof:9
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment