1function time = add_periods_to_array_of_dates(time, freq, p)  % --*-- Unitary tests --*--
2
3% Adds a p periods (p can be negative) to a date (or a set of dates) characterized by array time and frequency freq.
4
5% Copyright (C) 2013-2017 Dynare Team
6%
7% This file is part of Dynare.
8%
9% Dynare is free software: you can redistribute it and/or modify
10% it under the terms of the GNU General Public License as published by
11% the Free Software Foundation, either version 3 of the License, or
12% (at your option) any later version.
13%
14% Dynare is distributed in the hope that it will be useful,
15% but WITHOUT ANY WARRANTY; without even the implied warranty of
16% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17% GNU General Public License for more details.
18%
19% You should have received a copy of the GNU General Public License
20% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
21
22if isequal(rows(time),1) && length(p)>1
23    time = repmat(time,length(p),1);
24end
25
26time(:,1) = time(:,1) + fix(p/freq);
27time(:,2) = time(:,2) + rem(p,freq);
28
29id1 = find(time(:,2)>freq);
30if ~isempty(id1)
31    time(id1,1) = time(id1,1) + 1;
32    time(id1,2) = time(id1,2) - freq;
33end
34
35id2 = find(time(:,2)<1);
36if ~isempty(id2)
37    time(id2,1) = time(id2,1) - 1;
38    time(id2,2) = time(id2,2) + freq;
39end
40
41%@test:1
42%$ t(1) = dassert(add_periods_to_array_of_dates([1950 1], 4, 1),[1950 2]);
43%$ t(2) = dassert(add_periods_to_array_of_dates([1950 1], 4, 2),[1950 3]);
44%$ t(3) = dassert(add_periods_to_array_of_dates([1950 1], 4, 3),[1950 4]);
45%$ t(4) = dassert(add_periods_to_array_of_dates([1950 1], 4, 4),[1951 1]);
46%$ t(5) = dassert(add_periods_to_array_of_dates([1950 1], 4, 5),[1951 2]);
47%$ t(6) = dassert(add_periods_to_array_of_dates([1950 1], 4, 6),[1951 3]);
48%$ t(7) = dassert(add_periods_to_array_of_dates([1950 1], 4, 7),[1951 4]);
49%$ t(8) = dassert(add_periods_to_array_of_dates([1950 1], 4, 8),[1952 1]);
50%$ T = all(t);
51%@eof:1
52
53%@test:2
54%$ t(1) = dassert(add_periods_to_array_of_dates(repmat([1950 1],8,1), 4, transpose(1:8)),[1950 2; 1950 3; 1950 4; 1951 1; 1951 2; 1951 3; 1951 4; 1952 1]);
55%$ T = all(t);
56%@eof:2
57
58%@test:3
59%$ t(1) = dassert(add_periods_to_array_of_dates([1950 1], 1, 1),[1951 1]);
60%$ T = all(t);
61%@eof:3
62
63%@test:4
64%$ t(1) = dassert(add_periods_to_array_of_dates([1950 1; 1950 2; 1950 3; 1950 4], 4, 1),[1950 2; 1950 3; 1950 4; 1951 1]);
65%$ T = all(t);
66%@eof:4
67
68%@test:5
69%$ t(1) = dassert(add_periods_to_array_of_dates([1950 1], 4, transpose(1:8)),[1950 2; 1950 3; 1950 4; 1951 1; 1951 2; 1951 3; 1951 4; 1952 1]);
70%$ T = all(t);
71%@eof:5
72
73%@test:6
74%$ t(1) = dassert(add_periods_to_array_of_dates([1950 1], 4, -1),[1949 4]);
75%$ t(2) = dassert(add_periods_to_array_of_dates([1950 1], 4, -2),[1949 3]);
76%$ t(3) = dassert(add_periods_to_array_of_dates([1950 1], 4, -3),[1949 2]);
77%$ t(4) = dassert(add_periods_to_array_of_dates([1950 1], 4, -4),[1949 1]);
78%$ t(5) = dassert(add_periods_to_array_of_dates([1950 1], 4, -5),[1948 4]);
79%$ t(6) = dassert(add_periods_to_array_of_dates([1950 1], 4, -6),[1948 3]);
80%$ t(7) = dassert(add_periods_to_array_of_dates([1950 1], 4, -7),[1948 2]);
81%$ t(8) = dassert(add_periods_to_array_of_dates([1950 1], 4, -8),[1948 1]);
82%$ T = all(t);
83%@eof:6