changeset 13065:1ca3cde15b18

codepsprint: tests for testfun functions * demo.m, rundemos.m: Trivial tests. * speed.m: New tests.
author John W. Eaton <jwe@octave.org>
date Sat, 03 Sep 2011 14:57:12 -0400
parents bae887ebea48
children 756af8385e26
files scripts/testfun/demo.m scripts/testfun/rundemos.m scripts/testfun/speed.m
diffstat 3 files changed, 60 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/testfun/demo.m
+++ b/scripts/testfun/demo.m
@@ -141,3 +141,6 @@
 %! plot (t,x)
 %! %-------------------------------------------------
 %! % the figure window shows one cycle of a sine wave
+
+%!error demo ();
+%!error demo (1, 2, 3);
--- a/scripts/testfun/rundemos.m
+++ b/scripts/testfun/rundemos.m
@@ -81,3 +81,5 @@
     retval = findstr (str, "%!demo");
   endif
 endfunction
+
+%!error rundemos ("foo", 1);
--- a/scripts/testfun/speed.m
+++ b/scripts/testfun/speed.m
@@ -25,7 +25,8 @@
 ## each @var{n}, an initialization expression (@var{init}) is computed to
 ## create any data needed for the test.  If a second expression (@var{f2}) is
 ## given then the execution times of the two expressions are compared.  When
-## called without output arguments the results are displayed graphically.
+## called without output arguments the results are printed to stdout and
+## displayed graphically.
 ##
 ## @table @code
 ## @item @var{f}
@@ -115,7 +116,7 @@
 ##
 ## @example
 ## @group
-## speed ("v = sum (x)", "", [10000, 100000], ...
+## speed ("sum (x)", "", [10000, 100000], ...
 ##        "v = 0; for i = 1:length (x), v += x(i); end")
 ## @end group
 ## @end example
@@ -127,10 +128,10 @@
 ##
 ## @example
 ## @group
-## speed ("v = xcorr (x, n)", "x = rand (128, 1);", 100,
-##        "v2 = xcorr_orig (x, n)", -100*eps)
-## speed ("v = xcorr (x, 15)", "x = rand (20+n, 1);", 100,
-##        "v2 = xcorr_orig (x, n)", -100*eps)
+## speed ("xcorr (x, n)", "x = rand (128, 1);", 100,
+##        "xcorr_orig (x, n)", -100*eps)
+## speed ("xcorr (x, 15)", "x = rand (20+n, 1);", 100,
+##        "xcorr_orig (x, n)", -100*eps)
 ## @end group
 ## @end example
 ##
@@ -191,7 +192,12 @@
 
   __torig = __tnew = zeros (size (__test_n));
 
-  disp (cstrcat ("testing ", __f1, "\ninit: ", __init));
+  ## Print and plot the data if no output is requested.
+  do_display = (nargout == 0);
+
+  if (do_display)
+    disp (cstrcat ("testing ", __f1, "\ninit: ", __init));
+  endif
 
   ## Make sure the functions are freshly loaded by evaluating them at
   ## test_n(1); first have to initialize the args though.
@@ -208,8 +214,10 @@
     n = __test_n(k);
     eval (cstrcat (__init, ";"));
 
-    printf ("n%i = %i  ",k, n);
-    fflush (stdout);
+    if (do_display)
+      printf ("n%i = %i  ",k, n);
+      fflush (stdout);
+    endif
     eval (cstrcat ("__t = time();", __f1, "; __v1=ans; __t = time()-__t;"));
     if (__t < 0.25)
       eval (cstrcat ("__t2 = time();", __f1, "; __t2 = time()-__t2;"));
@@ -251,14 +259,12 @@
     __order.a = exp (p(2));
   endif
 
-  ## Plot the data if no output is requested.
-  doplot = (nargout == 0);
-
-  if (doplot)
+  if (do_display)
     figure;
   endif
 
-  if (doplot && ! isempty (__f2))
+  if (do_display && ! isempty (__f2))
+
     subplot (1, 2, 1);
     semilogx (__test_n, __torig./__tnew,
               cstrcat ("-*r;", strrep (__f1, ";", "."), "/",
@@ -284,7 +290,7 @@
     printf ("\n\nMean runtime ratio = %.3g for '%s' vs '%s'\n",
             ratio, __f2, __f1);
 
-  elseif (doplot)
+  elseif (do_display)
 
     loglog (__test_n, __tnew*1000, "*-g;execution time;");
     xlabel ("test length");
@@ -293,7 +299,7 @@
 
   endif
 
-  if (doplot)
+  if (do_display)
 
     ## Plot time complexity approximation (using milliseconds).
     order = sprintf ("O(n^%g)", round (10*p(1))/10);
@@ -314,10 +320,12 @@
       time = sprintf ("%g ns", dt*1e9);
     endif
 
-    ## Display nicely formatted complexity.
-    printf ("\nFor %s:\n", __f1);
-    printf ("  asymptotic power: %s\n", order);
-    printf ("  approximate time per operation: %s\n", time);
+    if (do_display)
+      ## Display nicely formatted complexity.
+      printf ("\nFor %s:\n", __f1);
+      printf ("  asymptotic power: %s\n", order);
+      printf ("  approximate time per operation: %s\n", time);
+    endif
 
   endif
 
@@ -378,3 +386,30 @@
 %!  disp("This time, the for loop is done away with entirely.");
 %!  disp("Notice how much bigger the speedup is than in example 1.");
 %! endif
+
+%!error speed ();
+%!error speed (1, 2, 3, 4, 5, 6, 7);
+
+%!test
+%! [order, n, T_f1, T_f2] = speed ("airy (x)", "x = rand (n, 10)", [100, 1000]);
+%! assert (isstruct (order));
+%! assert (size (order), [1, 1]);
+%! assert (fieldnames (order), {"p"; "a"});
+%! assert (isnumeric (n));
+%! assert (size (n), [1, 15]);
+%! assert (isnumeric (T_f1));
+%! assert (size (T_f1), [1, 15]);
+%! assert (isnumeric (T_f1));
+%! assert (size (T_f2), [1, 15]);
+
+%!test
+%! [order, n, T_f1, T_f2] = speed ("sum (x)", "", [100, 1000], "v = 0; for i = 1:length (x), v += x(i); end");
+%! assert (isstruct (order));
+%! assert (size (order), [1, 1]);
+%! assert (fieldnames (order), {"p"; "a"});
+%! assert (isnumeric (n));
+%! assert (size (n), [1, 15]);
+%! assert (isnumeric (T_f1));
+%! assert (size (T_f1), [1, 15]);
+%! assert (isnumeric (T_f1));
+%! assert (size (T_f2), [1, 15]);