Mercurial > octave
changeset 22093:6457ff946024
Set error ids in validateattributes
* scripts/general/validateattributes.m : Set correct error IDs
for errors thrown in validateattributes.
author | Carlo de Falco <carlo.defalco@polimi.it> |
---|---|
date | Tue, 12 Jul 2016 10:17:45 +0200 |
parents | d3cef63f79ac |
children | 9203833cab7d |
files | scripts/general/validateattributes.m |
diffstat | 1 files changed, 325 insertions(+), 49 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/general/validateattributes.m Mon Jul 11 15:08:15 2016 -0700 +++ b/scripts/general/validateattributes.m Tue Jul 12 10:17:45 2016 +0200 @@ -1,4 +1,5 @@ ## Copyright (C) 2013-2015 Carnë Draug +## Copyright (C) 2016 Carlo de Falco ## ## This file is part of Octave. ## @@ -173,9 +174,11 @@ if (nargin < 3 || nargin > 6) print_usage (); elseif (! iscellstr (cls)) - error ("validateattributes: CLASSES must be a cell array of strings"); + error ("Octave:invalid-invalid-type", + "validateattributes: CLASSES must be a cell array of strings"); elseif (! iscell (attr)) - error ("validateattributes: ATTRIBUTES must be a cell array"); + error ("Octave:invalid-invalid-type", + "validateattributes: ATTRIBUTES must be a cell array"); endif ## Built start of error message from the extra optional arguments @@ -188,19 +191,22 @@ elseif (nargin == 4 && valid_arg_idx (fourth)) var_name = sprintf ("input %d", fourth); else - error ("validateattributes: 4th input argument must be ARG_IDX or FUNC_NAME"); + error ("Octave:invalid-input-arg", + "validateattributes: 4th input argument must be ARG_IDX or FUNC_NAME"); endif if (nargin > 4) var_name = varargin{2}; if (! ischar (var_name)) - error ("validateattributes: VAR_NAME must be a string"); + error ("Octave:invalid-invalid-type", + "validateattributes: VAR_NAME must be a string"); endif if (nargin > 5) arg_idx = varargin{3}; if (! valid_arg_idx (arg_idx)) - error ("validateattributes: ARG_IDX must be a positive integer"); + error ("Octave:invalid-input-arg", + "validateattributes: ARG_IDX must be a positive integer"); endif var_name = sprintf ("%s (argument #%i)", var_name, arg_idx); endif @@ -224,7 +230,8 @@ cls = unique (cls); classes = sprintf (" %s", cls{:}); - error ("%s must be of class:\n\n %s\n\nbut was of class %s", + error ("Octave:invalid-invalid-type", + "%s must be of class:\n\n %s\n\nbut was of class %s", err_ini, classes, class (A)); endif @@ -247,38 +254,86 @@ ## which attributes are checked more often, and place them in that ## order inside the switch block. switch (tolower (attr{idx++})) - case "2d", problem = ndims (A) != 2; - case "3d", problem = ndims (A) > 3; - case "column", problem = ! iscolumn (A); - case "row", problem = ! isrow (A); - case "scalar", problem = ! isscalar (A); - case "vector", problem = ! isvector (A); - case "square", problem = ! issquare (A); - case "diag", problem = ! isdiag (A); - case "nonempty", problem = isempty (A); - case "nonsparse", problem = issparse (A); - case "binary", problem = ! islogical (A) && ... - any ((A(:) != 1) & (A(:) != 0)); - case "even", problem = any (rem (A(:), 2) != 0); - case "odd", problem = any (mod (A(:), 2) != 1); - case "integer", problem = ! isinteger (A) && ... - any (ceil (A(:)) != A(:)); - case "real", problem = ! isreal (A); - case "finite", problem = ! isinteger (A) && ... - ! all (isfinite (A(:))); - case "nonnan", problem = ! isinteger (A) && ... - any (isnan (A(:))); - case "nonnegative", problem = any (A(:) < 0); - case "nonzero", problem = any (A(:) == 0); - case "positive", problem = any (A(:) <= 0); - case "decreasing", problem = (any (isnan (A(:))) - || any (diff (A(:)) >= 0)); - case "increasing", problem = (any (isnan (A(:))) - || any (diff (A(:)) <= 0)); - case "nondecreasing", problem = (any (isnan (A(:))) - || any (diff (A(:)) < 0)); - case "nonincreasing", problem = (any (isnan (A(:))) - || any (diff (A(:)) > 0)); + case "2d", + problem = ndims (A) != 2; + err_id = "Octave:expected-2d"; + case "3d", + problem = ndims (A) > 3; + err_id = "Octave:expected-3d"; + case "column", + problem = ! iscolumn (A); + err_id = "Octave:expected-column"; + case "row", + problem = ! isrow (A); + err_id = "Octave:expected-row"; + case "scalar", + problem = ! isscalar (A); + err_id = "Octave:expected-scalar"; + case "vector", + problem = ! isvector (A); + err_id = "Octave:expected-vector"; + case "square", + problem = ! issquare (A); + err_id = "Octave:expected-square"; + case "diag", + problem = ! isdiag (A); + err_id = "Octave:expected-diag"; + case "nonempty", + problem = isempty (A); + err_id = "Octave:expected-nonempty"; + case "nonsparse", + problem = issparse (A); + err_id = "Octave:expected-nonsparse"; + case "binary", + problem = ! islogical (A) && ... + any ((A(:) != 1) & (A(:) != 0)); + err_id = "Octave:expected-binary"; + case "even", + problem = any (rem (A(:), 2) != 0); + err_id = "Octave:expected-even"; + case "odd", + problem = any (mod (A(:), 2) != 1); + err_id = "Octave:expected-odd"; + case "integer", + problem = ! isinteger (A) && ... + any (ceil (A(:)) != A(:)); + err_id = "Octave:expected-integer"; + case "real", + problem = ! isreal (A); + err_id = "Octave:expected-real"; + case "finite", + problem = ! isinteger (A) && ... + ! all (isfinite (A(:))); + err_id = "Octave:expected-finite"; + case "nonnan", + problem = ! isinteger (A) && ... + any (isnan (A(:))); + err_id = "Octave:expected-nonnan"; + case "nonnegative", + problem = any (A(:) < 0); + err_id = "Octave:expected-nonnegative"; + case "nonzero", + problem = any (A(:) == 0); + err_id = "Octave:expected-nonzero"; + case "positive", + problem = any (A(:) <= 0); + err_id = "Octave:expected-positive"; + case "decreasing", + problem = (any (isnan (A(:))) + || any (diff (A(:)) >= 0)); + err_id = "Octave:expected-decreasing"; + case "increasing", + problem = (any (isnan (A(:))) + || any (diff (A(:)) <= 0)); + err_id = "Octave:expected-increasing"; + case "nondecreasing", + problem = (any (isnan (A(:))) + || any (diff (A(:)) < 0)); + err_id = "Octave:expected-nondecreasing"; + case "nonincreasing", + problem = (any (isnan (A(:))) + || any (diff (A(:)) > 0)); + err_id = "Octave:expected-nonincreasing"; case "size", A_size = size (A); w_size = attr{idx++}; @@ -287,47 +342,69 @@ A_size_str = sprintf ("%dx", size (A))(1:end-1); w_size_str = sprintf ("%ix", w_size)(1:end-1); w_size_str = strrep (w_size_str, "NaN", "N"); - error ("%s must be of size %s but was %s", err_ini, w_size_str, A_size_str); + err_id = "Octave:incorrect-size"; + error (err_id, + "%s must be of size %s but was %s", + err_ini, w_size_str, A_size_str); endif case "numel", if (numel (A) != attr{idx++}) - error ("%s must have %d elements", err_ini, attr{idx-1}); + err_id = "Octave:incorrect-numel"; + error (err_id, + "%s must have %d elements", err_ini, attr{idx-1}); endif case "ncols", if (columns (A) != attr{idx++}) - error ("%s must have %d columns", err_ini, attr{idx-1}); + err_id = "Octave:incorrect-numcols"; + error (err_id, + "%s must have %d columns", err_ini, attr{idx-1}); endif case "nrows", if (rows (A) != attr{idx++}) - error ("%s must have %d rows", err_ini, attr{idx-1}); + err_id = "Octave:incorrect-numrows"; + error (err_id, + "%s must have %d rows", err_ini, attr{idx-1}); endif case "ndims", ## Note that a [4 5 1] matrix is not considered to have ndims == 3 ## but is ok for "3d". This is not a bug. if (ndims (A) != attr{idx++}) - error ("%s must have %d dimensions", err_ini, attr{idx-1}); + err_id = "Octave:incorrect-numdims"; + error (err_id, + "%s must have %d dimensions", err_ini, attr{idx-1}); endif case ">" if (! all (A(:) > attr{idx++})) - error ("%s must be greater than %f", err_ini, attr{idx-1}); + err_id = "Octave:expected-greater"; + error (err_id, + "%s must be greater than %f", err_ini, attr{idx-1}); endif case ">=" if (! all (A(:) >= attr{idx++})) - error ("%s must be greater than or equal to %f", err_ini, attr{idx-1}); + err_id = "Octave:expected-greater-equal"; + error (err_id, + "%s must be greater than or equal to %f", err_ini, attr{idx-1}); endif case "<" if (! all (A(:) < attr{idx++})) - error ("%s must be less than %f", err_ini, attr{idx-1}); + err_id = "Octave:expected-less"; + error (err_id, + "%s must be less than %f", err_ini, attr{idx-1}); endif case "<=" if (! all (A(:) <= attr{idx++})) - error ("%s must be less than or equal to %f", err_ini, attr{idx-1}); + err_id = "Octave:expected-less-equal"; + error (err_id, + "%s must be less than or equal to %f", err_ini, attr{idx-1}); endif otherwise - error ("validateattributes: unknown ATTRIBUTE %s", attr{idx-1}); + err_id = "Octave:invalid-input-arg"; + error (err_id, + "validateattributes: unknown ATTRIBUTE %s", attr{idx-1}); endswitch if (problem) - error ("%s must be %s", err_ini, attr{idx-1}); + error (err_id, + "%s must be %s", err_ini, attr{idx-1}); endif endwhile @@ -443,3 +520,202 @@ %!test %! validateattributes ([0 1 0 1], {"double", "uint8"}, {"binary", "size", [NaN 4], "nonnan"}); + +%!test +%! try validateattributes (ones(1,2,3), {'numeric'}, {'2d'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-2d'); +%! end_try_catch + +%!test +%! try validateattributes (ones(1,2,3,4), {'numeric'}, {'3d'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-3d'); +%! end_try_catch + +%!test +%! try validateattributes ([1 2], {'numeric'}, {'column'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-column'); +%! end_try_catch + +%!test +%! try validateattributes ([1 2].', {'numeric'}, {'row'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-row'); +%! end_try_catch + +%!test +%! try validateattributes ([1 2], {'numeric'}, {'scalar'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-scalar'); +%! end_try_catch + +%!test +%! try validateattributes (ones(3), {'numeric'}, {'vector'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-vector'); +%! end_try_catch + +%!test +%! try validateattributes ([1 2], {'numeric'}, {'size', [1 1]}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:incorrect-size'); +%! end_try_catch + +%!test +%! try validateattributes (1, {'numeric'}, {'numel',7}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:incorrect-numel'); +%! end_try_catch + +%!test +%! try validateattributes (1, {'numeric'}, {'ncols',7}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:incorrect-numcols'); +%! end_try_catch + +%!test +%! try validateattributes (1, {'numeric'}, {'nrows',7}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:incorrect-numrows'); +%! end_try_catch + +%!test +%! try validateattributes (1, {'numeric'}, {'ndims',5}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:incorrect-numdims'); +%! end_try_catch + +%!test +%! try validateattributes ([1 2], {'numeric'}, {'square'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-square'); +%! end_try_catch + +%!test +%! try validateattributes ([1 2], {'numeric'}, {'diag'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-diag'); +%! end_try_catch + +%!test +%! try validateattributes ([], {'numeric'}, {'nonempty'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-nonempty'); +%! end_try_catch + +%!test +%! try validateattributes (speye(2), {'numeric'}, {'nonsparse'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-nonsparse'); +%! end_try_catch + +%!test +%! try validateattributes (1, {'numeric'}, {'>', 3}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-greater'); +%! end_try_catch + +%!test +%! try validateattributes (1, {'numeric'}, {'>=', 3}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-greater-equal'); +%! end_try_catch + +%!test +%! try validateattributes (1, {'numeric'}, {'<', -3}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-less'); +%! end_try_catch + +%!test +%! try validateattributes (1, {'numeric'}, {'<=', -3}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-less-equal'); +%! end_try_catch + +%!test +%! try validateattributes (3, {'numeric'}, {'binary'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-binary'); +%! end_try_catch + +%!test +%! try validateattributes (1, {'numeric'}, {'even'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-even'); +%! end_try_catch + +%!test +%! try validateattributes (2, {'numeric'}, {'odd'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-odd'); +%! end_try_catch + +%!test +%! try validateattributes (1.1, {'numeric'}, {'integer'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-integer'); +%! end_try_catch + +%!test +%! try validateattributes (1+1i*2, {'numeric'}, {'real'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-real'); +%! end_try_catch + +%!test +%! try validateattributes (Inf, {'numeric'}, {'finite'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-finite'); +%! end_try_catch + +%!test +%! try validateattributes (NaN, {'numeric'}, {'nonnan'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-nonnan'); +%! end_try_catch + +%!test +%! try validateattributes (-1, {'numeric'}, {'nonnegative'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-nonnegative'); +%! end_try_catch + +%!test +%! try validateattributes (0, {'numeric'}, {'nonzero'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-nonzero'); +%! end_try_catch + +%!test +%! try validateattributes (-1, {'numeric'}, {'positive'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-positive'); +%! end_try_catch + +%!test +%! try validateattributes ([1 2], {'numeric'}, {'decreasing'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-decreasing'); +%! end_try_catch + +%!test +%! try validateattributes ([2 1], {'numeric'}, {'increasing'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-increasing'); +%! end_try_catch + +%!test +%! try validateattributes ([1 0], {'numeric'}, {'nondecreasing'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-nondecreasing'); +%! end_try_catch + +%!test +%! try validateattributes ([1 2], {'numeric'}, {'nonincreasing'}); +%! catch id, +%! assert (getfield (id, 'identifier'), 'Octave:expected-nonincreasing'); +%! end_try_catch +