Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions cpp/src/arrow/util/buffer-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,47 @@ TEST_F(TestBuffer, ResizeOOM) {
ASSERT_RAISES(OutOfMemory, buf.Resize(to_alloc));
}

TEST_F(TestBuffer, EqualsWithSameContent) {
MemoryPool* pool = default_memory_pool();
const int32_t bufferSize = 128 * 1024;
uint8_t* rawBuffer1;
ASSERT_OK(pool->Allocate(bufferSize, &rawBuffer1));
memset(rawBuffer1, 12, bufferSize);
uint8_t* rawBuffer2;
ASSERT_OK(pool->Allocate(bufferSize, &rawBuffer2));
memset(rawBuffer2, 12, bufferSize);
uint8_t* rawBuffer3;
ASSERT_OK(pool->Allocate(bufferSize, &rawBuffer3));
memset(rawBuffer3, 3, bufferSize);

Buffer buffer1(rawBuffer1, bufferSize);
Buffer buffer2(rawBuffer2, bufferSize);
Buffer buffer3(rawBuffer3, bufferSize);
ASSERT_TRUE(buffer1.Equals(buffer2));
ASSERT_FALSE(buffer1.Equals(buffer3));

pool->Free(rawBuffer1, bufferSize);
pool->Free(rawBuffer2, bufferSize);
pool->Free(rawBuffer3, bufferSize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change these tests to use PoolBuffer instead of Buffer (and do so in the future)? That way memory will be automatically deallocated in destructors.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late and thanks Wes. I just attempted to change the codes using PoolBuffer, and found that the current codes are more readable regarding the test logic. I do agree for most tests that need buffer, PoolBuffer would be better to save the free. Sure I will use it in the future.

}

TEST_F(TestBuffer, EqualsWithSameBuffer) {
MemoryPool* pool = default_memory_pool();
const int32_t bufferSize = 128 * 1024;
uint8_t* rawBuffer;
ASSERT_OK(pool->Allocate(bufferSize, &rawBuffer));
memset(rawBuffer, 111, bufferSize);

Buffer buffer1(rawBuffer, bufferSize);
Buffer buffer2(rawBuffer, bufferSize);
ASSERT_TRUE(buffer1.Equals(buffer2));

const int64_t nbytes = bufferSize / 2;
Buffer buffer3(rawBuffer, nbytes);
ASSERT_TRUE(buffer1.Equals(buffer3, nbytes));
ASSERT_FALSE(buffer1.Equals(buffer3, nbytes + 1));

pool->Free(rawBuffer, bufferSize);
}

} // namespace arrow
9 changes: 6 additions & 3 deletions cpp/src/arrow/util/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ class Buffer : public std::enable_shared_from_this<Buffer> {
// Return true if both buffers are the same size and contain the same bytes
// up to the number of compared bytes
bool Equals(const Buffer& other, int64_t nbytes) const {
return this == &other || (size_ >= nbytes && other.size_ >= nbytes &&
!memcmp(data_, other.data_, nbytes));
return this == &other ||
(size_ >= nbytes && other.size_ >= nbytes &&
(data_ == other.data_ || !memcmp(data_, other.data_, nbytes)));
}

bool Equals(const Buffer& other) const {
return this == &other || (size_ == other.size_ && !memcmp(data_, other.data_, size_));
return this == &other ||
(size_ == other.size_ &&
(data_ == other.data_ || !memcmp(data_, other.data_, size_)));
}

const uint8_t* data() const { return data_; }
Expand Down