# HG changeset patch
# User Ken Marek <marek_ka@mercer.edu>
# Date 1666032831 14400
#      Mon Oct 17 14:53:51 2022 -0400
# Node ID b2f8fb2843d41e26f168ae0d8fa1d70b5c3c2bb5
# Parent  5af91eaa0034d80d6bb4b184da891456fb76323b
Add self test for OutputSel option in ode23, ode23s, ode45

* ode23.m: Add self test for OutputSel
* ode23s.m: same as above
* ode45.m: same as above

diff -r 5af91eaa0034 -r b2f8fb2843d4 scripts/ode/ode23.m
--- a/scripts/ode/ode23.m	Mon Oct 17 06:24:34 2022 -0400
+++ b/scripts/ode/ode23.m	Mon Oct 17 14:53:51 2022 -0400
@@ -363,6 +363,35 @@
 %!    error ("fout: invalid flag <%s>", flag);
 %!  endif
 %!endfunction
+%!function stop_solve = OutputSel_test (t, y, flag, x)
+%!  ## x == 1: select y(1)
+%!  ## x == 2: select y(2)
+%!  ## x == 3: select y([1,2])
+%!  persistent y_last
+%!  if strcmp (flag, "init")
+%!    y_last = y;
+%!    if (x == 1) || (x == 2)
+%!      assert (length (y) == 1);
+%!    elseif x == 3
+%!      assert (length (y) == 2);
+%!    endif
+%!  elseif strcmp (flag, "done")
+%!    y_exp = fref ().';
+%!    if x < 3
+%!      assert (y_last, y_exp(x), 5e-4);
+%!    else
+%!      assert (y_last, y_exp, 5e-4);
+%!    endif
+%!  else # flag == ""
+%!    y_last = y(:,end);
+%!    if (x == 1) || (x == 2)
+%!      assert (length (t) == length (y));
+%!    else
+%!      assert (2 * length (t) == length (y(:)));
+%!    endif
+%!  endif
+%!  stop_solve = 0;
+%!endfunction
 %!
 %!test  # two output arguments
 %! [t, y] = ode23 (@fpol, [0 2], [2 0]);
@@ -440,8 +469,15 @@
 %! opt = odeset ("NonNegative", 2);
 %! sol = ode23 (@fpol, [0 2], [2 0], opt);
 %! assert ([sol.x(end); sol.y(:,end)], [2; 2; 0], 1e-1);
-%!test  # Details of OutputSel can't be tested
-%! opt = odeset ("OutputFcn", @fout, "OutputSel", 1);
+%!test  # OutputSel (see function OutputSel_test for asserts)
+%! opt = odeset ("OutputFcn", @(t, y, flag) OutputSel_test (t, y, flag, 1), ...
+%!               "OutputSel", 1);
+%! sol = ode23 (@fpol, [0 2], [2 0], opt);
+%! opt = odeset ("OutputFcn", @(t, y, flag) OutputSel_test (t, y, flag, 2), ...
+%!               "OutputSel", 2);
+%! sol = ode23 (@fpol, [0 2], [2 0], opt);
+%! opt = odeset ("OutputFcn", @(t, y, flag) OutputSel_test (t, y, flag, 3), ...
+%!               "OutputSel", [1,2]);
 %! sol = ode23 (@fpol, [0 2], [2 0], opt);
 %!test  # Stats must add further elements in sol
 %! opt = odeset ("Stats", "on");
diff -r 5af91eaa0034 -r b2f8fb2843d4 scripts/ode/ode23s.m
--- a/scripts/ode/ode23s.m	Mon Oct 17 06:24:34 2022 -0400
+++ b/scripts/ode/ode23s.m	Mon Oct 17 14:53:51 2022 -0400
@@ -397,6 +397,35 @@
 %!    error ("fout: invalid flag <%s>", flag);
 %!  endif
 %!endfunction
+%!function stop_solve = OutputSel_test (t, y, flag, x)
+%!  ## x == 1: select y(1)
+%!  ## x == 2: select y(2)
+%!  ## x == 3: select y([1,2])
+%!  persistent y_last
+%!  if strcmp (flag, "init")
+%!    y_last = y;
+%!    if (x == 1) || (x == 2)
+%!      assert (length (y) == 1);
+%!    elseif x == 3
+%!      assert (length (y) == 2);
+%!    endif
+%!  elseif strcmp (flag, "done")
+%!    y_exp = fref ().';
+%!    if x < 3
+%!      assert (y_last, y_exp(x), 1e-4);
+%!    else
+%!      assert (y_last, y_exp, 1e-4);
+%!    endif
+%!  else # flag == ""
+%!    y_last = y(:,end);
+%!    if (x == 1) || (x == 2)
+%!      assert (length (t) == length (y));
+%!    else
+%!      assert (2 * length (t) == length (y(:)));
+%!    endif
+%!  endif
+%!  stop_solve = 0;
+%!endfunction
 %!
 %!test  # two output arguments
 %! [t, y] = ode23s (@fpol, [0 2], [2 0]);
