diff options
Diffstat (limited to 'vespalib')
-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(); } } |