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(const charT* s, const Allocator& a = Allocator()); // constexpr since C++20
12
13#include <string>
14#include <stdexcept>
15#include <algorithm>
16#include <cassert>
17#include <cstddef>
18
19#include "test_macros.h"
20#include "test_allocator.h"
21#include "min_allocator.h"
22#include "asan_testing.h"
23
24template <class Alloc, class charT>
25TEST_CONSTEXPR_CXX20 void test(const charT* s) {
26 typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;
27 typedef typename S::traits_type T;
28 std::size_t n = T::length(s);
29 S s2(s);
30 LIBCPP_ASSERT(s2.__invariants());
31 assert(s2.size() == n);
32 assert(T::compare(s2.data(), s, n) == 0);
33 assert(s2.get_allocator() == Alloc());
34 assert(s2.capacity() >= s2.size());
35 LIBCPP_ASSERT(is_string_asan_correct(s2));
36}
37
38template <class Alloc, class charT>
39TEST_CONSTEXPR_CXX20 void test(const charT* s, const Alloc& a) {
40 typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;
41 typedef typename S::traits_type T;
42 std::size_t n = T::length(s);
43 S s2(s, a);
44 LIBCPP_ASSERT(s2.__invariants());
45 assert(s2.size() == n);
46 assert(T::compare(s2.data(), s, n) == 0);
47 assert(s2.get_allocator() == a);
48 assert(s2.capacity() >= s2.size());
49 LIBCPP_ASSERT(is_string_asan_correct(s2));
50}
51
52template <class Alloc>
53TEST_CONSTEXPR_CXX20 void test(const Alloc& a) {
54 test<Alloc>("");
55 test<Alloc>("", Alloc(a));
56
57 test<Alloc>("1");
58 test<Alloc>("1", Alloc(a));
59
60 test<Alloc>("1234567980");
61 test<Alloc>("1234567980", Alloc(a));
62
63 test<Alloc>("123456798012345679801234567980123456798012345679801234567980");
64 test<Alloc>("123456798012345679801234567980123456798012345679801234567980", Alloc(a));
65}
66
67TEST_CONSTEXPR_CXX20 bool test() {
68 test(std::allocator<char>());
69 test(test_allocator<char>());
70 test(test_allocator<char>(2));
71#if TEST_STD_VER >= 11
72 test(min_allocator<char>());
73 test(safe_allocator<char>());
74#endif
75
76 return true;
77}
78
79int main(int, char**) {
80 test();
81#if TEST_STD_VER > 17
82 static_assert(test());
83#endif
84
85 return 0;
86}
87

source code of libcxx/test/std/strings/basic.string/string.cons/pointer_alloc.pass.cpp