1 #include "internal/catch_stringref.h" 2 3 #include "catch.hpp" 4 5 #include <cstring> 6 7 TEST_CASE( "StringRef", "[Strings][StringRef]" ) { 8 9 using Catch::StringRef; 10 11 SECTION( "Empty string" ) { 12 StringRef empty; 13 REQUIRE( empty.empty() ); 14 REQUIRE( empty.size() == 0 ); 15 REQUIRE( empty.isNullTerminated() ); 16 REQUIRE( std::strcmp( empty.c_str(), "" ) == 0 ); 17 } 18 19 SECTION( "From string literal" ) { 20 StringRef s = "hello"; 21 REQUIRE( s.empty() == false ); 22 REQUIRE( s.size() == 5 ); 23 REQUIRE( s.isNullTerminated() ); 24 25 auto rawChars = s.data(); 26 REQUIRE( std::strcmp( rawChars, "hello" ) == 0 ); 27 28 REQUIRE_NOTHROW(s.c_str()); 29 REQUIRE(s.c_str() == rawChars); 30 REQUIRE(s.data() == rawChars); 31 } 32 SECTION( "From sub-string" ) { 33 StringRef original = StringRef( "original string" ).substr(0, 8); 34 REQUIRE( original == "original" ); 35 36 REQUIRE_FALSE(original.isNullTerminated()); 37 REQUIRE_THROWS(original.c_str()); 38 REQUIRE_NOTHROW(original.data()); 39 } 40 41 42 SECTION( "Substrings" ) { 43 StringRef s = "hello world!"; 44 StringRef ss = s.substr(0, 5); 45 46 SECTION( "zero-based substring" ) { 47 REQUIRE( ss.empty() == false ); 48 REQUIRE( ss.size() == 5 ); 49 REQUIRE( std::strncmp( ss.data(), "hello", 5 ) == 0 ); 50 REQUIRE( ss == "hello" ); 51 } 52 53 SECTION( "non-zero-based substring") { 54 ss = s.substr( 6, 6 ); 55 REQUIRE( ss.size() == 6 ); 56 REQUIRE( std::strcmp( ss.c_str(), "world!" ) == 0 ); 57 } 58 59 SECTION( "Pointer values of full refs should match" ) { 60 StringRef s2 = s; 61 REQUIRE( s.data() == s2.data() ); 62 } 63 64 SECTION( "Pointer values of substring refs should also match" ) { 65 REQUIRE( s.data() == ss.data() ); 66 } 67 68 SECTION("Past the end substring") { 69 REQUIRE(s.substr(s.size() + 1, 123).empty()); 70 } 71 72 SECTION("Substring off the end are trimmed") { 73 ss = s.substr(6, 123); 74 REQUIRE(std::strcmp(ss.c_str(), "world!") == 0); 75 } 76 // TODO: substring into string + size is longer than end 77 } 78 79 SECTION( "Comparisons are deep" ) { 80 char buffer1[] = "Hello"; 81 char buffer2[] = "Hello"; 82 CHECK((char*)buffer1 != (char*)buffer2); 83 84 StringRef left(buffer1), right(buffer2); 85 REQUIRE( left == right ); 86 REQUIRE(left != left.substr(0, 3)); 87 } 88 89 SECTION( "from std::string" ) { 90 std::string stdStr = "a standard string"; 91 92 SECTION( "implicitly constructed" ) { 93 StringRef sr = stdStr; 94 REQUIRE( sr == "a standard string" ); 95 REQUIRE( sr.size() == stdStr.size() ); 96 } 97 SECTION( "explicitly constructed" ) { 98 StringRef sr( stdStr ); 99 REQUIRE( sr == "a standard string" ); 100 REQUIRE( sr.size() == stdStr.size() ); 101 } 102 SECTION( "assigned" ) { 103 StringRef sr; 104 sr = stdStr; 105 REQUIRE( sr == "a standard string" ); 106 REQUIRE( sr.size() == stdStr.size() ); 107 } 108 } 109 110 SECTION( "to std::string" ) { 111 StringRef sr = "a stringref"; 112 113 SECTION( "explicitly constructed" ) { 114 std::string stdStr( sr ); 115 REQUIRE( stdStr == "a stringref" ); 116 REQUIRE( stdStr.size() == sr.size() ); 117 } 118 SECTION( "assigned" ) { 119 std::string stdStr; 120 stdStr = static_cast<std::string>(sr); 121 REQUIRE( stdStr == "a stringref" ); 122 REQUIRE( stdStr.size() == sr.size() ); 123 } 124 } 125 } 126 127 TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") { 128 using Catch::StringRef; 129 SECTION("Simple constructors") { 130 STATIC_REQUIRE(StringRef{}.size() == 0); 131 132 STATIC_REQUIRE(StringRef{ "abc", 3 }.size() == 3); 133 STATIC_REQUIRE(StringRef{ "abc", 3 }.isNullTerminated()); 134 135 STATIC_REQUIRE(StringRef{ "abc", 2 }.size() == 2); 136 STATIC_REQUIRE_FALSE(StringRef{ "abc", 2 }.isNullTerminated()); 137 } 138 SECTION("UDL construction") { 139 constexpr auto sr1 = "abc"_catch_sr; 140 STATIC_REQUIRE_FALSE(sr1.empty()); 141 STATIC_REQUIRE(sr1.size() == 3); 142 STATIC_REQUIRE(sr1.isNullTerminated()); 143 144 using Catch::operator"" _sr; 145 constexpr auto sr2 = ""_sr; 146 STATIC_REQUIRE(sr2.empty()); 147 STATIC_REQUIRE(sr2.size() == 0); 148 STATIC_REQUIRE(sr2.isNullTerminated()); 149 } 150 } 151