Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5fbf9e7

Browse files
authored
Merge pull request #1715 from evoskuil/master
Add write_line to stream writers.
2 parents 5ccca74 + 496ff8c commit 5fbf9e7

File tree

7 files changed

+253
-1
lines changed

7 files changed

+253
-1
lines changed

include/bitcoin/system/impl/stream/streamers/byte_writer.ipp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,14 @@ void byte_writer<OStream>::write_string_buffer(const std::string& value,
252252
write_bytes(data_chunk(size - length, pad()));
253253
}
254254

255+
template <typename OStream>
256+
void byte_writer<OStream>::write_line(const std::string& value,
257+
const std::string& end) NOEXCEPT
258+
{
259+
write_string_buffer(value, value.size());
260+
write_string_buffer(end, end.size());
261+
}
262+
255263
// context
256264
// ----------------------------------------------------------------------------
257265

include/bitcoin/system/stream/streamers/byte_writer.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ class byte_writer
101101
void write_string_buffer(const std::string& value, size_t size) NOEXCEPT
102102
override;
103103

104+
/// Write value and specified terminator.
105+
void write_line(const std::string& value="",
106+
const std::string& end="\r\n") NOEXCEPT override;
107+
104108
/// Streams.
105109
/// -----------------------------------------------------------------------
106110

include/bitcoin/system/stream/streamers/interfaces/bytewriter.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ class bytewriter
8080
virtual void write_string_buffer(const std::string& value, size_t size)
8181
NOEXCEPT = 0;
8282

83+
/// Write value and specified terminator.
84+
virtual void write_line(const std::string& value="",
85+
const std::string& end="\r\n") NOEXCEPT = 0;
86+
8387
/// Streams.
8488
/// -----------------------------------------------------------------------
8589

test/stream/streamers/bit_flipper.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,65 @@ BOOST_AUTO_TEST_CASE(bit_flipper__write_string_buffer__value__expected)
15191519
BOOST_REQUIRE(writer);
15201520
}
15211521

1522+
// write_line
1523+
1524+
BOOST_AUTO_TEST_CASE(bit_flipper__write_line__default__expected)
1525+
{
1526+
std::stringstream stream;
1527+
flip::bits::iostream writer(stream);
1528+
writer.write_line();
1529+
BOOST_REQUIRE_EQUAL(stream.str(), "\r\n");
1530+
BOOST_REQUIRE(writer);
1531+
}
1532+
1533+
BOOST_AUTO_TEST_CASE(bit_flipper__write_line__empty_default__expected)
1534+
{
1535+
std::stringstream stream;
1536+
flip::bits::iostream writer(stream);
1537+
writer.write_line("");
1538+
BOOST_REQUIRE_EQUAL(stream.str(), "\r\n");
1539+
BOOST_REQUIRE(writer);
1540+
}
1541+
1542+
BOOST_AUTO_TEST_CASE(bit_flipper__write_line__empty_empty__expected)
1543+
{
1544+
std::stringstream stream;
1545+
flip::bits::iostream writer(stream);
1546+
writer.write_line("", "");
1547+
BOOST_REQUIRE(stream.str().empty());
1548+
BOOST_REQUIRE(writer);
1549+
}
1550+
1551+
BOOST_AUTO_TEST_CASE(bit_flipper__write_line__value_empty__expected)
1552+
{
1553+
std::stringstream stream;
1554+
flip::bits::iostream writer(stream);
1555+
const std::string expected{ "abcdefghijklmnopqrstuvwxyz" };
1556+
writer.write_line(expected, "");
1557+
BOOST_REQUIRE_EQUAL(stream.str(), expected);
1558+
BOOST_REQUIRE(writer);
1559+
}
1560+
1561+
BOOST_AUTO_TEST_CASE(bit_flipper__write_line__value_explicit__expected)
1562+
{
1563+
std::stringstream stream;
1564+
flip::bits::iostream writer(stream);
1565+
const std::string expected{ "abcdefghijklmnopqrstuvwxyz" };
1566+
writer.write_line(expected, "|");
1567+
BOOST_REQUIRE_EQUAL(stream.str(), expected + "|");
1568+
BOOST_REQUIRE(writer);
1569+
}
1570+
1571+
BOOST_AUTO_TEST_CASE(bit_flipper__write_line__value_default__expected)
1572+
{
1573+
std::stringstream stream;
1574+
flip::bits::iostream writer(stream);
1575+
const std::string expected{ "abcdefghijklmnopqrstuvwxyz" };
1576+
writer.write_line(expected);
1577+
BOOST_REQUIRE_EQUAL(stream.str(), expected + "\r\n");
1578+
BOOST_REQUIRE(writer);
1579+
}
1580+
15221581
#endif // BIT_FLIPPER_WRITER_STRINGS
15231582

