From fae6d17af1bbcfd1120d7754266968cf386a756f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?=
 <stephane.adjemian@univ-lemans.fr>
Date: Wed, 18 Nov 2015 11:33:20 +0100
Subject: [PATCH] Change the behaviour of the append method...

As in commit 6beaf7b00a9334ecd896508a48dfcf333d3c2002 (for the sort method).
---
 src/@dates/append.m  |  66 +++++++++++++--
 src/@dates/append_.m | 189 +++++++++++++++++++++++++++++++++++++++++++
 src/@dates/pop.m     |   4 +-
 src/@dates/subsref.m |   2 +-
 4 files changed, 250 insertions(+), 11 deletions(-)
 create mode 100644 src/@dates/append_.m

diff --git a/src/@dates/append.m b/src/@dates/append.m
index 359cb0d..076d1fc 100644
--- a/src/@dates/append.m
+++ b/src/@dates/append.m
@@ -39,7 +39,8 @@ if ~isequal(o.freq, d.freq)
     error('dates:append:ArgCheck','dates must have common frequency!')
 end
 
-o.time = [o.time; d.time];
+o = copy(o);
+o.append_(d);
 
 %@test:1
 %$ % Define some dates
@@ -50,16 +51,23 @@ o.time = [o.time; d.time];
 %$ B5 = '2009Q2';
 %$
 %$ % Define expected results.
-%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4; 2009 2];
+%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4];
 %$ e.freq = 4;
 %$
 %$ % Call the tested routine.
 %$ d = dates(B4,B3,B2,B1);
-%$ d.append(dates(B5));
+%$ try
+%$     d.append(dates(B5));
+%$     t(1) = true;
+%$ catch
+%$     t(1) = false;
+%$ end
 %$
 %$ % Check the results.
-%$ t(1) = dassert(d.time,e.time);
-%$ t(2) = dassert(d.freq,e.freq);
+%$ if t(1)
+%$     t(2) = dassert(d.time,e.time);
+%$     t(3) = dassert(d.freq,e.freq);
+%$ end
 %$ T = all(t);
 %@eof:1
 
@@ -73,14 +81,56 @@ o.time = [o.time; d.time];
 %$
 %$ % Define expected results.
 %$ e.time = [1945 3; 1950 1; 1950 2; 1953 4; 2009 2];
+%$ f.time = [1945 3; 1950 1; 1950 2; 1953 4];
 %$ e.freq = 4;
 %$
 %$ % Call the tested routine.
 %$ d = dates(B4,B3,B2,B1);
-%$ d.append(B5);
+%$ try
+%$     c = d.append(B5);
+%$     t(1) = true;
+%$ catch
+%$     t(1) = false;
+%$ end
 %$
 %$ % Check the results.
-%$ t(1) = dassert(d.time,e.time);
-%$ t(2) = dassert(d.freq,e.freq);
+%$ if t(1)
+%$     t(2) = dassert(d.time,f.time);
+%$     t(3) = dassert(c.time,e.time);
+%$     t(4) = dassert(c.freq,e.freq);
+%$     t(5) = dassert(d.freq,e.freq);
+%$ end
 %$ T = all(t);
 %@eof:2
