changeset 31546:c664627d601e

tsearchn.m: Use Octave coding conventions. * tsearchn.m: Use function name in error() messages. Use "endif" rather than bare "end". Capitalize function input parameters in error() messages to match documentation. Cuddle parentheses to variable when performing indexing. Use '!' for logical not operator. Use two newlines between end of function and start of BIST tests. Add BIST tests for input validation.
author Rik <rik@octave.org>
date Fri, 25 Nov 2022 09:41:12 -0800
parents 85a073f7c5f9
children 212cc32100f5
files scripts/geometry/tsearchn.m
diffstat 1 files changed, 15 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/geometry/tsearchn.m	Fri Nov 25 11:33:20 2022 -0500
+++ b/scripts/geometry/tsearchn.m	Fri Nov 25 09:41:12 2022 -0800
@@ -46,16 +46,16 @@
   endif
 
   if (columns (x) != columns (xi))
-    error ("columns (x) should equal columns (xi)")
-  end
+    error ("tsearchn: number of columns of X and XI must match");
+  endif
 
   if (max (t(:)) > rows (x))
-    error ("triangles should only access points in x")
-  end
+    error ("tsearchn: triangulation T must not access points outside X");
+  endif
 
   if (nargout <= 1 && columns (x) == 2)  # pass to the faster tsearch.cc
     idx = tsearch (x(:,1), x(:,2), t, xi(:,1), xi(:,2));
-    return
+    return;
   endif
 
   nt = rows (t);
@@ -88,13 +88,14 @@
     ## (all (b >= 0) && all (b <= 1)).  As sum (b,2) == 1, we only need to
     ## test all(b>=0).
     inside = all (b >= -1e-12, 2);  # -1e-12 instead of 0 for rounding errors
-    idx (ni (inside)) = i;
+    idx(ni(inside)) = i;
     p(ni(inside), :) = b(inside, :);
-    ni = ni (~inside);
+    ni = ni(! inside);
   endfor
 
 endfunction
 
+
 %!shared x, tri
 %! x = [-1,-1;-1,1;1,-1];
 %! tri = [1, 2, 3];
@@ -118,3 +119,10 @@
 %! [idx, p] = tsearchn (x,tri,[1,1]);
 %! assert (idx, NaN);
 %! assert (p, [NaN, NaN, NaN]);
+
+## Test input validation
+%!error <Invalid call> tsearchn ()
+%!error <Invalid call> tsearchn (1)
+%!error <Invalid call> tsearchn (1, 2)
+%!error <number of columns of X and XI must match> tsearchn ([1,2], 3, 4)
+%!error <T must not access points outside X> tsearchn (1, 2, 3)