1
0
mirror of https://github.com/catchorg/Catch2.git synced 2025-01-16 07:08:01 +00:00

Compare commits

...

8 Commits

Author SHA1 Message Date
Martin Hořeňovský
14533f5bb6
Make Colour's op<< hidden friend 2020-05-10 07:32:40 +02:00
Martin Hořeňovský
895d0a0696
Small improvements for StringRef
* `operator[]` is constexpr
* `operator<<` and `operator+=` are hidden friends
2020-05-10 07:22:11 +02:00
Martin Hořeňovský
094d840efe
Cleanup SourceLineInfo implementation
Special member functions are now implicit, which should make them
both noexcept and constexpr. The `operator<<` has been made into
hidden friend as per best practices.
2020-05-10 06:57:02 +02:00
Martin Hořeňovský
a595066ff9
Use internal linkage for float stringification helper 2020-05-10 06:54:23 +02:00
Martin Hořeňovský
cb25c4a8a3
Reinline some StringMaker impls 2020-05-09 21:26:42 +02:00
Martin Hořeňovský
b93cf932fb
Use UDLs to construct StringRefs for decomposed operators directly 2020-05-09 21:07:55 +02:00
Martin Hořeňovský
eef6c9b79b
Microopt: stream single char instead of single char string 2020-05-09 21:02:12 +02:00
Martin Hořeňovský
b5a287f09f
Make rest of the generators final 2020-05-09 20:56:25 +02:00
15 changed files with 80 additions and 82 deletions

View File

