Mercurial > hg > octave-lyh
annotate scripts/testfun/assert.m @ 11563:3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 18 Jan 2011 20:55:01 -0800 |
parents | fd0a3ac60b0e |
children | c792872f8942 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2000-2011 Paul Kienzle |
5589 | 2 ## |
7016 | 3 ## This file is part of Octave. |
5589 | 4 ## |
7016 | 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. | |
9 ## | |
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. | |
5589 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5589 | 18 |
19 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {} assert (@var{cond}) |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
21 ## @deftypefnx {Function File} {} assert (@var{cond}, @var{errmsg}, @dots{}) |
9313 | 22 ## @deftypefnx {Function File} {} assert (@var{cond}, @var{msg_id}, @var{errmsg}, @dots{}) |
11563
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
23 ## @deftypefnx {Function File} {} assert (@var{observed}, @var{expected}) |
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
24 ## @deftypefnx {Function File} {} assert (@var{observed}, @var{expected}, @var{tol}) |
5589 | 25 ## |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8989
diff
changeset
|
26 ## Produces an error if the condition is not met. @code{assert} can be |
5589 | 27 ## called in three different ways. |
28 ## | |
29 ## @table @code | |
30 ## @item assert (@var{cond}) | |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
31 ## @itemx assert (@var{cond}, @var{errmsg}, @dots{}) |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
32 ## @itemx assert (@var{cond}, @var{msg_id}, @var{errmsg}, @dots{}) |
5589 | 33 ## Called with a single argument @var{cond}, @code{assert} produces an |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8989
diff
changeset
|
34 ## error if @var{cond} is zero. If called with a single argument a |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8989
diff
changeset
|
35 ## generic error message. With more than one argument, the additional |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
36 ## arguments are passed to the @code{error} function. |
5589 | 37 ## |
38 ## @item assert (@var{observed}, @var{expected}) | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8989
diff
changeset
|
39 ## Produce an error if observed is not the same as expected. Note that |
5589 | 40 ## observed and expected can be strings, scalars, vectors, matrices, |
41 ## lists or structures. | |
42 ## | |
43 ## @item assert(@var{observed}, @var{expected}, @var{tol}) | |
7027 | 44 ## Accept a tolerance when comparing numbers. |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
45 ## If @var{tol} is positive use it as an absolute tolerance, will produce an |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
46 ## error if |
7027 | 47 ## @code{abs(@var{observed} - @var{expected}) > abs(@var{tol})}. |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
48 ## If @var{tol} is negative use it as a relative tolerance, will produce an |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
49 ## error if |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
50 ## @code{abs(@var{observed} - @var{expected}) > abs(@var{tol} * |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
51 ## @var{expected})}. If @var{expected} is zero @var{tol} will always be used as |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
52 ## an absolute tolerance. |
5589 | 53 ## @end table |
5642 | 54 ## @seealso{test} |
5589 | 55 ## @end deftypefn |
56 | |
8202
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7770
diff
changeset
|
57 ## FIXME: Output throttling: don't print out the entire 100x100 matrix, |
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7770
diff
changeset
|
58 ## but instead give a summary; don't print out the whole list, just |
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7770
diff
changeset
|
59 ## say what the first different element is, etc. To do this, make |
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7770
diff
changeset
|
60 ## the message generation type specific. |
6494 | 61 |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
62 function assert (cond, varargin) |
6494 | 63 |
64 in = deblank (argn(1,:)); | |
65 for i = 2:rows (argn) | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7151
diff
changeset
|
66 in = cstrcat (in, ",", deblank (argn(i,:))); |
7151 | 67 endfor |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7151
diff
changeset
|
68 in = cstrcat ("(", in, ")"); |
5589 | 69 |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
70 if (nargin == 1 || (nargin > 1 && islogical (cond) && ischar (varargin{1}))) |
9841
6f1ea8241c99
make isnumeric yield false on logicals
Jaroslav Hajek <highegg@gmail.com>
parents:
9448
diff
changeset
|
71 if ((! isnumeric (cond) && ! islogical (cond)) || ! all (cond(:))) |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
72 if (nargin == 1) |
10549 | 73 ## Say which elements failed? |
74 error ("assert %s failed", in); | |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
75 else |
10549 | 76 error (varargin{:}); |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
77 endif |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
78 endif |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
79 else |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
80 if (nargin < 2 || nargin > 3) |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
81 print_usage (); |
5589 | 82 endif |
83 | |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
84 expected = varargin {1}; |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
85 if (nargin < 3) |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
86 tol = 0; |
5589 | 87 else |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
88 tol = varargin {2}; |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
89 endif |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
90 |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
91 if (exist ("argn") == 0) |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
92 argn = " "; |
5589 | 93 endif |
94 | |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
95 coda = ""; |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
96 iserror = 0; |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
97 |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
98 |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
99 if (ischar (expected)) |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
100 iserror = (! ischar (cond) || ! strcmp (cond, expected)); |
5589 | 101 |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
102 elseif (iscell (expected)) |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
103 if (! iscell (cond) || any (size (cond) != size (expected))) |
10549 | 104 iserror = 1; |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
105 else |
10549 | 106 try |
107 for i = 1:length (expected(:)) | |
108 assert (cond{i}, expected{i}, tol); | |
109 endfor | |
110 catch | |
111 iserror = 1; | |
112 end_try_catch | |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
113 endif |
5589 | 114 |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
115 elseif (isstruct (expected)) |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
116 if (! isstruct (cond) || any (size (cond) != size (expected)) |
10549 | 117 || rows (fieldnames (cond)) != rows (fieldnames (expected))) |
118 iserror = 1; | |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
119 else |
10549 | 120 try |
121 empty = numel (cond) == 0; | |
122 normal = numel (cond) == 1; | |
123 for [v, k] = cond | |
124 if (! isfield (expected, k)) | |
125 error (); | |
126 endif | |
127 if (empty) | |
128 v = cell (1, 0); | |
129 endif | |
130 if (normal) | |
131 v = {v}; | |
132 else | |
133 v = v(:)'; | |
134 endif | |
135 assert (v, {expected.(k)}, tol); | |
136 endfor | |
137 catch | |
138 iserror = 1; | |
139 end_try_catch | |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
140 endif |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
141 |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
142 elseif (ndims (cond) != ndims (expected) |
10549 | 143 || any (size (cond) != size (expected))) |
5589 | 144 iserror = 1; |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
145 coda = "Dimensions don't match"; |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
146 |
9447
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
147 else |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
148 if (nargin < 3) |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
149 ## Without explicit tolerance, be more strict. |
9448
cb4a4119a21a
Fix string comparison bug in assert.m
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9447
diff
changeset
|
150 if (! strcmp(class (cond), class (expected))) |
9447
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
151 iserror = 1; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
152 coda = cstrcat ("Class ", class (cond), " != ", class (expected)); |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
153 elseif (isnumeric (cond)) |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
154 if (issparse (cond) != issparse (expected)) |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
155 if (issparse (cond)) |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
156 iserror = 1; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
157 coda = "sparse != non-sparse"; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
158 else |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
159 iserror = 1; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
160 coda = "non-sparse != sparse"; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
161 endif |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
162 elseif (iscomplex (cond) != iscomplex (expected)) |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
163 if (iscomplex (cond)) |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
164 iserror = 1; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
165 coda = "complex != real"; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
166 else |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
167 iserror = 1; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
168 coda = "real != complex"; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
169 endif |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
170 endif |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
171 endif |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
172 endif |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
173 |
9447
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
174 if (! iserror) |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
175 ## Numeric. |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
176 A = cond(:); |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
177 B = expected(:); |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
178 ## Check exceptional values. |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
179 if (any (isna (A) != isna (B))) |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
180 iserror = 1; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
181 coda = "NAs don't match"; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
182 elseif (any (isnan (A) != isnan (B))) |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
183 iserror = 1; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
184 coda = "NaNs don't match"; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
185 ## Try to avoid problems comparing strange values like Inf+NaNi. |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
186 elseif (any (isinf (A) != isinf (B)) |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
187 || any (A(isinf (A) & ! isnan (A)) != B(isinf (B) & ! isnan (B)))) |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
188 iserror = 1; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
189 coda = "Infs don't match"; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
190 else |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
191 ## Check normal values. |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
192 A = A(finite (A)); |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
193 B = B(finite (B)); |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
194 if (tol == 0) |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
195 err = any (A != B); |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
196 errtype = "values do not match"; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
197 elseif (tol >= 0) |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
198 err = max (abs (A - B)); |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
199 errtype = "maximum absolute error %g exceeds tolerance %g"; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
200 else |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
201 abserr = max (abs (A(B == 0))); |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
202 A = A(B != 0); |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
203 B = B(B != 0); |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
204 relerr = max (abs (A - B) ./ abs (B)); |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
205 err = max ([abserr; relerr]); |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
206 errtype = "maximum relative error %g exceeds tolerance %g"; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
207 endif |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
208 if (err > abs (tol)) |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
209 iserror = 1; |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
210 coda = sprintf (errtype, err, abs (tol)); |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
211 endif |
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
212 endif |
5589 | 213 endif |
9447
bb2ae2210e37
relax assert for exact matches
Jaroslav Hajek <highegg@gmail.com>
parents:
9313
diff
changeset
|
214 |
5589 | 215 endif |
216 | |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
217 if (! iserror) |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
218 return; |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
219 endif |
5589 | 220 |
8506 | 221 ## Pretty print the "expected but got" info, trimming leading and |
222 ## trailing "\n". | |
7770
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
223 str = disp (expected); |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
224 idx = find (str != "\n"); |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
225 if (! isempty (idx)) |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
226 str = str(idx(1):idx(end)); |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
227 endif |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
228 str2 = disp (cond); |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
229 idx = find (str2 != "\n"); |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
230 if (! isempty (idx)) |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
231 str2 = str2 (idx(1):idx(end)); |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
232 endif |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
233 msg = cstrcat ("assert ", in, " expected\n", str, "\nbut got\n", str2); |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
234 if (! isempty (coda)) |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
235 msg = cstrcat (msg, "\n", coda); |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
236 endif |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
237 error ("%s", msg); |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
238 ## disp (msg); |
c6a1a217ac3c
Compatibility fix for assert
David Bateman <dbateman@free.fr>
parents:
7703
diff
changeset
|
239 ## error ("assertion failed"); |
5589 | 240 endif |
241 endfunction | |
242 | |
243 ## empty | |
244 %!assert([]) | |
245 %!assert(zeros(3,0),zeros(3,0)) | |
246 %!error assert(zeros(3,0),zeros(0,2)) | |
247 %!error assert(zeros(3,0),[]) | |
6455 | 248 %!fail("assert(zeros(2,0,2),zeros(2,0))", "Dimensions don't match") |
5589 | 249 |
250 ## conditions | |
251 %!assert(isempty([])) | |
252 %!assert(1) | |
253 %!error assert(0) | |
254 %!assert(ones(3,1)) | |
255 %!assert(ones(1,3)) | |
256 %!assert(ones(3,4)) | |
257 %!error assert([1,0,1]) | |
258 %!error assert([1;1;0]) | |
259 %!error assert([1,0;1,1]) | |
260 | |
261 ## vectors | |
262 %!assert([1,2,3],[1,2,3]); | |
263 %!assert([1;2;3],[1;2;3]); | |
264 %!error assert([2;2;3],[1;2;3]); | |
265 %!error assert([1,2,3],[1;2;3]); | |
266 %!error assert([1,2],[1,2,3]); | |
267 %!error assert([1;2;3],[1;2]); | |
268 %!assert([1,2;3,4],[1,2;3,4]); | |
269 %!error assert([1,4;3,4],[1,2;3,4]) | |
270 %!error assert([1,3;2,4;3,5],[1,2;3,4]) | |
271 | |
272 ## exceptional values | |
273 %!assert([NaN, NA, Inf, -Inf, 1+eps, eps],[NaN, NA, Inf, -Inf, 1, 0],eps) | |
274 %!error assert(NaN, 1) | |
275 %!error assert(NA, 1) | |
276 %!error assert(-Inf, Inf) | |
277 | |
278 ## scalars | |
279 %!error assert(3, [3,3; 3,3]) | |
280 %!error assert([3,3; 3,3], 3) | |
281 %!assert(3, 3); | |
282 %!assert(3+eps, 3, eps); | |
283 %!assert(3, 3+eps, eps); | |
284 %!error assert(3+2*eps, 3, eps); | |
285 %!error assert(3, 3+2*eps, eps); | |
286 | |
7027 | 287 ## must give a little space for floating point errors on relative |
5589 | 288 %!assert(100+100*eps, 100, -2*eps); |
289 %!assert(100, 100+100*eps, -2*eps); | |
290 %!error assert(100+300*eps, 100, -2*eps); | |
291 %!error assert(100, 100+300*eps, -2*eps); | |
292 %!error assert(3, [3,3]); | |
293 %!error assert(3,4); | |
294 | |
7027 | 295 ## test relative vs. absolute tolerances |
296 %!test assert (0.1+eps, 0.1, 2*eps); # accept absolute | |
297 %!error assert (0.1+eps, 0.1, -2*eps); # fail relative | |
298 %!test assert (100+100*eps, 100, -2*eps); # accept relative | |
299 %!error assert (100+100*eps, 100, 2*eps); # fail absolute | |
300 | |
5589 | 301 ## structures |
302 %!shared x,y | |
303 %! x.a = 1; x.b=[2, 2]; | |
304 %! y.a = 1; y.b=[2, 2]; | |
305 %!assert (x,y) | |
306 %!test y.b=3; | |
307 %!error assert (x,y) | |
308 %!error assert (3, x); | |
309 %!error assert (x, 3); | |
310 | |
311 ## check usage statements | |
312 %!error assert | |
313 %!error assert(1,2,3,4,5) | |
314 | |
315 ## strings | |
316 %!assert("dog","dog") | |
317 %!error assert("dog","cat") | |
318 %!error assert("dog",3); | |
319 %!error assert(3,"dog"); |