Mercurial > hg > octave-terminal
changeset 13305:63463570d9fe
Add %!endfunction block keyword to test.m
test.m: Accept, but don't process, %!endfunction block keyword
* structfun.m, fminunc.m, fsolve.m, sqp.m, unwrap.m, test.m: Close %!function
declarations with %!endfunction. Until Bug #34497 (unable to clear command line
functions from user workspace) is fixed, use Octave reserved namespace for test
functions.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 09 Oct 2011 07:25:07 -0700 |
parents | e1524d82f8e0 |
children | ad9b78544a22 |
files | scripts/general/structfun.m scripts/optimization/fminunc.m scripts/optimization/fsolve.m scripts/optimization/sqp.m scripts/signal/unwrap.m scripts/testfun/test.m |
diffstat | 6 files changed, 48 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/general/structfun.m +++ b/scripts/general/structfun.m @@ -120,9 +120,10 @@ %! "UniformOutput", false); %! assert (o, l); -%!function [a, b] = twoouts (x) +%!function [a, b] = __twoouts (x) %! a = x + x; %! b = x * x; +%!endfunction %!test %! s = struct ("a", {1, 2, 3}, "b", {4, 5, 6}); @@ -132,7 +133,7 @@ %! d(1:2, 1, 1) = [1; 16]; %! d(1:2, 1, 2) = [4; 25]; %! d(1:2, 1, 3) = [9; 36]; -%! [aa, bb] = structfun(@twoouts, s); +%! [aa, bb] = structfun(@__twoouts, s); %! assert(aa, c); %! assert(bb, d); @@ -140,6 +141,6 @@ %! s = struct ("a", {1, 2, 3}, "b", {4, 5, 6}); %! c = struct ("a", {2, 4, 6}, "b", {8, 10, 12}); %! d = struct ("a", {1, 4, 9}, "b", {16, 25, 36}); -%! [aa, bb] = structfun(@twoouts, s, "UniformOutput", false); +%! [aa, bb] = structfun(@__twoouts, s, "UniformOutput", false); %! assert(aa, c); %! assert(bb, d);
--- a/scripts/optimization/fminunc.m +++ b/scripts/optimization/fminunc.m @@ -359,17 +359,18 @@ endif endfunction -%!function f = rosenb (x) +%!function f = __rosenb (x) %! n = length (x); %! f = sumsq (1 - x(1:n-1)) + 100 * sumsq (x(2:n) - x(1:n-1).^2); +%!endfunction %!test -%! [x, fval, info, out] = fminunc (@rosenb, [5, -5]); +%! [x, fval, info, out] = fminunc (@__rosenb, [5, -5]); %! tol = 2e-5; %! assert (info > 0); %! assert (x, ones (1, 2), tol); %! assert (fval, 0, tol); %!test -%! [x, fval, info, out] = fminunc (@rosenb, zeros (1, 4)); +%! [x, fval, info, out] = fminunc (@__rosenb, zeros (1, 4)); %! tol = 2e-5; %! assert (info > 0); %! assert (x, ones (1, 4), tol);
--- a/scripts/optimization/fsolve.m +++ b/scripts/optimization/fsolve.m @@ -460,7 +460,7 @@ endif endfunction -%!function retval = f (p) +%!function retval = __f (p) %! x = p(1); %! y = p(2); %! z = p(3); @@ -468,17 +468,18 @@ %! retval(1) = sin(x) + y**2 + log(z) - 7; %! retval(2) = 3*x + 2**y -z**3 + 1; %! retval(3) = x + y + z - 5; +%!endfunction %!test %! x_opt = [ 0.599054; %! 2.395931; %! 2.005014 ]; %! tol = 1.0e-5; -%! [x, fval, info] = fsolve (@f, [ 0.5; 2.0; 2.5 ]); +%! [x, fval, info] = fsolve (@__f, [ 0.5; 2.0; 2.5 ]); %! assert (info > 0); %! assert (norm (x - x_opt, Inf) < tol); %! assert (norm (fval) < tol); -%!function retval = f (p) +%!function retval = __f (p) %! x = p(1); %! y = p(2); %! z = p(3); @@ -488,15 +489,16 @@ %! retval(2) = 6*x - 4*y + exp (3*z + w) - 11; %! retval(3) = x^4 - 4*y^2 + 6*z - 8*w - 20; %! retval(4) = x^2 + 2*y^3 + z - w - 4; +%!endfunction %!test %! x_opt = [ -0.767297326653401, 0.590671081117440, 1.47190018629642, -1.52719341133957 ]; %! tol = 1.0e-5; -%! [x, fval, info] = fsolve (@f, [-1, 1, 2, -1]); +%! [x, fval, info] = fsolve (@__f, [-1, 1, 2, -1]); %! assert (info > 0); %! assert (norm (x - x_opt, Inf) < tol); %! assert (norm (fval) < tol); -%!function retval = f (p) +%!function retval = __f (p) %! x = p(1); %! y = p(2); %! z = p(3); @@ -505,17 +507,18 @@ %! retval(2) = 3*x + 2**y -z**3 + 1; %! retval(3) = x + y + z - 5; %! retval(4) = x*x + y - z*log(z) - 1.36; +%!endfunction %!test %! x_opt = [ 0.599054; %! 2.395931; %! 2.005014 ]; %! tol = 1.0e-5; -%! [x, fval, info] = fsolve (@f, [ 0.5; 2.0; 2.5 ]); +%! [x, fval, info] = fsolve (@__f, [ 0.5; 2.0; 2.5 ]); %! assert (info > 0); %! assert (norm (x - x_opt, Inf) < tol); %! assert (norm (fval) < tol); -%!function retval = f (p) +%!function retval = __f (p) %! x = p(1); %! y = p(2); %! z = p(3); @@ -523,13 +526,14 @@ %! retval(1) = sin(x) + y**2 + log(z) - 7; %! retval(2) = 3*x + 2**y -z**3 + 1; %! retval(3) = x + y + z - 5; +%!endfunction %!test %! x_opt = [ 0.599054; %! 2.395931; %! 2.005014 ]; %! tol = 1.0e-5; %! opt = optimset ("Updating", "qrp"); -%! [x, fval, info] = fsolve (@f, [ 0.5; 2.0; 2.5 ], opt); +%! [x, fval, info] = fsolve (@__f, [ 0.5; 2.0; 2.5 ], opt); %! assert (info > 0); %! assert (norm (x - x_opt, Inf) < tol); %! assert (norm (fval) < tol); @@ -553,6 +557,7 @@ %! y(1) = (1+i)*x(1)^2 - (1-i)*x(2) - 2; %! y(2) = sqrt (x(1)*x(2)) - (1-2i)*x(3) + (3-4i); %! y(3) = x(1) * x(2) - x(3)^2 + (3+2i); +%!endfunction %!test %! x_opt = [-1+i, 1-i, 2+i];
--- a/scripts/optimization/sqp.m +++ b/scripts/optimization/sqp.m @@ -736,19 +736,21 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Test Code -%!function r = g (x) +%!function r = __g (x) %! r = [sumsq(x)-10; %! x(2)*x(3)-5*x(4)*x(5); %! x(1)^3+x(2)^3+1 ]; +%!endfunction %! -%!function obj = phi (x) +%!function obj = __phi (x) %! obj = exp(prod(x)) - 0.5*(x(1)^3+x(2)^3+1)^2; +%!endfunction %! %!test %! %! x0 = [-1.8; 1.7; 1.9; -0.8; -0.8]; %! -%! [x, obj, info, iter, nf, lambda] = sqp (x0, @phi, @g, []); +%! [x, obj, info, iter, nf, lambda] = sqp (x0, @__phi, @__g, []); %! %! x_opt = [-1.717143501952599; %! 1.595709610928535;
--- a/scripts/signal/unwrap.m +++ b/scripts/signal/unwrap.m @@ -89,7 +89,7 @@ endfunction -%!function t = xassert(a,b,tol) +%!function t = __xassert(a,b,tol) %! if (nargin == 1) %! t = all(a(:)); %! else @@ -104,6 +104,7 @@ %! t = 1; %! endif %! endif +%!endfunction %! %!test %! @@ -114,18 +115,18 @@ %! w = r - 2*pi*floor((r+pi)/(2*pi)); # wrapped into [-pi,pi] %! tol = 1e3*eps; # maximum expected deviation %! -%! t(++i) = xassert(r, unwrap(w), tol); #unwrap single row -%! t(++i) = xassert(r', unwrap(w'), tol); #unwrap single column -%! t(++i) = xassert([r',r'], unwrap([w',w']), tol); #unwrap 2 columns -%! t(++i) = xassert([r;r], unwrap([w;w],[],2), tol); #verify that dim works -%! t(++i) = xassert(r+10, unwrap(10+w), tol); #verify that r(1)>pi works +%! t(++i) = __xassert(r, unwrap(w), tol); #unwrap single row +%! t(++i) = __xassert(r', unwrap(w'), tol); #unwrap single column +%! t(++i) = __xassert([r',r'], unwrap([w',w']), tol); #unwrap 2 columns +%! t(++i) = __xassert([r;r], unwrap([w;w],[],2), tol); #check that dim works +%! t(++i) = __xassert(r+10, unwrap(10+w), tol); #check r(1)>pi works %! -%! t(++i) = xassert(w', unwrap(w',[],2)); #unwrap col by rows should not change it -%! t(++i) = xassert(w, unwrap(w,[],1)); #unwrap row by cols should not change it -%! t(++i) = xassert([w;w], unwrap([w;w])); #unwrap 2 rows by cols should not change them +%! t(++i) = __xassert(w', unwrap(w',[],2)); #unwrap col by rows should not change it +%! t(++i) = __xassert(w, unwrap(w,[],1)); #unwrap row by cols should not change it +%! t(++i) = __xassert([w;w], unwrap([w;w])); #unwrap 2 rows by cols should not change them %! %! ## verify that setting tolerance too low will cause bad results. -%! t(++i) = xassert(any(abs(r - unwrap(w,0.8)) > 100)); +%! t(++i) = __xassert(any(abs(r - unwrap(w,0.8)) > 100)); %! %! assert(all(t)); %!
--- a/scripts/testfun/test.m +++ b/scripts/testfun/test.m @@ -364,6 +364,14 @@ endif __code = ""; +### ENDFUNCTION + + elseif (strcmp (__type, "endfunction")) + ## endfunction simply declares the end of a previous function block. + ## There is no processing to be done here, just skip to next block. + __istest = 0; + __code = ""; + ### ASSERT/FAIL elseif (strcmp (__type, "assert") || strcmp (__type, "fail")) @@ -761,16 +769,19 @@ %!function x = __test_a(y) %! x = 2*y; +%!endfunction %!assert(__test_a(2),4); # Test a test function %!function __test_a (y) %! x = 2*y; +%!endfunction %!test %! __test_a(2); # Test a test function with no return value %!function [x,z] = __test_a (y) %! x = 2*y; %! z = 3*y; +%!endfunction %!test # Test a test function with multiple returns %! [x,z] = __test_a(3); %! assert(x,6);