changeset 25252:75eea4768e24 stable

Add BIST tests for colloc input validation (bug #53653) * colloc.cc: Add '#include <algorithm>" to list of includes. * colloc.cc (Fcolloc): Use std::transform to make optional string argument lower case. Change input parsing to directly look for strings "r","right", "l", "left". Change error message about incorrect number of roots to be clearer. Add BIST tests for input validation.
author Rik <rik@octave.org>
date Sun, 15 Apr 2018 09:42:00 -0700
parents 0bc58956aa40
children be6b35bf21bd
files libinterp/corefcn/colloc.cc
diffstat 1 files changed, 16 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/colloc.cc	Sat Apr 14 22:33:01 2018 -0700
+++ b/libinterp/corefcn/colloc.cc	Sun Apr 15 09:42:00 2018 -0700
@@ -24,6 +24,7 @@
 #  include "config.h"
 #endif
 
+#include <algorithm>
 #include <string>
 
 #include "CollocWt.h"
@@ -67,10 +68,11 @@
     {
       std::string s = args(i).xstring_value ("colloc: optional arguments must be strings");
 
-      if ((s.length () == 1 && (s[0] == 'R' || s[0] == 'r')) || s == "right")
+      std::transform (s.begin (), s.end (), s.begin (), ::tolower);
+
+      if (s == "r" || s == "right")
         right = 1;
-      else if ((s.length () == 1 && (s[0] == 'L' || s[0] == 'l'))
-               || s == "left")
+      else if (s == "l" || s == "left")
         left = 1;
       else
         error (R"(colloc: string argument must be "left" or "right")");
@@ -78,7 +80,7 @@
 
   ntot += left + right;
   if (ntot < 1)
-    error ("colloc: the total number of roots must be positive");
+    error (R"("colloc: the total number of roots (N + "left" + "right") must be >= 1)");
 
   CollocWt wts (ncol, left, right);
 
@@ -97,4 +99,14 @@
 %!assert (colloc (1, "right"), [0.5; 1])
 %!assert (colloc (1, "left", "right"), [0; 0.5; 1])
 
+## Test input validation
+%!error colloc ()
+%!error colloc (1,2,3,4)
+%!error <N must be a scalar> colloc (ones (2,2))
+%!error <N cannot be NaN> colloc (NaN)
+%!error <N must be positive> colloc (-1)
+%!error <optional arguments must be strings> colloc (1, 1)
+%!error <string argument must be "left" or "right"> colloc (1, "foobar")
+%!error <total number of roots .* must be .= 1> colloc (0)
+
 */