2020-01-20 23:24:04 +01:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2020-05-10 10:09:01 +02:00
|
|
|
#include <catch2/internal/catch_stringref.hpp>
|
2017-06-29 12:18:14 +02:00
|
|
|
|
2017-06-29 12:43:20 +02:00
|
|
|
#include <cstring>
|
|
|
|
|
2018-02-08 13:10:12 +01:00
|
|
|
TEST_CASE( "StringRef", "[Strings][StringRef]" ) {
|
2017-06-29 12:18:14 +02:00
|
|
|
using Catch::StringRef;
|
2017-08-14 10:04:14 +02:00
|
|
|
|
2017-06-29 12:18:14 +02:00
|
|
|
SECTION( "Empty string" ) {
|
|
|
|
StringRef empty;
|
|
|
|
REQUIRE( empty.empty() );
|
|
|
|
REQUIRE( empty.size() == 0 );
|
2021-03-25 20:02:47 +01:00
|
|
|
REQUIRE( std::strcmp( empty.data(), "" ) == 0 );
|
2017-06-29 12:18:14 +02:00
|
|
|
}
|
2018-02-08 13:10:12 +01:00
|
|
|
|
2017-06-29 12:18:14 +02:00
|
|
|
SECTION( "From string literal" ) {
|
|
|
|
StringRef s = "hello";
|
|
|
|
REQUIRE( s.empty() == false );
|
|
|
|
REQUIRE( s.size() == 5 );
|
2018-02-08 13:10:12 +01:00
|
|
|
|
2019-10-21 17:44:11 +02:00
|
|
|
auto rawChars = s.data();
|
2017-06-29 12:18:14 +02:00
|
|
|
REQUIRE( std::strcmp( rawChars, "hello" ) == 0 );
|
2018-02-08 13:10:12 +01:00
|
|
|
|
2019-10-21 17:44:11 +02:00
|
|
|
REQUIRE(s.data() == rawChars);
|
2017-06-29 12:18:14 +02:00
|
|
|
}
|
|
|
|
SECTION( "From sub-string" ) {
|
|
|
|
StringRef original = StringRef( "original string" ).substr(0, 8);
|
|
|
|
REQUIRE( original == "original" );
|
2018-02-08 13:10:12 +01:00
|
|
|
|
2019-10-21 17:44:11 +02:00
|
|
|
REQUIRE_NOTHROW(original.data());
|
2017-06-29 12:18:14 +02:00
|
|
|
}
|
2018-02-08 13:10:12 +01:00
|
|
|
|
|
|
|
|
2017-06-29 12:18:14 +02:00
|
|
|
SECTION( "Substrings" ) {
|
|
|
|
StringRef s = "hello world!";
|
|
|
|
StringRef ss = s.substr(0, 5);
|
2018-02-08 13:10:12 +01:00
|
|
|
|
2017-06-29 12:18:14 +02:00
|
|
|
SECTION( "zero-based substring" ) {
|
|
|
|
REQUIRE( ss.empty() == false );
|
|
|
|
REQUIRE( ss.size() == 5 );
|
2019-10-21 17:44:11 +02:00
|
|
|
REQUIRE( std::strncmp( ss.data(), "hello", 5 ) == 0 );
|
2017-06-29 12:18:14 +02:00
|
|
|
REQUIRE( ss == "hello" );
|
|
|
|
}
|
2018-02-08 13:10:12 +01:00
|
|
|
|
2017-06-29 12:18:14 +02:00
|
|
|
SECTION( "non-zero-based substring") {
|
|
|
|
ss = s.substr( 6, 6 );
|
|
|
|
REQUIRE( ss.size() == 6 );
|
2021-03-25 20:02:47 +01:00
|
|
|
REQUIRE( std::strcmp( ss.data(), "world!" ) == 0 );
|
2017-06-29 12:18:14 +02:00
|
|
|
}
|
2018-02-08 13:10:12 +01:00
|
|
|
|
2017-06-29 12:18:14 +02:00
|
|
|
SECTION( "Pointer values of full refs should match" ) {
|
|
|
|
StringRef s2 = s;
|
2019-10-21 17:44:11 +02:00
|
|
|
REQUIRE( s.data() == s2.data() );
|
2017-06-29 12:18:14 +02:00
|
|
|
}
|
2018-02-08 13:10:12 +01:00
|
|
|
|
2019-10-21 17:44:11 +02:00
|
|
|
SECTION( "Pointer values of substring refs should also match" ) {
|
|
|
|
REQUIRE( s.data() == ss.data() );
|
2017-06-29 12:18:14 +02:00
|
|
|
}
|
2019-09-08 21:04:23 +02:00
|
|
|
|
|
|
|
SECTION("Past the end substring") {
|
|
|
|
REQUIRE(s.substr(s.size() + 1, 123).empty());
|
|
|
|
}
|
2019-10-21 17:44:11 +02:00
|
|
|
|
|
|
|
SECTION("Substring off the end are trimmed") {
|
|
|
|
ss = s.substr(6, 123);
|
2021-03-25 20:02:47 +01:00
|
|
|
REQUIRE(std::strcmp(ss.data(), "world!") == 0);
|
2019-10-21 17:44:11 +02:00
|
|
|
}
|
2020-02-13 15:01:03 +01:00
|
|
|
SECTION("substring start after the end is empty") {
|
|
|
|
REQUIRE(s.substr(1'000'000, 1).empty());
|
|
|
|
}
|
2017-06-29 12:18:14 +02:00
|
|
|
}
|
2018-02-08 13:10:12 +01:00
|
|
|
|
2019-10-21 17:44:11 +02:00
|
|
|
SECTION( "Comparisons are deep" ) {
|
|
|
|
char buffer1[] = "Hello";
|
|
|
|
char buffer2[] = "Hello";
|
2020-12-28 14:00:19 +01:00
|
|
|
CHECK(reinterpret_cast<char*>(buffer1) != reinterpret_cast<char*>(buffer2));
|
2019-10-21 17:44:11 +02:00
|
|
|
|
|
|
|
StringRef left(buffer1), right(buffer2);
|
|
|
|
REQUIRE( left == right );
|
|
|
|
REQUIRE(left != left.substr(0, 3));
|
2017-06-29 12:18:14 +02:00
|
|
|
}
|
2017-08-14 09:39:14 +02:00
|
|
|
|
|
|
|
SECTION( "from std::string" ) {
|
|
|
|
std::string stdStr = "a standard string";
|
|
|
|
|
|
|
|
SECTION( "implicitly constructed" ) {
|
|
|
|
StringRef sr = stdStr;
|
|
|
|
REQUIRE( sr == "a standard string" );
|
|
|
|
REQUIRE( sr.size() == stdStr.size() );
|
|
|
|
}
|
|
|
|
SECTION( "explicitly constructed" ) {
|
|
|
|
StringRef sr( stdStr );
|
|
|
|
REQUIRE( sr == "a standard string" );
|
|
|
|
REQUIRE( sr.size() == stdStr.size() );
|
|
|
|
}
|
|
|
|
SECTION( "assigned" ) {
|
|
|
|
StringRef sr;
|
|
|
|
sr = stdStr;
|
|
|
|
REQUIRE( sr == "a standard string" );
|
|
|
|
REQUIRE( sr.size() == stdStr.size() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION( "to std::string" ) {
|
|
|
|
StringRef sr = "a stringref";
|
|
|
|
|
|
|
|
SECTION( "explicitly constructed" ) {
|
|
|
|
std::string stdStr( sr );
|
|
|
|
REQUIRE( stdStr == "a stringref" );
|
|
|
|
REQUIRE( stdStr.size() == sr.size() );
|
|
|
|
}
|
|
|
|
SECTION( "assigned" ) {
|
|
|
|
std::string stdStr;
|
2019-09-08 14:49:40 +02:00
|
|
|
stdStr = static_cast<std::string>(sr);
|
2017-08-14 09:39:14 +02:00
|
|
|
REQUIRE( stdStr == "a stringref" );
|
|
|
|
REQUIRE( stdStr.size() == sr.size() );
|
|
|
|
}
|
|
|
|
}
|
2020-05-31 22:30:41 +02:00
|
|
|
|
|
|
|
SECTION("std::string += StringRef") {
|
|
|
|
StringRef sr = "the stringref contents";
|
|
|
|
std::string lhs("some string += ");
|
|
|
|
lhs += sr;
|
|
|
|
REQUIRE(lhs == "some string += the stringref contents");
|
|
|
|
}
|
|
|
|
SECTION("StringRef + StringRef") {
|
|
|
|
StringRef sr1 = "abraka", sr2 = "dabra";
|
|
|
|
std::string together = sr1 + sr2;
|
|
|
|
REQUIRE(together == "abrakadabra");
|
|
|
|
}
|
2017-06-29 12:18:14 +02:00
|
|
|
}
|
2019-10-21 17:44:11 +02:00
|
|
|
|
|
|
|
TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") {
|
|
|
|
using Catch::StringRef;
|
|
|
|
SECTION("Simple constructors") {
|
2020-02-13 16:57:33 +01:00
|
|
|
constexpr StringRef empty{};
|
2020-02-13 15:01:03 +01:00
|
|
|
STATIC_REQUIRE(empty.size() == 0);
|
|
|
|
STATIC_REQUIRE(empty.begin() == empty.end());
|
|
|
|
|
|
|
|
constexpr char const* const abc = "abc";
|
|
|
|
|
|
|
|
constexpr StringRef stringref(abc, 3);
|
|
|
|
STATIC_REQUIRE(stringref.size() == 3);
|
|
|
|
STATIC_REQUIRE(stringref.data() == abc);
|
|
|
|
STATIC_REQUIRE(stringref.begin() == abc);
|
|
|
|
STATIC_REQUIRE(stringref.begin() != stringref.end());
|
|
|
|
STATIC_REQUIRE(stringref.substr(10, 0).empty());
|
|
|
|
STATIC_REQUIRE(stringref.substr(2, 1).data() == abc + 2);
|
2020-05-10 07:22:11 +02:00
|
|
|
STATIC_REQUIRE(stringref[1] == 'b');
|
2020-02-13 15:01:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
constexpr StringRef shortened(abc, 2);
|
|
|
|
STATIC_REQUIRE(shortened.size() == 2);
|
|
|
|
STATIC_REQUIRE(shortened.data() == abc);
|
|
|
|
STATIC_REQUIRE(shortened.begin() != shortened.end());
|
2019-10-21 17:44:11 +02:00
|
|
|
}
|
|
|
|
SECTION("UDL construction") {
|
|
|
|
constexpr auto sr1 = "abc"_catch_sr;
|
|
|
|
STATIC_REQUIRE_FALSE(sr1.empty());
|
|
|
|
STATIC_REQUIRE(sr1.size() == 3);
|
|
|
|
|
|
|
|
using Catch::operator"" _sr;
|
|
|
|
constexpr auto sr2 = ""_sr;
|
|
|
|
STATIC_REQUIRE(sr2.empty());
|
|
|
|
STATIC_REQUIRE(sr2.size() == 0);
|
|
|
|
}
|
|
|
|
}
|
2021-09-27 18:30:31 +02:00
|
|
|
|
|
|
|
TEST_CASE("StringRef::compare", "[Strings][StringRef]") {
|
|
|
|
using Catch::StringRef;
|
|
|
|
|
|
|
|
SECTION("Same length on both sides") {
|
|
|
|
StringRef sr1("abcdc");
|
|
|
|
StringRef sr2("abcdd");
|
|
|
|
StringRef sr3("abcdc");
|
|
|
|
|
|
|
|
REQUIRE(sr1.compare(sr2) < 0);
|
|
|
|
REQUIRE(sr2.compare(sr1) > 0);
|
|
|
|
REQUIRE(sr1.compare(sr3) == 0);
|
|
|
|
REQUIRE(sr3.compare(sr1) == 0);
|
|
|
|
}
|
|
|
|
SECTION("Different lengths") {
|
|
|
|
StringRef sr1("def");
|
|
|
|
StringRef sr2("deff");
|
|
|
|
StringRef sr3("ab");
|
|
|
|
|
|
|
|
REQUIRE(sr1.compare(sr2) < 0);
|
|
|
|
REQUIRE(sr2.compare(sr1) > 0);
|
|
|
|
REQUIRE(sr1.compare(sr3) > 0);
|
|
|
|
REQUIRE(sr2.compare(sr3) > 0);
|
|
|
|
REQUIRE(sr3.compare(sr1) < 0);
|
|
|
|
REQUIRE(sr3.compare(sr2) < 0);
|
|
|
|
}
|
|
|
|
}
|