Mercurial > hg > octave-nkf
annotate scripts/testfun/fail.m @ 9665:1dba57e9d08d
use blas_trans_type for xgemm
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Sat, 26 Sep 2009 10:41:07 +0200 |
parents | 1bf0ce0930be |
children | 95c3e38098bf |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2005, 2006, 2007, 2009 Paul Kienzle |
6494 | 2 ## |
7016 | 3 ## This file is part of Octave. |
6494 | 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. | |
6494 | 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/>. | |
18 ## | |
19 ## Original version by Paul Kienzle distributed as free software in the | |
20 ## public domain. | |
6494 | 21 |
5589 | 22 ## -*- texinfo -*- |
23 ## @deftypefn {Function File} {} fail (@var{code},@var{pattern}) | |
24 ## @deftypefnx {Function File} {} fail (@var{code},'warning',@var{pattern}) | |
25 ## | |
26 ## Return true if @var{code} fails with an error message matching | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
27 ## @var{pattern}, otherwise produce an error. Note that @var{code} |
5589 | 28 ## is a string and if @var{code} runs successfully, the error produced is: |
29 ## | |
30 ## @example | |
31 ## expected error but got none | |
32 ## @end example | |
33 ## | |
34 ## If the code fails with a different error, the message produced is: | |
35 ## | |
36 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
37 ## @group |
5589 | 38 ## expected <pattern> |
39 ## but got <text of actual error> | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
40 ## @end group |
5589 | 41 ## @end example |
42 ## | |
43 ## The angle brackets are not part of the output. | |
44 ## | |
45 ## Called with three arguments, the behavior is similar to | |
46 ## @code{fail(@var{code}, @var{pattern})}, but produces an error if no | |
47 ## warning is given during code execution or if the code fails. | |
48 ## | |
49 ## @end deftypefn | |
50 | |
51 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
52 | |
6494 | 53 function ret = fail (code, pattern, warning_pattern) |
54 | |
55 if (nargin < 1 || nargin > 3) | |
6046 | 56 print_usage (); |
5589 | 57 endif |
58 | |
59 ## sort out arguments | |
6494 | 60 test_warning = (nargin > 1 && strcmp (pattern, "warning")); |
61 if (nargin == 3) | |
5589 | 62 pattern = warning_pattern; |
8507 | 63 elseif (nargin == 1 || (nargin == 2 && test_warning)) |
5589 | 64 pattern = ""; |
65 endif | |
6494 | 66 |
67 ## match any nonempty message | |
68 if (isempty (pattern)) | |
69 pattern = "."; | |
70 endif | |
5589 | 71 |
72 ## allow assert(fail()) | |
6494 | 73 if (nargout) |
74 ret = 1; | |
75 endif | |
5589 | 76 |
6494 | 77 if (test_warning) |
8506 | 78 ## Perform the warning test. |
79 ## Clear old warnings. | |
80 lastwarn (); | |
81 ## Make sure warnings are turned on. | |
82 state = warning ("query", "quiet"); | |
6494 | 83 warning ("on", "quiet"); |
5589 | 84 try |
85 ## printf("lastwarn before %s: %s\n",code,lastwarn); | |
6494 | 86 evalin ("caller", sprintf ("%s;", code)); |
5589 | 87 ## printf("lastwarn after %s: %s\n",code,lastwarn); |
8506 | 88 ## Retrieve new warnings. |
89 err = lastwarn (); | |
6494 | 90 warning (state.state, "quiet"); |
91 if (isempty (err)) | |
92 msg = sprintf ("expected warning <%s> but got none", pattern); | |
5589 | 93 else |
8506 | 94 ## Transform "warning: ...\n" to "...". |
95 err([1:9, end]) = []; | |
6494 | 96 if (! isempty (regexp (err, pattern, "once"))) |
97 return; | |
98 endif | |
99 msg = sprintf ("expected warning <%s>\nbut got <%s>", pattern, err); | |
5589 | 100 endif |
101 catch | |
6494 | 102 warning (state.state, "quiet"); |
5589 | 103 err = lasterr; |
8506 | 104 ## Transform "error: ...\n", to "...". |
105 err([1:7, end]) = []; | |
6494 | 106 msg = sprintf ("expected warning <%s> but got error <%s>", pattern, err); |
7151 | 107 end_try_catch |
5589 | 108 |
109 else | |
8506 | 110 ## Perform the error test. |
5589 | 111 try |
6494 | 112 evalin ("caller", sprintf ("%s;", code)); |
113 msg = sprintf ("expected error <%s> but got none", pattern); | |
5589 | 114 catch |
6494 | 115 err = lasterr (); |
116 if (strcmp (err(1:7), "error:")) | |
117 err([1:6, end]) = []; # transform "error: ...\n", to "..." | |
5589 | 118 endif |
6494 | 119 if (! isempty (regexp (err, pattern, "once"))) |
120 return; | |
121 endif | |
122 msg = sprintf ("expected error <%s>\nbut got <%s>", pattern, err); | |
7151 | 123 end_try_catch |
5589 | 124 endif |
125 | |
8506 | 126 ## If we get here, then code didn't fail or error didn't match. |
6494 | 127 error (msg); |
8506 | 128 |
5589 | 129 endfunction |
130 | |
131 %!fail ('[1,2]*[2,3]','nonconformant') | |
132 %!fail ("fail('[1,2]*[2;3]','nonconformant')","expected error <nonconformant> but got none") | |
133 %!fail ("fail('[1,2]*[2,3]','usage:')","expected error <usage:>\nbut got.*nonconformant") | |
134 %!fail ("warning('test warning')",'warning','test warning'); | |
135 | |
136 %!# fail ("warning('next test')",'warning','next test'); ## only allowed one warning test?!? | |
137 | |
138 ## Comment out the following tests if you don't want to see what | |
139 ## errors look like | |
140 % !fail ('a*[2;3]', 'nonconformant') | |
141 % !fail ('a*[2,3]', 'usage:') | |
142 % !fail ("warning('warning failure')", 'warning', 'success') |