2017-08-08 18:53:01 +02:00
|
|
|
/*
|
|
|
|
* Created by Phil Nash on 8/8/2017.
|
|
|
|
* Copyright 2017 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_DECOMPOSER_H_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_DECOMPOSER_H_INCLUDED
|
|
|
|
|
|
|
|
#include "catch_tostring.h"
|
|
|
|
#include "catch_stringref.h"
|
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
#include <iosfwd>
|
2017-08-08 18:53:01 +02:00
|
|
|
|
2017-08-09 10:29:44 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4389) // '==' : signed/unsigned mismatch
|
|
|
|
#pragma warning(disable:4018) // more "signed/unsigned mismatch"
|
|
|
|
#pragma warning(disable:4312) // Converting int to T* using reinterpret_cast (issue on x64 platform)
|
2017-09-07 12:15:07 +02:00
|
|
|
#pragma warning(disable:4180) // qualifier applied to function type has no meaning
|
2017-08-09 10:29:44 +02:00
|
|
|
#endif
|
|
|
|
|
2017-08-08 18:53:01 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
|
|
|
struct ITransientExpression {
|
2017-11-27 20:49:26 +01:00
|
|
|
auto isBinaryExpression() const -> bool { return m_isBinaryExpression; }
|
|
|
|
auto getResult() const -> bool { return m_result; }
|
2017-08-08 18:53:01 +02:00
|
|
|
virtual void streamReconstructedExpression( std::ostream &os ) const = 0;
|
2017-08-09 10:29:44 +02:00
|
|
|
|
2017-11-27 20:49:26 +01:00
|
|
|
ITransientExpression( bool isBinaryExpression, bool result )
|
|
|
|
: m_isBinaryExpression( isBinaryExpression ),
|
|
|
|
m_result( result )
|
|
|
|
{}
|
|
|
|
|
|
|
|
// We don't actually need a virtual destructor, but many static analysers
|
2017-08-09 10:29:44 +02:00
|
|
|
// complain if it's not here :-(
|
2017-09-07 16:51:33 +02:00
|
|
|
virtual ~ITransientExpression();
|
2017-11-27 20:49:26 +01:00
|
|
|
|
|
|
|
bool m_isBinaryExpression;
|
|
|
|
bool m_result;
|
|
|
|
|
2017-08-08 18:53:01 +02:00
|
|
|
};
|
|
|
|
|
2017-09-06 15:15:48 +02:00
|
|
|
void formatReconstructedExpression( std::ostream &os, std::string const& lhs, StringRef op, std::string const& rhs );
|
2017-08-08 18:53:01 +02:00
|
|
|
|
|
|
|
template<typename LhsT, typename RhsT>
|
|
|
|
class BinaryExpr : public ITransientExpression {
|
|
|
|
LhsT m_lhs;
|
2017-09-06 15:15:48 +02:00
|
|
|
StringRef m_op;
|
2017-08-08 18:53:01 +02:00
|
|
|
RhsT m_rhs;
|
|
|
|
|
|
|
|
void streamReconstructedExpression( std::ostream &os ) const override {
|
|
|
|
formatReconstructedExpression
|
|
|
|
( os, Catch::Detail::stringify( m_lhs ), m_op, Catch::Detail::stringify( m_rhs ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-08-30 15:42:01 +02:00
|
|
|
BinaryExpr( bool comparisonResult, LhsT lhs, StringRef op, RhsT rhs )
|
2017-11-27 20:49:26 +01:00
|
|
|
: ITransientExpression{ true, comparisonResult },
|
2017-08-08 18:53:01 +02:00
|
|
|
m_lhs( lhs ),
|
2017-08-14 09:54:57 +02:00
|
|
|
m_op( op ),
|
2017-08-08 18:53:01 +02:00
|
|
|
m_rhs( rhs )
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename LhsT>
|
|
|
|
class UnaryExpr : public ITransientExpression {
|
|
|
|
LhsT m_lhs;
|
|
|
|
|
|
|
|
void streamReconstructedExpression( std::ostream &os ) const override {
|
|
|
|
os << Catch::Detail::stringify( m_lhs );
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-11-28 09:24:26 +01:00
|
|
|
explicit UnaryExpr( LhsT lhs )
|
2017-11-27 20:49:26 +01:00
|
|
|
: ITransientExpression{ false, lhs ? true : false },
|
|
|
|
m_lhs( lhs )
|
|
|
|
{}
|
2017-08-08 18:53:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Specialised comparison functions to handle equality comparisons between ints and pointers (NULL deduces as an int)
|
|
|
|
template<typename LhsT, typename RhsT>
|
2018-02-03 18:16:33 +01:00
|
|
|
auto compareEqual( LhsT const& lhs, RhsT const& rhs ) -> bool { return static_cast<bool>(lhs == rhs); }
|
2017-08-08 18:53:01 +02:00
|
|
|
template<typename T>
|
2017-09-07 16:51:33 +02:00
|
|
|
auto compareEqual( T* const& lhs, int rhs ) -> bool { return lhs == reinterpret_cast<void const*>( rhs ); }
|
2017-08-08 18:53:01 +02:00
|
|
|
template<typename T>
|
2017-09-07 16:51:33 +02:00
|
|
|
auto compareEqual( T* const& lhs, long rhs ) -> bool { return lhs == reinterpret_cast<void const*>( rhs ); }
|
2017-08-30 11:40:29 +02:00
|
|
|
template<typename T>
|
2017-09-07 16:51:33 +02:00
|
|
|
auto compareEqual( int lhs, T* const& rhs ) -> bool { return reinterpret_cast<void const*>( lhs ) == rhs; }
|
2017-08-30 11:40:29 +02:00
|
|
|
template<typename T>
|
2017-09-07 16:51:33 +02:00
|
|
|
auto compareEqual( long lhs, T* const& rhs ) -> bool { return reinterpret_cast<void const*>( lhs ) == rhs; }
|
2017-08-08 18:53:01 +02:00
|
|
|
|
|
|
|
template<typename LhsT, typename RhsT>
|
2018-02-03 18:16:33 +01:00
|
|
|
auto compareNotEqual( LhsT const& lhs, RhsT&& rhs ) -> bool { return static_cast<bool>(lhs != rhs); }
|
2017-08-08 18:53:01 +02:00
|
|
|
template<typename T>
|
2017-09-07 16:51:33 +02:00
|
|
|
auto compareNotEqual( T* const& lhs, int rhs ) -> bool { return lhs != reinterpret_cast<void const*>( rhs ); }
|
2017-08-08 18:53:01 +02:00
|
|
|
template<typename T>
|
2017-09-07 16:51:33 +02:00
|
|
|
auto compareNotEqual( T* const& lhs, long rhs ) -> bool { return lhs != reinterpret_cast<void const*>( rhs ); }
|
2017-08-30 11:40:29 +02:00
|
|
|
template<typename T>
|
2017-09-07 16:51:33 +02:00
|
|
|
auto compareNotEqual( int lhs, T* const& rhs ) -> bool { return reinterpret_cast<void const*>( lhs ) != rhs; }
|
2017-08-30 11:40:29 +02:00
|
|
|
template<typename T>
|
2017-09-07 16:51:33 +02:00
|
|
|
auto compareNotEqual( long lhs, T* const& rhs ) -> bool { return reinterpret_cast<void const*>( lhs ) != rhs; }
|
2017-08-08 18:53:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
template<typename LhsT>
|
|
|
|
class ExprLhs {
|
|
|
|
LhsT m_lhs;
|
|
|
|
public:
|
2017-11-28 09:24:26 +01:00
|
|
|
explicit ExprLhs( LhsT lhs ) : m_lhs( lhs ) {}
|
2017-08-08 18:53:01 +02:00
|
|
|
|
|
|
|
template<typename RhsT>
|
2017-10-13 15:16:14 +02:00
|
|
|
auto operator == ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
|
2017-11-28 09:24:26 +01:00
|
|
|
return { compareEqual( m_lhs, rhs ), m_lhs, "==", rhs };
|
2017-08-08 18:53:01 +02:00
|
|
|
}
|
|
|
|
auto operator == ( bool rhs ) -> BinaryExpr<LhsT, bool> const {
|
2017-11-28 09:24:26 +01:00
|
|
|
return { m_lhs == rhs, m_lhs, "==", rhs };
|
2017-08-08 18:53:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename RhsT>
|
2017-10-13 15:16:14 +02:00
|
|
|
auto operator != ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
|
2017-11-28 09:24:26 +01:00
|
|
|
return { compareNotEqual( m_lhs, rhs ), m_lhs, "!=", rhs };
|
2017-08-08 18:53:01 +02:00
|
|
|
}
|
|
|
|
auto operator != ( bool rhs ) -> BinaryExpr<LhsT, bool> const {
|
2017-11-28 09:24:26 +01:00
|
|
|
return { m_lhs != rhs, m_lhs, "!=", rhs };
|
2017-08-08 18:53:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename RhsT>
|
2017-10-13 15:16:14 +02:00
|
|
|
auto operator > ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
|
2018-01-12 15:43:56 +01:00
|
|
|
return { static_cast<bool>(m_lhs > rhs), m_lhs, ">", rhs };
|
2017-08-08 18:53:01 +02:00
|
|
|
}
|
|
|
|
template<typename RhsT>
|
2017-10-13 15:16:14 +02:00
|
|
|
auto operator < ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
|
2018-01-12 15:43:56 +01:00
|
|
|
return { static_cast<bool>(m_lhs < rhs), m_lhs, "<", rhs };
|
2017-08-08 18:53:01 +02:00
|
|
|
}
|
|
|
|
template<typename RhsT>
|
2017-10-13 15:16:14 +02:00
|
|
|
auto operator >= ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
|
2018-01-12 15:43:56 +01:00
|
|
|
return { static_cast<bool>(m_lhs >= rhs), m_lhs, ">=", rhs };
|
2017-08-08 18:53:01 +02:00
|
|
|
}
|
|
|
|
template<typename RhsT>
|
2017-10-13 15:16:14 +02:00
|
|
|
auto operator <= ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
|
2018-01-12 15:43:56 +01:00
|
|
|
return { static_cast<bool>(m_lhs <= rhs), m_lhs, "<=", rhs };
|
2017-08-08 18:53:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto makeUnaryExpr() const -> UnaryExpr<LhsT> {
|
2017-11-28 09:24:26 +01:00
|
|
|
return UnaryExpr<LhsT>{ m_lhs };
|
2017-08-08 18:53:01 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void handleExpression( ITransientExpression const& expr );
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void handleExpression( ExprLhs<T> const& expr ) {
|
|
|
|
handleExpression( expr.makeUnaryExpr() );
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Decomposer {
|
|
|
|
template<typename T>
|
|
|
|
auto operator <= ( T const& lhs ) -> ExprLhs<T const&> {
|
2017-11-28 09:24:26 +01:00
|
|
|
return ExprLhs<T const&>{ lhs };
|
2017-08-08 18:53:01 +02:00
|
|
|
}
|
2017-11-28 09:24:26 +01:00
|
|
|
|
2017-08-08 18:53:01 +02:00
|
|
|
auto operator <=( bool value ) -> ExprLhs<bool> {
|
2017-11-28 09:24:26 +01:00
|
|
|
return ExprLhs<bool>{ value };
|
2017-08-08 18:53:01 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
2017-10-30 09:27:00 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2017-08-08 18:53:01 +02:00
|
|
|
#endif // TWOBLUECUBES_CATCH_DECOMPOSER_H_INCLUDED
|