15241583
#endif // BIT_FLIPPER_WRITER

test/stream/streamers/bit_writer.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,65 @@ BOOST_AUTO_TEST_CASE(bit_writer__write_string_buffer__value__expected)
468468
BOOST_REQUIRE(writer);
469469
}
470470

471+
// write_line
472+
473+
BOOST_AUTO_TEST_CASE(bit_writer__write_line__default__expected)
474+
{
475+
std::ostringstream stream;
476+
write::bits::ostream writer(stream);
477+
writer.write_line();
478+
BOOST_REQUIRE_EQUAL(stream.str(), "\r\n");
479+
BOOST_REQUIRE(writer);
480+
}
481+
482+
BOOST_AUTO_TEST_CASE(bit_writer__write_line__empty_default__expected)
483+
{
484+
std::ostringstream stream;
485+
write::bits::ostream writer(stream);
486+
writer.write_line("");
487+
BOOST_REQUIRE_EQUAL(stream.str(), "\r\n");
488+
BOOST_REQUIRE(writer);
489+
}
490+
491+
BOOST_AUTO_TEST_CASE(bit_writer__write_line__empty_empty__expected)
492+
{
493+
std::ostringstream stream;
494+
write::bits::ostream writer(stream);
495+
writer.write_line("", "");
496+
BOOST_REQUIRE(stream.str().empty());
497+
BOOST_REQUIRE(writer);
498+
}
499+
500+
BOOST_AUTO_TEST_CASE(bit_writer__write_line__value_empty__expected)
501+
{
502+
std::ostringstream stream;
503+
write::bits::ostream writer(stream);
504+
const std::string expected{ "abcdefghijklmnopqrstuvwxyz" };
505+
writer.write_line(expected, "");
506+
BOOST_REQUIRE_EQUAL(stream.str(), expected);
507+
BOOST_REQUIRE(writer);
508+
}
509+
510+
BOOST_AUTO_TEST_CASE(bit_writer__write_line__value_explicit__expected)
511+
{
512+
std::ostringstream stream;
513+
write::bits::ostream writer(stream);
514+
const std::string expected{ "abcdefghijklmnopqrstuvwxyz" };
515+
writer.write_line(expected, "|");
516+
BOOST_REQUIRE_EQUAL(stream.str(), expected + "|");
517+
BOOST_REQUIRE(writer);
518+
}
519+
520+
BOOST_AUTO_TEST_CASE(bit_writer__write_line__value_default__expected)
521+
{
522+
std::ostringstream stream;
523+
write::bits::ostream writer(stream);
524+
const std::string expected{ "abcdefghijklmnopqrstuvwxyz" };
525+
writer.write_line(expected);
526+
BOOST_REQUIRE_EQUAL(stream.str(), expected + "\r\n");
527+
BOOST_REQUIRE(writer);
528+
}
529+
471530
#endif // BIT_WRITER_STRINGS
472531

473532
BOOST_AUTO_TEST_SUITE_END()

test/stream/streamers/byte_flipper.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ constexpr uint8_t pad = 0x00;
3434
#define BYTE_FLIPPER_READER_BYTES
3535
#define BYTE_FLIPPER_READER_STRINGS
3636

37-
// Exact copy of all but 2 byte_writer tests, replaced by flip::bytes::iostream.
37+
// Exact copy of all but 2 byte_flipper tests, replaced by flip::bytes::iostream.
3838
#define BYTE_FLIPPER_WRITER
3939
#define BYTE_FLIPPER_WRITER_CONTEXT
4040
#define BYTE_FLIPPER_WRITER_BIG_ENDIAN
@@ -1516,6 +1516,65 @@ BOOST_AUTO_TEST_CASE(byte_flipper__write_string_buffer__value__expected)
15161516
BOOST_REQUIRE(writer);
15171517
}
15181518

