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