[mlir][acc] Introduce MappableType interface (#122146)
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.cons / string_view.pass.cpp
blobad37b50b83ba7eee740036eb420faf3aa218b4bd
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 //===----------------------------------------------------------------------===//
9 // <string>
11 // explicit basic_string(basic_string_view<CharT, traits> sv, const Allocator& a = Allocator()); // constexpr since C++20
13 #include <algorithm>
14 #include <cassert>
15 #include <stdexcept>
16 #include <string_view>
17 #include <string>
18 #include <type_traits>
20 #include "min_allocator.h"
21 #include "test_allocator.h"
22 #include "test_macros.h"
23 #include "asan_testing.h"
25 static_assert(!std::is_convertible<std::string_view, std::string const&>::value, "");
26 static_assert(!std::is_convertible<std::string_view, std::string>::value, "");
28 template <class Alloc, class CharT>
29 TEST_CONSTEXPR_CXX20 void test(std::basic_string_view<CharT> sv) {
30 typedef std::basic_string<CharT, std::char_traits<CharT>, Alloc> S;
31 typedef typename S::traits_type T;
33 S s2(sv);
34 LIBCPP_ASSERT(s2.__invariants());
35 assert(s2.size() == sv.size());
36 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
37 assert(s2.get_allocator() == Alloc());
38 assert(s2.capacity() >= s2.size());
39 LIBCPP_ASSERT(is_string_asan_correct(s2));
42 S s2;
43 s2 = sv;
44 LIBCPP_ASSERT(s2.__invariants());
45 assert(s2.size() == sv.size());
46 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
47 assert(s2.get_allocator() == Alloc());
48 assert(s2.capacity() >= s2.size());
49 LIBCPP_ASSERT(is_string_asan_correct(s2));
53 template <class Alloc, class CharT>
54 TEST_CONSTEXPR_CXX20 void test(std::basic_string_view<CharT> sv, const Alloc& a) {
55 typedef std::basic_string<CharT, std::char_traits<CharT>, Alloc> S;
56 typedef typename S::traits_type T;
58 S s2(sv, a);
59 LIBCPP_ASSERT(s2.__invariants());
60 assert(s2.size() == sv.size());
61 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
62 assert(s2.get_allocator() == a);
63 assert(s2.capacity() >= s2.size());
64 LIBCPP_ASSERT(is_string_asan_correct(s2));
67 S s2(a);
68 s2 = sv;
69 LIBCPP_ASSERT(s2.__invariants());
70 assert(s2.size() == sv.size());
71 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
72 assert(s2.get_allocator() == a);
73 assert(s2.capacity() >= s2.size());
74 LIBCPP_ASSERT(is_string_asan_correct(s2));
78 template <class Alloc>
79 TEST_CONSTEXPR_CXX20 void test_string(const Alloc& a) {
80 typedef std::basic_string_view<char, std::char_traits<char> > SV;
82 test<Alloc>(SV(""));
83 test<Alloc>(SV(""), Alloc(a));
85 test<Alloc>(SV("1"));
86 test<Alloc>(SV("1"), Alloc(a));
88 test<Alloc>(SV("1234567980"));
89 test<Alloc>(SV("1234567980"), Alloc(a));
91 test<Alloc>(SV("123456798012345679801234567980123456798012345679801234567980"));
92 test<Alloc>(SV("123456798012345679801234567980123456798012345679801234567980"), Alloc(a));
95 TEST_CONSTEXPR_CXX20 bool test() {
96 test_string(std::allocator<char>());
97 test_string(test_allocator<char>());
98 test_string(test_allocator<char>(2));
99 #if TEST_STD_VER >= 11
100 test_string(min_allocator<char>());
101 test_string(safe_allocator<char>());
102 #endif
104 return true;
107 int main(int, char**) {
108 test();
109 #if TEST_STD_VER > 17
110 static_assert(test());
111 #endif
113 return 0;