code: def test_cast_scalar(device, value, shape, dtype):
      np_dtype = numpy.dtype(dtype)
      try:
          np_value = np_dtype.type(value)
      except (ValueError, OverflowError):
          return
  
      a_np = numpy.asarray([np_value], dtype).reshape(shape)
      a_chx = chainerx.array(a_np)
  
      def should_cast_succeed(typ):
          try:
              typ(np_value)
              return True
          except (ValueError, OverflowError):
              return False
  
      # Cast to float
      if should_cast_succeed(float):
          assert type(float(a_chx)) is float
          if math.isnan(float(a_np)):
              assert math.isnan(float(a_chx))
          else:
              assert float(a_np) == float(a_chx)
      # Cast to int
      if should_cast_succeed(int):
          assert type(int(a_chx)) is int
          assert int(a_np) == int(a_chx)
      # Cast to bool
      if should_cast_succeed(bool):
          assert type(bool(a_chx)) is bool
          assert bool(a_np) == bool(a_chx)
  
      # item()
      item_actual = a_chx.item()
      np_dtype = numpy.dtype(dtype)
      item_expected = np_dtype.type(value).item()
      assert isinstance(item_actual, type(item_expected))
      assert (
          numpy.isnan(item_actual) and numpy.isnan(item_expected)
      ) or item_actual == item_expected
  
imports: import copy
  import math
  import pickle
  import numpy
  import pytest
  import chainerx
  import chainerx.testing
  from chainerx_tests import array_utils
label_desc: Use image node_id 4 for calling a global function with example usage: test_cast_scalar(device, value, shape, dtype) and returns: True, False