+
+%@test:3
+%$ % 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];
+%$ f.time = [1945 3; 1950 1; 1950 2; 1953 4];
+%$ e.freq = 4;
+%$
+%$ % Call the tested routine.
+%$ d = dates(B4,B3,B2,B1);
+%$ try
+%$     c = append(d, B5);
+%$     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(c.freq,e.freq);
+%$     t(5) = dassert(d.freq,e.freq);
+%$ end
+%$ T = all(t);
+%@eof:3
diff --git a/src/@dates/append_.m b/src/@dates/append_.m
new file mode 100644
index 0000000..48f7336
--- /dev/null
+++ b/src/@dates/append_.m
@@ -0,0 +1,189 @@
+function o = append_(o, d) % --*-- Unitary tests --*--
+
+% append method for dates class (in place modification).
+%
+% INPUTS 
+% - o [dates]
+% - a [dates or string] dates object with one element or string that can be interpreted as a date.
+%
+% OUTPUTS 
+% - o [dates] dates object containing dates defined in o and d.
+
+% Copyright (C) 2012-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 isa(d, 'dates')
+    if ~isequal(length(d), 1)
+        error('dates:append_:ArgCheck','Input argument %s has to be a dates object with one element.', inputname(2))
+    end
+    if isempty(d)
+        return
+    end
+elseif isdate(d)
+    d = dates(d);
+end
+
+if ~isequal(o.freq, d.freq)
+    error('dates:append_:ArgCheck','dates must have common frequency!')
+end
+
+o.time = [o.time; d.time];
+
+%@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);
+%$ try
+%$     d.append_(dates(B5));
+%$     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
+
+%@test:2
+%$ % 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);
+%$ try
+%$     d.append_(B5);
+%$     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:2
+
+%@test:3
+%$ % 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);
+%$ try
+%$     c = d.append_(B5);
+%$     t(1) = true;
+%$ catch
+%$     t(1) = false;
+%$ end
+%$
+%$ % Check the results.
+%$ if t(1)
+%$     t(2) = dassert(d.time,e.time);
+%$     t(3) = dassert(c.time,e.time);
+%$     t(4) = dassert(d.freq,e.freq);
+%$     t(5) = dassert(c.freq,e.freq);
+%$ end
+%$ T = all(t);
+%@eof:3
+
+
+%@test:4
+%$ % Define some dates
+%$ B1 = '1953Q4';
+%$ B2 = '1950Q2';
+%$ B3 = '1950Q1';
+%$ B4 = '1945Q3';
+%$ B5 = '2009Q2';
+%$
+%$ % Call the tested routine.
+%$ d = dates(B4,B3);
+%$ e = dates(B1,B2,B5);
+%$ try
+%$     d.append_(e);
+%$     t(1) = false;
+%$ catch
+%$     t(1) = true;
+%$ end
+%$
+%$ T = all(t);
+%@eof:4
+
+%@test:5
+%$ % Define some dates
+%$ B = '1950Q2';
+%$
+%$ % Call the tested routine.
+%$ d = dates(B);
+%$ try
+%$     d.append_('1950Q3');
+%$     t(1) = true;
+%$ catch
+%$     t(1) = false;
+%$ end
+%$
+%$ if t(1)
+%$     t(2) = dassert(d.time, [1950 2; 1950 3]);
+%$ end
+%$
+%$ T = all(t);
+%@eof:5
+
+%@test:6
+%$ % Define some dates
+%$ B = '1950Q2';
+%$
+%$ % Call the tested routine.
+%$ d = dates(B);
+%$ try
+%$     d.append_('1950Z3');
+%$     t(1) = false;
+%$ catch
+%$     t(1) = true;
+%$ end
+%$
+%$ T = all(t);
+%@eof:6
diff --git a/src/@dates/pop.m b/src/@dates/pop.m
index bc9c25a..39232c6 100644
--- a/src/@dates/pop.m
+++ b/src/@dates/pop.m
@@ -80,7 +80,7 @@ end
 %$
 %$ % Call the tested routine
 %$ d = dates(B4,B3,B2,B1);
-%$ d.append(dates(B5));
+%$ d.append_(dates(B5));
 %$ d.pop();
 %$ t(1) = dassert(d.time,e.time(1:end-1,:));
 %$ t(2) = dassert(d.freq,e.freq);
@@ -109,7 +109,7 @@ end
 %$
 %$ % Call the tested routine
 %$ d = dates(B1,B2,B3,B4);
-%$ d.append(dates(B5));
+%$ d.append_(dates(B5));
 %$ d.pop();
 %$ t(1) = dassert(d,dates(B1,B2,B3,B4));
 %$ d.pop(B1);
diff --git a/src/@dates/subsref.m b/src/@dates/subsref.m
index b49f3ef..4c8c913 100644
--- a/src/@dates/subsref.m
+++ b/src/@dates/subsref.m
@@ -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','pop','remove'}% Public methods (with arguments).
+      case {'append','append_','pop','remove'}% Public methods (with arguments).
         if isequal(S(2).type,'()')
             B = feval(S(1).subs,A,S(2).subs{:});
             S = shiftS(S,1);
-- 
GitLab