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