[libc++] Add tests for std::string default constructor and destructor

Reviewed By: ldionne, var-const, #libc, nilayvaish

Spies: nilayvaish, libcxx-commits

Differential Revision: https://reviews.llvm.org/D123129
This commit is contained in:
Nikolas Klauser 2022-04-08 12:20:56 +02:00
parent 492c5c05e1
commit 628fcfd520
3 changed files with 65 additions and 54 deletions

View File

@ -0,0 +1,43 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string() noexcept(is_nothrow_default_constructible<allocator_type>::value);
#include <cassert>
#include <string>
#include "test_macros.h"
#include "test_allocator.h"
#if TEST_STD_VER >= 11
// Test the noexcept specification, which is a conforming extension
LIBCPP_STATIC_ASSERT(std::is_nothrow_default_constructible<std::string>::value, "");
LIBCPP_STATIC_ASSERT(std::is_nothrow_default_constructible<
std::basic_string<char, std::char_traits<char>, test_allocator<char>>>::value, "");
LIBCPP_STATIC_ASSERT(!std::is_nothrow_default_constructible<
std::basic_string<char, std::char_traits<char>, limited_allocator<char, 10>>>::value, "");
#endif
bool test() {
std::string str;
assert(str.empty());
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
#endif
return 0;
}

View File

@ -1,40 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <string>
// basic_string()
// noexcept(is_nothrow_default_constructible<allocator_type>::value);
// This tests a conforming extension
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
int main(int, char**)
{
{
typedef std::string C;
static_assert(std::is_nothrow_default_constructible<C>::value, "");
}
{
typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> C;
static_assert(std::is_nothrow_default_constructible<C>::value, "");
}
{
typedef std::basic_string<char, std::char_traits<char>, limited_allocator<char, 10>> C;
static_assert(!std::is_nothrow_default_constructible<C>::value, "");
}
return 0;
}

View File

@ -34,22 +34,30 @@ std::string s;
std::wstring ws;
#endif
static_assert(std::is_nothrow_destructible<std::string>::value, "");
static_assert(std::is_nothrow_destructible<
std::basic_string<char, std::char_traits<char>, test_allocator<char>>>::value, "");
LIBCPP_STATIC_ASSERT(!std::is_nothrow_destructible<
std::basic_string<char, std::char_traits<char>, throwing_alloc<char>>>::value, "");
bool test() {
test_allocator_statistics alloc_stats;
{
std::basic_string<char, std::char_traits<char>, test_allocator<char>> str2((test_allocator<char>(&alloc_stats)));
str2 = "long long string so no SSO";
assert(alloc_stats.alloc_count == 1);
}
assert(alloc_stats.alloc_count == 0);
return true;
}
int main(int, char**)
{
{
typedef std::string C;
static_assert(std::is_nothrow_destructible<C>::value, "");
}
{
typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> C;
static_assert(std::is_nothrow_destructible<C>::value, "");
}
#if defined(_LIBCPP_VERSION)
{
typedef std::basic_string<char, std::char_traits<char>, throwing_alloc<char>> C;
static_assert(!std::is_nothrow_destructible<C>::value, "");
}
#endif // _LIBCPP_VERSION
test();
#if TEST_STD_VER > 17
// static_assert(test());
#endif
return 0;
}