code: def test_maxiter_callback(self):
      # Test behavior of `maxiter` parameter and `callback` interface
      loc = 0.612814
      bracket = (-5, 0, 5)
      maxiter = 5
  
      res = _chandrupatla_minimize(
          self.f, *bracket, args=(loc,), maxiter=maxiter
      )
      assert not np.any(res.success)
      assert np.all(res.nfev == maxiter + 3)
      assert np.all(res.nit == maxiter)
  
      def callback(res):
          callback.iter += 1
          callback.res = res
          assert hasattr(res, "x")
          if callback.iter == 0:
              # callback is called once with initial bracket
              assert (res.xl, res.xm, res.xr) == bracket
          else:
              changed_xr = (res.xl == callback.xl) & (
                  res.xr != callback.xr
              )
              changed_xl = (res.xl != callback.xl) & (
                  res.xr == callback.xr
              )
              assert np.all(changed_xr | changed_xl)
  
          callback.xl = res.xl
          callback.xr = res.xr
          assert res.status == _chandrupatla._EINPROGRESS
          assert_equal(self.f(res.xl, loc), res.fl)
          assert_equal(self.f(res.xm, loc), res.fm)
          assert_equal(self.f(res.xr, loc), res.fr)
          assert_equal(self.f(res.x, loc), res.fun)
          if callback.iter == maxiter:
              raise StopIteration
  
      callback.xl = np.nan
      callback.xr = np.nan
      callback.iter = -1  # callback called once before first iteration
      callback.res = None
  
      res2 = _chandrupatla_minimize(
          self.f, *bracket, args=(loc,), callback=callback
      )
  
      # terminating with callback is identical to terminating due to maxiter
      # (except for `status`)
      for key in res.keys():
          if key == "status":
              assert res[key] == _chandrupatla._ECONVERR
              assert callback.res[key] == _chandrupatla._EINPROGRESS
              assert res2[key] == _chandrupatla._ECALLBACK
          else:
              assert res2[key] == callback.res[key] == res[key]
  
imports: import pytest
  import numpy
  from numpy.testing import assert_allclose, assert_equal, assert_array_less
  from scipy import stats
  import scipy.optimize._chandrupatla
  from scipy.optimize._chandrupatla import _chandrupatla_minimize
  from itertools import permutations
label_desc: Use image node_id 6 for calling the TestChandrupatlaMinimize obj's underlying member method code with example usage: obj.test_maxiter_callback() without return types