Debian Codegraph @

src/external/catch2/tests/SelfTest/IntrospectiveTests/String.tests.cpp

raw
1
2 // Copyright Catch2 Authors
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE.txt or copy at
5 // https://www.boost.org/LICENSE_1_0.txt)
6
7 // SPDX-License-Identifier: BSL-1.0
8
9 #include <catch2/catch_test_macros.hpp>
10 #include <catch2/internal/catch_stringref.hpp>
11
12 #include <cstring>
13
14 TEST_CASE( "StringRef", "[Strings][StringRef]" ) {
15 using Catch::StringRef;
16
17 SECTION( "Empty string" ) {
18 StringRef empty;
19 REQUIRE( empty.empty() );
20 REQUIRE( empty.size() == 0 );
21 REQUIRE( std::strcmp( empty.data(), "" ) == 0 );
22 }
23
24 SECTION( "From string literal" ) {
25 StringRef s = "hello";
26 REQUIRE( s.empty() == false );
27 REQUIRE( s.size() == 5 );
28
29 auto rawChars = s.data();
30 REQUIRE( std::strcmp( rawChars, "hello" ) == 0 );
31
32 REQUIRE(s.data() == rawChars);
33 }
34 SECTION( "From sub-string" ) {
35 StringRef original = StringRef( "original string" ).substr(0, 8);
36 REQUIRE( original == "original" );
37
38 REQUIRE_NOTHROW(original.data());
39 }
40 SECTION( "Copy construction is shallow" ) {
41 StringRef original = StringRef( "original string" );
42 StringRef copy = original;
43 REQUIRE(original.begin() == copy.begin());
44 }
45 SECTION( "Copy assignment is shallow" ) {
46 StringRef original = StringRef( "original string" );
47 StringRef copy;
48 copy = original;
49 REQUIRE(original.begin() == copy.begin());
50 }
51
52 SECTION( "Substrings" ) {
53 StringRef s = "hello world!";
54 StringRef ss = s.substr(0, 5);
55
56 SECTION( "zero-based substring" ) {
57 REQUIRE( ss.empty() == false );
58 REQUIRE( ss.size() == 5 );
59 REQUIRE( std::strncmp( ss.data(), "hello", 5 ) == 0 );
60 REQUIRE( ss == "hello" );
61 }
62
63 SECTION( "non-zero-based substring") {
64 ss = s.substr( 6, 6 );
65 REQUIRE( ss.size() == 6 );
66 REQUIRE( std::strcmp( ss.data(), "world!" ) == 0 );
67 }
68
69 SECTION( "Pointer values of full refs should match" ) {
70 StringRef s2 = s;
71 REQUIRE( s.data() == s2.data() );
72 }
73
74 SECTION( "Pointer values of substring refs should also match" ) {
75 REQUIRE( s.data() == ss.data() );
76 }
77
78 SECTION("Past the end substring") {
79 REQUIRE(s.substr(s.size() + 1, 123).empty());
80 }
81
82 SECTION("Substring off the end are trimmed") {
83 ss = s.substr(6, 123);
84 REQUIRE(std::strcmp(ss.data(), "world!") == 0);
85 }
86 SECTION("substring start after the end is empty") {
87 REQUIRE(s.substr(1'000'000, 1).empty());
88 }
89 }
90
91 SECTION( "Comparisons are deep" ) {
92 char buffer1[] = "Hello";
93 char buffer2[] = "Hello";
94 CHECK(reinterpret_cast<char*>(buffer1) != reinterpret_cast<char*>(buffer2));
95
96 StringRef left(buffer1), right(buffer2);
97 REQUIRE( left == right );
98 REQUIRE(left != left.substr(0, 3));
99 }
100
101 SECTION( "from std::string" ) {
102 std::string stdStr = "a standard string";
103
104 SECTION( "implicitly constructed" ) {
105 StringRef sr = stdStr;
106 REQUIRE( sr == "a standard string" );
107 REQUIRE( sr.size() == stdStr.size() );
108 }
109 SECTION( "explicitly constructed" ) {
110 StringRef sr( stdStr );
111 REQUIRE( sr == "a standard string" );
112 REQUIRE( sr.size() == stdStr.size() );
113 }
114 SECTION( "assigned" ) {
115 StringRef sr;
116 sr = stdStr;
117 REQUIRE( sr == "a standard string" );
118 REQUIRE( sr.size() == stdStr.size() );
119 }
120 }
121
122 SECTION( "to std::string" ) {
123 StringRef sr = "a stringref";
124
125 SECTION( "explicitly constructed" ) {
126 std::string stdStr( sr );
127 REQUIRE( stdStr == "a stringref" );
128 REQUIRE( stdStr.size() == sr.size() );
129 }
130 SECTION( "assigned" ) {
131 std::string stdStr;
132 stdStr = static_cast<std::string>(sr);
133 REQUIRE( stdStr == "a stringref" );
134 REQUIRE( stdStr.size() == sr.size() );
135 }
136 }
137
138 SECTION("std::string += StringRef") {
139 StringRef sr = "the stringref contents";
140 std::string lhs("some string += ");
141 lhs += sr;
142 REQUIRE(lhs == "some string += the stringref contents");
143 }
144 SECTION("StringRef + StringRef") {
145 StringRef sr1 = "abraka", sr2 = "dabra";
146 std::string together = sr1 + sr2;
147 REQUIRE(together == "abrakadabra");
148 }
149 }
150
151 TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") {
152 using Catch::StringRef;
153 SECTION("Simple constructors") {
154 constexpr StringRef empty{};
155 STATIC_REQUIRE(empty.size() == 0);
156 STATIC_REQUIRE(empty.begin() == empty.end());
157
158 constexpr char const* const abc = "abc";
159
160 constexpr StringRef stringref(abc, 3);
161 STATIC_REQUIRE(stringref.size() == 3);
162 STATIC_REQUIRE(stringref.data() == abc);
163 STATIC_REQUIRE(stringref.begin() == abc);
164 STATIC_REQUIRE(stringref.begin() != stringref.end());
165 STATIC_REQUIRE(stringref.substr(10, 0).empty());
166 STATIC_REQUIRE(stringref.substr(2, 1).data() == abc + 2);
167 STATIC_REQUIRE(stringref[1] == 'b');
168
169
170 constexpr StringRef shortened(abc, 2);
171 STATIC_REQUIRE(shortened.size() == 2);
172 STATIC_REQUIRE(shortened.data() == abc);
173 STATIC_REQUIRE(shortened.begin() != shortened.end());
174 }
175 SECTION("UDL construction") {
176 constexpr auto sr1 = "abc"_catch_sr;
177 STATIC_REQUIRE_FALSE(sr1.empty());
178 STATIC_REQUIRE(sr1.size() == 3);
179
180 using Catch::operator""_sr;
181 constexpr auto sr2 = ""_sr;
182 STATIC_REQUIRE(sr2.empty());
183 STATIC_REQUIRE(sr2.size() == 0);
184 }
185 }
186
187 TEST_CASE("StringRef::compare", "[Strings][StringRef][approvals]") {
188 using Catch::StringRef;
189
190 SECTION("Same length on both sides") {
191 StringRef sr1("abcdc");
192 StringRef sr2("abcdd");
193 StringRef sr3("abcdc");
194
195 REQUIRE(sr1.compare(sr2) < 0);
196 REQUIRE(sr2.compare(sr1) > 0);
197 REQUIRE(sr1.compare(sr3) == 0);
198 REQUIRE(sr3.compare(sr1) == 0);
199 }
200 SECTION("Different lengths") {
201 StringRef sr1("def");
202 StringRef sr2("deff");
203 StringRef sr3("ab");
204
205 REQUIRE(sr1.compare(sr2) < 0);
206 REQUIRE(sr2.compare(sr1) > 0);
207 REQUIRE(sr1.compare(sr3) > 0);
208 REQUIRE(sr2.compare(sr3) > 0);
209 REQUIRE(sr3.compare(sr1) < 0);
210 REQUIRE(sr3.compare(sr2) < 0);
211 }
212 }