code: def test_BufferWriter():
      def allocate(size):
          cbuf = global_context.new_buffer(size)
          writer = cuda.BufferWriter(cbuf)
          return cbuf, writer
  
      def test_writes(total_size, chunksize, buffer_size=0):
          cbuf, writer = allocate(total_size)
          arr, buf = make_random_buffer(size=total_size, target="host")
  
          if buffer_size > 0:
              writer.buffer_size = buffer_size
  
          position = writer.tell()
          assert position == 0
          writer.write(buf.slice(length=chunksize))
          assert writer.tell() == chunksize
          writer.seek(0)
          position = writer.tell()
          assert position == 0
  
          while position < total_size:
              bytes_to_write = min(chunksize, total_size - position)
              writer.write(
                  buf.slice(offset=position, length=bytes_to_write)
              )
              position += bytes_to_write
  
          writer.flush()
          assert cbuf.size == total_size
          cbuf.context.synchronize()
          buf2 = cbuf.copy_to_host()
          cbuf.context.synchronize()
          assert buf2.size == total_size
          arr2 = np.frombuffer(buf2, dtype=np.uint8)
          np.testing.assert_equal(arr, arr2)
  
      total_size, chunk_size = 1 << 16, 1000
      test_writes(total_size, chunk_size)
      test_writes(total_size, chunk_size, total_size // 16)
  
      cbuf, writer = allocate(100)
      writer.write(np.arange(100, dtype=np.uint8))
      writer.writeat(50, np.arange(25, dtype=np.uint8))
      writer.write(np.arange(25, dtype=np.uint8))
      writer.flush()
  
      arr = np.frombuffer(cbuf.copy_to_host(), np.uint8)
      np.testing.assert_equal(arr[:50], np.arange(50, dtype=np.uint8))
      np.testing.assert_equal(arr[50:75], np.arange(25, dtype=np.uint8))
      np.testing.assert_equal(arr[75:], np.arange(25, dtype=np.uint8))
  
imports: import sys
  import sysconfig
  import pytest
  import pyarrow
  import numpy
label_desc: Use image node_id 16 for calling a global function with example usage: test_BufferWriter() and returns: cbuf,  writer