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 //===----------------------------------------------------------------------===//
8
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10
11 // <sstream>
12
13 // template <class charT, class traits, class Allocator>
14 // void swap(basic_stringbuf<charT, traits, Allocator>& x,
15 // basic_stringbuf<charT, traits, Allocator>& y)
16 // noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
17 // allocator_traits<allocator_type>::is_always_equal::value);
18
19 #include <sstream>
20 #include <cassert>
21
22 #include "test_macros.h"
23
24 template <class T>
25 struct test_alloc {
26 using value_type = T;
27
allocatetest_alloc28 [[nodiscard]] constexpr T* allocate(std::size_t) { return nullptr; }
deallocatetest_alloc29 void deallocate(void*, std::size_t) {}
30 };
31
32 template <class T>
33 struct test_alloc_propagate_on_container_swap : test_alloc<T> {
34 using propagate_on_container_swap = std::true_type;
35 };
36
37 template <class T>
38 struct test_alloc_is_always_equal : test_alloc<T> {
39 using is_always_equal = std::true_type;
40 };
41
42 template <class T>
43 struct test_alloc_propagate_on_container_swap_is_always_equal : test_alloc<T> {
44 using propagate_on_container_swap = std::true_type;
45 using is_always_equal = std::true_type;
46 };
47
48 template <class T>
49 struct test_alloc_not_empty : test_alloc<T> {
50 bool dummy;
51 };
52
53 template <class T>
54 struct test_alloc_propagate_on_container_swap_not_empty : test_alloc<T> {
55 using propagate_on_container_swap = std::true_type;
56 bool dummy;
57 };
58
59 template <class CharT>
test()60 static void test() {
61 {
62 std::basic_stringbuf<CharT, std::char_traits<CharT>, test_alloc<CharT>> buf1;
63 std::basic_stringbuf<CharT, std::char_traits<CharT>, test_alloc<CharT>> buf;
64 static_assert(noexcept(swap(buf, buf1)));
65 }
66 {
67 std::basic_stringbuf<CharT, std::char_traits<CharT>, test_alloc_propagate_on_container_swap<CharT>> buf1;
68 std::basic_stringbuf<CharT, std::char_traits<CharT>, test_alloc_propagate_on_container_swap<CharT>> buf;
69 static_assert(noexcept(swap(buf, buf1)));
70 }
71 {
72 std::basic_stringbuf<CharT, std::char_traits<CharT>, test_alloc_is_always_equal<CharT>> buf1;
73 std::basic_stringbuf<CharT, std::char_traits<CharT>, test_alloc_is_always_equal<CharT>> buf;
74 static_assert(noexcept(swap(buf, buf1)));
75 }
76 {
77 std::basic_stringbuf<CharT, std::char_traits<CharT>, test_alloc_propagate_on_container_swap_is_always_equal<CharT>>
78 buf1;
79 std::basic_stringbuf<CharT, std::char_traits<CharT>, test_alloc_propagate_on_container_swap_is_always_equal<CharT>>
80 buf;
81 static_assert(noexcept(swap(buf, buf1)));
82 }
83 {
84 std::basic_stringbuf<CharT, std::char_traits<CharT>, test_alloc_not_empty<CharT>> buf1;
85 std::basic_stringbuf<CharT, std::char_traits<CharT>, test_alloc_not_empty<CharT>> buf;
86 LIBCPP_STATIC_ASSERT(!noexcept(swap(buf, buf1)));
87 }
88 {
89 std::basic_stringbuf<CharT, std::char_traits<CharT>, test_alloc_propagate_on_container_swap_not_empty<CharT>> buf1;
90 std::basic_stringbuf<CharT, std::char_traits<CharT>, test_alloc_propagate_on_container_swap_not_empty<CharT>> buf;
91 static_assert(noexcept(swap(buf, buf1)));
92 }
93 }
94
main(int,char **)95 int main(int, char**) {
96 test<char>();
97 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
98 test<wchar_t>();
99 #endif
100 return 0;
101 }
102