mirror of
https://github.com/boostorg/url.git
synced 2026-02-15 01:22:11 +00:00
refactor const_string
This commit is contained in:
@@ -10,33 +10,189 @@
|
||||
// Test that header file is self-contained.
|
||||
#include <boost/url/const_string.hpp>
|
||||
|
||||
#include <boost/url/static_pool.hpp>
|
||||
|
||||
#include "test_suite.hpp"
|
||||
|
||||
namespace boost {
|
||||
namespace urls {
|
||||
|
||||
#ifdef BOOST_URL_HAS_STRING_VIEW
|
||||
BOOST_STATIC_ASSERT(is_stringlike<
|
||||
std::string_view>::value);
|
||||
#endif
|
||||
|
||||
class string_test
|
||||
struct const_string_test
|
||||
{
|
||||
public:
|
||||
using A = std::allocator<char>;
|
||||
|
||||
void
|
||||
testConstString()
|
||||
{
|
||||
// const_string()
|
||||
// ~const_string()
|
||||
{
|
||||
const_string s;
|
||||
BOOST_TEST(s == "");
|
||||
BOOST_TEST(s.empty());
|
||||
|
||||
// same buffer
|
||||
BOOST_TEST(
|
||||
const_string().data() ==
|
||||
const_string().data());
|
||||
}
|
||||
|
||||
// const_string(string_view, InitFn)
|
||||
// ~const_string()
|
||||
{
|
||||
const_string s(3, A{},
|
||||
[](std::size_t, char* dest)
|
||||
{
|
||||
dest[0] = 'a';
|
||||
dest[1] = 'b';
|
||||
dest[2] = 'c';
|
||||
});
|
||||
BOOST_TEST(s == "abc");
|
||||
BOOST_TEST(! s.empty());
|
||||
}
|
||||
|
||||
// const_string(string_view, Allocator)
|
||||
// ~const_string()
|
||||
{
|
||||
char const* const x = "x";
|
||||
const_string s(x, A{});
|
||||
BOOST_TEST(s == "x");
|
||||
BOOST_TEST(! s.empty());
|
||||
|
||||
// different buffer
|
||||
BOOST_TEST(s.data() != x);
|
||||
}
|
||||
|
||||
// const_string(const_string const&)
|
||||
{
|
||||
const_string s1("abc", A{});
|
||||
const_string s2(s1);
|
||||
BOOST_TEST(s1 == "abc");
|
||||
BOOST_TEST(s2 == "abc");
|
||||
BOOST_TEST(s2 == s1);
|
||||
|
||||
// same buffer
|
||||
BOOST_TEST(s1.data() == s2.data());
|
||||
}
|
||||
|
||||
// operator=(const_string const&)
|
||||
{
|
||||
const_string s1("abc", A{});
|
||||
const_string s2;
|
||||
BOOST_TEST(s1 == "abc");
|
||||
BOOST_TEST(s2.empty());
|
||||
s2 = s1;
|
||||
BOOST_TEST(s1 == "abc");
|
||||
BOOST_TEST(s2 == "abc");
|
||||
BOOST_TEST(s2 == s1);
|
||||
|
||||
// same buffer
|
||||
BOOST_TEST(s1.data() == s2.data());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testFactory()
|
||||
{
|
||||
using factory = const_string::factory;
|
||||
|
||||
// factory()
|
||||
// ~factory()
|
||||
{
|
||||
factory a;
|
||||
}
|
||||
|
||||
// factory(std::allocator)
|
||||
// ~factory()
|
||||
{
|
||||
factory a(A{});
|
||||
}
|
||||
|
||||
// factory(Allocator)
|
||||
// ~factory()
|
||||
{
|
||||
static_pool<100> sp;
|
||||
factory a(sp.allocator());
|
||||
}
|
||||
|
||||
// operator()(size_t, InitFn)
|
||||
{
|
||||
{
|
||||
const_string s;
|
||||
{
|
||||
factory a;
|
||||
s = a(3,
|
||||
[](std::size_t, char* dest)
|
||||
{
|
||||
dest[0] = 'a';
|
||||
dest[1] = 'b';
|
||||
dest[2] = 'c';
|
||||
});
|
||||
}
|
||||
BOOST_TEST(s == "abc");
|
||||
BOOST_TEST(! s.empty());
|
||||
}
|
||||
{
|
||||
static_pool<100> sp;
|
||||
const_string s;
|
||||
{
|
||||
factory a(sp.allocator());
|
||||
s = a(3,
|
||||
[](std::size_t, char* dest)
|
||||
{
|
||||
dest[0] = 'a';
|
||||
dest[1] = 'b';
|
||||
dest[2] = 'c';
|
||||
});
|
||||
}
|
||||
BOOST_TEST(s == "abc");
|
||||
BOOST_TEST(! s.empty());
|
||||
}
|
||||
}
|
||||
|
||||
// operator()(string_view)
|
||||
{
|
||||
const char* const abc = "abc";
|
||||
|
||||
{
|
||||
const_string s;
|
||||
{
|
||||
factory a;
|
||||
s = a(abc);
|
||||
}
|
||||
BOOST_TEST(s == "abc");
|
||||
BOOST_TEST(! s.empty());
|
||||
|
||||
// different buffer
|
||||
BOOST_TEST(s.data() != abc);
|
||||
}
|
||||
{
|
||||
static_pool<100> sp;
|
||||
const_string s;
|
||||
{
|
||||
factory a(sp.allocator());
|
||||
s = a(abc);
|
||||
}
|
||||
BOOST_TEST(s == "abc");
|
||||
BOOST_TEST(! s.empty());
|
||||
|
||||
// different buffer
|
||||
BOOST_TEST(s.data() != abc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
run()
|
||||
{
|
||||
const_string sv("hello");
|
||||
auto sv2 = sv;
|
||||
BOOST_TEST(sv2 == sv);
|
||||
sv = {};
|
||||
BOOST_TEST(sv2 != sv);
|
||||
testConstString();
|
||||
testFactory();
|
||||
}
|
||||
};
|
||||
|
||||
TEST_SUITE(
|
||||
string_test,
|
||||
"boost.url.string");
|
||||
const_string_test,
|
||||
"boost.url.const_string");
|
||||
|
||||
} // urls
|
||||
} // boost
|
||||
|
||||
Reference in New Issue
Block a user