Debian Codegraph @

libstdc++-v3/testsuite/21_strings/basic_string/modifiers/replace/replace_with_range.cc

raw
1 // { dg-do run { target c++23 } }
2
3 #include <span>
4 #include <string>
5 #include <testsuite_allocator.h>
6 #include <testsuite_hooks.h>
7 #include <testsuite_iterators.h>
8
9 template<typename Range, typename Alloc>
10 constexpr void
11 do_test()
12 {
13 // The vector's value_type.
14 using V = typename std::allocator_traits<Alloc>::value_type;
15 using CT = std::char_traits<V>;
16
17 // The range's value_type.
18 using T = std::ranges::range_value_t<Range>;
19 T a[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
20 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
21
22 auto eq = [](const std::basic_string<V, CT, Alloc>& l, std::span<T> r) {
23 if (l.size() != r.size())
24 return false;
25 for (auto i = 0u; i < l.size(); ++i)
26 if (l[i] != r[i])
27 return false;
28 return true;
29 };
30
31 std::basic_string<V, CT, Alloc> v;
32 v.replace_with_range(v.end(), v.end(), Range(a, a));
33 VERIFY( v.empty() );
34 v.replace_with_range(v.end(), v.end(), Range(a, a+4));
35 VERIFY( eq(v, {a, 4}) );
36 v.replace_with_range(v.end(), v.end(), Range(a+4, a+9));
37 VERIFY( eq(v, {a, 9}) );
38 std::basic_string<V, CT, Alloc> s = v;
39 v.replace_with_range(v.end(), v.end(), Range(a+9, a+20));
40 VERIFY( eq(v, {a, 20}) );
41
42 v.replace_with_range(v.begin()+10, v.begin()+20, Range(a+5, a+10));
43 VERIFY( v.size() == 15 );
44 v.replace_with_range(v.begin(), v.begin()+10, Range(a, a+5));
45 VERIFY( eq(v, {a, 10}) );
46
47 s = v;
48 v.replace_with_range(v.begin(), v.begin()+4, Range(a, a+8));
49 VERIFY( v.size() == 14 );
50 v.replace_with_range(v.begin()+8, v.begin()+12, Range(a+8, a+16));
51 VERIFY( v.size() == 18 );
52 v.replace_with_range(v.begin()+16, v.begin()+18, Range(a+16, a+20));
53 VERIFY( eq(v, {a, 20}) );
54 }
55
56 template<typename Range>
57 void
58 do_test_a()
59 {
60 do_test<Range, std::allocator<char>>();
61 do_test<Range, __gnu_test::SimpleAllocator<char>>();
62 do_test<Range, std::allocator<wchar_t>>();
63 do_test<Range, __gnu_test::SimpleAllocator<wchar_t>>();
64 }
65
66 bool
67 test_ranges()
68 {
69 using namespace __gnu_test;
70
71 do_test_a<test_forward_range<char>>();
72 do_test_a<test_forward_sized_range<char>>();
73 do_test_a<test_sized_range_sized_sent<char, forward_iterator_wrapper>>();
74
75 do_test_a<test_input_range<char>>();
76 do_test_a<test_input_sized_range<char>>();
77 do_test_a<test_sized_range_sized_sent<char, input_iterator_wrapper>>();
78
79 do_test_a<test_range<char, input_iterator_wrapper_nocopy>>();
80 do_test_a<test_sized_range<char, input_iterator_wrapper_nocopy>>();
81 do_test_a<test_sized_range_sized_sent<char, input_iterator_wrapper_nocopy>>();
82
83 // Not lvalue-convertible to char
84 struct C {
85 C(char v) : val(v) { }
86 operator char() && { return val; }
87 bool operator==(char b) const { return b == val; }
88 char val;
89 };
90 using rvalue_input_range = test_range<C, input_iterator_wrapper_rval>;
91 do_test<rvalue_input_range, std::allocator<char>>();
92
93 return true;
94 }
95
96 void
97 test_overlapping()
98 {
99 std::string const s = "1234abcd";
100
101 std::string c = s;
102 c.replace_with_range(c.end(), c.end(), std::string_view(c));
103 VERIFY( c == "1234abcd1234abcd" );
104
105 c = s;
106 c.replace_with_range(c.begin(), c.begin()+4, std::string_view(c).substr(4, 4));
107 VERIFY( c == "abcdabcd" );
108
109 c = s;
110 c.replace_with_range(c.begin()+2, c.begin()+4, std::string_view(c).substr(0, 4));
111 VERIFY( c == "121234abcd" );
112
113 c = s;
114 c.replace_with_range(c.begin()+2, c.begin()+2, std::string_view(c).substr(0, 4));
115 VERIFY( c == "12123434abcd" );
116 }
117
118 constexpr bool
119 test_constexpr()
120 {
121 #if _GLIBCXX_USE_CXX11_ABI
122 // XXX: this doesn't test the non-forward_range code paths are constexpr.
123 do_test<std::string_view, std::allocator<char>>();
124 #endif // _GLIBCXX_USE_CXX11_ABI
125 return true;
126 }
127
128 int main()
129 {
130 test_ranges();
131 test_overlapping();
132 static_assert( test_constexpr() );
133 }