# HG changeset patch # User John W. Eaton # Date 1591818092 14400 # Node ID 8dd50efa3c47f48cd0e7aa428c29e9e77c9c7908 # Parent 23fe97205db5560c1755e49c934932b89f62b931 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. diff -r 23fe97205db5 -r 8dd50efa3c47 test/fcn-handle/module.mk --- 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) diff -r 23fe97205db5 -r 8dd50efa3c47 test/fcn-handle/shared-ctx.tst --- /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); diff -r 23fe97205db5 -r 8dd50efa3c47 test/fcn-handle/shared_ctx.m --- /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