Fix compilation for types where relops don't return bool

Closes #1147
This commit is contained in:
Martin Hořeňovský 2018-01-12 15:43:56 +01:00
parent 9700ee4fc0
commit de36b2ada6
8 changed files with 182 additions and 15 deletions

View File

@ -82,7 +82,7 @@ namespace Catch {
// Specialised comparison functions to handle equality comparisons between ints and pointers (NULL deduces as an int) // Specialised comparison functions to handle equality comparisons between ints and pointers (NULL deduces as an int)
template<typename LhsT, typename RhsT> template<typename LhsT, typename RhsT>
auto compareEqual( LhsT const& lhs, RhsT const& rhs ) -> bool { return lhs == rhs; }; auto compareEqual( LhsT const& lhs, RhsT const& rhs ) -> bool { return static_cast<bool>(lhs == rhs); };
template<typename T> template<typename T>
auto compareEqual( T* const& lhs, int rhs ) -> bool { return lhs == reinterpret_cast<void const*>( rhs ); } auto compareEqual( T* const& lhs, int rhs ) -> bool { return lhs == reinterpret_cast<void const*>( rhs ); }
template<typename T> template<typename T>
@ -93,7 +93,7 @@ namespace Catch {
auto compareEqual( long lhs, T* const& rhs ) -> bool { return reinterpret_cast<void const*>( lhs ) == rhs; } auto compareEqual( long lhs, T* const& rhs ) -> bool { return reinterpret_cast<void const*>( lhs ) == rhs; }
template<typename LhsT, typename RhsT> template<typename LhsT, typename RhsT>
auto compareNotEqual( LhsT const& lhs, RhsT&& rhs ) -> bool { return lhs != rhs; }; auto compareNotEqual( LhsT const& lhs, RhsT&& rhs ) -> bool { return static_cast<bool>(lhs != rhs); };
template<typename T> template<typename T>
auto compareNotEqual( T* const& lhs, int rhs ) -> bool { return lhs != reinterpret_cast<void const*>( rhs ); } auto compareNotEqual( T* const& lhs, int rhs ) -> bool { return lhs != reinterpret_cast<void const*>( rhs ); }
template<typename T> template<typename T>
@ -128,19 +128,19 @@ namespace Catch {
template<typename RhsT> template<typename RhsT>
auto operator > ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { auto operator > ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
return { m_lhs > rhs, m_lhs, ">", rhs }; return { static_cast<bool>(m_lhs > rhs), m_lhs, ">", rhs };
} }
template<typename RhsT> template<typename RhsT>
auto operator < ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { auto operator < ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
return { m_lhs < rhs, m_lhs, "<", rhs }; return { static_cast<bool>(m_lhs < rhs), m_lhs, "<", rhs };
} }
template<typename RhsT> template<typename RhsT>
auto operator >= ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { auto operator >= ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
return { m_lhs >= rhs, m_lhs, ">=", rhs }; return { static_cast<bool>(m_lhs >= rhs), m_lhs, ">=", rhs };
} }
template<typename RhsT> template<typename RhsT>
auto operator <= ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { auto operator <= ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
return { m_lhs <= rhs, m_lhs, "<=", rhs }; return { static_cast<bool>(m_lhs <= rhs), m_lhs, "<=", rhs };
} }
auto makeUnaryExpr() const -> UnaryExpr<LhsT> { auto makeUnaryExpr() const -> UnaryExpr<LhsT> {

View File

@ -3,6 +3,12 @@ Decomposition.tests.cpp:<line number>: passed: fptr == 0 for: 0 == 0
Decomposition.tests.cpp:<line number>: passed: fptr == 0l for: 0 == 0 Decomposition.tests.cpp:<line number>: passed: fptr == 0l for: 0 == 0
Compilation.tests.cpp:<line number>: passed: y.v == 0 for: 0 == 0 Compilation.tests.cpp:<line number>: passed: y.v == 0 for: 0 == 0
Compilation.tests.cpp:<line number>: passed: 0 == y.v for: 0 == 0 Compilation.tests.cpp:<line number>: passed: 0 == y.v for: 0 == 0
Compilation.tests.cpp:<line number>: passed: t1 == t2 for: {?} == {?}
Compilation.tests.cpp:<line number>: passed: t1 != t2 for: {?} != {?}
Compilation.tests.cpp:<line number>: passed: t1 < t2 for: {?} < {?}
Compilation.tests.cpp:<line number>: passed: t1 > t2 for: {?} > {?}
Compilation.tests.cpp:<line number>: passed: t1 <= t2 for: {?} <= {?}
Compilation.tests.cpp:<line number>: passed: t1 >= t2 for: {?} >= {?}
Exception.tests.cpp:<line number>: failed: unexpected exception with message: 'answer := 42' with 1 message: 'expected exception' Exception.tests.cpp:<line number>: failed: unexpected exception with message: 'answer := 42' with 1 message: 'expected exception'
Exception.tests.cpp:<line number>: failed: unexpected exception with message: 'answer := 42'; expression was: thisThrows() with 1 message: 'expected exception' Exception.tests.cpp:<line number>: failed: unexpected exception with message: 'answer := 42'; expression was: thisThrows() with 1 message: 'expected exception'
Exception.tests.cpp:<line number>: passed: thisThrows() with 1 message: 'answer := 42' Exception.tests.cpp:<line number>: passed: thisThrows() with 1 message: 'answer := 42'

View File

@ -1064,6 +1064,6 @@ with expansion:
"first" == "second" "first" == "second"
=============================================================================== ===============================================================================
test cases: 195 | 144 passed | 47 failed | 4 failed as expected test cases: 196 | 145 passed | 47 failed | 4 failed as expected
assertions: 983 | 857 passed | 105 failed | 21 failed as expected assertions: 989 | 863 passed | 105 failed | 21 failed as expected

View File

@ -51,6 +51,48 @@ PASSED:
with expansion: with expansion:
0 == 0 0 == 0
-------------------------------------------------------------------------------
#1147
-------------------------------------------------------------------------------
Compilation.tests.cpp:<line number>
...............................................................................
Compilation.tests.cpp:<line number>:
PASSED:
REQUIRE( t1 == t2 )
with expansion:
{?} == {?}
Compilation.tests.cpp:<line number>:
PASSED:
REQUIRE( t1 != t2 )
with expansion:
{?} != {?}
Compilation.tests.cpp:<line number>:
PASSED:
REQUIRE( t1 < t2 )
with expansion:
{?} < {?}
Compilation.tests.cpp:<line number>:
PASSED:
REQUIRE( t1 > t2 )
with expansion:
{?} > {?}
Compilation.tests.cpp:<line number>:
PASSED:
REQUIRE( t1 <= t2 )
with expansion:
{?} <= {?}
Compilation.tests.cpp:<line number>:
PASSED:
REQUIRE( t1 >= t2 )
with expansion:
{?} >= {?}
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
#748 - captures with unexpected exceptions #748 - captures with unexpected exceptions
outside assertions outside assertions
@ -8246,6 +8288,6 @@ Misc.tests.cpp:<line number>:
PASSED: PASSED:
=============================================================================== ===============================================================================
test cases: 195 | 142 passed | 49 failed | 4 failed as expected test cases: 196 | 143 passed | 49 failed | 4 failed as expected
assertions: 982 | 853 passed | 108 failed | 21 failed as expected assertions: 988 | 859 passed | 108 failed | 21 failed as expected

View File

@ -51,6 +51,48 @@ PASSED:
with expansion: with expansion:
0 == 0 0 == 0
-------------------------------------------------------------------------------
#1147
-------------------------------------------------------------------------------
Compilation.tests.cpp:<line number>
...............................................................................
Compilation.tests.cpp:<line number>:
PASSED:
REQUIRE( t1 == t2 )
with expansion:
{?} == {?}
Compilation.tests.cpp:<line number>:
PASSED:
REQUIRE( t1 != t2 )
with expansion:
{?} != {?}
Compilation.tests.cpp:<line number>:
PASSED:
REQUIRE( t1 < t2 )
with expansion:
{?} < {?}
Compilation.tests.cpp:<line number>:
PASSED:
REQUIRE( t1 > t2 )
with expansion:
{?} > {?}
Compilation.tests.cpp:<line number>:
PASSED:
REQUIRE( t1 <= t2 )
with expansion:
{?} <= {?}
Compilation.tests.cpp:<line number>:
PASSED:
REQUIRE( t1 >= t2 )
with expansion:
{?} >= {?}
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
#748 - captures with unexpected exceptions #748 - captures with unexpected exceptions
outside assertions outside assertions
@ -257,6 +299,6 @@ with expansion:
!true !true
=============================================================================== ===============================================================================
test cases: 10 | 7 passed | 1 failed | 2 failed as expected test cases: 11 | 8 passed | 1 failed | 2 failed as expected
assertions: 28 | 21 passed | 4 failed | 3 failed as expected assertions: 34 | 27 passed | 4 failed | 3 failed as expected

View File

@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact <testsuitesloose text artifact
> >
<testsuite name="<exe-name>" errors="15" failures="94" tests="983" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> <testsuite name="<exe-name>" errors="15" failures="94" tests="989" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testcase classname="<exe-name>.global" name="# A test name that starts with a #" time="{duration}"/> <testcase classname="<exe-name>.global" name="# A test name that starts with a #" time="{duration}"/>
<testcase classname="<exe-name>.global" name="#1005: Comparing pointer to int and long (NULL can be either on various systems)" time="{duration}"/> <testcase classname="<exe-name>.global" name="#1005: Comparing pointer to int and long (NULL can be either on various systems)" time="{duration}"/>
<testcase classname="<exe-name>.global" name="#1027" time="{duration}"/> <testcase classname="<exe-name>.global" name="#1027" time="{duration}"/>
<testcase classname="<exe-name>.global" name="#1147" time="{duration}"/>
<testcase classname="<exe-name>.global" name="#748 - captures with unexpected exceptions/outside assertions" time="{duration}"> <testcase classname="<exe-name>.global" name="#748 - captures with unexpected exceptions/outside assertions" time="{duration}">
<error type="TEST_CASE"> <error type="TEST_CASE">
expected exception expected exception

View File

@ -42,6 +42,57 @@
</Expression> </Expression>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<TestCase name="#1147" filename="projects/<exe-name>/UsageTests/Compilation.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Compilation.tests.cpp" >
<Original>
t1 == t2
</Original>
<Expanded>
{?} == {?}
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Compilation.tests.cpp" >
<Original>
t1 != t2
</Original>
<Expanded>
{?} != {?}
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Compilation.tests.cpp" >
<Original>
t1 &lt; t2
</Original>
<Expanded>
{?} &lt; {?}
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Compilation.tests.cpp" >
<Original>
t1 > t2
</Original>
<Expanded>
{?} > {?}
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Compilation.tests.cpp" >
<Original>
t1 &lt;= t2
</Original>
<Expanded>
{?} &lt;= {?}
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Compilation.tests.cpp" >
<Original>
t1 >= t2
</Original>
<Expanded>
{?} >= {?}
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="#748 - captures with unexpected exceptions" tags="[!shouldfail][!throws][.][failing]" filename="projects/<exe-name>/UsageTests/Exception.tests.cpp" > <TestCase name="#748 - captures with unexpected exceptions" tags="[!shouldfail][!throws][.][failing]" filename="projects/<exe-name>/UsageTests/Exception.tests.cpp" >
<Section name="outside assertions" filename="projects/<exe-name>/UsageTests/Exception.tests.cpp" > <Section name="outside assertions" filename="projects/<exe-name>/UsageTests/Exception.tests.cpp" >
<Info> <Info>
@ -9168,7 +9219,7 @@ loose text artifact
</Section> </Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<OverallResults successes="853" failures="109" expectedFailures="21"/> <OverallResults successes="859" failures="109" expectedFailures="21"/>
</Group> </Group>
<OverallResults successes="853" failures="108" expectedFailures="21"/> <OverallResults successes="859" failures="108" expectedFailures="21"/>
</Catch> </Catch>

View File

@ -12,6 +12,19 @@ namespace { namespace CompilationTests {
#ifndef COMPILATION_TEST_HELPERS_INCLUDED // Don't compile this more than once per TU #ifndef COMPILATION_TEST_HELPERS_INCLUDED // Don't compile this more than once per TU
#define COMPILATION_TEST_HELPERS_INCLUDED #define COMPILATION_TEST_HELPERS_INCLUDED
// Comparison operators can return non-booleans.
// This is unusual, but should be supported.
struct logic_t {
logic_t operator< (logic_t) const { return {}; }
logic_t operator<=(logic_t) const { return {}; }
logic_t operator> (logic_t) const { return {}; }
logic_t operator>=(logic_t) const { return {}; }
logic_t operator==(logic_t) const { return {}; }
logic_t operator!=(logic_t) const { return {}; }
explicit operator bool() const { return true; }
};
// This is a minimal example for an issue we have found in 1.7.0 // This is a minimal example for an issue we have found in 1.7.0
struct foo { struct foo {
int i; int i;
@ -109,4 +122,16 @@ namespace { namespace CompilationTests {
REQUIRE(0 == y.v); REQUIRE(0 == y.v);
} }
// Comparison operators can return non-booleans.
// This is unusual, but should be supported.
TEST_CASE("#1147") {
logic_t t1, t2;
REQUIRE(t1 == t2);
REQUIRE(t1 != t2);
REQUIRE(t1 < t2);
REQUIRE(t1 > t2);
REQUIRE(t1 <= t2);
REQUIRE(t1 >= t2);
}
}} // namespace CompilationTests }} // namespace CompilationTests