[libc][NFC] Remove extra ; in exhaustive_test.h. (#124216)
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.cons / dtor.pass.cpp
blobe9f174068473d55011024aea7935c313ca789066
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03
11 // <string>
13 // ~basic_string() // implied noexcept; // constexpr since C++20
15 #include <string>
16 #include <cassert>
18 #include "test_macros.h"
19 #include "test_allocator.h"
21 template <class T>
22 struct throwing_alloc {
23 typedef T value_type;
24 throwing_alloc(const throwing_alloc&);
25 T* allocate(std::size_t);
26 ~throwing_alloc() noexcept(false);
29 // Test that it's possible to take the address of basic_string's destructors
30 // by creating globals which will register their destructors with cxa_atexit.
31 std::string unused_string;
32 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
33 std::wstring unused_wide_string;
34 #endif
36 static_assert(std::is_nothrow_destructible<std::string>::value, "");
37 static_assert(
38 std::is_nothrow_destructible< std::basic_string<char, std::char_traits<char>, test_allocator<char>>>::value, "");
39 LIBCPP_STATIC_ASSERT(
40 !std::is_nothrow_destructible< std::basic_string<char, std::char_traits<char>, throwing_alloc<char>>>::value, "");
42 TEST_CONSTEXPR_CXX20 bool test() {
43 test_allocator_statistics alloc_stats;
45 std::basic_string<char, std::char_traits<char>, test_allocator<char>> str2((test_allocator<char>(&alloc_stats)));
46 str2 = "long long string so no SSO";
47 assert(alloc_stats.alloc_count > 0);
48 LIBCPP_ASSERT(alloc_stats.alloc_count == 1);
50 assert(alloc_stats.alloc_count == 0);
52 return true;
55 int main(int, char**) {
56 test();
57 #if TEST_STD_VER > 17
58 static_assert(test());
59 #endif
61 return 0;