Rework StringRef interface and internals

Now it no longer tries to be this weird hybrid between an owning
and non-owning reference, and is only ever non-owning. This is also
reflected in its interface, for example `StringRef::isNullTerminated`
is now public, and `StringRef::c_str()` has the precondition that it
is true.

Overview of the changes:
* The `StringRef::m_data` member has been completely removed, as it
had no more uses.
* `StringRef::isSubstring()` has been made public and renamed to
`StringRef::isNullTerminated()`, so that the name reflects what the
method actually does.
* `StringRef::currentData()` has been renamed to `StringRef::data()`,
to be in line with common C++ containers and container-alikes.
* `StringRef::c_str()` will no longer silently make copies. It instead
has a precondition that `isNullTerminated()` is true.
* If the user needs a null-terminated string, they should use the
`std::string` conversion operator and call `c_str()` on the resulting
`std::string`.
* Some small optimizations in various places.
* Basic functionality is now `constexpr`.
This commit is contained in:
Martin Hořeňovský 2019-10-21 17:44:11 +02:00
parent 87b745da66
commit 50cc14c94c
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
8 changed files with 317 additions and 417 deletions

View File

@ -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 <algorithm>
#include <ostream>
#include <cstring>
#include <cstdint>
@ -22,63 +18,33 @@ namespace Catch {
: StringRef( rawChars, static_cast<StringRef::size_type>(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() )
CATCH_ENFORCE(isNullTerminated(), "Called StringRef::c_str() on a non-null-terminated instance");
return m_start;
const_cast<StringRef *>( this )->takeOwnership();
return m_data;
}
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, size );
else
if (start < m_size) {
return StringRef(m_start + start, (std::min)(m_size - 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 );
auto StringRef::operator == ( StringRef const& other ) const noexcept -> bool {
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

View File

@ -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 );
}

View File

@ -1095,37 +1095,32 @@ Matchers.tests.cpp:<line number>: passed: testStringForMatching(), EndsWith("sub
Matchers.tests.cpp:<line number>: passed: testStringForMatching(), EndsWith(" SuBsTrInG", Catch::CaseSensitive::No) for: "this string contains 'abc' as a substring" ends with: " substring" (case insensitive)
String.tests.cpp:<line number>: passed: empty.empty() for: true
String.tests.cpp:<line number>: passed: empty.size() == 0 for: 0 == 0
String.tests.cpp:<line number>: passed: empty.isNullTerminated() for: true
String.tests.cpp:<line number>: passed: std::strcmp( empty.c_str(), "" ) == 0 for: 0 == 0
String.tests.cpp:<line number>: passed: s.empty() == false for: false == false
String.tests.cpp:<line number>: passed: s.size() == 5 for: 5 == 5
String.tests.cpp:<line number>: passed: isSubstring( s ) == false for: false == false
String.tests.cpp:<line number>: passed: s.isNullTerminated() for: true
String.tests.cpp:<line number>: passed: std::strcmp( rawChars, "hello" ) == 0 for: 0 == 0
String.tests.cpp:<line number>: passed: isOwned( s ) == false for: false == false
String.tests.cpp:<line number>: passed: s.c_str()
String.tests.cpp:<line number>: passed: s.c_str() == rawChars for: "hello" == "hello"
String.tests.cpp:<line number>: passed: isOwned( s ) == false for: false == false
String.tests.cpp:<line number>: passed: s.data() == rawChars for: "hello" == "hello"
String.tests.cpp:<line number>: passed: original == "original"
String.tests.cpp:<line number>: passed: isSubstring( original ) for: true
String.tests.cpp:<line number>: passed: isOwned( original ) == false for: false == false
String.tests.cpp:<line number>: passed: isOwned( original ) for: true
String.tests.cpp:<line number>: passed: !(original.isNullTerminated()) for: !false
String.tests.cpp:<line number>: passed: original.c_str()
String.tests.cpp:<line number>: passed: original.data()
String.tests.cpp:<line number>: passed: ss.empty() == false for: false == false
String.tests.cpp:<line number>: passed: ss.size() == 5 for: 5 == 5
String.tests.cpp:<line number>: passed: std::strcmp( ss.c_str(), "hello" ) == 0 for: 0 == 0
String.tests.cpp:<line number>: passed: std::strncmp( ss.data(), "hello", 5 ) == 0 for: 0 == 0
String.tests.cpp:<line number>: passed: ss == "hello" for: hello == "hello"
String.tests.cpp:<line number>: passed: isSubstring( ss ) for: true
String.tests.cpp:<line number>: passed: isOwned( ss ) == false for: false == false
String.tests.cpp:<line number>: passed: rawChars == s.currentData() for: "hello world!" == "hello world!"
String.tests.cpp:<line number>: passed: ss.c_str() != rawChars for: "hello" != "hello world!"
String.tests.cpp:<line number>: passed: isOwned( ss ) for: true
String.tests.cpp:<line number>: passed: isOwned(ss) == false for: false == false
String.tests.cpp:<line number>: passed: ss == "hello" for: hello == "hello"
String.tests.cpp:<line number>: passed: rawChars == ss.currentData() for: "hello world!" == "hello world!"
String.tests.cpp:<line number>: passed: ss.size() == 6 for: 6 == 6
String.tests.cpp:<line number>: passed: std::strcmp( ss.c_str(), "world!" ) == 0 for: 0 == 0
String.tests.cpp:<line number>: passed: s.c_str() == s2.c_str() for: "hello world!" == "hello world!"
String.tests.cpp:<line number>: passed: s.c_str() != ss.c_str() for: "hello world!" != "hello"
String.tests.cpp:<line number>: passed: s.data() == s2.data() for: "hello world!" == "hello world!"
String.tests.cpp:<line number>: passed: s.data() == ss.data() for: "hello world!" == "hello world!"
String.tests.cpp:<line number>: passed: s.substr(s.size() + 1, 123).empty() for: true
String.tests.cpp:<line number>: passed: StringRef("hello") == StringRef("hello") for: hello == hello
String.tests.cpp:<line number>: passed: StringRef("hello") != StringRef("cello") for: hello != cello
String.tests.cpp:<line number>: passed: std::strcmp(ss.c_str(), "world!") == 0 for: 0 == 0
String.tests.cpp:<line number>: passed: buffer1 != buffer2 for: "Hello" != "Hello"
String.tests.cpp:<line number>: passed: left == right for: Hello == Hello
String.tests.cpp:<line number>: passed: left != left.substr(0, 3) for: Hello != Hel
String.tests.cpp:<line number>: passed: sr == "a standard string" for: a standard string == "a standard string"
String.tests.cpp:<line number>: passed: sr.size() == stdStr.size() for: 17 == 17
String.tests.cpp:<line number>: passed: sr == "a standard string" for: a standard string == "a standard string"
@ -1136,6 +1131,17 @@ String.tests.cpp:<line number>: passed: stdStr == "a stringref" for: "a stringre
String.tests.cpp:<line number>: passed: stdStr.size() == sr.size() for: 11 == 11
String.tests.cpp:<line number>: passed: stdStr == "a stringref" for: "a stringref" == "a stringref"
String.tests.cpp:<line number>: passed: stdStr.size() == sr.size() for: 11 == 11
String.tests.cpp:<line number>: passed: with 1 message: 'StringRef{}.size() == 0'
String.tests.cpp:<line number>: passed: with 1 message: 'StringRef{ "abc", 3 }.size() == 3'
String.tests.cpp:<line number>: passed: with 1 message: 'StringRef{ "abc", 3 }.isNullTerminated()'
String.tests.cpp:<line number>: passed: with 1 message: 'StringRef{ "abc", 2 }.size() == 2'
String.tests.cpp:<line number>: passed: with 1 message: '!(StringRef{ "abc", 2 }.isNullTerminated())'
String.tests.cpp:<line number>: passed: with 1 message: '!(sr1.empty())'
String.tests.cpp:<line number>: passed: with 1 message: 'sr1.size() == 3'
String.tests.cpp:<line number>: passed: with 1 message: 'sr1.isNullTerminated()'
String.tests.cpp:<line number>: passed: with 1 message: 'sr2.empty()'
String.tests.cpp:<line number>: passed: with 1 message: 'sr2.size() == 0'
String.tests.cpp:<line number>: passed: with 1 message: 'sr2.isNullTerminated()'
ToStringChrono.tests.cpp:<line number>: passed: minute == seconds for: 1 m == 60 s
ToStringChrono.tests.cpp:<line number>: passed: hour != seconds for: 1 h != 60 s
ToStringChrono.tests.cpp:<line number>: passed: micro != milli for: 1 us != 1 ms

View File

@ -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

View File

@ -7995,6 +7995,11 @@ String.tests.cpp:<line number>: PASSED:
with expansion:
0 == 0
String.tests.cpp:<line number>: PASSED:
REQUIRE( empty.isNullTerminated() )
with expansion:
true
String.tests.cpp:<line number>: PASSED:
REQUIRE( std::strcmp( empty.c_str(), "" ) == 0 )
with expansion:
@ -8018,27 +8023,17 @@ with expansion:
5 == 5
String.tests.cpp:<line number>: PASSED:
REQUIRE( isSubstring( s ) == false )
REQUIRE( s.isNullTerminated() )
with expansion:
false == false
true
String.tests.cpp:<line number>: PASSED:
REQUIRE( std::strcmp( rawChars, "hello" ) == 0 )
with expansion:
0 == 0
-------------------------------------------------------------------------------
StringRef
From string literal
c_str() does not cause copy
-------------------------------------------------------------------------------
String.tests.cpp:<line number>
...............................................................................
String.tests.cpp:<line number>: PASSED:
REQUIRE( isOwned( s ) == false )
with expansion:
false == false
REQUIRE_NOTHROW( s.c_str() )
String.tests.cpp:<line number>: PASSED:
REQUIRE( s.c_str() == rawChars )
@ -8046,9 +8041,9 @@ with expansion:
"hello" == "hello"
String.tests.cpp:<line number>: PASSED:
REQUIRE( isOwned( s ) == false )
REQUIRE( s.data() == rawChars )
with expansion:
false == false
"hello" == "hello"
-------------------------------------------------------------------------------
StringRef
@ -8061,19 +8056,15 @@ String.tests.cpp:<line number>: PASSED:
REQUIRE( original == "original" )
String.tests.cpp:<line number>: PASSED:
REQUIRE( isSubstring( original ) )
REQUIRE_FALSE( original.isNullTerminated() )
with expansion:
true
!false
String.tests.cpp:<line number>: PASSED:
REQUIRE( isOwned( original ) == false )
with expansion:
false == false
REQUIRE_THROWS( original.c_str() )
String.tests.cpp:<line number>: PASSED:
REQUIRE( isOwned( original ) )
with expansion:
true
REQUIRE_NOTHROW( original.data() )
-------------------------------------------------------------------------------
StringRef
@ -8094,7 +8085,7 @@ with expansion:
5 == 5
String.tests.cpp:<line number>: 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:<line number>: PASSED:
with expansion:
hello == "hello"
-------------------------------------------------------------------------------
StringRef
Substrings
c_str() causes copy
-------------------------------------------------------------------------------
String.tests.cpp:<line number>
...............................................................................
String.tests.cpp:<line number>: PASSED:
REQUIRE( isSubstring( ss ) )
with expansion:
true
String.tests.cpp:<line number>: PASSED:
REQUIRE( isOwned( ss ) == false )
with expansion:
false == false
String.tests.cpp:<line number>: PASSED:
REQUIRE( rawChars == s.currentData() )
with expansion:
"hello world!" == "hello world!"
String.tests.cpp:<line number>: PASSED:
REQUIRE( ss.c_str() != rawChars )
with expansion:
"hello" != "hello world!"
String.tests.cpp:<line number>: PASSED:
REQUIRE( isOwned( ss ) )
with expansion:
true
-------------------------------------------------------------------------------
StringRef
Substrings
c_str() causes copy
Self-assignment after substring
-------------------------------------------------------------------------------
String.tests.cpp:<line number>
...............................................................................
String.tests.cpp:<line number>: PASSED:
REQUIRE( isOwned(ss) == false )
with expansion:
false == false
String.tests.cpp:<line number>: PASSED:
REQUIRE( ss == "hello" )
with expansion:
hello == "hello"
String.tests.cpp:<line number>: PASSED:
REQUIRE( rawChars == ss.currentData() )
with expansion:
"hello world!" == "hello world!"
-------------------------------------------------------------------------------
StringRef
Substrings
@ -8187,22 +8121,22 @@ String.tests.cpp:<line number>
...............................................................................
String.tests.cpp:<line number>: 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:<line number>
...............................................................................
String.tests.cpp:<line number>: 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:<line number>
...............................................................................
String.tests.cpp:<line number>: 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:<line number>
...............................................................................
String.tests.cpp:<line number>: PASSED:
REQUIRE( StringRef("hello") != StringRef("cello") )
CHECK( buffer1 != buffer2 )
with expansion:
hello != cello
"Hello" != "Hello"
String.tests.cpp:<line number>: PASSED:
REQUIRE( left == right )
with expansion:
Hello == Hello
String.tests.cpp:<line number>: PASSED:
REQUIRE( left != left.substr(0, 3) )
with expansion:
Hello != Hel
-------------------------------------------------------------------------------
StringRef
@ -8324,6 +8276,64 @@ String.tests.cpp:<line number>: PASSED:
with expansion:
11 == 11
-------------------------------------------------------------------------------
StringRef at compilation time
Simple constructors
-------------------------------------------------------------------------------
String.tests.cpp:<line number>
...............................................................................
String.tests.cpp:<line number>: PASSED:
with message:
StringRef{}.size() == 0
String.tests.cpp:<line number>: PASSED:
with message:
StringRef{ "abc", 3 }.size() == 3
String.tests.cpp:<line number>: PASSED:
with message:
StringRef{ "abc", 3 }.isNullTerminated()
String.tests.cpp:<line number>: PASSED:
with message:
StringRef{ "abc", 2 }.size() == 2
String.tests.cpp:<line number>: PASSED:
with message:
!(StringRef{ "abc", 2 }.isNullTerminated())
-------------------------------------------------------------------------------
StringRef at compilation time
UDL construction
-------------------------------------------------------------------------------
String.tests.cpp:<line number>
...............................................................................
String.tests.cpp:<line number>: PASSED:
with message:
!(sr1.empty())
String.tests.cpp:<line number>: PASSED:
with message:
sr1.size() == 3
String.tests.cpp:<line number>: PASSED:
with message:
sr1.isNullTerminated()
String.tests.cpp:<line number>: PASSED:
with message:
sr2.empty()
String.tests.cpp:<line number>: PASSED:
with message:
sr2.size() == 0
String.tests.cpp:<line number>: PASSED:
with message:
sr2.isNullTerminated()
-------------------------------------------------------------------------------
Stringifying std::chrono::duration helpers
-------------------------------------------------------------------------------
@ -12956,6 +12966,6 @@ Misc.tests.cpp:<line number>
Misc.tests.cpp:<line number>: 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

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact
>
<testsuite name="<exe-name>" errors="17" failures="132" tests="1639" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testsuite name="<exe-name>" errors="17" failures="132" tests="1645" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<properties>
<property name="filters" value="~[!nonportable]~[!benchmark]~[approvals]"/>
<property name="random-seed" value="1"/>
@ -726,21 +726,21 @@ Matchers.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="String matchers" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/Empty string" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/From string literal" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/From string literal/c_str() does not cause copy" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/From sub-string" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/Substrings/zero-based substring" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/Substrings/c_str() causes copy" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/Substrings/c_str() causes copy/Self-assignment after substring" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/Substrings/non-zero-based substring" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/Substrings/Pointer values of full refs should match" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/Substrings/Pointer values of substring refs should not match" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/Substrings/Pointer values of substring refs should also match" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/Substrings/Past the end substring" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/Comparisons" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/Substrings/Substring off the end are trimmed" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/Comparisons are deep" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/from std::string/implicitly constructed" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/from std::string/explicitly constructed" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/from std::string/assigned" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/to std::string/explicitly constructed" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/to std::string/assigned" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef at compilation time/Simple constructors" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef at compilation time/UDL construction" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Stringifying std::chrono::duration helpers" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Stringifying std::chrono::duration with weird ratios" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Stringifying std::chrono::time_point&lt;system_clock>" time="{duration}"/>

View File

@ -9846,6 +9846,14 @@ Message from section two
0 == 0
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
empty.isNullTerminated()
</Original>
<Expanded>
true
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
std::strcmp( empty.c_str(), "" ) == 0
@ -9854,7 +9862,7 @@ Message from section two
0 == 0
</Expanded>
</Expression>
<OverallResults successes="3" failures="0" expectedFailures="0"/>
<OverallResults successes="4" failures="0" expectedFailures="0"/>
</Section>
<Section name="From string literal" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
@ -9875,10 +9883,10 @@ Message from section two
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
isSubstring( s ) == false
s.isNullTerminated()
</Original>
<Expanded>
false == false
true
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
@ -9889,13 +9897,12 @@ Message from section two
0 == 0
</Expanded>
</Expression>
<Section name="c_str() does not cause copy" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE_NOTHROW" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
isOwned( s ) == false
s.c_str()
</Original>
<Expanded>
false == false
s.c_str()
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
@ -9908,14 +9915,12 @@ Message from section two
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
isOwned( s ) == false
s.data() == rawChars
</Original>
<Expanded>
false == false
"hello" == "hello"
</Expanded>
</Expression>
<OverallResults successes="3" failures="0" expectedFailures="0"/>
</Section>
<OverallResults successes="7" failures="0" expectedFailures="0"/>
</Section>
<Section name="From sub-string" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
@ -9927,28 +9932,28 @@ Message from section two
original == "original"
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE_FALSE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
isSubstring( original )
!(original.isNullTerminated())
</Original>
<Expanded>
true
!false
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE_THROWS" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
isOwned( original ) == false
original.c_str()
</Original>
<Expanded>
false == false
original.c_str()
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE_NOTHROW" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
isOwned( original )
original.data()
</Original>
<Expanded>
true
original.data()
</Expanded>
</Expression>
<OverallResults successes="4" failures="0" expectedFailures="0"/>
@ -9973,7 +9978,7 @@ Message from section two
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
std::strcmp( ss.c_str(), "hello" ) == 0
std::strncmp( ss.data(), "hello", 5 ) == 0
</Original>
<Expanded>
0 == 0
@ -9991,79 +9996,6 @@ Message from section two
</Section>
<OverallResults successes="4" failures="0" expectedFailures="0"/>
</Section>
<Section name="Substrings" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Section name="c_str() causes copy" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
isSubstring( ss )
</Original>
<Expanded>
true
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
isOwned( ss ) == false
</Original>
<Expanded>
false == false
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
rawChars == s.currentData()
</Original>
<Expanded>
"hello world!" == "hello world!"
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
ss.c_str() != rawChars
</Original>
<Expanded>
"hello" != "hello world!"
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
isOwned( ss )
</Original>
<Expanded>
true
</Expanded>
</Expression>
<Section name="Self-assignment after substring" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
isOwned(ss) == false
</Original>
<Expanded>
false == false
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
ss == "hello"
</Original>
<Expanded>
hello == "hello"
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
rawChars == ss.currentData()
</Original>
<Expanded>
"hello world!" == "hello world!"
</Expanded>
</Expression>
<OverallResults successes="3" failures="0" expectedFailures="0"/>
</Section>
<OverallResults successes="8" failures="0" expectedFailures="0"/>
</Section>
<OverallResults successes="8" failures="0" expectedFailures="0"/>
</Section>
<Section name="Substrings" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Section name="non-zero-based substring" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
@ -10090,7 +10022,7 @@ Message from section two
<Section name="Pointer values of full refs should match" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
s.c_str() == s2.c_str()
s.data() == s2.data()
</Original>
<Expanded>
"hello world!" == "hello world!"
@ -10101,13 +10033,13 @@ Message from section two
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<Section name="Substrings" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Section name="Pointer values of substring refs should not match" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Section name="Pointer values of substring refs should also match" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
s.c_str() != ss.c_str()
s.data() == ss.data()
</Original>
<Expanded>
"hello world!" != "hello"
"hello world!" == "hello world!"
</Expanded>
</Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
@ -10128,24 +10060,46 @@ Message from section two
</Section>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<Section name="Comparisons" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Section name="Substrings" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Section name="Substring off the end are trimmed" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
StringRef("hello") == StringRef("hello")
std::strcmp(ss.c_str(), "world!") == 0
</Original>
<Expanded>
hello == hello
0 == 0
</Expanded>
</Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<Section name="Comparisons are deep" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="CHECK" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
buffer1 != buffer2
</Original>
<Expanded>
"Hello" != "Hello"
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
StringRef("hello") != StringRef("cello")
left == right
</Original>
<Expanded>
hello != cello
Hello == Hello
</Expanded>
</Expression>
<OverallResults successes="2" failures="0" expectedFailures="0"/>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
left != left.substr(0, 3)
</Original>
<Expanded>
Hello != Hel
</Expanded>
</Expression>
<OverallResults successes="3" failures="0" expectedFailures="0"/>
</Section>
<Section name="from std::string" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Section name="implicitly constructed" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
@ -10259,6 +10213,15 @@ Message from section two
</Section>
<OverallResult success="true"/>
</TestCase>
<TestCase name="StringRef at compilation time" tags="[StringRef][Strings][constexpr]" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Section name="Simple constructors" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<OverallResults successes="5" failures="0" expectedFailures="0"/>
</Section>
<Section name="UDL construction" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<OverallResults successes="6" failures="0" expectedFailures="0"/>
</Section>
<OverallResult success="true"/>
</TestCase>
<TestCase name="Stringifying std::chrono::duration helpers" tags="[chrono][toString]" filename="projects/<exe-name>/UsageTests/ToStringChrono.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringChrono.tests.cpp" >
<Original>
@ -15427,7 +15390,7 @@ loose text artifact
</Section>
<OverallResult success="true"/>
</TestCase>
<OverallResults successes="1469" failures="149" expectedFailures="21"/>
<OverallResults successes="1475" failures="149" expectedFailures="21"/>
</Group>
<OverallResults successes="1469" failures="148" expectedFailures="21"/>
<OverallResults successes="1475" failures="148" expectedFailures="21"/>
</Catch>

View File

@ -4,39 +4,15 @@
#include <cstring>
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());
}
}