diff options
author | Nikolas Klauser <nikolasklauser@berlin.de> | 2022-04-08 12:20:56 +0200 |
---|---|---|
committer | Nikolas Klauser <nikolasklauser@berlin.de> | 2022-04-08 12:21:43 +0200 |
commit | 628fcfd5204cf87a733ede91a648bed4c20eb21a (patch) | |
tree | a102d8c010b5244bdb15bfad103e994d001ffbe3 | |
parent | 492c5c05e111b81e2e7f1faa69756082fb826085 (diff) | |
download | llvm-628fcfd5204cf87a733ede91a648bed4c20eb21a.zip llvm-628fcfd5204cf87a733ede91a648bed4c20eb21a.tar.gz llvm-628fcfd5204cf87a733ede91a648bed4c20eb21a.tar.bz2 |
[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
-rw-r--r-- | libcxx/test/std/strings/basic.string/string.cons/default.pass.cpp | 43 | ||||
-rw-r--r-- | libcxx/test/std/strings/basic.string/string.cons/default_noexcept.pass.cpp | 40 | ||||
-rw-r--r-- | libcxx/test/std/strings/basic.string/string.cons/dtor.pass.cpp (renamed from libcxx/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp) | 36 |
3 files changed, 65 insertions, 54 deletions
diff --git a/libcxx/test/std/strings/basic.string/string.cons/default.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/default.pass.cpp new file mode 100644 index 0000000..812bc71 --- /dev/null +++ b/libcxx/test/std/strings/basic.string/string.cons/default.pass.cpp @@ -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; +} diff --git a/libcxx/test/std/strings/basic.string/string.cons/default_noexcept.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/default_noexcept.pass.cpp deleted file mode 100644 index d551b59..0000000 --- a/libcxx/test/std/strings/basic.string/string.cons/default_noexcept.pass.cpp +++ /dev/null @@ -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; -} diff --git a/libcxx/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/dtor.pass.cpp index 83d2a92..1b49881 100644 --- a/libcxx/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.cons/dtor.pass.cpp @@ -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; } |