Hi, as I mentioned in a previous thread I am interested in helping to
clean up and improve the communications package.  Can you add me to the
project commit group?  My sourceforge name is mtmiller.

I would like to commit the following patch for starters, more to come.

Summary of changes to bi2de, de2bi, oct2dec:
  * fix de2bi matlab compatibility
  * fix oct2dec to error on invalid input instead of returning NaN
  * improve argument error checking and reporting
  * add test suites
  * clean up indenting and style

Thanks!

-- 
mike
comm: clean up bi2de, de2bi, oct2dec indenting, docstrings, and add tests
--- a/main/comm/doc/comms.txi
+++ b/main/comm/doc/comms.txi
@@ -1497,7 +1497,7 @@ example is
 
 @example
 octave:1> a = minpol(gf(14,5));
-octave:2> b = de2bi(a.x,"left-msb");
+octave:2> b = de2bi(a.x,[],"left-msb");
 @end example
 
 converts the polynomial form of the minimum polynomial of 14 in GF(2^5) into
--- a/main/comm/inst/bi2de.m
+++ b/main/comm/inst/bi2de.m
@@ -15,6 +15,7 @@
 
 ## -*- texinfo -*-
 ## @deftypefn {Function File} {@var{d} = } bi2de (@var{b})
+## @deftypefnx {Function File} {@var{d} = } bi2de (@var{b},@var{f})
 ## @deftypefnx {Function File} {@var{d} = } bi2de (@var{b},@var{p})
 ## @deftypefnx {Function File} {@var{d} = } bi2de (@var{b},@var{p},@var{f})
 ##
@@ -29,13 +30,9 @@
 ## The variable @var{f} defines whether the first or last element of @var{b}
 ## is considered to be the most-significant. Valid values of @var{f} are
 ## 'right-msb' or 'left-msb'. By default @var{f} is 'right-msb'.
-## @end deftypefn
+##
 ## @seealso{de2bi}
-
-## 2001-02-02
-##   initial release
-## 2003-02-02
-##   add orientation of b and help in texinfo
+## @end deftypefn
 
 function d = bi2de (b, p, f)
 
@@ -43,31 +40,31 @@ function d = bi2de (b, p, f)
     case 1,
       p = 2;
       f = 'right-msb';
-     case 2,
-      if (ischar(p))
+    case 2,
+      if (ischar (p))
         f = p;
         p = 2;
       else
         f = 'right-msb';
       endif
-     case 3,
-      if (ischar(p))
+    case 3,
+      if (ischar (p))
         tmp = f;
         f = p;
         p = tmp;
       endif
-   otherwise
-      error ("usage: d = bi2de (b, [p])");
+    otherwise
+      print_usage ();
   endswitch
 
-  if ( any (b (:) < 0) || any (b (:) > p - 1) )
-    error ("bi2de: d must only contain value in [0, p-1]");
+  if ( any (b(:) < 0) || any (b(:) != floor (b(:))) || any (b(:) > p - 1) )
+    error ("bi2de: d must only contain integers in the range [0, p-1]");
   endif
 
-  if (strcmp(f,'left-msb'))
+  if (strcmp (f, 'left-msb'))
     b = b(:,size(b,2):-1:1);
-  elseif (!strcmp(f,'right-msb'))
-    error("bi2de: unrecognized flag");
+  elseif (!strcmp (f, 'right-msb'))
+    error ("bi2de: unrecognized flag");
   endif
 
   if (length (b) == 0)
