Improve streambuf unit test

This commit is contained in:
Vinnie Falco
2015-01-16 09:03:53 -08:00
parent faf91d6697
commit 8432b9e29a

View File

@@ -26,11 +26,11 @@ namespace asio {
class streambuf_test : public unit_test::suite
{
public:
// Convert a ConstBufferSequence to a string
template <class ConstBufferSequence>
// Convert a buffer sequence to a string
template <class Buffers>
static
std::string
to_str (ConstBufferSequence const& b)
to_str (Buffers const& b)
{
std::string s;
auto const n = boost::asio::buffer_size(b);
@@ -40,6 +40,84 @@ public:
return s;
}
// Fill a buffer sequence with predictable data
template <class Buffers>
static
void
fill (Buffers const& b)
{
char c = 0;
auto first = boost::asio::buffers_begin(b);
auto last = boost::asio::buffers_end(b);
while (first != last)
*first++ = c++;
}
// Check that a buffer sequence has predictable data
template <class Buffers>
void
check (Buffers const& b, char c = 0)
{
auto first = boost::asio::buffers_begin(b);
auto last = boost::asio::buffers_end(b);
while (first != last)
expect (*first++ == c++);
}
void
test_prepare()
{
testcase << "prepare";
beast::asio::streambuf b(11);
for (std::size_t n = 0; n < 97; ++n)
{
fill(b.prepare(n));
b.commit(n);
check(b.data());
b.consume(n);
}
}
void
test_commit()
{
testcase << "commit";
beast::asio::streambuf b(11);
for (std::size_t n = 0; n < 97; ++n)
{
fill(b.prepare(n));
char c = 0;
for (int i = 1;; ++i)
{
b.commit(i);
check(b.data(), c);
b.consume(i);
if (b.size() < 1)
break;
c += i;
}
}
}
void
test_consume()
{
testcase << "consume";
beast::asio::streambuf b(11);
for (std::size_t n = 0; n < 97; ++n)
{
fill(b.prepare(n));
b.commit(n);
char c = 0;
for (int i = 1; b.size() > 0; ++i)
{
check(b.data(), c);
b.consume(i);
c += i;
}
}
}
void run()
{
{
@@ -67,8 +145,10 @@ public:
b.commit(10);
expect(to_str(b.data()) == "567890ABCD");
}
pass();
test_prepare();
test_commit();
test_consume();
}
};