@ -53,7 +53,7 @@ namespace Catch {
return !(*this == other);
}
auto operator[] ( size_type index ) const noexcept -> char {
constexpr auto operator[] ( size_type index ) const noexcept -> char {
assert(index < m_size);
return m_start[index];
}
@ -97,10 +97,12 @@ namespace Catch {
public: // iterators
constexpr const_iterator begin() const { return m_start; }
constexpr const_iterator end() const { return m_start + m_size; }
friend std::string& operator += (std::string& lhs, StringRef const& sr);
friend std::ostream& operator << (std::ostream& os, StringRef const& sr);
};
auto operator += ( std::string& lhs, StringRef const& sr ) -> std::string&;
auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&;
constexpr auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef {
return StringRef( rawChars, size );

View File

@ -41,7 +41,27 @@ namespace Detail {
return value ? Little : Big;
}
};
}
template<typename T>
std::string fpToString(T value, int precision) {
if (Catch::isnan(value)) {
return "nan";
}
ReusableStringStream rss;
rss << std::setprecision(precision)
<< std::fixed
<< value;
std::string d = rss.str();
std::size_t i = d.find_last_not_of('0');
if (i != std::string::npos && i != d.size() - 1) {
if (d[i] == '.')
i++;
d = d.substr(0, i + 1);
}
return d;
}
} // end unnamed namespace
std::string rawMemoryToString( const void *object, std::size_t size ) {
// Reverse order for little endian architectures
@ -58,29 +78,9 @@ namespace Detail {
rss << std::setw(2) << static_cast<unsigned>(bytes[i]);
return rss.str();
}
}
} // end Detail namespace
template<typename T>
std::string fpToString( T value, int precision ) {
if (Catch::isnan(value)) {
return "nan";
}
ReusableStringStream rss;
rss << std::setprecision( precision )
<< std::fixed
<< value;
std::string d = rss.str();
std::size_t i = d.find_last_not_of( '0' );
if( i != std::string::npos && i != d.size()-1 ) {
if( d[i] == '.' )
i++;
d = d.substr( 0, i+1 );
}
return d;
}
//// ======================================================= ////
//
@ -201,11 +201,6 @@ std::string StringMaker<unsigned long long>::convert(unsigned long long value) {
return rss.str();
}
std::string StringMaker<bool>::convert(bool b) {
return b ? "true" : "false";
}
std::string StringMaker<signed char>::convert(signed char value) {
if (value == '\r') {
return "'\\r'";
@ -230,20 +225,16 @@ std::string StringMaker<unsigned char>::convert(unsigned char c) {
return ::Catch::Detail::stringify(static_cast<char>(c));
}
std::string StringMaker<std::nullptr_t>::convert(std::nullptr_t) {
return "nullptr";
}
int StringMaker<float>::precision = 5;
std::string StringMaker<float>::convert(float value) {
return fpToString(value, precision) + 'f';
return Detail::fpToString(value, precision) + 'f';
}
int StringMaker<double>::precision = 10;
std::string StringMaker<double>::convert(double value) {
return fpToString(value, precision);
return Detail::fpToString(value, precision);
}
} // end namespace Catch

View File

@ -239,7 +239,10 @@ namespace Catch {
template<>
struct StringMaker<bool> {
static std::string convert(bool b);
static std::string convert(bool b) {
using namespace std::string_literals;
return b ? "true"s : "false"s;
}
};
template<>
@ -257,7 +260,10 @@ namespace Catch {
template<>
struct StringMaker<std::nullptr_t> {
static std::string convert(std::nullptr_t);
static std::string convert(std::nullptr_t) {
using namespace std::string_literals;
return "nullptr"s;
}
};
template<>
@ -512,18 +518,14 @@ namespace Catch {
template <class Ratio>
struct ratio_string {
static std::string symbol();
static std::string symbol() {
Catch::ReusableStringStream rss;
rss << '[' << Ratio::num << '/'
<< Ratio::den << ']';
return rss.str();
}
};
template <class Ratio>
std::string ratio_string<Ratio>::symbol() {
Catch::ReusableStringStream rss;
rss << '[' << Ratio::num << '/'
<< Ratio::den << ']';
return rss.str();
}
template <>
struct ratio_string<std::atto> {
static std::string symbol() { return "a"; }

View File

@ -14,7 +14,7 @@ namespace Catch {
namespace Generators {
template <typename T>
class TakeGenerator : public IGenerator<T> {
class TakeGenerator final : public IGenerator<T> {
GeneratorWrapper<T> m_generator;
size_t m_returned = 0;
size_t m_target;
@ -51,7 +51,7 @@ namespace Generators {
template <typename T, typename Predicate>
class FilterGenerator : public IGenerator<T> {
class FilterGenerator final : public IGenerator<T> {
GeneratorWrapper<T> m_generator;
Predicate m_predicate;
public:
@ -91,7 +91,7 @@ namespace Generators {
}
template <typename T>
class RepeatGenerator : public IGenerator<T> {
class RepeatGenerator final : public IGenerator<T> {
static_assert(!std::is_same<T, bool>::value,
"RepeatGenerator currently does not support bools"
"because of std::vector<bool> specialization");
@ -147,7 +147,7 @@ namespace Generators {
}
template <typename T, typename U, typename Func>
class MapGenerator : public IGenerator<T> {
class MapGenerator final : public IGenerator<T> {
// TBD: provide static assert for mapping function, for friendly error message
GeneratorWrapper<U> m_generator;
Func m_function;

View File

@ -50,19 +50,15 @@ namespace Catch {
line( _line )
{}
SourceLineInfo( SourceLineInfo const& other ) = default;
SourceLineInfo& operator = ( SourceLineInfo const& ) = default;
SourceLineInfo( SourceLineInfo&& ) noexcept = default;
SourceLineInfo& operator = ( SourceLineInfo&& ) noexcept = default;
bool operator == ( SourceLineInfo const& other ) const noexcept;
bool operator < ( SourceLineInfo const& other ) const noexcept;
char const* file;
std::size_t line;
friend std::ostream& operator << (std::ostream& os, SourceLineInfo const& info);
};
std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info );
// Bring in operator<< from global namespace into Catch namespace
// This is necessary because the overload of operator<< above makes

View File

@ -60,9 +60,10 @@ namespace Catch {
private:
bool m_moved = false;
friend std::ostream& operator << (std::ostream& os, Colour const&);
};
std::ostream& operator << ( std::ostream& os, Colour const& );
} // end namespace Catch

View File

@ -12,13 +12,13 @@
namespace Catch {
ITransientExpression::~ITransientExpression() = default;
void formatReconstructedExpression( std::ostream &os, std::string const& lhs, StringRef op, std::string const& rhs ) {
if( lhs.size() + rhs.size() < 40 &&
lhs.find('\n') == std::string::npos &&
rhs.find('\n') == std::string::npos )
os << lhs << " " << op << " " << rhs;
os << lhs << ' ' << op << ' ' << rhs;
else
os << lhs << "\n" << op << "\n" << rhs;
os << lhs << '\n' << op << '\n' << rhs;
}
}

View File

@ -178,47 +178,47 @@ namespace Catch {
template<typename RhsT>
auto operator == ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
return { compareEqual( m_lhs, rhs ), m_lhs, "==", rhs };
return { compareEqual( m_lhs, rhs ), m_lhs, "=="_sr, rhs };
}
auto operator == ( bool rhs ) -> BinaryExpr<LhsT, bool> const {
return { m_lhs == rhs, m_lhs, "==", rhs };
return { m_lhs == rhs, m_lhs, "=="_sr, rhs };
}
template<typename RhsT>
auto operator != ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
return { compareNotEqual( m_lhs, rhs ), m_lhs, "!=", rhs };
return { compareNotEqual( m_lhs, rhs ), m_lhs, "!="_sr, rhs };
}
auto operator != ( bool rhs ) -> BinaryExpr<LhsT, bool> const {
return { m_lhs != rhs, m_lhs, "!=", rhs };
return { m_lhs != rhs, m_lhs, "!="_sr, rhs };
}
template<typename RhsT>
auto operator > ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
return { static_cast<bool>(m_lhs > rhs), m_lhs, ">", rhs };
return { static_cast<bool>(m_lhs > rhs), m_lhs, ">"_sr, rhs };
}
template<typename RhsT>
auto operator < ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
return { static_cast<bool>(m_lhs < rhs), m_lhs, "<", rhs };
return { static_cast<bool>(m_lhs < rhs), m_lhs, "<"_sr, rhs };
}
template<typename RhsT>
auto operator >= ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
return { static_cast<bool>(m_lhs >= rhs), m_lhs, ">=", rhs };
return { static_cast<bool>(m_lhs >= rhs), m_lhs, ">="_sr, rhs };
}
template<typename RhsT>
auto operator <= ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
return { static_cast<bool>(m_lhs <= rhs), m_lhs, "<=", rhs };
return { static_cast<bool>(m_lhs <= rhs), m_lhs, "<="_sr, rhs };
}
template <typename RhsT>
auto operator | (RhsT const& rhs) -> BinaryExpr<LhsT, RhsT const&> const {
return { static_cast<bool>(m_lhs | rhs), m_lhs, "|", rhs };
return { static_cast<bool>(m_lhs | rhs), m_lhs, "|"_sr, rhs };
}
template <typename RhsT>
auto operator & (RhsT const& rhs) -> BinaryExpr<LhsT, RhsT const&> const {
return { static_cast<bool>(m_lhs & rhs), m_lhs, "&", rhs };
return { static_cast<bool>(m_lhs & rhs), m_lhs, "&"_sr, rhs };
}
template <typename RhsT>
auto operator ^ (RhsT const& rhs) -> BinaryExpr<LhsT, RhsT const&> const {
return { static_cast<bool>(m_lhs ^ rhs), m_lhs, "^", rhs };
return { static_cast<bool>(m_lhs ^ rhs), m_lhs, "^"_sr, rhs };
}
template<typename RhsT>

View File

@ -1298,6 +1298,7 @@ String.tests.cpp:<line number>: passed: with 1 message: 'stringref.begin() == ab
String.tests.cpp:<line number>: passed: with 1 message: 'stringref.begin() != stringref.end()'
String.tests.cpp:<line number>: passed: with 1 message: 'stringref.substr(10, 0).empty()'
String.tests.cpp:<line number>: passed: with 1 message: 'stringref.substr(2, 1).data() == abc + 2'
String.tests.cpp:<line number>: passed: with 1 message: 'stringref[1] == 'b''
String.tests.cpp:<line number>: passed: with 1 message: 'shortened.size() == 2'
String.tests.cpp:<line number>: passed: with 1 message: 'shortened.data() == abc'
String.tests.cpp:<line number>: passed: with 1 message: 'shortened.begin() != shortened.end()'

View File

@ -1381,5 +1381,5 @@ due to unexpected exception with message:
===============================================================================
test cases: 334 | 260 passed | 70 failed | 4 failed as expected
assertions: 1895 | 1743 passed | 131 failed | 21 failed as expected
assertions: 1896 | 1744 passed | 131 failed | 21 failed as expected

View File

@ -9492,6 +9492,10 @@ String.tests.cpp:<line number>: PASSED:
with message:
stringref.substr(2, 1).data() == abc + 2
String.tests.cpp:<line number>: PASSED:
with message:
stringref[1] == 'b'
String.tests.cpp:<line number>: PASSED:
with message:
shortened.size() == 2
@ -14789,5 +14793,5 @@ Misc.tests.cpp:<line number>: PASSED:
===============================================================================
test cases: 334 | 244 passed | 86 failed | 4 failed as expected
assertions: 1912 | 1743 passed | 148 failed | 21 failed as expected
assertions: 1913 | 1744 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="1913" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testsuite name="<exe-name>" errors="17" failures="132" tests="1914" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<properties>
<property name="filters" value="~[!nonportable]~[!benchmark]~[approvals] *"/>
<property name="random-seed" value="1"/>

View File

@ -2510,6 +2510,8 @@ ok {test-number} - with 1 message: 'stringref.substr(10, 0).empty()'
# StringRef at compilation time
ok {test-number} - with 1 message: 'stringref.substr(2, 1).data() == abc + 2'
# StringRef at compilation time
ok {test-number} - with 1 message: 'stringref[1] == 'b''
# StringRef at compilation time
ok {test-number} - with 1 message: 'shortened.size() == 2'
# StringRef at compilation time
ok {test-number} - with 1 message: 'shortened.data() == abc'
@ -3816,5 +3818,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0
ok {test-number} -
# xmlentitycheck
ok {test-number} -
1..1904
1..1905

View File

@ -11728,7 +11728,7 @@ Message from section two
</TestCase>
<TestCase name="StringRef at compilation time" tags="[constexpr][StringRef][Strings]" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Section name="Simple constructors" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<OverallResults successes="14" failures="0" expectedFailures="0"/>
<OverallResults successes="15" failures="0" expectedFailures="0"/>
</Section>
<Section name="UDL construction" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<OverallResults successes="6" failures="0" expectedFailures="0"/>
@ -17756,7 +17756,7 @@ loose text artifact
</Section>
<OverallResult success="true"/>
</TestCase>
<OverallResults successes="1743" failures="149" expectedFailures="21"/>
<OverallResults successes="1744" failures="149" expectedFailures="21"/>
</Group>
<OverallResults successes="1743" failures="148" expectedFailures="21"/>
<OverallResults successes="1744" failures="148" expectedFailures="21"/>
</Catch>

View File

@ -126,8 +126,6 @@ TEST_CASE( "StringRef", "[Strings][StringRef]" ) {
}
TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") {
//TODO:
// * substr
using Catch::StringRef;
SECTION("Simple constructors") {
constexpr StringRef empty{};
@ -144,6 +142,7 @@ TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") {
STATIC_REQUIRE(stringref.begin() != stringref.end());
STATIC_REQUIRE(stringref.substr(10, 0).empty());
STATIC_REQUIRE(stringref.substr(2, 1).data() == abc + 2);
STATIC_REQUIRE(stringref[1] == 'b');
constexpr StringRef shortened(abc, 2);