Return a host or device buffer with random data.
(size, target='host')
| 97 | |
| 98 | |
| 99 | def make_random_buffer(size, target='host'): |
| 100 | """Return a host or device buffer with random data. |
| 101 | """ |
| 102 | if target == 'host': |
| 103 | assert size >= 0 |
| 104 | buf = pa.allocate_buffer(size) |
| 105 | assert buf.size == size |
| 106 | arr = np.frombuffer(buf, dtype=np.uint8) |
| 107 | assert arr.size == size |
| 108 | arr[:] = np.random.randint(low=1, high=255, size=size, dtype=np.uint8) |
| 109 | assert arr.sum() > 0 or size == 0 |
| 110 | arr_ = np.frombuffer(buf, dtype=np.uint8) |
| 111 | np.testing.assert_equal(arr, arr_) |
| 112 | return arr, buf |
| 113 | elif target == 'device': |
| 114 | arr, buf = make_random_buffer(size, target='host') |
| 115 | dbuf = global_context.new_buffer(size) |
| 116 | assert dbuf.size == size |
| 117 | dbuf.copy_from_host(buf, position=0, nbytes=size) |
| 118 | return arr, dbuf |
| 119 | raise ValueError('invalid target value') |
| 120 | |
| 121 | |
| 122 | @pytest.mark.parametrize("size", [0, 1, 1000]) |
no test coverage detected