diff scripts/miscellaneous/mustBeInteger.m @ 28723:dd9efd873596 stable

maint: Use Octave coding conventions in mustBe* functions. * mustBeFinite.m, mustBeGreaterThan.m, mustBeGreaterThanOrEqual.m, mustBeInteger.m, mustBeLessThan.m, mustBeLessThanOrEqual.m, mustBeMember.m, mustBeNegative.m, mustBeNonNan.m, mustBeNonempty.m, mustBeNonnegative.m, mustBeNonpositive.m, mustBeNonsparse.m, mustBeNonzero.m, mustBeNumeric.m, mustBeNumericOrLogical.m, mustBePositive.m, mustBeReal.m: Use parentheses around conditional in if statement. Use space between not operator '!' and argument. Use two newlines after "endfunction" before starting BIST tests. Use semicolons in statements within %!test blocks. Don't use semicolons in %!error tests. Add expected string for %!error BIST tests. Add input validation for number of inputs and BIST tests to check validation.
author Rik <rik@octave.org>
date Fri, 11 Sep 2020 20:01:56 -0700
parents a2177e663979
children de5f2f9a64ff 0a5b15007766
line wrap: on
line diff
--- a/scripts/miscellaneous/mustBeInteger.m	Fri Sep 11 15:39:17 2020 -0700
+++ b/scripts/miscellaneous/mustBeInteger.m	Fri Sep 11 20:01:56 2020 -0700
@@ -36,36 +36,47 @@
 ## @end deftypefn
 
 function mustBeInteger (x)
-  if isinteger (x) || islogical (x)
-    return
+
+  if (nargin != 1)
+    print_usage ();
   endif
+
+  if (isinteger (x) || islogical (x))
+    return;
+  endif
+
   but = [];
-  if ! isnumeric (x)
-    but = sprintf ("it was non-numeric (got a %s)", class (x));
-  elseif any (! isfinite (x))
-    but = "there were Inf values";
-  elseif ! isreal (x)
+  if (! isnumeric (x))
+    but = sprintf ("it was non-numeric (found a %s)", class (x));
+  elseif (! isreal (x))
     but = "it was complex";
-  elseif ! all (floor (x) == x)
+  elseif (any (! isfinite (x)))
+    but = "there were non-finite values";
+  elseif (any (x != fix (x)))
     but = "it had fractional values in some elements";
   end
-  if ! isempty (but)
+
+  if (! isempty (but))
     label = inputname (1);
-    if isempty (label)
+    if (isempty (label))
       label = "input";
     endif
     error ("%s must be integer-valued; but %s", label, but);
   endif
+
 endfunction
 
+
 %!test
-%! mustBeInteger ([])
-%! mustBeInteger (42)
-%! mustBeInteger (1:1000)
-%! mustBeInteger (int32(42))
+%! mustBeInteger ([]);
+%! mustBeInteger (42);
+%! mustBeInteger (1:1000);
+%! mustBeInteger (int32 (42));
+%! mustBeInteger (true);
 
-%!error mustBeInteger ()
-%!error mustBeInteger (1.23)
-%!error mustBeInteger ([1 2 3 4.4])
-%!error mustBeInteger (Inf)
-%!error mustBeInteger (NaN)
+%!error <Invalid call> mustBeInteger ()
+%!error <it was non-numeric> mustBeInteger ("Hello World")
+%!error <it was complex> mustBeInteger ([1, 2i])
+%!error <there were non-finite values> mustBeInteger (Inf)
+%!error <there were non-finite values> mustBeInteger (NaN)
+%!error <it had fractional values> mustBeInteger ([1 2 3 4.4])