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

Changed the behaviour of the pop method...

As in commit 6beaf7b0 (for the sort method).
parent fae6d17a
Branches
No related tags found
No related merge requests found
......@@ -30,40 +30,12 @@ function o = pop(o, p) % --*-- 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 o.isempty()
return
end
if nargin<2
% Remove last date
o.time = o.time(1:end-1,:);
return
end
o = copy(o);
if ~( isdates(p) || isdate(p) || (isscalar(p) && isint(p)) )
error('dates:pop','Input argument %s has to be a dates object with a single element, a string (which can be interpreted as a date) or an integer!',inputname(2))
end
if ischar(p)
p = dates(p);
end
if isnumeric(p)
idx = find(transpose(1:o.ndat())~=p);
o.time = o.time(idx,:);
if nargin>1
o.pop_(p);
else
if ~isequal(o.freq,p.freq)
error('dates:pop','Inputs must have common frequency!')
end
if p.length()>1
error('dates:pop','dates to be removed must have one element!')
end
if isempty(p)
return
end
idx = find(o==p);
jdx = find(transpose(1:o.ndat())~=idx(end));
o.time = o.time(jdx,:);
o.pop_();
end
%@test:1
......@@ -73,64 +45,119 @@ end
%$ B3 = '1950Q1';
%$ B4 = '1945Q3';
%$ B5 = '2009Q2';
%$ d = dates(B4,B3,B2,B1,B5);
%$
%$ % Define expected results
%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4; 2009 2];
%$ e.freq = 4;
%$
%$ % Call the tested routine
%$ d = dates(B4,B3,B2,B1);
%$ d.append_(dates(B5));
%$ d.pop();
%$ t(1) = dassert(d.time,e.time(1:end-1,:));
%$ t(2) = dassert(d.freq,e.freq);
%$ t(3) = size(e.time,1)-1==d.ndat();
%$ f = copy(d);
%$ d.pop(B1);
%$ t(4) = dassert(d.time,[1945 3; 1950 1; 1950 2]);
%$ t(5) = dassert(d.freq,e.freq);
%$ t(6) = size(e.time,1)-2==d.ndat();
%$ f.pop(dates(B1));
%$ t(7) = dassert(f.time,[1945 3; 1950 1; 1950 2]);
%$ t(8) = dassert(f.freq,e.freq);
%$ t(9) = size(e.time,1)-2==f.ndat();
%$
%$ % Check the results.
%$ try
%$ c = d.pop();
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(c.time,e.time(1:end-1,:));
%$ t(3) = dassert(c.freq,e.freq);
%$ t(4) = dassert(d.time,e.time);
%$ t(5) = dassert(d.freq,e.freq);
%$ end
%$
%$ T = all(t);
%@eof:1
%@test:2
%$ % Define some dates
%$ B1 = '1950Q1';
%$ B1 = '1953Q4';
%$ B2 = '1950Q2';
%$ B3 = '1950Q1';
%$ B4 = '1945Q3';
%$ B5 = '2009Q2';
%$ d = dates(B4,B3,B2,B1,B5);
%$
%$ % Define expected results
%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4; 2009 2];
%$ e.freq = 4;
%$
%$ % Call the tested routine
%$ d = dates(B1,B2,B3,B4);
%$ d.append_(dates(B5));
%$ d.pop();
%$ t(1) = dassert(d,dates(B1,B2,B3,B4));
%$ d.pop(B1);
%$ t(2) = dassert(d,dates(B1,B2,B4));
%$ d.pop(1);
%$ t(3) = dassert(d,dates(B2,B4));
%$ try
%$ c = d.pop(B5);
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(c.time,e.time(1:end-1,:));
%$ t(3) = dassert(c.freq,e.freq);
%$ t(4) = dassert(d.time,e.time);
%$ t(5) = dassert(d.freq,e.freq);
%$ end
%$
%$ T = all(t);
%@eof:2
%@test:3
%$ % Define some dates
%$ B1 = '1950Q1';
%$ B1 = '1953Q4';
%$ B2 = '1950Q2';
%$ B3 = '1950Q3';
%$ B3 = '1950Q1';
%$ B4 = '1945Q3';
%$ B5 = '2009Q2';
%$ d = dates(B4,B3,B2,B1,B5);
%$
%$ % Define expected results
%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4; 2009 2];
%$ e.freq = 4;
%$
%$ % Call the tested routine
%$ d = dates(B1,B2,B3);
%$ d.pop();
%$ t(1) = dassert(d,dates(B1,B2));
%$ d.pop(B1);
%$ t(2) = dassert(d,dates(B2));
%$ d.pop(1);
%$ t(3) = isempty(d);
%$ try
%$ c = d.pop(dates(B5));
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(c.time,e.time(1:end-1,:));
%$ t(3) = dassert(c.freq,e.freq);
%$ t(4) = dassert(d.time,e.time);
%$ t(5) = dassert(d.freq,e.freq);
%$ end
%$
%$ T = all(t);
%@eof:3
%@test:4
%$ % Define some dates
%$ B1 = '1953Q4';
%$ B2 = '1950Q2';
%$ B3 = '1950Q1';
%$ B4 = '1945Q3';
%$ B5 = '2009Q2';
%$ d = dates(B4,B3,B2,B1,B5);
%$
%$ % Define expected results
%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4; 2009 2];
%$ e.freq = 4;
%$
%$ % Call the tested routine
%$ try
%$ c = d.pop(3);
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(c.time,e.time([1 2 4 5],:));
%$ t(3) = dassert(c.freq,e.freq);
%$ t(4) = dassert(d.time,e.time);
%$ t(5) = dassert(d.freq,e.freq);
%$ end
%$
%$ T = all(t);
%@eof:4
\ No newline at end of file
function o = pop_(o, p) % --*-- Unitary tests --*--
% pop method for dates class (in place modification).
%
% INPUTS
% - o [dates]
% - p [dates] object with one element, string which can be interpreted as a date or integer scalar.
%
% OUTPUTS
% - o [dates]
%
% REMARKS
% 1. If a is a date appearing more than once in o, then only the last occurence is removed. If one wants to
% remove all the occurences of p in o, the remove method should be used instead.
%
% See also remove, setdiff.
% Copyright (C) 2013-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 o.isempty()
return
end
if nargin<2
% Remove last date
o.time = o.time(1:end-1,:);
return
end
if ~( isdates(p) || isdate(p) || (isscalar(p) && isint(p)) )
error('dates:pop','Input argument %s has to be a dates object with a single element, a string (which can be interpreted as a date) or an integer!',inputname(2))
end
if ischar(p)
p = dates(p);
end
if isnumeric(p)
idx = find(transpose(1:o.ndat())~=p);
o.time = o.time(idx,:);
else
if ~isequal(o.freq,p.freq)
error('dates:pop','Inputs must have common frequency!')
end
if p.length()>1
error('dates:pop','dates to be removed must have one element!')
end
if isempty(p)
return
end
idx = find(o==p);
jdx = find(transpose(1:o.ndat())~=idx(end));
o.time = o.time(jdx,:);
end
%@test:1
%$ % Define some dates
%$ B1 = '1953Q4';
%$ B2 = '1950Q2';
%$ B3 = '1950Q1';
%$ B4 = '1945Q3';
%$ B5 = '2009Q2';
%$
%$ % Define expected results
%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4; 2009 2];
%$ e.freq = 4;
%$
%$ % Call the tested routine
%$ d = dates(B4,B3,B2,B1);
%$ d.append_(dates(B5));
%$ try
%$ d.pop_();
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(d.time,e.time(1:end-1,:));
%$ t(3) = dassert(d.freq,e.freq);
%$ t(4) = dassert(size(e.time,1)-1,d.ndat());
%$ f = copy(d);
%$ try
%$ d.pop_(B1);
%$ t(5) = true;
%$ catch
%$ t(5) = false;
%$ end
%$ if t(5)
%$ t(6) = dassert(d.time,[1945 3; 1950 1; 1950 2]);
%$ t(7) = dassert(d.freq,e.freq);
%$ t(8) = dassert(size(e.time,1)-2, d.ndat());
%$ try
%$ f.pop_(dates(B1));
%$ t(9) = true;
%$ catch
%$ t(9) = false;
%$ end
%$ if t(9)
%$ t(10) = dassert(f.time,[1945 3; 1950 1; 1950 2]);
%$ t(11) = dassert(f.freq,e.freq);
%$ t(12) = dassert(size(e.time,1)-2, f.ndat());
%$ end
%$ end
%$ end
%$
%$ T = all(t);
%@eof:1
%@test:2
%$ % Define some dates
%$ B1 = '1950Q1';
%$ B2 = '1950Q2';
%$ B3 = '1950Q1';
%$ B4 = '1945Q3';
%$ B5 = '2009Q2';
%$
%$ % Call the tested routine
%$ d = dates(B1,B2,B3,B4);
%$ d.append_(dates(B5));
%$ try
%$ d.pop_();
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(d,dates(B1,B2,B3,B4));
%$ try
%$ d.pop_(B1);
%$ t(3) = true;
%$ catch
%$ t(3) = false;
%$ end
%$ if t(3)
%$ t(4) = dassert(d,dates(B1,B2,B4));
%$ try
%$ d.pop_(1);
%$ t(5) = true;
%$ catch
%$ t(5) = false;
%$ end
%$ if t(5)
%$ t(6) = dassert(d,dates(B2,B4));
%$ end
%$ end
%$ end
%$
%$ T = all(t);
%@eof:2
%@test:3
%$ % Define some dates
%$ B1 = '1950Q1';
%$ B2 = '1950Q2';
%$ B3 = '1950Q3';
%$ d = dates(B1,B2,B3);
%$
%$ % Call the tested routine
%$ try
%$ d.pop_();
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(d,dates(B1,B2));
%$ try
%$ d.pop_(B1);
%$ t(3) = true;
%$ catch
%$ t(3) = false;
%$ end
%$ if t(3)
%$ t(4) = dassert(d,dates(B2));
%$ try
%$ d.pop_(1);
%$ t(5) = true;
%$ catch
%$ t(5) = false;
%$ end
%$ if t(5)
%$ t(6) = isempty(d);
%$ end
%$ end
%$ end
%$ T = all(t);
%@eof:3
%@test:4
%$ % Define some dates
%$ B = '1950Q1';
%$ d = dates();
%$
%$ % Call the tested routine
%$ try
%$ d.pop_(B);
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = isempty(d);
%$ end
%$ T = all(t);
%@eof:4
%@test:5
%$ % Define some dates
%$ d = dates();
%$
%$ % Call the tested routine
%$ try
%$ d.pop_();
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = isempty(d);
%$ end
%$ T = all(t);
%@eof:5
......@@ -41,7 +41,7 @@ switch S(1).type
if length(S)>1 && isequal(S(2).type,'()') && isempty(S(2).subs)
S = shiftS(S,1);
end
case {'append','append_','pop','remove'}% Public methods (with arguments).
case {'append','append_','pop','pop_','remove'}% Public methods (with arguments).
if isequal(S(2).type,'()')
B = feval(S(1).subs,A,S(2).subs{:});
S = shiftS(S,1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment