diff --git a/include/internal/catch_stringref.cpp b/include/internal/catch_stringref.cpp index b46414ac..215feef3 100644 --- a/include/internal/catch_stringref.cpp +++ b/include/internal/catch_stringref.cpp @@ -5,14 +5,10 @@ * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) */ - -#if defined(__clang__) -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wexit-time-destructors" -#endif - +#include "catch_enforce.h" #include "catch_stringref.h" +#include #include #include #include @@ -22,63 +18,33 @@ namespace Catch { : StringRef( rawChars, static_cast(std::strlen(rawChars) ) ) {} - void StringRef::swap( StringRef& other ) noexcept { - std::swap( m_start, other.m_start ); - std::swap( m_size, other.m_size ); - std::swap( m_data, other.m_data ); - } - auto StringRef::c_str() const -> char const* { - if( !isSubstring() ) - return m_start; - - const_cast( this )->takeOwnership(); - return m_data; + CATCH_ENFORCE(isNullTerminated(), "Called StringRef::c_str() on a non-null-terminated instance"); + return m_start; } - auto StringRef::currentData() const noexcept -> char const* { + auto StringRef::data() const noexcept -> char const* { return m_start; } - auto StringRef::isOwned() const noexcept -> bool { - return m_data != nullptr; - } - auto StringRef::isSubstring() const noexcept -> bool { - return m_start[m_size] != '\0'; - } - - void StringRef::takeOwnership() { - if( !isOwned() ) { - m_data = new char[m_size+1]; - memcpy( m_data, m_start, m_size ); - m_data[m_size] = '\0'; + auto StringRef::substr( size_type start, size_type size ) const noexcept -> StringRef { + if (start < m_size) { + return StringRef(m_start + start, (std::min)(m_size - start, size)); + } else { + return StringRef(); } } - auto StringRef::substr( size_type start, size_type size ) const noexcept -> StringRef { - if( start < m_size ) - return StringRef( m_start+start, size ); - else - return StringRef(); - } auto StringRef::operator == ( StringRef const& other ) const noexcept -> bool { - return - size() == other.size() && - (std::strncmp( m_start, other.m_start, size() ) == 0); - } - auto StringRef::operator != ( StringRef const& other ) const noexcept -> bool { - return !operator==( other ); + return m_size == other.m_size + && (std::memcmp( m_start, other.m_start, m_size ) == 0); } auto operator << ( std::ostream& os, StringRef const& str ) -> std::ostream& { - return os.write(str.currentData(), str.size()); + return os.write(str.data(), str.size()); } auto operator+=( std::string& lhs, StringRef const& rhs ) -> std::string& { - lhs.append(rhs.currentData(), rhs.size()); + lhs.append(rhs.data(), rhs.size()); return lhs; } } // namespace Catch - -#if defined(__clang__) -# pragma clang diagnostic pop -#endif diff --git a/include/internal/catch_stringref.h b/include/internal/catch_stringref.h index a45147da..dc2e748c 100644 --- a/include/internal/catch_stringref.h +++ b/include/internal/catch_stringref.h @@ -16,49 +16,24 @@ namespace Catch { /// A non-owning string class (similar to the forthcoming std::string_view) /// Note that, because a StringRef may be a substring of another string, - /// it may not be null terminated. c_str() must return a null terminated - /// string, however, and so the StringRef will internally take ownership - /// (taking a copy), if necessary. In theory this ownership is not externally - /// visible - but it does mean (substring) StringRefs should not be shared between - /// threads. + /// it may not be null terminated. class StringRef { public: using size_type = std::size_t; using const_iterator = const char*; private: - friend struct StringRefTestAccess; - - char const* m_start; - size_type m_size; - - char* m_data = nullptr; - - void takeOwnership(); - static constexpr char const* const s_empty = ""; - public: // construction/ assignment - StringRef() noexcept - : StringRef( s_empty, 0 ) - {} + char const* m_start = s_empty; + size_type m_size = 0; - StringRef( StringRef const& other ) noexcept - : m_start( other.m_start ), - m_size( other.m_size ) - {} - - StringRef( StringRef&& other ) noexcept - : m_start( other.m_start ), - m_size( other.m_size ), - m_data( other.m_data ) - { - other.m_data = nullptr; - } + public: // construction + constexpr StringRef() noexcept = default; StringRef( char const* rawChars ) noexcept; - StringRef( char const* rawChars, size_type size ) noexcept + constexpr StringRef( char const* rawChars, size_type size ) noexcept : m_start( rawChars ), m_size( size ) {} @@ -68,27 +43,15 @@ namespace Catch { m_size( stdString.size() ) {} - ~StringRef() noexcept { - delete[] m_data; - } - - auto operator = ( StringRef const &other ) noexcept -> StringRef& { - delete[] m_data; - m_data = nullptr; - m_start = other.m_start; - m_size = other.m_size; - return *this; - } - explicit operator std::string() const { return std::string(m_start, m_size); } - void swap( StringRef& other ) noexcept; - public: // operators auto operator == ( StringRef const& other ) const noexcept -> bool; - auto operator != ( StringRef const& other ) const noexcept -> bool; + auto operator != (StringRef const& other) const noexcept -> bool { + return !(*this == other); + } auto operator[] ( size_type index ) const noexcept -> char { assert(index < m_size); @@ -96,42 +59,45 @@ namespace Catch { } public: // named queries - auto empty() const noexcept -> bool { + constexpr auto empty() const noexcept -> bool { return m_size == 0; } - auto size() const noexcept -> size_type { + constexpr auto size() const noexcept -> size_type { return m_size; } + // Returns the current start pointer. If the StringRef is not + // null-terminated, throws std::domain_exception auto c_str() const -> char const*; public: // substrings and searches - auto substr( size_type start, size_type size ) const noexcept -> StringRef; + // Returns a substring of [start, start + length). + // If start + length > size(), then the substring is [start, size()). + // If start > size(), then the substring is empty. + auto substr( size_type start, size_type length ) const noexcept -> StringRef; - // Returns the current start pointer. - // Note that the pointer can change when if the StringRef is a substring - auto currentData() const noexcept -> char const*; + // Returns the current start pointer. May not be null-terminated. + auto data() const noexcept -> char const*; + + constexpr auto isNullTerminated() const noexcept -> bool { + return m_start[m_size] == '\0'; + } public: // iterators - const_iterator begin() const { return m_start; } - const_iterator end() const { return m_start + m_size; } - - private: // ownership queries - may not be consistent between calls - auto isOwned() const noexcept -> bool; - auto isSubstring() const noexcept -> bool; + constexpr const_iterator begin() const { return m_start; } + constexpr const_iterator end() const { return m_start + m_size; } }; auto operator += ( std::string& lhs, StringRef const& sr ) -> std::string&; auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&; - inline auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef { + constexpr auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef { return StringRef( rawChars, size ); } - } // namespace Catch -inline auto operator "" _catch_sr( char const* rawChars, std::size_t size ) noexcept -> Catch::StringRef { +constexpr auto operator "" _catch_sr( char const* rawChars, std::size_t size ) noexcept -> Catch::StringRef { return Catch::StringRef( rawChars, size ); } diff --git a/projects/SelfTest/Baselines/compact.sw.approved.txt b/projects/SelfTest/Baselines/compact.sw.approved.txt index 311183bc..4090d70c 100644 --- a/projects/SelfTest/Baselines/compact.sw.approved.txt +++ b/projects/SelfTest/Baselines/compact.sw.approved.txt @@ -1095,37 +1095,32 @@ Matchers.tests.cpp:: passed: testStringForMatching(), EndsWith("sub Matchers.tests.cpp:: passed: testStringForMatching(), EndsWith(" SuBsTrInG", Catch::CaseSensitive::No) for: "this string contains 'abc' as a substring" ends with: " substring" (case insensitive) String.tests.cpp:: passed: empty.empty() for: true String.tests.cpp:: passed: empty.size() == 0 for: 0 == 0 +String.tests.cpp:: passed: empty.isNullTerminated() for: true String.tests.cpp:: passed: std::strcmp( empty.c_str(), "" ) == 0 for: 0 == 0 String.tests.cpp:: passed: s.empty() == false for: false == false String.tests.cpp:: passed: s.size() == 5 for: 5 == 5 -String.tests.cpp:: passed: isSubstring( s ) == false for: false == false +String.tests.cpp:: passed: s.isNullTerminated() for: true String.tests.cpp:: passed: std::strcmp( rawChars, "hello" ) == 0 for: 0 == 0 -String.tests.cpp:: passed: isOwned( s ) == false for: false == false +String.tests.cpp:: passed: s.c_str() String.tests.cpp:: passed: s.c_str() == rawChars for: "hello" == "hello" -String.tests.cpp:: passed: isOwned( s ) == false for: false == false +String.tests.cpp:: passed: s.data() == rawChars for: "hello" == "hello" String.tests.cpp:: passed: original == "original" -String.tests.cpp:: passed: isSubstring( original ) for: true -String.tests.cpp:: passed: isOwned( original ) == false for: false == false -String.tests.cpp:: passed: isOwned( original ) for: true +String.tests.cpp:: passed: !(original.isNullTerminated()) for: !false +String.tests.cpp:: passed: original.c_str() +String.tests.cpp:: passed: original.data() String.tests.cpp:: passed: ss.empty() == false for: false == false String.tests.cpp:: passed: ss.size() == 5 for: 5 == 5 -String.tests.cpp:: passed: std::strcmp( ss.c_str(), "hello" ) == 0 for: 0 == 0 +String.tests.cpp:: passed: std::strncmp( ss.data(), "hello", 5 ) == 0 for: 0 == 0 String.tests.cpp:: passed: ss == "hello" for: hello == "hello" -String.tests.cpp:: passed: isSubstring( ss ) for: true -String.tests.cpp:: passed: isOwned( ss ) == false for: false == false -String.tests.cpp:: passed: rawChars == s.currentData() for: "hello world!" == "hello world!" -String.tests.cpp:: passed: ss.c_str() != rawChars for: "hello" != "hello world!" -String.tests.cpp:: passed: isOwned( ss ) for: true -String.tests.cpp:: passed: isOwned(ss) == false for: false == false -String.tests.cpp:: passed: ss == "hello" for: hello == "hello" -String.tests.cpp:: passed: rawChars == ss.currentData() for: "hello world!" == "hello world!" String.tests.cpp:: passed: ss.size() == 6 for: 6 == 6 String.tests.cpp:: passed: std::strcmp( ss.c_str(), "world!" ) == 0 for: 0 == 0 -String.tests.cpp:: passed: s.c_str() == s2.c_str() for: "hello world!" == "hello world!" -String.tests.cpp:: passed: s.c_str() != ss.c_str() for: "hello world!" != "hello" +String.tests.cpp:: passed: s.data() == s2.data() for: "hello world!" == "hello world!" +String.tests.cpp:: passed: s.data() == ss.data() for: "hello world!" == "hello world!" String.tests.cpp:: passed: s.substr(s.size() + 1, 123).empty() for: true -String.tests.cpp:: passed: StringRef("hello") == StringRef("hello") for: hello == hello -String.tests.cpp:: passed: StringRef("hello") != StringRef("cello") for: hello != cello +String.tests.cpp:: passed: std::strcmp(ss.c_str(), "world!") == 0 for: 0 == 0 +String.tests.cpp:: passed: buffer1 != buffer2 for: "Hello" != "Hello" +String.tests.cpp:: passed: left == right for: Hello == Hello +String.tests.cpp:: passed: left != left.substr(0, 3) for: Hello != Hel String.tests.cpp:: passed: sr == "a standard string" for: a standard string == "a standard string" String.tests.cpp:: passed: sr.size() == stdStr.size() for: 17 == 17 String.tests.cpp:: passed: sr == "a standard string" for: a standard string == "a standard string" @@ -1136,6 +1131,17 @@ String.tests.cpp:: passed: stdStr == "a stringref" for: "a stringre String.tests.cpp:: passed: stdStr.size() == sr.size() for: 11 == 11 String.tests.cpp:: passed: stdStr == "a stringref" for: "a stringref" == "a stringref" String.tests.cpp:: passed: stdStr.size() == sr.size() for: 11 == 11 +String.tests.cpp:: passed: with 1 message: 'StringRef{}.size() == 0' +String.tests.cpp:: passed: with 1 message: 'StringRef{ "abc", 3 }.size() == 3' +String.tests.cpp:: passed: with 1 message: 'StringRef{ "abc", 3 }.isNullTerminated()' +String.tests.cpp:: passed: with 1 message: 'StringRef{ "abc", 2 }.size() == 2' +String.tests.cpp:: passed: with 1 message: '!(StringRef{ "abc", 2 }.isNullTerminated())' +String.tests.cpp:: passed: with 1 message: '!(sr1.empty())' +String.tests.cpp:: passed: with 1 message: 'sr1.size() == 3' +String.tests.cpp:: passed: with 1 message: 'sr1.isNullTerminated()' +String.tests.cpp:: passed: with 1 message: 'sr2.empty()' +String.tests.cpp:: passed: with 1 message: 'sr2.size() == 0' +String.tests.cpp:: passed: with 1 message: 'sr2.isNullTerminated()' ToStringChrono.tests.cpp:: passed: minute == seconds for: 1 m == 60 s ToStringChrono.tests.cpp:: passed: hour != seconds for: 1 h != 60 s ToStringChrono.tests.cpp:: passed: micro != milli for: 1 us != 1 ms diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index fd405a84..f6f43449 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -1380,6 +1380,6 @@ due to unexpected exception with message: Why would you throw a std::string? =============================================================================== -test cases: 304 | 230 passed | 70 failed | 4 failed as expected -assertions: 1621 | 1469 passed | 131 failed | 21 failed as expected +test cases: 305 | 231 passed | 70 failed | 4 failed as expected +assertions: 1627 | 1475 passed | 131 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index f234f882..0e7f7162 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -7995,6 +7995,11 @@ String.tests.cpp:: PASSED: with expansion: 0 == 0 +String.tests.cpp:: PASSED: + REQUIRE( empty.isNullTerminated() ) +with expansion: + true + String.tests.cpp:: PASSED: REQUIRE( std::strcmp( empty.c_str(), "" ) == 0 ) with expansion: @@ -8018,27 +8023,17 @@ with expansion: 5 == 5 String.tests.cpp:: PASSED: - REQUIRE( isSubstring( s ) == false ) + REQUIRE( s.isNullTerminated() ) with expansion: - false == false + true String.tests.cpp:: PASSED: REQUIRE( std::strcmp( rawChars, "hello" ) == 0 ) with expansion: 0 == 0 -------------------------------------------------------------------------------- -StringRef - From string literal - c_str() does not cause copy -------------------------------------------------------------------------------- -String.tests.cpp: -............................................................................... - String.tests.cpp:: PASSED: - REQUIRE( isOwned( s ) == false ) -with expansion: - false == false + REQUIRE_NOTHROW( s.c_str() ) String.tests.cpp:: PASSED: REQUIRE( s.c_str() == rawChars ) @@ -8046,9 +8041,9 @@ with expansion: "hello" == "hello" String.tests.cpp:: PASSED: - REQUIRE( isOwned( s ) == false ) + REQUIRE( s.data() == rawChars ) with expansion: - false == false + "hello" == "hello" ------------------------------------------------------------------------------- StringRef @@ -8061,19 +8056,15 @@ String.tests.cpp:: PASSED: REQUIRE( original == "original" ) String.tests.cpp:: PASSED: - REQUIRE( isSubstring( original ) ) + REQUIRE_FALSE( original.isNullTerminated() ) with expansion: - true + !false String.tests.cpp:: PASSED: - REQUIRE( isOwned( original ) == false ) -with expansion: - false == false + REQUIRE_THROWS( original.c_str() ) String.tests.cpp:: PASSED: - REQUIRE( isOwned( original ) ) -with expansion: - true + REQUIRE_NOTHROW( original.data() ) ------------------------------------------------------------------------------- StringRef @@ -8094,7 +8085,7 @@ with expansion: 5 == 5 String.tests.cpp:: PASSED: - REQUIRE( std::strcmp( ss.c_str(), "hello" ) == 0 ) + REQUIRE( std::strncmp( ss.data(), "hello", 5 ) == 0 ) with expansion: 0 == 0 @@ -8103,63 +8094,6 @@ String.tests.cpp:: PASSED: with expansion: hello == "hello" -------------------------------------------------------------------------------- -StringRef - Substrings - c_str() causes copy -------------------------------------------------------------------------------- -String.tests.cpp: -............................................................................... - -String.tests.cpp:: PASSED: - REQUIRE( isSubstring( ss ) ) -with expansion: - true - -String.tests.cpp:: PASSED: - REQUIRE( isOwned( ss ) == false ) -with expansion: - false == false - -String.tests.cpp:: PASSED: - REQUIRE( rawChars == s.currentData() ) -with expansion: - "hello world!" == "hello world!" - -String.tests.cpp:: PASSED: - REQUIRE( ss.c_str() != rawChars ) -with expansion: - "hello" != "hello world!" - -String.tests.cpp:: PASSED: - REQUIRE( isOwned( ss ) ) -with expansion: - true - -------------------------------------------------------------------------------- -StringRef - Substrings - c_str() causes copy - Self-assignment after substring -------------------------------------------------------------------------------- -String.tests.cpp: -............................................................................... - -String.tests.cpp:: PASSED: - REQUIRE( isOwned(ss) == false ) -with expansion: - false == false - -String.tests.cpp:: PASSED: - REQUIRE( ss == "hello" ) -with expansion: - hello == "hello" - -String.tests.cpp:: PASSED: - REQUIRE( rawChars == ss.currentData() ) -with expansion: - "hello world!" == "hello world!" - ------------------------------------------------------------------------------- StringRef Substrings @@ -8187,22 +8121,22 @@ String.tests.cpp: ............................................................................... String.tests.cpp:: PASSED: - REQUIRE( s.c_str() == s2.c_str() ) + REQUIRE( s.data() == s2.data() ) with expansion: "hello world!" == "hello world!" ------------------------------------------------------------------------------- StringRef Substrings - Pointer values of substring refs should not match + Pointer values of substring refs should also match ------------------------------------------------------------------------------- String.tests.cpp: ............................................................................... String.tests.cpp:: PASSED: - REQUIRE( s.c_str() != ss.c_str() ) + REQUIRE( s.data() == ss.data() ) with expansion: - "hello world!" != "hello" + "hello world!" == "hello world!" ------------------------------------------------------------------------------- StringRef @@ -8219,20 +8153,38 @@ with expansion: ------------------------------------------------------------------------------- StringRef - Comparisons + Substrings + Substring off the end are trimmed ------------------------------------------------------------------------------- String.tests.cpp: ............................................................................... String.tests.cpp:: PASSED: - REQUIRE( StringRef("hello") == StringRef("hello") ) + REQUIRE( std::strcmp(ss.c_str(), "world!") == 0 ) with expansion: - hello == hello + 0 == 0 + +------------------------------------------------------------------------------- +StringRef + Comparisons are deep +------------------------------------------------------------------------------- +String.tests.cpp: +............................................................................... String.tests.cpp:: PASSED: - REQUIRE( StringRef("hello") != StringRef("cello") ) + CHECK( buffer1 != buffer2 ) with expansion: - hello != cello + "Hello" != "Hello" + +String.tests.cpp:: PASSED: + REQUIRE( left == right ) +with expansion: + Hello == Hello + +String.tests.cpp:: PASSED: + REQUIRE( left != left.substr(0, 3) ) +with expansion: + Hello != Hel ------------------------------------------------------------------------------- StringRef @@ -8324,6 +8276,64 @@ String.tests.cpp:: PASSED: with expansion: 11 == 11 +------------------------------------------------------------------------------- +StringRef at compilation time + Simple constructors +------------------------------------------------------------------------------- +String.tests.cpp: +............................................................................... + +String.tests.cpp:: PASSED: +with message: + StringRef{}.size() == 0 + +String.tests.cpp:: PASSED: +with message: + StringRef{ "abc", 3 }.size() == 3 + +String.tests.cpp:: PASSED: +with message: + StringRef{ "abc", 3 }.isNullTerminated() + +String.tests.cpp:: PASSED: +with message: + StringRef{ "abc", 2 }.size() == 2 + +String.tests.cpp:: PASSED: +with message: + !(StringRef{ "abc", 2 }.isNullTerminated()) + +------------------------------------------------------------------------------- +StringRef at compilation time + UDL construction +------------------------------------------------------------------------------- +String.tests.cpp: +............................................................................... + +String.tests.cpp:: PASSED: +with message: + !(sr1.empty()) + +String.tests.cpp:: PASSED: +with message: + sr1.size() == 3 + +String.tests.cpp:: PASSED: +with message: + sr1.isNullTerminated() + +String.tests.cpp:: PASSED: +with message: + sr2.empty() + +String.tests.cpp:: PASSED: +with message: + sr2.size() == 0 + +String.tests.cpp:: PASSED: +with message: + sr2.isNullTerminated() + ------------------------------------------------------------------------------- Stringifying std::chrono::duration helpers ------------------------------------------------------------------------------- @@ -12956,6 +12966,6 @@ Misc.tests.cpp: Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 304 | 214 passed | 86 failed | 4 failed as expected -assertions: 1638 | 1469 passed | 148 failed | 21 failed as expected +test cases: 305 | 215 passed | 86 failed | 4 failed as expected +assertions: 1644 | 1475 passed | 148 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index bfacb033..61b364f3 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -726,21 +726,21 @@ Matchers.tests.cpp: - - - - + - + + + + diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index 6193dfb3..4b67b803 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -9846,6 +9846,14 @@ Message from section two 0 == 0 + + + empty.isNullTerminated() + + + true + + std::strcmp( empty.c_str(), "" ) == 0 @@ -9854,7 +9862,7 @@ Message from section two 0 == 0 - +
@@ -9875,10 +9883,10 @@ Message from section two - isSubstring( s ) == false + s.isNullTerminated() - false == false + true @@ -9889,33 +9897,30 @@ Message from section two 0 == 0 -
- - - isOwned( s ) == false - - - false == false - - - - - s.c_str() == rawChars - - - "hello" == "hello" - - - - - isOwned( s ) == false - - - false == false - - - -
+ + + s.c_str() + + + s.c_str() + + + + + s.c_str() == rawChars + + + "hello" == "hello" + + + + + s.data() == rawChars + + + "hello" == "hello" + +
@@ -9927,28 +9932,28 @@ Message from section two original == "original" - + - isSubstring( original ) + !(original.isNullTerminated()) - true + !false - + - isOwned( original ) == false + original.c_str() - false == false + original.c_str() - + - isOwned( original ) + original.data() - true + original.data() @@ -9973,7 +9978,7 @@ Message from section two - std::strcmp( ss.c_str(), "hello" ) == 0 + std::strncmp( ss.data(), "hello", 5 ) == 0 0 == 0 @@ -9991,79 +9996,6 @@ Message from section two
-
-
- - - isSubstring( ss ) - - - true - - - - - isOwned( ss ) == false - - - false == false - - - - - rawChars == s.currentData() - - - "hello world!" == "hello world!" - - - - - ss.c_str() != rawChars - - - "hello" != "hello world!" - - - - - isOwned( ss ) - - - true - - -
- - - isOwned(ss) == false - - - false == false - - - - - ss == "hello" - - - hello == "hello" - - - - - rawChars == ss.currentData() - - - "hello world!" == "hello world!" - - - -
- -
- -
@@ -10090,7 +10022,7 @@ Message from section two
- s.c_str() == s2.c_str() + s.data() == s2.data() "hello world!" == "hello world!" @@ -10101,13 +10033,13 @@ Message from section two
-
+
- s.c_str() != ss.c_str() + s.data() == ss.data() - "hello world!" != "hello" + "hello world!" == "hello world!" @@ -10128,24 +10060,46 @@ Message from section two
-
- +
+
+ + + std::strcmp(ss.c_str(), "world!") == 0 + + + 0 == 0 + + + +
+ +
+
+ - StringRef("hello") == StringRef("hello") + buffer1 != buffer2 - hello == hello + "Hello" != "Hello" - StringRef("hello") != StringRef("cello") + left == right - hello != cello + Hello == Hello - + + + left != left.substr(0, 3) + + + Hello != Hel + + +
@@ -10259,6 +10213,15 @@ Message from section two
+ +
+ +
+
+ +
+ +
@@ -15427,7 +15390,7 @@ loose text artifact
- + - + diff --git a/projects/SelfTest/IntrospectiveTests/String.tests.cpp b/projects/SelfTest/IntrospectiveTests/String.tests.cpp index 456dd4a7..91a2b4ab 100644 --- a/projects/SelfTest/IntrospectiveTests/String.tests.cpp +++ b/projects/SelfTest/IntrospectiveTests/String.tests.cpp @@ -4,39 +4,15 @@ #include -namespace Catch { - - // Implementation of test accessors - struct StringRefTestAccess { - static auto isOwned( StringRef const& stringRef ) -> bool { - return stringRef.isOwned(); - } - static auto isSubstring( StringRef const& stringRef ) -> bool { - return stringRef.isSubstring(); - } - }; - - - namespace { - auto isOwned( StringRef const& stringRef ) -> bool { - return StringRefTestAccess::isOwned( stringRef ); - } - auto isSubstring( StringRef const& stringRef ) -> bool { - return StringRefTestAccess::isSubstring( stringRef ); - } - } // end anonymous namespace - -} // namespace Catch - TEST_CASE( "StringRef", "[Strings][StringRef]" ) { using Catch::StringRef; - using Catch::isOwned; using Catch::isSubstring; SECTION( "Empty string" ) { StringRef empty; REQUIRE( empty.empty() ); REQUIRE( empty.size() == 0 ); + REQUIRE( empty.isNullTerminated() ); REQUIRE( std::strcmp( empty.c_str(), "" ) == 0 ); } @@ -44,28 +20,22 @@ TEST_CASE( "StringRef", "[Strings][StringRef]" ) { StringRef s = "hello"; REQUIRE( s.empty() == false ); REQUIRE( s.size() == 5 ); - REQUIRE( isSubstring( s ) == false ); + REQUIRE( s.isNullTerminated() ); - auto rawChars = s.currentData(); + auto rawChars = s.data(); REQUIRE( std::strcmp( rawChars, "hello" ) == 0 ); - SECTION( "c_str() does not cause copy" ) { - REQUIRE( isOwned( s ) == false ); - - REQUIRE( s.c_str() == rawChars ); - - REQUIRE( isOwned( s ) == false ); - } + REQUIRE_NOTHROW(s.c_str()); + REQUIRE(s.c_str() == rawChars); + REQUIRE(s.data() == rawChars); } SECTION( "From sub-string" ) { StringRef original = StringRef( "original string" ).substr(0, 8); REQUIRE( original == "original" ); - REQUIRE( isSubstring( original ) ); - REQUIRE( isOwned( original ) == false ); - original.c_str(); // Forces it to take ownership - - REQUIRE( isOwned( original ) ); + REQUIRE_FALSE(original.isNullTerminated()); + REQUIRE_THROWS(original.c_str()); + REQUIRE_NOTHROW(original.data()); } @@ -76,26 +46,9 @@ TEST_CASE( "StringRef", "[Strings][StringRef]" ) { SECTION( "zero-based substring" ) { REQUIRE( ss.empty() == false ); REQUIRE( ss.size() == 5 ); - REQUIRE( std::strcmp( ss.c_str(), "hello" ) == 0 ); + REQUIRE( std::strncmp( ss.data(), "hello", 5 ) == 0 ); REQUIRE( ss == "hello" ); } - SECTION( "c_str() causes copy" ) { - REQUIRE( isSubstring( ss ) ); - REQUIRE( isOwned( ss ) == false ); - - auto rawChars = ss.currentData(); - REQUIRE( rawChars == s.currentData() ); // same pointer value - REQUIRE( ss.c_str() != rawChars ); - - REQUIRE( isOwned( ss ) ); - - SECTION( "Self-assignment after substring" ) { - ss = *&ss; // the *& are there to suppress warnings (see: "Improvements to Clang's diagnostics" in https://rev.ng/gitlab/revng-bar-2019/clang/raw/master/docs/ReleaseNotes.rst) - REQUIRE( isOwned(ss) == false ); - REQUIRE( ss == "hello" ); - REQUIRE( rawChars == ss.currentData() ); // same pointer value - } - } SECTION( "non-zero-based substring") { ss = s.substr( 6, 6 ); @@ -105,21 +58,32 @@ TEST_CASE( "StringRef", "[Strings][StringRef]" ) { SECTION( "Pointer values of full refs should match" ) { StringRef s2 = s; - REQUIRE( s.c_str() == s2.c_str() ); + REQUIRE( s.data() == s2.data() ); } - SECTION( "Pointer values of substring refs should not match" ) { - REQUIRE( s.c_str() != ss.c_str() ); + SECTION( "Pointer values of substring refs should also match" ) { + REQUIRE( s.data() == ss.data() ); } SECTION("Past the end substring") { REQUIRE(s.substr(s.size() + 1, 123).empty()); } + + SECTION("Substring off the end are trimmed") { + ss = s.substr(6, 123); + REQUIRE(std::strcmp(ss.c_str(), "world!") == 0); + } + // TODO: substring into string + size is longer than end } - SECTION( "Comparisons" ) { - REQUIRE( StringRef("hello") == StringRef("hello") ); - REQUIRE( StringRef("hello") != StringRef("cello") ); + SECTION( "Comparisons are deep" ) { + char buffer1[] = "Hello"; + char buffer2[] = "Hello"; + CHECK(buffer1 != buffer2); + + StringRef left(buffer1), right(buffer2); + REQUIRE( left == right ); + REQUIRE(left != left.substr(0, 3)); } SECTION( "from std::string" ) { @@ -159,3 +123,28 @@ TEST_CASE( "StringRef", "[Strings][StringRef]" ) { } } } + +TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") { + using Catch::StringRef; + SECTION("Simple constructors") { + STATIC_REQUIRE(StringRef{}.size() == 0); + + STATIC_REQUIRE(StringRef{ "abc", 3 }.size() == 3); + STATIC_REQUIRE(StringRef{ "abc", 3 }.isNullTerminated()); + + STATIC_REQUIRE(StringRef{ "abc", 2 }.size() == 2); + STATIC_REQUIRE_FALSE(StringRef{ "abc", 2 }.isNullTerminated()); + } + SECTION("UDL construction") { + constexpr auto sr1 = "abc"_catch_sr; + STATIC_REQUIRE_FALSE(sr1.empty()); + STATIC_REQUIRE(sr1.size() == 3); + STATIC_REQUIRE(sr1.isNullTerminated()); + + using Catch::operator"" _sr; + constexpr auto sr2 = ""_sr; + STATIC_REQUIRE(sr2.empty()); + STATIC_REQUIRE(sr2.size() == 0); + STATIC_REQUIRE(sr2.isNullTerminated()); + } +}