diff options
Diffstat (limited to 'libadhocutil/unittests/testBuffer.cpp')
-rw-r--r-- | libadhocutil/unittests/testBuffer.cpp | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/libadhocutil/unittests/testBuffer.cpp b/libadhocutil/unittests/testBuffer.cpp new file mode 100644 index 0000000..5d64642 --- /dev/null +++ b/libadhocutil/unittests/testBuffer.cpp @@ -0,0 +1,119 @@ +#define BOOST_TEST_MODULE Buffer +#include <boost/test/unit_test.hpp> + +#include "buffer.h" + +BOOST_AUTO_TEST_CASE ( create ) +{ + Buffer a; + Buffer b("const"); + auto nonconst = (char*)malloc(9); + strcpy(nonconst, "nonconst"); + Buffer c(nonconst, true); + Buffer d(nonconst, false); + + BOOST_REQUIRE_EQUAL(false, a); + BOOST_REQUIRE_EQUAL(true, !a); + BOOST_REQUIRE_EQUAL(0, a.length()); + BOOST_REQUIRE_EQUAL("", a.str()); + + BOOST_REQUIRE_EQUAL(true, b); + BOOST_REQUIRE_EQUAL(false, !b); + BOOST_REQUIRE_EQUAL(5, b.length()); + BOOST_REQUIRE_EQUAL("const", b.str()); + + BOOST_REQUIRE_EQUAL(8, c.length()); + BOOST_REQUIRE_EQUAL("nonconst", c.str()); + + BOOST_REQUIRE_EQUAL(8, d.length()); + BOOST_REQUIRE_EQUAL("nonconst", d.str()); + + nonconst[0] = 'N'; + BOOST_REQUIRE_EQUAL("nonconst", c.str()); + BOOST_REQUIRE_EQUAL("Nonconst", d.str()); +} + +BOOST_AUTO_TEST_CASE( writestream ) +{ + std::stringstream buf; + Buffer b; + b.append("Some text.").append("And then ").append("some more."); + buf << b; + BOOST_REQUIRE_EQUAL("Some text.And then some more.", buf.str()); +} + +BOOST_AUTO_TEST_CASE( appendempty ) +{ + Buffer b; + // These should not add content + b.append(""); + BOOST_REQUIRE(!b); + b.append(std::string()); + BOOST_REQUIRE(!b); + b.appendf("%s", ""); + BOOST_REQUIRE(!b); + b.appendbf("%s", ""); + BOOST_REQUIRE(!b); +} + +BOOST_AUTO_TEST_CASE( appendthings ) +{ + Buffer b; + b.append("string a") + .append(std::string(" b")) + .appendf(" num %d", 1) + .appendbf(" num %d", 2); + BOOST_REQUIRE_EQUAL(22, b.length()); + BOOST_REQUIRE_EQUAL("string a b num 1 num 2", b.str()); + const char * cstring = b; + BOOST_REQUIRE_EQUAL(22, strlen(cstring)); + BOOST_REQUIRE_EQUAL("string a b num 1 num 2", cstring); +} + +BOOST_AUTO_TEST_CASE( writeto ) +{ + Buffer b; + b.append("string a") + .append(std::string(" b")) + .appendf(" num %d", 1) + .appendbf(" num %d", 2); + char buf[23]; + b.writeto(buf, 23, 0); + BOOST_REQUIRE_EQUAL(0, memcmp(buf, "string a b num 1 num 2", 23)); +} + +BOOST_AUTO_TEST_CASE( operators ) +{ + auto expected = "cstringstd::string"; + Buffer a; + Buffer b; + a += "cstring"; + a += std::string("std::string"); + BOOST_REQUIRE_EQUAL(expected, a.str()); + b = a; + BOOST_REQUIRE_EQUAL(expected, a.str()); + BOOST_REQUIRE_EQUAL(expected, b.str()); + + Buffer c; + c = expected; + BOOST_REQUIRE_EQUAL(expected, c.str()); + + Buffer d; + d = std::string(expected); + BOOST_REQUIRE_EQUAL(expected, d.str()); +} + +BOOST_AUTO_TEST_CASE( clear ) +{ + Buffer b("some"); + BOOST_REQUIRE(b); + b.clear(); + BOOST_REQUIRE(!b); +} + +BOOST_AUTO_TEST_CASE( replacesstringbf ) +{ + auto str = Buffer().appendbf("something %d", 1234).str(); + BOOST_REQUIRE_EQUAL("something 1234", str); +} + |