xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/initializer_list_assignment.pass.cpp (revision 9ed20568e7de53dce85f1631d7d8c1415e7930ae)
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
10 
11 // <string>
12 
13 // basic_string& operator=(initializer_list<charT> il); // constexpr since C++20
14 
15 #include <string>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 #include "min_allocator.h"
20 #include "asan_testing.h"
21 
22 // clang-format off
23 template <template <class> class Alloc>
test_string()24 TEST_CONSTEXPR_CXX20 void test_string() {
25   {
26     typedef std::basic_string<char, std::char_traits<char>, Alloc<char>> S;
27     S s;
28     S& result = (s = {'a', 'b', 'c'});
29     assert(s == "abc");
30     assert(&result == &s);
31     LIBCPP_ASSERT(is_string_asan_correct(s));
32     LIBCPP_ASSERT(is_string_asan_correct(result));
33   }
34   {
35     typedef std::basic_string<char, std::char_traits<char>, Alloc<char>> S;
36     S s;
37     s = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
38          'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'};
39     assert(s == "aaaaaaaaaaaaaaaaaaaaaaaa");
40     LIBCPP_ASSERT(is_string_asan_correct(s));
41   }
42 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
43   {
44     typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, Alloc<wchar_t>> S;
45     S s;
46     S& result = (s = {L'a', L'b', L'c'});
47     assert(s == L"abc");
48     assert(&result == &s);
49     LIBCPP_ASSERT(is_string_asan_correct(s));
50     LIBCPP_ASSERT(is_string_asan_correct(result));
51   }
52   {
53     typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, Alloc<wchar_t>> S;
54     S s;
55     S& result = (s = {L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a', L'a'});
56     assert(s == L"aaaaaaaaaaaaaaaaaaaaaaaaa");
57     assert(&result == &s);
58     LIBCPP_ASSERT(is_string_asan_correct(s));
59     LIBCPP_ASSERT(is_string_asan_correct(result));
60   }
61 #endif
62 }
63 // clang-format on
64 
test()65 TEST_CONSTEXPR_CXX20 bool test() {
66   test_string<std::allocator>();
67   test_string<min_allocator>();
68   test_string<safe_allocator>();
69 
70   return true;
71 }
72 
main(int,char **)73 int main(int, char**) {
74   test();
75 #if TEST_STD_VER > 17
76   static_assert(test());
77 #endif
78 
79   return 0;
80 }
81