@@ -440,8 +469,15 @@
 %! opt = odeset ("RelTol", 1e-8, "NormControl", "on");
 %! sol = ode23s (@fpol, [0 2], [2 0], opt);
 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-4);
-%!test  # Details of OutputSel can't be tested
-%! opt = odeset ("OutputFcn", @fout, "OutputSel", 1);
+%!test  # OutputSel (see function OutputSel_test for asserts)
+%! opt = odeset ("OutputFcn", @(t, y, flag) OutputSel_test (t, y, flag, 1), ...
+%!               "OutputSel", 1);
+%! sol = ode23s (@fpol, [0 2], [2 0], opt);
+%! opt = odeset ("OutputFcn", @(t, y, flag) OutputSel_test (t, y, flag, 2), ...
+%!               "OutputSel", 2);
+%! sol = ode23s (@fpol, [0 2], [2 0], opt);
+%! opt = odeset ("OutputFcn", @(t, y, flag) OutputSel_test (t, y, flag, 3), ...
+%!               "OutputSel", [1,2]);
 %! sol = ode23s (@fpol, [0 2], [2 0], opt);
 %!test  # Stats must add further elements in sol
 %! opt = odeset ("Stats", "on");
diff -r 5af91eaa0034 -r b2f8fb2843d4 scripts/ode/ode45.m
--- a/scripts/ode/ode45.m	Mon Oct 17 06:24:34 2022 -0400
+++ b/scripts/ode/ode45.m	Mon Oct 17 14:53:51 2022 -0400
@@ -362,6 +362,35 @@
 %!    error ("fout: invalid flag <%s>", flag);
 %!  endif
 %!endfunction
+%!function stop_solve = OutputSel_test (t, y, flag, x)
+%!  ## x == 1: select y(1)
+%!  ## x == 2: select y(2)
+%!  ## x == 3: select y([1,2])
+%!  persistent y_last
+%!  if strcmp (flag, "init")
+%!    y_last = y;
+%!    if (x == 1) || (x == 2)
+%!      assert (length (y) == 1);
+%!    elseif x == 3
+%!      assert (length (y) == 2);
+%!    endif
+%!  elseif strcmp (flag, "done")
+%!    y_exp = fref ().';
+%!    if x < 3
+%!      assert (y_last, y_exp(x), 1e-4);
+%!    else
+%!      assert (y_last, y_exp, 1e-4);
+%!    endif
+%!  else # flag == ""
+%!    y_last = y(:,end);
+%!    if (x == 1) || (x == 2)
+%!      assert (length (t) == length (y));
+%!    else
+%!      assert (2 * length (t) == length (y(:)));
+%!    endif
+%!  endif
+%!  stop_solve = 0;
+%!endfunction
 %!
 %!test  # two output arguments
 %! [t, y] = ode45 (@fpol, [0 2], [2 0]);
@@ -451,8 +480,15 @@
 %! opt = odeset ("NonNegative", 2);
 %! sol = ode45 (@fpol, [0 2], [2 0], opt);
 %! assert ([sol.x(end); sol.y(:,end)], [2; 2; 0], 0.5);
-%!test  # Details of OutputSel can't be tested
-%! opt = odeset ("OutputFcn", @fout, "OutputSel", 1);
+%!test  # OutputSel (see function OutputSel_test for asserts)
+%! opt = odeset ("OutputFcn", @(t, y, flag) OutputSel_test (t, y, flag, 1), ...
+%!               "OutputSel", 1);
+%! sol = ode45 (@fpol, [0 2], [2 0], opt);
+%! opt = odeset ("OutputFcn", @(t, y, flag) OutputSel_test (t, y, flag, 2), ...
+%!               "OutputSel", 2);
+%! sol = ode45 (@fpol, [0 2], [2 0], opt);
+%! opt = odeset ("OutputFcn", @(t, y, flag) OutputSel_test (t, y, flag, 3), ...
+%!               "OutputSel", [1,2]);
 %! sol = ode45 (@fpol, [0 2], [2 0], opt);
 %!test  # Stats must add further elements in sol
 %! opt = odeset ("Stats", "on");
