Skip to content

Commit

Permalink
add capacity function to 2 buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
Viatorus committed Feb 17, 2024
1 parent 46cfdf5 commit 0e64844
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/emio/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ class memory_buffer final : public buffer {
static_cast<void>(request_write_area(0, vec_.capacity()));
}

/**
* Returns the number of chars that the buffer has currently allocated space for.
* @return The capacity.
*/
[[nodiscard]] constexpr size_t capacity() const noexcept {
return vec_.capacity();
}

protected:
constexpr result<std::span<char>> request_write_area(const size_t used, const size_t size) noexcept override {
const size_t new_size = vec_.size() + size;
Expand Down Expand Up @@ -234,6 +242,14 @@ class span_buffer : public buffer {
this->set_write_area(span_);
}

/**
* Returns the number of chars that the buffer has space for.
* @return The capacity.
*/
[[nodiscard]] constexpr size_t capacity() const noexcept {
return span_.size();
}

private:
std::span<char> span_;
};
Expand Down
11 changes: 11 additions & 0 deletions test/unit_test/test_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ TEST_CASE("memory_buffer", "[buffer]") {
INFO("Default constructed: " << default_constructed);

emio::memory_buffer<15> buf = default_constructed ? emio::memory_buffer<15>{} : emio::memory_buffer<15>{18};
CHECK(buf.capacity() == (default_constructed ? 15 : 18));
CHECK(buf.view().empty());

emio::result<std::span<char>> area = buf.get_write_area_of(first_size);
Expand All @@ -56,7 +57,11 @@ TEST_CASE("memory_buffer", "[buffer]") {
CHECK(buf.view().size() == first_size + second_size + third_size);
CHECK(buf.view() == expected_str);

const size_t curr_capacity = buf.capacity();
CHECK(curr_capacity >= buf.view().size());
buf.reset();
CHECK(buf.capacity() == curr_capacity);

area = buf.get_write_area_of(first_size);
REQUIRE(area);
CHECK(area->size() == first_size);
Expand All @@ -82,6 +87,7 @@ TEST_CASE("memory_buffer regression bug 1", "[buffer]") {
}();

emio::memory_buffer buf;
CHECK(buf.capacity() == emio::default_cache_size);

// Write a.
for (size_t i = 0; i < default_capacity + 1U; i++) {
Expand Down Expand Up @@ -116,6 +122,7 @@ TEST_CASE("memory_buffer at compile-time", "[buffer]") {
bool result = true;

emio::memory_buffer<1> buf{};
result &= buf.capacity() == 1;
result &= buf.view().empty();

emio::result<std::span<char>> area = buf.get_write_area_of(first_size);
Expand All @@ -135,6 +142,7 @@ TEST_CASE("memory_buffer at compile-time", "[buffer]") {
fill(area, 'z');
result &= buf.view().size() == first_size + second_size + third_size;
result &= buf.view() == "xxxxxxxyyyyyzzzzzzzz";
result &= buf.capacity() >= buf.view().size();

return result;
}();
Expand All @@ -153,6 +161,7 @@ TEST_CASE("span_buffer", "[buffer]") {

std::array<char, first_size + second_size + third_size> storage{};
emio::span_buffer buf{storage};
CHECK(buf.capacity() == storage.size());
CHECK(buf.view().empty());

emio::result<std::span<char>> area = buf.get_write_area_of(first_size);
Expand Down Expand Up @@ -187,7 +196,9 @@ TEST_CASE("span_buffer", "[buffer]") {
REQUIRE(area);
CHECK(area->empty());

CHECK(buf.capacity() == storage.size());
buf.reset();
CHECK(buf.capacity() == storage.size());
area = buf.get_write_area_of(first_size);
REQUIRE(area);
CHECK(area->size() == first_size);
Expand Down

0 comments on commit 0e64844

Please sign in to comment.