MCPcopy Create free account
hub / github.com/apache/arrow / test_BufferWriter

Function test_BufferWriter

python/pyarrow/tests/test_cuda.py:565–613  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

563
564
565def test_BufferWriter():
566 def allocate(size):
567 cbuf = global_context.new_buffer(size)
568 writer = cuda.BufferWriter(cbuf)
569 return cbuf, writer
570
571 def test_writes(total_size, chunksize, buffer_size=0):
572 cbuf, writer = allocate(total_size)
573 arr, buf = make_random_buffer(size=total_size, target='host')
574
575 if buffer_size > 0:
576 writer.buffer_size = buffer_size
577
578 position = writer.tell()
579 assert position == 0
580 writer.write(buf.slice(length=chunksize))
581 assert writer.tell() == chunksize
582 writer.seek(0)
583 position = writer.tell()
584 assert position == 0
585
586 while position < total_size:
587 bytes_to_write = min(chunksize, total_size - position)
588 writer.write(buf.slice(offset=position, length=bytes_to_write))
589 position += bytes_to_write
590
591 writer.flush()
592 assert cbuf.size == total_size
593 cbuf.context.synchronize()
594 buf2 = cbuf.copy_to_host()
595 cbuf.context.synchronize()
596 assert buf2.size == total_size
597 arr2 = np.frombuffer(buf2, dtype=np.uint8)
598 np.testing.assert_equal(arr, arr2)
599
600 total_size, chunk_size = 1 << 16, 1000
601 test_writes(total_size, chunk_size)
602 test_writes(total_size, chunk_size, total_size // 16)
603
604 cbuf, writer = allocate(100)
605 writer.write(np.arange(100, dtype=np.uint8))
606 writer.writeat(50, np.arange(25, dtype=np.uint8))
607 writer.write(np.arange(25, dtype=np.uint8))
608 writer.flush()
609
610 arr = np.frombuffer(cbuf.copy_to_host(), np.uint8)
611 np.testing.assert_equal(arr[:50], np.arange(50, dtype=np.uint8))
612 np.testing.assert_equal(arr[50:75], np.arange(25, dtype=np.uint8))
613 np.testing.assert_equal(arr[75:], np.arange(25, dtype=np.uint8))
614
615
616def test_BufferWriter_edge_cases():

Callers

nothing calls this directly

Calls 4

test_writesFunction · 0.85
allocateFunction · 0.85
flushMethod · 0.80
writeMethod · 0.45

Tested by

no test coverage detected