xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/size_char.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 // <string>
10 
11 // basic_string<charT,traits,Allocator>&
12 //   assign(size_type n, charT c); // constexpr since C++20
13 
14 #include <string>
15 #include <cassert>
16 
17 #include "test_macros.h"
18 #include "min_allocator.h"
19 #include "asan_testing.h"
20 
21 template <class S>
test(S s,typename S::size_type n,typename S::value_type c,S expected)22 TEST_CONSTEXPR_CXX20 void test(S s, typename S::size_type n, typename S::value_type c, S expected) {
23   s.assign(n, c);
24   LIBCPP_ASSERT(s.__invariants());
25   assert(s == expected);
26   LIBCPP_ASSERT(is_string_asan_correct(s));
27 }
28 
29 template <class S>
test_string()30 TEST_CONSTEXPR_CXX20 void test_string() {
31   test(S(), 0, 'a', S());
32   test(S(), 1, 'a', S(1, 'a'));
33   test(S(), 10, 'a', S(10, 'a'));
34   test(S(), 100, 'a', S(100, 'a'));
35 
36   test(S("12345"), 0, 'a', S());
37   test(S("12345"), 1, 'a', S(1, 'a'));
38   test(S("12345"), 10, 'a', S(10, 'a'));
39 
40   test(S("12345678901234567890"), 0, 'a', S());
41   test(S("12345678901234567890"), 1, 'a', S(1, 'a'));
42   test(S("12345678901234567890"), 10, 'a', S(10, 'a'));
43 }
44 
test()45 TEST_CONSTEXPR_CXX20 bool test() {
46   test_string<std::string>();
47 #if TEST_STD_VER >= 11
48   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
49   test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>();
50 #endif
51 
52   return true;
53 }
54 
main(int,char **)55 int main(int, char**) {
56   test();
57 #if TEST_STD_VER > 17
58   static_assert(test());
59 #endif
60 
61   return 0;
62 }
63