def test_buffer_basic(): n = 10 buf = Buffer(np.arange(n, dtype=np.float64)) assert buf.size == n assert buf.capacity == n np.testing.assert_equal(buf.mem.copy_to_host(), np.arange(n, dtype=np.float64))
def test_pickle_buffer(): arr = np.arange(10) buf = Buffer(arr, size=4, capacity=arr.size) assert buf.size == 4 assert buf.capacity == 10 pickled = pickle.dumps(buf) unpacked = pickle.loads(pickled) # Check that unpacked capacity equals buf.size assert unpacked.size == 4 assert unpacked.capacity == 4 assert unpacked.mem.size == unpacked.capacity np.testing.assert_equal(unpacked.to_array(), arr[:unpacked.size])
def test_buffer_append(): n = 10 expected = np.arange(n, dtype=np.float64) buf = Buffer(expected, size=n - 4, capacity=n) assert buf.size == n - 4 assert buf.capacity == n np.testing.assert_equal(buf.mem.copy_to_host(), expected) np.testing.assert_equal(buf.to_array(), np.arange(n - 4, dtype=np.float64)) # Buffer.append buf.append(1.23) expected[n - 4] = 1.23 np.testing.assert_equal(buf.mem.copy_to_host(), expected) assert buf.size == n - 3 assert buf.capacity == n # Buffer.extend buf.extend(np.asarray([2, 3])) expected[n - 3] = 2 expected[n - 2] = 3 np.testing.assert_equal(buf.mem.copy_to_host(), expected) assert buf.size == n - 1 assert buf.capacity == n # Test out-of-bound with pytest.raises(MemoryError): buf.extend(np.asarray([2, 3])) np.testing.assert_equal(buf.mem.copy_to_host(), expected) assert buf.size == n - 1 assert buf.capacity == n # Append to last slot buf.append(10.125) expected[n - 1] = 10.125 np.testing.assert_equal(buf.mem.copy_to_host(), expected) assert buf.size == n assert buf.capacity == n with pytest.raises(MemoryError): buf.append(987654) np.testing.assert_equal(buf.to_array(), expected) assert buf.size == n assert buf.capacity == n