dates4display.m 3.94 KB
Newer Older
Stéphane Adjemian's avatar
Stéphane Adjemian committed
1
function str = dates4display(o, name, max_number_of_elements) % --*-- Unitary tests --*--
2 3 4

% Converts a list object to a string.
%
Stéphane Adjemian's avatar
Stéphane Adjemian committed
5
% INPUTS
6 7 8 9
% - o                      [list]     A dates object to be displayed.
% - name                   [string]   Name of the dates object o.
% - max_number_of_elements [integer]  Maximum number of elements displayed.
%
Stéphane Adjemian's avatar
Stéphane Adjemian committed
10 11
% OUTPUTS
% - str  [string] Representation of the dates object as a string.
12

Stéphane Adjemian's avatar
Stéphane Adjemian committed
13
% Copyright (C) 2014-2019 Dynare Team
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
%
% 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 isempty(o)
    str = sprintf('%s is an empty dates object.\n', name);
    return
end

str = sprintf('%s = <dates: ', name);

if o.length()<=max_number_of_elements
    % All the elements are displayed
    for i=1:length(o)-1
        str = sprintf('%s%s, ', str, date2string(o.time(i,:),o.freq));
    end
else
    % Only display four elements (two first and two last)
    for i=1:2
        str = sprintf('%s%s, ', str, date2string(o.time(i,:),o.freq));
    end
    str = sprintf('%s%s, ', str, '...');
    str = sprintf('%s%s, ', str, date2string(o.time(o.length()-1,:),o.freq));
end
Stéphane Adjemian's avatar
Stéphane Adjemian committed
48 49 50 51
str = sprintf('%s%s>\n', str, date2string(o.time(o.length(),:),o.freq));

%@test:1
%$ OPATH = pwd();
52
%$ DSERIES_PATH = strrep(which('initialize_dseries_class'),'/initialize_dseries_class.m','');
53
%$ cd([DSERIES_PATH '/@dates/private']);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
54 55 56 57 58 59 60 61 62 63
%$
%$ try
%$     toto = dates();
%$     str = dates4display(toto, 'toto', 5);
%$     t(1) = true;
%$ catch
%$     t(1) = false;
%$ end
%$
%$ if t(1)
64
%$     expected_str_1 = sprintf('toto is an empty dates object.\\n');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
65 66 67 68 69 70 71 72 73 74 75 76 77
%$     try
%$         t(2) = dassert(str, expected_str_1);
%$     catch
%$         t(2) = false;
%$     end
%$ end
%$
%$ T = all(t);
%$ cd(OPATH);
%@eof:1

%@test:2
%$ OPATH = pwd();
78
%$ DSERIES_PATH = strrep(which('initialize_dseries_class'),'/initialize_dseries_class.m','');
79
%$ cd([DSERIES_PATH '/@dates/private']);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
80 81 82 83 84 85 86 87 88 89
%$
%$ try
%$     toto = dates('1950Q1'):dates('1950Q2');
%$     str = dates4display(toto, 'toto', 5);
%$     t(1) = true;
%$ catch
%$     t(1) = false;
%$ end
%$
%$ if t(1)
90
%$     expected_str_2 = sprintf('toto = <dates: 1950Q1, 1950Q2>\\n');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
91 92 93 94 95 96 97 98 99 100 101 102 103
%$     try
%$         t(2) = dassert(str, expected_str_2);
%$     catch
%$         t(2) = false;
%$     end
%$ end
%$
%$ T = all(t);
%$ cd(OPATH);
%@eof:2

%@test:3
%$ OPATH = pwd();
104
%$ DSERIES_PATH = strrep(which('initialize_dseries_class'),'/initialize_dseries_class.m','');
105
%$ cd([DSERIES_PATH '/@dates/private']);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
106 107 108 109 110 111 112 113 114 115
%$
%$ try
%$     toto = dates('1950Q1'):dates('1951Q1');
%$     str = dates4display(toto, 'toto', 4);
%$     t(1) = true;
%$ catch
%$     t(1) = false;
%$ end
%$
%$ if t(1)
116
%$     expected_str_3 = sprintf('toto = <dates: 1950Q1, 1950Q2, ..., 1950Q4, 1951Q1>\\n');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
117 118 119 120 121 122 123 124 125 126 127 128 129
%$     try
%$         t(2) = dassert(str, expected_str_3);
%$     catch
%$         t(2) = false;
%$     end
%$ end
%$
%$ T = all(t);
%$ cd(OPATH);
%@eof:3

%@test:4
%$ OPATH = pwd();
130
%$ DSERIES_PATH = strrep(which('initialize_dseries_class'),'/initialize_dseries_class.m','');
131
%$ cd([DSERIES_PATH '/@dates/private']);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
132 133 134 135 136 137 138 139 140 141
%$
%$ try
%$     toto = dates('1950Q1'):dates('1951Q1');
%$     str = dates4display(toto, 'toto', 6);
%$     t(1) = true;
%$ catch
%$     t(1) = false;
%$ end
%$
%$ if t(1)
142
%$     expected_str_4 = sprintf('toto = <dates: 1950Q1, 1950Q2, 1950Q3, 1950Q4, 1951Q1>\\n');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
143 144 145 146 147 148 149 150 151 152
%$     try
%$         t(2) = dassert(str, expected_str_4);
%$     catch
%$         t(2) = false;
%$     end
%$ end
%$
%$ T = all(t);
%$ cd(OPATH);
%@eof:4