function spline_test145 ( )

%*****************************************************************************80
%
%% spline_test145() tests spline_constant_val().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    21 November 2021
%
%  Author
%
%    John Burkardt
%
  ndata = 12;
  n_test = 20;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'spline_test145():\n' );
  fprintf ( 1, '  SPLINE_CONSTANT_VAL evaluates a piecewise \n' );
  fprintf ( 1, '  constant spline.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Runge''s function, evenly spaced knots.\n' );
%
%  Set the data.
%
  tlo = -1.0;
  thi = +1.0;

  tdata = linspace ( tlo, thi, ndata - 1 );

  for i = 1 : ndata

      if ( i == 1 )
      tval = tdata(1);
    elseif ( i < ndata )
      tval = 0.5 * ( tdata(i-1) + tdata(i) );
    elseif ( i == ndata )
      tval = tdata(i-1);
    end

    ydata(i) = frunge ( tval );

  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '  The data to be interpolated:\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Number of data values = %d\n', ndata );
  fprintf ( 1, '\n' );
  fprintf ( 1, '       T             Y\n' );
  fprintf ( 1, '\n' );

  for i = 1 : ndata
    fprintf ( 1, '%c                  %14f\n', '*', ydata(i) );
    if ( i < ndata )
      fprintf ( 1, '%c  %14f\n', '*', tdata(i) );
    end
  end
%
%  Sample the spline.
%
  alo = tlo - 1.0;
  ahi = thi + 1.0;

  t_test = alo + ( ahi - alo ) * rand ( n_test, 1 );

  t_test = r8vec_sort_bubble_a ( n_test, t_test );

  fprintf ( 1, '\n' );
  fprintf ( 1, '     T     Y(interp)    Y(exact)\n' );
  fprintf ( 1, '\n' );

  j = 0;
  fprintf ( 1, '%c                  %14f\n', '*', ydata(j+1) );
  j = j + 1;

  for i = 1 : n_test

    tval = t_test(i);

    yval = spline_constant_val ( ndata, tdata, ydata, tval );

    if ( j <= ndata - 1 )
      while ( tdata(j) <= tval )
        fval = frunge ( tdata(j) );
        fprintf ( 1, '%c  %14f                  %14f\n', '*', tdata(j), fval );
        fprintf ( 1, '%c                  %14f\n', '*', ydata(j+1) );
        j = j + 1;
        if ( ndata <= j )
          break;
        end
      end
    end

    fval = frunge ( tval );

    fprintf ( 1, '%c  %14f  %14f  %14f\n', ' ', tval, yval, fval );

  end

  return
end
 