1519+
// write_line
1520+
1521+
BOOST_AUTO_TEST_CASE(byte_flipper__write_line__default__expected)
1522+
{
1523+
std::stringstream stream;
1524+
flip::bytes::iostream writer(stream);
1525+
writer.write_line();
1526+
BOOST_REQUIRE_EQUAL(stream.str(), "\r\n");
1527+
BOOST_REQUIRE(writer);
1528+
}
1529+
1530+
BOOST_AUTO_TEST_CASE(byte_flipper__write_line__empty_default__expected)
1531+
{
1532+
std::stringstream stream;
1533+
flip::bytes::iostream writer(stream);
1534+
writer.write_line("");
1535+
BOOST_REQUIRE_EQUAL(stream.str(), "\r\n");
1536+
BOOST_REQUIRE(writer);
1537+
}
1538+
1539+
BOOST_AUTO_TEST_CASE(byte_flipper__write_line__empty_empty__expected)
1540+
{
1541+
std::stringstream stream;
1542+
flip::bytes::iostream writer(stream);
1543+
writer.write_line("", "");
1544+
BOOST_REQUIRE(stream.str().empty());
1545+
BOOST_REQUIRE(writer);
1546+
}
1547+
1548+
BOOST_AUTO_TEST_CASE(byte_flipper__write_line__value_empty__expected)
1549+
{
1550+
std::stringstream stream;
1551+
flip::bytes::iostream writer(stream);
1552+
const std::string expected{ "abcdefghijklmnopqrstuvwxyz" };
1553+
writer.write_line(expected, "");
1554+
BOOST_REQUIRE_EQUAL(stream.str(), expected);
1555+
BOOST_REQUIRE(writer);
1556+
}
1557+
1558+
BOOST_AUTO_TEST_CASE(byte_flipper__write_line__value_explicit__expected)
1559+
{
1560+
std::stringstream stream;
1561+
flip::bytes::iostream writer(stream);
1562+
const std::string expected{ "abcdefghijklmnopqrstuvwxyz" };
1563+
writer.write_line(expected, "|");
1564+
BOOST_REQUIRE_EQUAL(stream.str(), expected + "|");
1565+
BOOST_REQUIRE(writer);
1566+
}
1567+
1568+
BOOST_AUTO_TEST_CASE(byte_flipper__write_line__value_default__expected)
1569+
{
1570+
std::stringstream stream;
1571+
flip::bytes::iostream writer(stream);
1572+
const std::string expected{ "abcdefghijklmnopqrstuvwxyz" };
1573+
writer.write_line(expected);
1574+
BOOST_REQUIRE_EQUAL(stream.str(), expected + "\r\n");
1575+
BOOST_REQUIRE(writer);
1576+
}
1577+
15191578
#endif // BYTE_FLIPPER_WRITER_STRINGS
15201579

15211580
#endif // BYTE_FLIPPER_WRITER

test/stream/streamers/byte_writer.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,65 @@ BOOST_AUTO_TEST_CASE(byte_writer__write_string_buffer__value__expected)
565565
BOOST_REQUIRE(writer);
566566
}
567567

568+
// write_line
569+
570+
BOOST_AUTO_TEST_CASE(byte_writer__write_line__default__expected)
571+
{
572+
std::ostringstream stream;
573+
write::bytes::ostream writer(stream);
574+
writer.write_line();
575+
BOOST_REQUIRE_EQUAL(stream.str(), "\r\n");
576+
BOOST_REQUIRE(writer);
577+
}
578+
579+
BOOST_AUTO_TEST_CASE(byte_writer__write_line__empty_default__expected)
580+
{
581+
std::ostringstream stream;
582+
write::bytes::ostream writer(stream);
583+
writer.write_line("");
584+
BOOST_REQUIRE_EQUAL(stream.str(), "\r\n");
585+
BOOST_REQUIRE(writer);
586+
}
587+
588+
BOOST_AUTO_TEST_CASE(byte_writer__write_line__empty_empty__expected)
589+
{
590+
std::ostringstream stream;
591+
write::bytes::ostream writer(stream);
592+
writer.write_line("", "");
593+
BOOST_REQUIRE(stream.str().empty());
594+
BOOST_REQUIRE(writer);
595+
}
596+
597+
BOOST_AUTO_TEST_CASE(byte_writer__write_line__value_empty__expected)
598+
{
599+
std::ostringstream stream;
600+
write::bytes::ostream writer(stream);
601+
const std::string expected{ "abcdefghijklmnopqrstuvwxyz" };
602+
writer.write_line(expected, "");
603+
BOOST_REQUIRE_EQUAL(stream.str(), expected);
604+
BOOST_REQUIRE(writer);
605+
}
606+
607+
BOOST_AUTO_TEST_CASE(byte_writer__write_line__value_explicit__expected)
608+
{
609+
std::ostringstream stream;
610+
write::bytes::ostream writer(stream);
611+
const std::string expected{ "abcdefghijklmnopqrstuvwxyz" };
612+
writer.write_line(expected, "|");
613+
BOOST_REQUIRE_EQUAL(stream.str(), expected + "|");
614+
BOOST_REQUIRE(writer);
615+
}
616+
617+
BOOST_AUTO_TEST_CASE(byte_writer__write_line__value_default__expected)
618+
{
619+
std::ostringstream stream;
620+
write::bytes::ostream writer(stream);
621+
const std::string expected{ "abcdefghijklmnopqrstuvwxyz" };
622+
writer.write_line(expected);
623+
BOOST_REQUIRE_EQUAL(stream.str(), expected + "\r\n");
624+
BOOST_REQUIRE(writer);
625+
}
626+
568627
#endif // BYTE_WRITER_STRINGS
569628

570629
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)