diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-01-10 16:13:28 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-10 16:13:28 +0100 |
commit | 1402b8c5ac7cf608bcbdabb46873d6a3308ab69d (patch) | |
tree | 1352e5ef2b79f46e71c95e18b04385542303e31c | |
parent | 3e5d7fbea7cc63af994c9bb82ed7117f76ab127e (diff) | |
parent | f4ec71e9c9b32c77a84e57b8376ed3a69d0f79c8 (diff) |
Merge pull request #25484 from vespa-engine/havardpe/smart-buffer-empty-function
add 'empty' function to smart buffer
-rw-r--r-- | vespalib/src/tests/data/smart_buffer/smart_buffer_test.cpp | 14 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/data/smart_buffer.h | 3 |
2 files changed, 16 insertions, 1 deletions
diff --git a/vespalib/src/tests/data/smart_buffer/smart_buffer_test.cpp b/vespalib/src/tests/data/smart_buffer/smart_buffer_test.cpp index c461da1fcf7..b860aa3326a 100644 --- a/vespalib/src/tests/data/smart_buffer/smart_buffer_test.cpp +++ b/vespalib/src/tests/data/smart_buffer/smart_buffer_test.cpp @@ -24,6 +24,7 @@ TEST("require that basic read/write works") { SmartBuffer buf(3); TEST_DO(checkBuffer("", buf)); { // read from empty buffer + EXPECT_TRUE(buf.empty()); EXPECT_EQUAL(0u, buf.obtain().size); } { // write to buffer @@ -34,6 +35,7 @@ TEST("require that basic read/write works") { mem.data[1] = 'b'; mem.data[2] = 'c'; EXPECT_EQUAL(&buf, &buf.commit(3)); + EXPECT_FALSE(buf.empty()); mem = buf.reserve(0); TEST_DO(checkBuffer("abc", buf)); EXPECT_LESS_EQUAL(0u, mem.size); @@ -61,6 +63,7 @@ TEST("require that basic read/write works") { EXPECT_LESS_EQUAL(5u, mem.size); } { // read until end + EXPECT_FALSE(buf.empty()); Memory mem = buf.obtain(); TEST_DO(checkBuffer("cd", buf)); TEST_DO(checkMemory("cd", mem)); @@ -69,6 +72,7 @@ TEST("require that basic read/write works") { TEST_DO(checkBuffer("d", buf)); TEST_DO(checkMemory("d", mem)); EXPECT_EQUAL(&buf, &buf.evict(1)); + EXPECT_TRUE(buf.empty()); mem = buf.obtain(); TEST_DO(checkBuffer("", buf)); TEST_DO(checkMemory("", mem)); @@ -83,16 +87,21 @@ TEST("require that requested initial size is not adjusted") { TEST("require that buffer auto-resets when empty") { SmartBuffer buf(64); EXPECT_EQUAL(buf.reserve(10).size, 64u); + EXPECT_TRUE(buf.empty()); write_buf("abc", buf); + EXPECT_FALSE(buf.empty()); EXPECT_EQUAL(buf.reserve(10).size, 61u); buf.evict(3); + EXPECT_TRUE(buf.empty()); EXPECT_EQUAL(buf.reserve(10).size, 64u); } TEST("require that buffer can grow") { SmartBuffer buf(64); EXPECT_EQUAL(buf.capacity(), 64u); + EXPECT_TRUE(buf.empty()); write_buf("abc", buf); + EXPECT_FALSE(buf.empty()); write_buf("abc", buf); buf.evict(3); EXPECT_EQUAL(buf.reserve(70).size, size_t(128 - 3)); @@ -103,7 +112,9 @@ TEST("require that buffer can grow") { TEST("require that buffer can grow more than 2x") { SmartBuffer buf(64); EXPECT_EQUAL(buf.capacity(), 64u); + EXPECT_TRUE(buf.empty()); write_buf("abc", buf); + EXPECT_FALSE(buf.empty()); write_buf("abc", buf); buf.evict(3); EXPECT_EQUAL(buf.reserve(170).size, 170u); @@ -114,7 +125,9 @@ TEST("require that buffer can grow more than 2x") { TEST("require that buffer can be compacted") { SmartBuffer buf(16); EXPECT_EQUAL(buf.capacity(), 16u); + EXPECT_TRUE(buf.empty()); write_buf("abc", buf); + EXPECT_FALSE(buf.empty()); write_buf("abc", buf); buf.evict(3); write_buf("abc", buf); @@ -133,6 +146,7 @@ TEST("require that buffer can be compacted") { TEST("require that a completely empty buffer can be created") { SmartBuffer buf(0); EXPECT_EQUAL(buf.capacity(), 0u); + EXPECT_TRUE(buf.empty()); EXPECT_TRUE(buf.obtain().data == nullptr); } diff --git a/vespalib/src/vespa/vespalib/data/smart_buffer.h b/vespalib/src/vespa/vespalib/data/smart_buffer.h index 17fb7614f0e..fc7042c5eea 100644 --- a/vespalib/src/vespa/vespalib/data/smart_buffer.h +++ b/vespalib/src/vespa/vespalib/data/smart_buffer.h @@ -33,9 +33,10 @@ private: public: SmartBuffer(size_t initial_size); ~SmartBuffer(); + bool empty() const { return (read_len() == 0); } size_t capacity() const { return _data.size(); } void drop_if_empty() { - if ((read_len() == 0) && (_data.size() > 0)) { + if (empty() && (_data.size() > 0)) { drop(); } } |