Debian Codegraph @

tests/histval_initval_file_unit_tests.m

raw
1 source_dir = getenv('source_root');
2 addpath([source_dir filesep 'matlab']);
3
4 dynare_config;
5
6 cd histval_initval_file
7 failed_tests = {};
8
9 ds = dseries(randn(10,4));
10
11 M = struct();
12 M.fname = '';
13 M.endo_nbr = 3;
14 M.orig_endo_nbr = 3;
15 M.endo_names = {'Variable_1','Variable_2','Variable_3'};
16 M.exo_nbr = 1;
17 M.exo_names = {'Variable_4'};
18 M.exo_det_nbr = 0;
19 M.orig_maximum_lag = 2;
20 M.orig_maximum_lead = 0;
21
22 caller = 'INITVAL';
23
24 options = struct();
25 options.series = 'ds';
26 ds1 = histvalf_initvalf(caller, M, options);
27
28 failed_tests = my_assert(failed_tests, all(all(ds1 == ds)), 'basic test');
29
30 options = struct();
31 options.series = 'ds1';
32 options.first_obs = 2;
33 ds1 = histvalf_initvalf(caller, M, options);
34 failed_tests = my_assert(failed_tests, ds1.init == dates('2Y'), ...
35 'init test 1');
36
37 options = struct();
38 options.series = 'ds';
39 options.first_obs = 2;
40 options.last_obs = 9;
41 ds1 = histvalf_initvalf(caller, M, options);
42 failed_tests = my_assert(failed_tests, ds1.init == dates('2Y'), ...
43 'first_obs last_obs test 1');
44 failed_tests = my_assert(failed_tests, ds1.last == dates('9Y'), ...
45 'first_obs last_obs test 2');
46
47 options = struct();
48 options.series = 'ds';
49 options.last_obs = 9;
50 ds1 = histvalf_initvalf(caller, M, options);
51 failed_tests = my_assert(failed_tests, ds1.init == dates('1Y'), ...
52 'last_obs test 1');
53 failed_tests = my_assert(failed_tests, ds1.last == dates('9Y'), ...
54 'last_obs test 2');
55
56 options = struct();
57 options.series = 'ds';
58 options.first_obs = 2;
59 options.last_obs = 9;
60 options.nobs = 8;
61 ds1 = histvalf_initvalf(caller, M, options);
62 failed_tests = my_assert(failed_tests, ds1.init == dates('2Y'), ...
63 'first_obs, last_obs, nobs test 1');
64 failed_tests = my_assert(failed_tests, ds1.last == dates('9Y'), ...
65 'first_obs, last_obs, nobs test 2');
66
67 options = struct();
68 options.series = 'ds';
69 options.last_obs = 9;
70 options.nobs = 8;
71 ds1 = histvalf_initvalf(caller, M, options);
72 failed_tests = my_assert(failed_tests, ds1.init == dates('2Y'), ...
73 'last_obs, nobs test 1');
74 failed_tests = my_assert(failed_tests, ds1.last == dates('9Y'), ...
75 'last_obs, nobs test 2');
76
77 options = struct();
78 options.series = 'ds';
79 options.first_obs = 2;
80 options.last_obs = 9;
81 options.nobs = 7;
82
83 try
84 ds1 = histvalf_initvalf(caller, M, options);
85 error('This test didn''t catch the error')
86 catch me
87 if ~strcmp(me.message, strcat('INITVAL_FILE: FIRST_OBS, LAST_OBS and NOBS contain', ...
88 ' inconsistent information. Use only two of these', ...
89 ' options.'))
90 failed_tests = cat(1, failed_tests, 'Wrong nobs error message' );
91 end
92 end
93
94 options = struct();
95 options.series = 'ds';
96 options.first_obs = 0;
97
98 try
99 ds1 = histvalf_initvalf(caller, M, options);
100 error('This test didn''t catch the error')
101 catch me
102 if ~strcmp(me.message, strcat(caller, '_FILE: first_obs must be a positive number'))
103 failed_tests = cat(1, failed_tests, ...
104 'Wrong first period error message');
105 end
106 end
107
108 options = struct();
109 options.series = 'ds';
110 options.last_obs = 11;
111
112 try
113 ds1 = histvalf_initvalf(caller, M, options);
114 error('This test didn''t catch the error')
115 catch me
116 if ~strcmp(me.message, strcat(caller, '_FILE: last_obs = 11 is larger than the number', ...
117 ' of observations in the dataset (10)'))
118 failed_tests = cat(1, failed_tests, ...
119 'Wrong last period error message');
120 end
121 end
122
123 fh = fopen('data.m', 'w');
124 init__ = 'INIT__ = ''1Y'';';
125 fprintf(fh, [init__ '\n']);
126 eval(init__);
127 names__ = 'NAMES__ = {''x'', ''y''};';
128 fprintf(fh, [names__ '\n']);
129 eval(names__);
130 tex__ = 'TEX__ = {''x'', ''y''};';
131 fprintf(fh, [tex__ '\n']);
132 eval(tex__);
133 x = randn(10, 1);
134 fprintf(fh, 'x = [');
135 fprintf(fh, '%f ', x);
136 fprintf(fh, '];\n');
137 y = randn(10, 1);
138 fprintf(fh, 'y = [');
139 fprintf(fh, '%f ', y);
140 fprintf(fh, '];\n');
141 fclose(fh);
142
143 if isoctave
144 % To ensure that Octave sees the newly-created data.m script
145 rehash
146 end
147
148 M.endo_nbr = 1;
149 M.orig_endo_nbr = 1;
150 M.endo_names = {'y'};
151 M.exo_nbr = 1;
152 M.exo_names = {'x'};
153 M.exo_det_nbr = 0;
154
155 options = struct();
156 options.datafile = 'data.m';
157 series = histvalf_initvalf('INITVAL_FILE', M, options);
158 failed_tests = my_assert(failed_tests, series.init == dates('1Y'), ...
159 '*.m file first_obs test');
160 failed_tests = my_assert(failed_tests, series.nobs == 10, ...
161 '*.m file nobs test');
162
163 save('data.mat', 'INIT__', 'NAMES__', 'TEX__', 'x', 'y');
164 options = struct();
165 options.datafile = 'data.mat';
166 series = histvalf_initvalf('INITVAL_FILE', M, options);
167 failed_tests = my_assert(failed_tests, series.init == dates('1Y'), ...
168 '*.mat file first_obs test');
169 failed_tests = my_assert(failed_tests, series.nobs == 10, ...
170 '*.mat file nobs test');
171
172 fh = fopen('data.csv', 'w');
173 fprintf(fh, 'x,y\n');
174 for i = 1:size(x,1)
175 fprintf(fh, '%f,%f\n', x(i), y(i));
176 end
177 fclose(fh);
178
179 % The table() function is not implemented in Octave
180 if ~isoctave
181 writetable(table(x,y), 'data.xlsx')
182 options = struct();
183 options.datafile = 'data.xlsx';
184 series = histvalf_initvalf('INITVAL_FILE', M, options);
185 failed_tests = my_assert(failed_tests, series.init == dates('1Y'), ...
186 '*.xlsx file first_obs test');
187 failed_tests = my_assert(failed_tests, series.nobs == 10, ...
188 '*.xlsx file nobs test');
189 end
190
191 % The table() function is not implemented in Octave
192 % The test also does not work under GNU/Linux + MATLAB R2020b (Unicode issue in xlsread)
193 if ~isoctave && ispc
194 writetable(table(x,y), 'data.xls')
195 options = struct();
196 options.datafile = 'data.xls';
197 series = histvalf_initvalf('INITVAL_FILE', M, options);
198 failed_tests = my_assert(failed_tests, series.init == dates('1Y'), ...
199 '*.xls file first_obs test');
200 failed_tests = my_assert(failed_tests, series.nobs == 10, ...
201 '*.xls file nobs test');
202 end
203
204 if length(failed_tests) > 0
205 fprintf('\n*** Failed tests: %s\n', failed_tests{:})
206 end
207
208 quit(length(failed_tests) > 0)