Buffer Inspection
bufgrind
bufgrind is a test utility that iterates through every way to split a buffer
sequence into two adjacent pieces. For an N-byte input it produces N+1 split
positions: (0, N), (1, N-1), …, (N, 0). The two pieces at each
position concatenate back to the original sequence. Any code that processes
a buffer in chunks is exercised at every possible chunk boundary with a single
while loop.
bufgrind does not perform I/O and does not consult a fuse, so the snippets
on this page drive it under f.inert(…). A single pass is sufficient to
visit every split position, and there are no async failure sites to inject.
#include <boost/capy/test/bufgrind.hpp>
#include <boost/capy/test/buffer_to_string.hpp>
#include <boost/capy/test/fuse.hpp>
#include <boost/capy/buffers/make_buffer.hpp>
namespace capy = boost::capy;
void test_all_splits()
{
std::string data = "hello";
auto cb = capy::make_buffer(data);
capy::test::fuse f;
auto r = f.inert([&](capy::test::fuse&) -> capy::task<> {
capy::test::bufgrind bg(cb);
while(bg)
{
auto [b1, b2] = co_await bg.next();
BOOST_TEST_EQ(capy::test::buffer_to_string(b1, b2), data);
}
});
BOOST_TEST(r.success);
}
Iteration Pattern
For a 5-byte input "hello", bufgrind yields six positions:
pos=0: b1="" b2="hello"
pos=1: b1="h" b2="ello"
pos=2: b1="he" b2="llo"
pos=3: b1="hel" b2="lo"
pos=4: b1="hell" b2="o"
pos=5: b1="hello" b2=""
An empty buffer sequence yields one position where both pieces are empty, so the loop body always executes at least once.
Step Size
When the input is large, visiting every byte boundary is expensive. Pass a
step parameter to skip positions. The final position (equal to the total
size) is always visited regardless of step alignment.
std::string data = "0123456789"; // 10 bytes
auto cb = capy::make_buffer(data);
capy::test::bufgrind bg(cb, 3);
// Visits positions: 0, 3, 6, 9, 10
while(bg)
{
auto [b1, b2] = co_await bg.next();
// exercise parser at each split point
}
A step of 0 is treated as 1. A step larger than the total size reduces to two positions: 0 and size.
Mutability Preservation
bufgrind is templated on a ConstBufferSequence but the slices it
produces follow the mutability of the input. Each half is itself a
buffer sequence (slice_type). Passing a mutable_buffer yields halves
that model MutableBufferSequence; passing a const_buffer yields
halves that model ConstBufferSequence.
char data[] = "hello";
capy::mutable_buffer mb(data, 5);
capy::test::bufgrind bg(mb);
while(bg)
{
auto [b1, b2] = co_await bg.next();
// each half is itself a buffer sequence; a mutable input
// yields MutableBufferSequence halves callers may write into
static_assert(capy::MutableBufferSequence<decltype(b1)>);
}
See test::bufgrind for the complete member list: the
constructor’s step parameter, operator bool, and next.
buffer_to_string
buffer_to_string concatenates one or more buffer sequences into a
std::string. With a single argument it converts that buffer sequence;
with multiple arguments it concatenates them in order. The most common
use is asserting the combined content of a bufgrind split.
#include <boost/capy/test/buffer_to_string.hpp>
#include <boost/capy/buffers/make_buffer.hpp>
namespace capy = boost::capy;
void test_buffer_to_string()
{
// Single buffer sequence
capy::const_buffer cb(capy::make_buffer(std::string_view("hello")));
BOOST_TEST_EQ(capy::test::buffer_to_string(cb), "hello");
// Multiple buffer sequences concatenated in order
capy::const_buffer b1(capy::make_buffer(std::string_view("hello")));
capy::const_buffer b2(capy::make_buffer(std::string_view(" world")));
BOOST_TEST_EQ(capy::test::buffer_to_string(b1, b2), "hello world");
}
Use With bufgrind
The typical pattern passes both halves of a bufgrind split directly to
buffer_to_string to verify that each split reconstructs the original
input:
std::string original = "hello world";
auto cb = capy::make_buffer(original);
capy::test::fuse f;
auto r = f.inert([&](capy::test::fuse&) -> capy::task<> {
capy::test::bufgrind bg(cb);
while(bg)
{
auto [b1, b2] = co_await bg.next();
BOOST_TEST_EQ(capy::test::buffer_to_string(b1, b2), original);
}
});
BOOST_TEST(r.success);
See test::buffer_to_string for the exact signature.
Putting It Together
The following snippet tests a hypothetical parser that reads from a
stream, applying both utilities together.
For each split, one writer sends the first piece and a second writer sends the rest.
when_all posts its children in argument order, and run_blocking runs them in that order on one thread.
The parser therefore receives the two pieces in separate reads:
#include <boost/capy/test/bufgrind.hpp>
#include <boost/capy/test/buffer_to_string.hpp>
#include <boost/capy/test/fuse.hpp>
#include <boost/capy/test/stream.hpp>
#include <boost/capy/buffers/make_buffer.hpp>
#include <boost/capy/concept/read_stream.hpp>
#include <boost/capy/cond.hpp>
#include <boost/capy/results.hpp>
#include <boost/capy/task.hpp>
#include <boost/capy/when_all.hpp>
#include <boost/capy/write.hpp>
#include <array>
#include <string>
#include <system_error>
#include <utility>
namespace capy = boost::capy;
// Hypothetical parser: reads all bytes from a ReadStream
template<capy::ReadStream Stream>
capy::task<capy::results<std::string>> read_all(Stream& stream)
{
std::string out;
std::array<char, 64> buf;
for(;;)
{
auto [ec, n] = co_await stream.read_some(capy::make_buffer(buf));
out.append(buf.data(), n);
if(ec == capy::cond::eof)
co_return capy::results<std::string>{std::error_code(), std::move(out)};
if(ec)
co_return capy::results<std::string>{ec, std::move(out)};
}
}
// Sends one piece to the peer, closing the stream after the last piece
capy::task<capy::results<>> send_piece(
capy::test::stream& s, std::string piece, bool last)
{
auto [ec, _] = co_await capy::write(s, capy::make_buffer(piece));
if(last)
s.close();
co_return capy::results<>{ec};
}
void test_parser_all_splits()
{
std::string input = "GET / HTTP/1.1\r\n";
auto cb = capy::make_buffer(input);
capy::test::fuse f;
auto r = f.inert([&](capy::test::fuse&) -> capy::task<> {
capy::test::bufgrind bg(cb);
while(bg)
{
auto [b1, b2] = co_await bg.next();
// A named pair, because GCC 15 can skip destroying a
// structured binding's object in a coroutine loop
auto pair = capy::test::make_stream_pair(f);
auto& [a, b] = pair;
// when_all posts its children in argument order and
// run_blocking runs them in turn: b1 arrives, the parser
// reads it, and only then does b2 arrive.
auto [ec, sent1, got, sent2] = co_await capy::when_all(
send_piece(a, capy::test::buffer_to_string(b1), false),
read_all(b),
send_piece(a, capy::test::buffer_to_string(b2), true));
BOOST_TEST(!ec);
BOOST_TEST_EQ(got, input);
}
});
BOOST_TEST(r.success);
}
See the Index by Kind for the full API browser.