code: def test_shared_range():
      for ft in sctypes["float"]:
          for it in sctypes["int"] + sctypes["uint"]:
              # Test that going a bit above or below the calculated min and max
              # either generates the same number when cast, or the max int value
              # (if this system generates that) or something smaller (because of
              # overflow)
              mn, mx = shared_range(ft, it)
              with suppress_warnings():
                  ovs = ft(mx) + np.arange(2048, dtype=ft)
              # Float16 can overflow to inf
              bit_bigger = ovs[np.isfinite(ovs)].astype(it)
              casted_mx = ft(mx).astype(it)
              imax = int(np.iinfo(it).max)
              thresh_overflow = False
              if casted_mx != imax:
                  # The shared_range have told us that they believe the imax does
                  # not have an exact representation.
                  fimax = ft(imax)
                  if np.isfinite(fimax):
                      assert int(fimax) != imax
                  # Therefore the imax, cast back to float, and to integer, will
                  # overflow. If it overflows to the imax, we need to allow for
                  # that possibility in the testing of our overflowed values
                  imax_roundtrip = fimax.astype(it)
                  if imax_roundtrip == imax:
                      thresh_overflow = True
              if thresh_overflow:
                  assert np.all(
                      (bit_bigger == casted_mx) | (bit_bigger == imax)
                  )
              else:
                  assert np.all(bit_bigger <= casted_mx)
              if it in sctypes["uint"]:
                  assert mn == 0
                  continue
              # And something larger for the minimum
              with suppress_warnings():  # overflow
                  ovs = ft(mn) - np.arange(2048, dtype=ft)
              # Float16 can overflow to inf
              bit_smaller = ovs[np.isfinite(ovs)].astype(it)
              casted_mn = ft(mn).astype(it)
              imin = int(np.iinfo(it).min)
              if casted_mn != imin:
                  # The shared_range have told us that they believe the imin does
                  # not have an exact representation.
                  fimin = ft(imin)
                  if np.isfinite(fimin):
                      assert int(fimin) != imin
                  # Therefore the imin, cast back to float, and to integer, will
                  # overflow. If it overflows to the imin, we need to allow for
                  # that possibility in the testing of our overflowed values
                  imin_roundtrip = fimin.astype(it)
                  if imin_roundtrip == imin:
                      thresh_overflow = True
              if thresh_overflow:
                  assert np.all(
                      (bit_smaller == casted_mn) | (bit_smaller == imin)
                  )
              else:
                  assert np.all(bit_smaller >= casted_mn)
  
file_path: nibabel/nibabel/tests/test_casting.py
imports: import os
  from platform import machine
  import numpy
  import pytest
  from numpy.testing import assert_array_equal
  from ..casting import CastingError, able_int_type, best_float, float_to_int, floor_log2, int_abs, longdouble_precision_improved, sctypes, shared_range, ulp
  from ..testing import suppress_warnings
label_desc: Use image node_id 1 for calling a global function with example usage: test_shared_range() without return types