@@ -76,4 +73,24 @@ function d = bi2de (b, p, f)
     d = b * ( p .^ [ 0 : (columns(b)-1) ]' );
   endif
 
-endfunction;
+endfunction
+
+%!shared x
+%! x = randi ([0 1], 100, 16);
+%!assert (bi2de (0), 0)
+%!assert (bi2de (1), 1)
+%!assert (bi2de (ones (1, 8)), 255)
+%!assert (bi2de ([7 7 7 7], 8), 4095)
+%!assert (size (bi2de (x)), [100 1])
+%!assert (bi2de (x, "right-msb"), bi2de (x))
+%!assert (bi2de (x, "left-msb"), bi2de (fliplr (x)))
+
+%% Test input validation
+%!error bi2de ()
+%!error bi2de (1, 2, 3, 4)
+%!error bi2de (1, 2, 3)
+%!error bi2de (1, 2, "invalid")
+%!error bi2de (0.1)
+%!error bi2de (-1)
+%!error bi2de (2)
+%!error bi2de (7, 6)
--- a/main/comm/inst/de2bi.m
+++ b/main/comm/inst/de2bi.m
@@ -26,7 +26,7 @@
 ## of elements of @var{d}. If @var{n} is defined then the returned matrix
 ## will have @var{n} columns. This number of columns can be either larger
 ## than the minimum needed and zeros will be added to the msb of the
-## binary representation or smaller than the minimum in which case the  
+## binary representation or smaller than the minimum in which case the
 ## least-significant part of the element is returned.
 ##
 ## If @var{p} is defined then it is used as the base for the decomposition
@@ -36,15 +36,11 @@
 ## The variable @var{f} defines whether the first or last element of @var{b}
 ## is considered to be the most-significant. Valid values of @var{f} are
 ## 'right-msb' or 'left-msb'. By default @var{f} is 'right-msb'.
-## @end deftypefn
+##
 ## @seealso{bi2de}
+## @end deftypefn
 
-## 2001-02-02
-##   initial release
-## 2003-02-02
-##   add orientation of b and help in texinfo
-
-function b = de2bi(d, n, p, f)
+function b = de2bi (d, n, p, f)
 
   if (nargin == 1)
     p = 2;
@@ -52,51 +48,60 @@ function b = de2bi(d, n, p, f)
     f = 'right-msb';
   elseif (nargin == 2)
     p = 2;
-    if (ischar(n))
-      f = n;
-      n = floor ( log (max (max (d), 1)) ./ log (p) ) + 1;
-    else
-      f = 'right-msb';
-    endif     
+    f = 'right-msb';
   elseif (nargin == 3)
-    if (ischar(n))
-      f = n;
-      n = p
-      p = 2;
-    elseif (ischar(p))
+    if (ischar (p))
       f = p;
       p = 2;
     else
       f = 'right-msb';
-    endif     
-  elseif (nargin == 3)
-    if (ischar(n))
-      tmp = f
-      f = n;
-      n = p
-      p = tmp;
-    elseif (ischar(p))
+    endif
+  elseif (nargin == 4)
+    if (ischar (p))
       tmp = f;
       f = p;
       p = tmp;
-    endif     
+    endif
   else
-    error ("usage: b = de2bi (d [, n [, p]])");
+    print_usage ();
   endif
 
   d = d(:);
   if ( any (d < 0) || any (d != floor (d)) )
-    error ("de2bi: only handles non-negative integers");
+    error ("de2bi: d must only contain non-negative integers");
+  endif
+
+  if (isempty (n))
+    n = floor ( log (max (max (d), 1)) ./ log (p) ) + 1;
   endif
 
   power = ones (length (d), 1) * (p .^ [0 : n-1] );
   d = d * ones (1, n);
   b = floor (rem (d, p*power) ./ power);
 
-  if (strcmp(f,'left-msb'))
+  if (strcmp (f, 'left-msb'))
     b = b(:,columns(b):-1:1);
-  elseif (!strcmp(f,'right-msb'))
-    error("de2bi: unrecognized flag");
+  elseif (!strcmp (f, 'right-msb'))
+    error ("de2bi: unrecognized flag");
   endif
-  
+
 endfunction
+
+%!shared x
+%! x = randi ([0 2^16-1], 100, 1);
+%!assert (de2bi (0), 0)
+%!assert (de2bi (1), 1)
+%!assert (de2bi (255), ones (1, 8))
+%!assert (de2bi (255, [], 256), 255)
+%!assert (de2bi (1023, 8, 8), [7 7 7 1 0 0 0 0])
+%!assert (size (de2bi (x, 16)), [100 16])
+%!assert (de2bi (x, 16, "right-msb"), de2bi (x, 16))
+%!assert (de2bi (x, 16, "left-msb"), fliplr (de2bi (x, 16)))
+
+%% Test input validation
+%!error de2bi ()
+%!error de2bi (1, 2, 3, 4, 5)
+%!error de2bi (1, 2, 3, 4)
+%!error de2bi (1, 2, 3, "invalid")
+%!error de2bi (0.1)
+%!error de2bi (-1)
--- a/main/comm/inst/oct2dec.m
+++ b/main/comm/inst/oct2dec.m
@@ -14,21 +14,48 @@
 ## this program; if not, see <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File}  @var{d} = oct2dec(@var{c})
+## @deftypefn {Function File}  @var{d} = oct2dec (@var{c})
+##
 ## Convert octal to decimal values.
-## @seealso{base2dec,bin2dec,dec2bi}
+##
+## Each element of the octal matrix @var{c} is converted to a decimal value.
+##
+## @seealso{base2dec,bin2dec,dec2bin}
 ## @end deftypefn
 
-function d = oct2dec(c)
-	 if (nargin != 1)
-		usage ("d = oct2dec(c)");
-	endif
-	 if(any(c(:) < 0) || any(c(:) ~= floor(c(:))))
-		error('c must be an octal matrix');
-		exit
-	end
-	l = size(c,2);
-	for k = 1:l
-		str = num2str(c(:,k));
-		d(:,k) = base2dec(str,8);
-	end
+function d = oct2dec (c)
+
+  if (nargin != 1)
+    print_usage ();
+  endif
+
+  # Check for negative or non-integer values
+  if (any (c(:) < 0) || any (c(:) != floor (c(:))))
+    error ("oct2dec: c must be an octal matrix");
+  endif
+
+  d = zeros (size (c));
+  l = size (c, 2);
+  for k = 1:l
+    str = num2str (c(:,k));
+    d(:,k) = base2dec (str, 8);
+    if (any (isnan (d(:,k))))
+      error ("oct2dec: c must be an octal matrix");
+    endif
+  endfor
+
+endfunction
+
+%!shared x,y
+%! x = reshape ([0:79], 10, 8)(1:8,:);
+%! y = reshape ([0:63], 8, 8);
+%!assert (oct2dec (0), 0)
+%!assert (oct2dec (77777777), 2^24 - 1)
+%!assert (oct2dec (x), y)
+
+%% Test input validation
+%!error oct2dec ()
+%!error oct2dec (0, 0)
+%!error <octal matrix> oct2dec (0.1)
+%!error <octal matrix> oct2dec (-1)
+%!error <octal matrix> oct2dec (8)
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Octave-dev mailing list
Octave-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/octave-dev

Reply via email to