code: def test_context_device_buffer(size):
      # Creating device buffer from host buffer;
      arr, buf = make_random_buffer(size)
      cudabuf = global_context.buffer_from_data(buf)
      assert cudabuf.size == size
      arr2 = np.frombuffer(cudabuf.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr, arr2)
  
      # CudaBuffer does not support buffer protocol
      with pytest.raises(BufferError):
          memoryview(cudabuf)
  
      # Creating device buffer from array:
      cudabuf = global_context.buffer_from_data(arr)
      assert cudabuf.size == size
      arr2 = np.frombuffer(cudabuf.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr, arr2)
  
      # Creating device buffer from bytes:
      cudabuf = global_context.buffer_from_data(arr.tobytes())
      assert cudabuf.size == size
      arr2 = np.frombuffer(cudabuf.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr, arr2)
  
      # Creating a device buffer from another device buffer, view:
      cudabuf2 = cudabuf.slice(0, cudabuf.size)
      assert cudabuf2.size == size
      arr2 = np.frombuffer(cudabuf2.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr, arr2)
  
      if size > 1:
          cudabuf2.copy_from_host(arr[size // 2 :])
          arr3 = np.frombuffer(cudabuf.copy_to_host(), dtype=np.uint8)
          np.testing.assert_equal(
              np.concatenate((arr[size // 2 :], arr[size // 2 :])), arr3
          )
          cudabuf2.copy_from_host(arr[: size // 2])  # restoring arr
  
      # Creating a device buffer from another device buffer, copy:
      cudabuf2 = global_context.buffer_from_data(cudabuf)
      assert cudabuf2.size == size
      arr2 = np.frombuffer(cudabuf2.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr, arr2)
  
      cudabuf2.copy_from_host(arr[size // 2 :])
      arr3 = np.frombuffer(cudabuf.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr, arr3)
  
      # Slice of a device buffer
      cudabuf2 = cudabuf.slice(0, cudabuf.size + 10)
      assert cudabuf2.size == size
      arr2 = np.frombuffer(cudabuf2.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr, arr2)
  
      cudabuf2 = cudabuf.slice(size // 4, size + 10)
      assert cudabuf2.size == size - size // 4
      arr2 = np.frombuffer(cudabuf2.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr[size // 4 :], arr2)
  
      # Creating a device buffer from a slice of host buffer
      soffset = size // 4
      ssize = 2 * size // 4
      cudabuf = global_context.buffer_from_data(
          buf, offset=soffset, size=ssize
      )
      assert cudabuf.size == ssize
      arr2 = np.frombuffer(cudabuf.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr[soffset : soffset + ssize], arr2)
  
      cudabuf = global_context.buffer_from_data(
          buf.slice(offset=soffset, length=ssize)
      )
      assert cudabuf.size == ssize
      arr2 = np.frombuffer(cudabuf.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr[soffset : soffset + ssize], arr2)
  
      # Creating a device buffer from a slice of an array
      cudabuf = global_context.buffer_from_data(
          arr, offset=soffset, size=ssize
      )
      assert cudabuf.size == ssize
      arr2 = np.frombuffer(cudabuf.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr[soffset : soffset + ssize], arr2)
  
      cudabuf = global_context.buffer_from_data(
          arr[soffset : soffset + ssize]
      )
      assert cudabuf.size == ssize
      arr2 = np.frombuffer(cudabuf.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr[soffset : soffset + ssize], arr2)
  
      # Creating a device buffer from a slice of bytes
      cudabuf = global_context.buffer_from_data(
          arr.tobytes(), offset=soffset, size=ssize
      )
      assert cudabuf.size == ssize
      arr2 = np.frombuffer(cudabuf.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr[soffset : soffset + ssize], arr2)
  
      # Creating a device buffer from size
      cudabuf = global_context.new_buffer(size)
      assert cudabuf.size == size
  
      # Creating device buffer from a slice of another device buffer:
      cudabuf = global_context.buffer_from_data(arr)
      cudabuf2 = cudabuf.slice(soffset, ssize)
      assert cudabuf2.size == ssize
      arr2 = np.frombuffer(cudabuf2.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr[soffset : soffset + ssize], arr2)
  
      # Creating device buffer from HostBuffer
  
      buf = cuda.new_host_buffer(size)
      arr_ = np.frombuffer(buf, dtype=np.uint8)
      arr_[:] = arr
      cudabuf = global_context.buffer_from_data(buf)
      assert cudabuf.size == size
      arr2 = np.frombuffer(cudabuf.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr, arr2)
  
      # Creating device buffer from HostBuffer slice
  
      cudabuf = global_context.buffer_from_data(
          buf, offset=soffset, size=ssize
      )
      assert cudabuf.size == ssize
      arr2 = np.frombuffer(cudabuf.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr[soffset : soffset + ssize], arr2)
  
      cudabuf = global_context.buffer_from_data(
          buf.slice(offset=soffset, length=ssize)
      )
      assert cudabuf.size == ssize
      arr2 = np.frombuffer(cudabuf.copy_to_host(), dtype=np.uint8)
      np.testing.assert_equal(arr[soffset : soffset + ssize], arr2)
  
imports: import sys
  import sysconfig
  import pytest
  import pyarrow
  import numpy
label_desc: Use image node_id 7 for calling a global function with example usage: test_context_device_buffer(size) without return types