1 ## Copyright (C) 2000-2012 Paul Kienzle
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} assert (@var{cond})
21 ## @deftypefnx {Function File} {} assert (@var{cond}, @var{errmsg}, @dots{})
22 ## @deftypefnx {Function File} {} assert (@var{cond}, @var{msg_id}, @var{errmsg}, @dots{})
23 ## @deftypefnx {Function File} {} assert (@var{observed}, @var{expected})
24 ## @deftypefnx {Function File} {} assert (@var{observed}, @var{expected}, @var{tol})
26 ## Produce an error if the specified condition is not met. @code{assert} can
27 ## be called in three different ways.
30 ## @item assert (@var{cond})
31 ## @itemx assert (@var{cond}, @var{errmsg}, @dots{})
32 ## @itemx assert (@var{cond}, @var{msg_id}, @var{errmsg}, @dots{})
33 ## Called with a single argument @var{cond}, @code{assert} produces an
34 ## error if @var{cond} is zero. When called with more than one argument the
35 ## additional arguments are passed to the @code{error} function.
37 ## @item assert (@var{observed}, @var{expected})
38 ## Produce an error if observed is not the same as expected. Note that
39 ## @var{observed} and @var{expected} can be scalars, vectors, matrices,
40 ## strings, cell arrays, or structures.
42 ## @item assert (@var{observed}, @var{expected}, @var{tol})
43 ## Produce an error if observed is not the same as expected but equality
44 ## comparison for numeric data uses a tolerance @var{tol}.
45 ## If @var{tol} is positive then it is an absolute tolerance which will produce
46 ## an error if @code{abs(@var{observed} - @var{expected}) > abs(@var{tol})}.
47 ## If @var{tol} is negative then it is a relative tolerance which will produce
48 ## an error if @code{abs(@var{observed} - @var{expected}) >
49 ## abs(@var{tol} * @var{expected})}. If @var{expected} is zero @var{tol} will
50 ## always be interpreted as an absolute tolerance.
52 ## @seealso{test, fail, error}
55 ## FIXME: Output throttling: don't print out the entire 100x100 matrix,
56 ## but instead give a summary; don't print out the whole list, just
57 ## say what the first different element is, etc. To do this, make
58 ## the message generation type specific.
60 function assert (cond, varargin)
62 in = deblank (argn(1,:));
64 in = cstrcat (in, ",", deblank (argn(i,:)));
66 in = cstrcat ("(", in, ")");
68 if (nargin == 1 || (nargin > 1 && islogical (cond) && ischar (varargin{1})))
69 if ((! isnumeric (cond) && ! islogical (cond)) || ! all (cond(:)))
71 ## Say which elements failed?
72 error ("assert %s failed", in);
78 if (nargin < 2 || nargin > 3)
82 expected = varargin{1};
89 if (exist ("argn") == 0)
97 if (ischar (expected))
98 iserror = (! ischar (cond) || ! strcmp (cond, expected));
100 elseif (iscell (expected))
101 if (! iscell (cond) || any (size (cond) != size (expected)))
105 for i = 1:length (expected(:))
106 assert (cond{i}, expected{i}, tol);
113 elseif (isstruct (expected))
114 if (! isstruct (cond) || any (size (cond) != size (expected))
115 || rows (fieldnames (cond)) != rows (fieldnames (expected)))
119 #empty = numel (cond) == 0;
120 empty = isempty (cond);
121 normal = (numel (cond) == 1);
123 if (! isfield (expected, k))
133 assert (v, {expected.(k)}, tol);
140 elseif (ndims (cond) != ndims (expected)
141 || any (size (cond) != size (expected)))
143 coda = "Dimensions don't match";
147 ## Without explicit tolerance, be more strict.
148 if (! strcmp (class (cond), class (expected)))
150 coda = cstrcat ("Class ", class (cond), " != ", class (expected));
151 elseif (isnumeric (cond))
152 if (issparse (cond) != issparse (expected))
155 coda = "sparse != non-sparse";
158 coda = "non-sparse != sparse";
160 elseif (iscomplex (cond) != iscomplex (expected))
161 if (iscomplex (cond))
163 coda = "complex != real";
166 coda = "real != complex";
176 ## Check exceptional values.
177 if (any (isna (A) != isna (B)))
179 coda = "NAs don't match";
180 elseif (any (isnan (A) != isnan (B)))
182 coda = "NaNs don't match";
183 ## Try to avoid problems comparing strange values like Inf+NaNi.
184 elseif (any (isinf (A) != isinf (B))
185 || any (A(isinf (A) & ! isnan (A)) != B(isinf (B) & ! isnan (B))))
187 coda = "Infs don't match";
189 ## Check normal values.
194 errtype = "values do not match";
196 err = max (abs (A - B));
197 errtype = "maximum absolute error %g exceeds tolerance %g";
199 abserr = max (abs (A(B == 0)));
202 relerr = max (abs (A - B) ./ abs (B));
203 err = max ([abserr; relerr]);
204 errtype = "maximum relative error %g exceeds tolerance %g";
208 coda = sprintf (errtype, err, abs (tol));
219 ## Pretty print the "expected but got" info, trimming leading and
221 str = disp (expected);
222 idx = find (str != "\n");
224 str = str(idx(1):idx(end));
227 idx = find (str2 != "\n");
229 str2 = str2 (idx(1):idx(end));
231 msg = cstrcat ("assert ", in, " expected\n", str, "\nbut got\n", str2);
232 if (! isempty (coda))
233 msg = cstrcat (msg, "\n", coda);
243 %!assert (zeros (3,0), zeros (3,0))
244 %!error assert (zeros (3,0), zeros (0,2))
245 %!error assert (zeros (3,0), [])
246 %!error <Dimensions don't match> assert (zeros (2,0,2), zeros (2,0))
249 %!assert (isempty ([]))
255 %!error assert ([1,0,1])
256 %!error assert ([1;1;0])
257 %!error assert ([1,0;1,1])
260 %!error assert (3, [3,3; 3,3])
261 %!error assert ([3,3; 3,3], 3)
263 %!assert (3+eps, 3, eps)
264 %!assert (3, 3+eps, eps)
265 %!error assert (3+2*eps, 3, eps)
266 %!error assert (3, 3+2*eps, eps)
269 %!assert ([1,2,3],[1,2,3]);
270 %!assert ([1;2;3],[1;2;3]);
271 %!error assert ([2;2;3],[1;2;3]);
272 %!error assert ([1,2,3],[1;2;3]);
273 %!error assert ([1,2],[1,2,3]);
274 %!error assert ([1;2;3],[1;2]);
275 %!assert ([1,2;3,4],[1,2;3,4]);
276 %!error assert ([1,4;3,4],[1,2;3,4])
277 %!error assert ([1,3;2,4;3,5],[1,2;3,4])
279 ## must give a small tolerance for floating point errors on relative
280 %!assert (100+100*eps, 100, -2*eps)
281 %!assert (100, 100+100*eps, -2*eps)
282 %!error assert (100+300*eps, 100, -2*eps)
283 %!error assert (100, 100+300*eps, -2*eps)
284 %!error assert (3, [3,3])
285 %!error assert (3, 4)
287 ## test relative vs. absolute tolerances
288 %!test assert (0.1+eps, 0.1, 2*eps); # accept absolute
289 %!error assert (0.1+eps, 0.1, -2*eps); # fail relative
290 %!test assert (100+100*eps, 100, -2*eps); # accept relative
291 %!error assert (100+100*eps, 100, 2*eps); # fail absolute
293 ## exceptional values
294 %!assert ([NaN, NA, Inf, -Inf, 1+eps, eps], [NaN, NA, Inf, -Inf, 1, 0], eps)
295 %!error assert (NaN, 1)
296 %!error assert (NA, 1)
297 %!error assert (-Inf, Inf)
300 %!assert ("dog", "dog")
301 %!error assert ("dog", "cat")
302 %!error assert ("dog", 3)
303 %!error assert (3, "dog")
307 %! x.a = 1; x.b=[2, 2];
308 %! y.a = 1; y.b=[2, 2];
311 %!error assert (x, y)
312 %!error assert (3, x)
313 %!error assert (x, 3)
315 %! # Empty structures
316 %! x = resize (x, 0, 1);
317 %! y = resize (y, 0, 1);
322 %! x = {[3], [1,2,3]; 100+100*eps, "dog"};
325 %! y = x; y(1,1) = [2];
326 %! fail ("assert (x, y)");
327 %! y = x; y(1,2) = [0, 2, 3];
328 %! fail ("assert (x, y)");
329 %! y = x; y(2,1) = 101;
330 %! fail ("assert (x, y)");
331 %! y = x; y(2,2) = "cat";
332 %! fail ("assert (x, y)");
334 %% Test input validation
336 %!error assert (1,2,3,4)