Mercurial > octave
changeset 28441:8dd50efa3c47 stable
new nested function handle tests
* test/fcn-handle/shared-ctx.tst, test/fcn-handle/shared_ctx.m: New files.
* test/fcn-handle/module.mk: Update.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 10 Jun 2020 15:41:32 -0400 |
parents | 23fe97205db5 |
children | 5bca1527b034 |
files | test/fcn-handle/module.mk test/fcn-handle/shared-ctx.tst test/fcn-handle/shared_ctx.m |
diffstat | 3 files changed, 39 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/test/fcn-handle/module.mk Wed Jun 10 15:39:30 2020 -0400 +++ b/test/fcn-handle/module.mk Wed Jun 10 15:41:32 2020 -0400 @@ -20,6 +20,8 @@ %reldir%/keyword.tst \ %reldir%/object-method.tst \ %reldir%/package-function.tst \ + %reldir%/shared-ctx.tst \ + %reldir%/shared_ctx.m \ %reldir%/static-method.tst TEST_FILES += $(fcn_handle_TEST_FILES)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/fcn-handle/shared-ctx.tst Wed Jun 10 15:41:32 2020 -0400 @@ -0,0 +1,15 @@ +## Test that multiple handles to nested functions created in the same +## context share that call stack context, but that separately created +## handles have a separate (shared) context. + +%!test +%! [add10, sub10, mul10, div10] = shared_ctx (10); +%! [add100, sub100, mul100, div100] = shared_ctx (100); +%! assert (add10 (2), 12); +%! assert (add100 (20), 120); +%! assert (sub10 (4), 8); +%! assert (sub100 (40), 80); +%! assert (mul10 (5), 40); +%! assert (mul100 (50), 4000); +%! assert (div10 (4), 10); +%! assert (div100 (40), 100);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/fcn-handle/shared_ctx.m Wed Jun 10 15:41:32 2020 -0400 @@ -0,0 +1,22 @@ +function [add, sub, mul, div] = shared_ctx (val) + add = @add_fun; + sub = @sub_fun; + mul = @mul_fun; + div = @div_fun; + function r = add_fun (x) + val += x; + r = val; + endfunction + function r = sub_fun (x) + val -= x; + r = val; + endfunction + function r = mul_fun (x) + val *= x; + r = val; + endfunction + function r = div_fun (x) + val /= x; + r = val; + endfunction +endfunction