[libc++] Refactor string unit tests to ease addition of new allocators
While doing this, I also found a few tests that were either clearly incorrect (e.g. testing the wrong function) or that lacked basic test coverage like testing std::string itself (e.g. the test was only checking std::basic_string with a custom allocator). In these cases, I did a few conservative drive-by changes. Differential Revision: https://reviews.llvm.org/D140550 Co-authored-by: Brendan Emery <brendan.emery@esrlabs.com>
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
#include "test_macros.h"
|
||||
|
||||
template <class S>
|
||||
TEST_CONSTEXPR_CXX20 void test(S s, test_allocator_statistics& alloc_stats) {
|
||||
TEST_CONSTEXPR_CXX20 void test_invariant(S s, test_allocator_statistics& alloc_stats) {
|
||||
alloc_stats.throw_after = 0;
|
||||
#ifndef TEST_HAS_NO_EXCEPTIONS
|
||||
try
|
||||
@@ -37,33 +37,52 @@ TEST_CONSTEXPR_CXX20 void test(S s, test_allocator_statistics& alloc_stats) {
|
||||
alloc_stats.throw_after = INT_MAX;
|
||||
}
|
||||
|
||||
template <class Alloc>
|
||||
TEST_CONSTEXPR_CXX20 void test_string(const Alloc& a) {
|
||||
using S = std::basic_string<char, std::char_traits<char>, Alloc>;
|
||||
{
|
||||
S const s((Alloc(a)));
|
||||
assert(s.capacity() >= 0);
|
||||
}
|
||||
{
|
||||
S const s(3, 'x', Alloc(a));
|
||||
assert(s.capacity() >= 3);
|
||||
}
|
||||
#if TEST_STD_VER >= 11
|
||||
// Check that we perform SSO
|
||||
{
|
||||
S const s;
|
||||
assert(s.capacity() > 0);
|
||||
ASSERT_NOEXCEPT(s.capacity());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CONSTEXPR_CXX20 bool test() {
|
||||
test_string(std::allocator<char>());
|
||||
test_string(test_allocator<char>());
|
||||
test_string(test_allocator<char>(3));
|
||||
test_string(min_allocator<char>());
|
||||
|
||||
{
|
||||
test_allocator_statistics alloc_stats;
|
||||
typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > S;
|
||||
S s((test_allocator<char>(&alloc_stats)));
|
||||
test(s, alloc_stats);
|
||||
test_invariant(s, alloc_stats);
|
||||
s.assign(10, 'a');
|
||||
s.erase(5);
|
||||
test(s, alloc_stats);
|
||||
test_invariant(s, alloc_stats);
|
||||
s.assign(100, 'a');
|
||||
s.erase(50);
|
||||
test(s, alloc_stats);
|
||||
test_invariant(s, alloc_stats);
|
||||
}
|
||||
#if TEST_STD_VER >= 11
|
||||
{
|
||||
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
|
||||
S s;
|
||||
assert(s.capacity() > 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
#if TEST_STD_VER > 17
|
||||
#if TEST_STD_VER >= 20
|
||||
static_assert(test());
|
||||
#endif
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
#include "min_allocator.h"
|
||||
|
||||
template <class S>
|
||||
TEST_CONSTEXPR_CXX20 void test1(const S& s) {
|
||||
TEST_CONSTEXPR_CXX20 void test_resize_max_size_minus_1(const S& s) {
|
||||
S s2(s);
|
||||
const std::size_t sz = s2.max_size() - 1;
|
||||
try {
|
||||
@@ -38,7 +38,7 @@ TEST_CONSTEXPR_CXX20 void test1(const S& s) {
|
||||
}
|
||||
|
||||
template <class S>
|
||||
TEST_CONSTEXPR_CXX20 void test2(const S& s) {
|
||||
TEST_CONSTEXPR_CXX20 void test_resize_max_size(const S& s) {
|
||||
S s2(s);
|
||||
const std::size_t sz = s2.max_size();
|
||||
try {
|
||||
@@ -49,45 +49,50 @@ TEST_CONSTEXPR_CXX20 void test2(const S& s) {
|
||||
assert(s.size() == sz);
|
||||
}
|
||||
|
||||
template <class S>
|
||||
TEST_CONSTEXPR_CXX20 void test(const S& s) {
|
||||
assert(s.max_size() >= s.size());
|
||||
test1(s);
|
||||
test2(s);
|
||||
}
|
||||
|
||||
template <class S>
|
||||
TEST_CONSTEXPR_CXX20 void test_string() {
|
||||
test(S());
|
||||
test(S("123"));
|
||||
test(S("12345678901234567890123456789012345678901234567890"));
|
||||
{
|
||||
S s;
|
||||
assert(s.max_size() >= s.size());
|
||||
assert(s.max_size() > 0);
|
||||
if (!TEST_IS_CONSTANT_EVALUATED) {
|
||||
test_resize_max_size_minus_1(s);
|
||||
test_resize_max_size(s);
|
||||
}
|
||||
}
|
||||
{
|
||||
S s("123");
|
||||
assert(s.max_size() >= s.size());
|
||||
assert(s.max_size() > 0);
|
||||
if (!TEST_IS_CONSTANT_EVALUATED) {
|
||||
test_resize_max_size_minus_1(s);
|
||||
test_resize_max_size(s);
|
||||
}
|
||||
}
|
||||
{
|
||||
S s("12345678901234567890123456789012345678901234567890");
|
||||
assert(s.max_size() >= s.size());
|
||||
assert(s.max_size() > 0);
|
||||
if (!TEST_IS_CONSTANT_EVALUATED) {
|
||||
test_resize_max_size_minus_1(s);
|
||||
test_resize_max_size(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CONSTEXPR_CXX20 bool test() {
|
||||
test_string<std::string>();
|
||||
#if TEST_STD_VER >= 11
|
||||
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
|
||||
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#if TEST_STD_VER > 17
|
||||
constexpr bool test_constexpr() {
|
||||
std::string str;
|
||||
|
||||
std::size_t size = str.max_size();
|
||||
assert(size > 0);
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
#if TEST_STD_VER > 17
|
||||
test_constexpr();
|
||||
static_assert(test_constexpr());
|
||||
#if TEST_STD_VER >= 20
|
||||
static_assert(test());
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -57,19 +57,18 @@ constexpr void test_truncating(std::size_t o, size_t N) {
|
||||
assert(s.c_str()[N] == '\0');
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
template <class String>
|
||||
constexpr bool test() {
|
||||
using S = std::basic_string<CharT>;
|
||||
test_appending<S>(10, 15, 15);
|
||||
test_appending<S>(10, 15, 20);
|
||||
test_appending<S>(10, 40, 40);
|
||||
test_appending<S>(10, 40, 50);
|
||||
test_appending<S>(30, 35, 35);
|
||||
test_appending<S>(30, 35, 45);
|
||||
test_appending<S>(10, 15, 30);
|
||||
test_truncating<S>(15, 10);
|
||||
test_truncating<S>(40, 35);
|
||||
test_truncating<S>(40, 10);
|
||||
test_appending<String>(10, 15, 15);
|
||||
test_appending<String>(10, 15, 20);
|
||||
test_appending<String>(10, 40, 40);
|
||||
test_appending<String>(10, 40, 50);
|
||||
test_appending<String>(30, 35, 35);
|
||||
test_appending<String>(30, 35, 45);
|
||||
test_appending<String>(10, 15, 30);
|
||||
test_truncating<String>(15, 10);
|
||||
test_truncating<String>(40, 35);
|
||||
test_truncating<String>(40, 10);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -85,19 +84,19 @@ void test_value_categories() {
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test<char>();
|
||||
test<char8_t>();
|
||||
test<char16_t>();
|
||||
test<char32_t>();
|
||||
test<std::basic_string<char, std::char_traits<char>, std::allocator<char>>>();
|
||||
test<std::basic_string<char8_t, std::char_traits<char8_t>, std::allocator<char8_t>>>();
|
||||
test<std::basic_string<char16_t, std::char_traits<char16_t>, std::allocator<char16_t>>>();
|
||||
test<std::basic_string<char32_t, std::char_traits<char32_t>, std::allocator<char32_t>>>();
|
||||
|
||||
static_assert(test<char>());
|
||||
static_assert(test<char8_t>());
|
||||
static_assert(test<char16_t>());
|
||||
static_assert(test<char32_t>());
|
||||
static_assert(test<std::basic_string<char, std::char_traits<char>, std::allocator<char>>>());
|
||||
static_assert(test<std::basic_string<char8_t, std::char_traits<char8_t>, std::allocator<char8_t>>>());
|
||||
static_assert(test<std::basic_string<char16_t, std::char_traits<char16_t>, std::allocator<char16_t>>>());
|
||||
static_assert(test<std::basic_string<char32_t, std::char_traits<char32_t>, std::allocator<char32_t>>>());
|
||||
|
||||
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
|
||||
test<wchar_t>();
|
||||
static_assert(test<wchar_t>());
|
||||
test<std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>>();
|
||||
static_assert(test<std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>>());
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user