catch2/include/internal/catch_evaluate.hpp

152 lines
6.1 KiB
C++
Raw Normal View History

/*
* Created by Phil on 04/03/2011.
* Copyright 2011 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)
*/
2011-03-09 10:38:33 +01:00
#ifndef TWOBLUECUBES_CATCH_EVALUATE_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_EVALUATE_HPP_INCLUDED
2012-05-15 08:42:26 +02:00
namespace Catch {
namespace Internal {
enum Operator {
2011-03-09 10:33:33 +01:00
IsEqualTo,
IsNotEqualTo,
IsLessThan,
IsGreaterThan,
IsLessThanOrEqualTo,
IsGreaterThanOrEqualTo
};
2011-03-09 20:45:05 +01:00
template<Operator Op> struct OperatorTraits { static const char* getName(){ return "*error*"; } };
template<> struct OperatorTraits<IsEqualTo> { static const char* getName(){ return "=="; } };
template<> struct OperatorTraits<IsNotEqualTo> { static const char* getName(){ return "!="; } };
template<> struct OperatorTraits<IsLessThan> { static const char* getName(){ return "<"; } };
template<> struct OperatorTraits<IsGreaterThan> { static const char* getName(){ return ">"; } };
template<> struct OperatorTraits<IsLessThanOrEqualTo> { static const char* getName(){ return "<="; } };
template<> struct OperatorTraits<IsGreaterThanOrEqualTo>{ static const char* getName(){ return ">="; } };
2011-03-09 20:45:05 +01:00
// So the compare overloads can be operator agnostic we convey the operator as a template
// enum, which is used to specialise an Evaluator for doing the comparison.
2011-03-09 10:33:33 +01:00
template<typename T1, typename T2, Operator Op>
class Evaluator{};
2011-03-09 10:33:33 +01:00
template<typename T1, typename T2>
2012-05-15 08:42:26 +02:00
struct Evaluator<T1, T2, IsEqualTo> {
static bool evaluate( const T1& lhs, const T2& rhs) {
return const_cast<T1&>( lhs ) == const_cast<T2&>( rhs );
2011-03-09 10:33:33 +01:00
}
};
2011-03-09 10:33:33 +01:00
template<typename T1, typename T2>
2012-05-15 08:42:26 +02:00
struct Evaluator<T1, T2, IsNotEqualTo> {
static bool evaluate( const T1& lhs, const T2& rhs ) {
return const_cast<T1&>( lhs ) != const_cast<T2&>( rhs );
2011-03-09 10:33:33 +01:00
}
};
2011-03-09 10:33:33 +01:00
template<typename T1, typename T2>
2012-05-15 08:42:26 +02:00
struct Evaluator<T1, T2, IsLessThan> {
static bool evaluate( const T1& lhs, const T2& rhs ) {
return const_cast<T1&>( lhs ) < const_cast<T2&>( rhs );
}
2011-03-09 10:33:33 +01:00
};
template<typename T1, typename T2>
2012-05-15 08:42:26 +02:00
struct Evaluator<T1, T2, IsGreaterThan> {
static bool evaluate( const T1& lhs, const T2& rhs ) {
return const_cast<T1&>( lhs ) > const_cast<T2&>( rhs );
}
};
template<typename T1, typename T2>
2012-05-15 08:42:26 +02:00
struct Evaluator<T1, T2, IsGreaterThanOrEqualTo> {
static bool evaluate( const T1& lhs, const T2& rhs ) {
return const_cast<T1&>( lhs ) >= const_cast<T2&>( rhs );
}
2011-03-09 10:33:33 +01:00
};
template<typename T1, typename T2>
2012-05-15 08:42:26 +02:00
struct Evaluator<T1, T2, IsLessThanOrEqualTo> {
static bool evaluate( const T1& lhs, const T2& rhs ) {
return const_cast<T1&>( lhs ) <= const_cast<T2&>( rhs );
}
};
template<Operator Op, typename T1, typename T2>
2012-05-15 08:42:26 +02:00
bool applyEvaluator( const T1& lhs, const T2& rhs ) {
return Evaluator<T1, T2, Op>::evaluate( lhs, rhs );
}
// "base" overload
2011-03-09 10:33:33 +01:00
template<Operator Op, typename T1, typename T2>
2012-05-15 08:42:26 +02:00
bool compare( const T1& lhs, const T2& rhs ) {
2011-03-09 10:33:33 +01:00
return Evaluator<T1, T2, Op>::evaluate( lhs, rhs );
}
// unsigned X to int
2012-05-15 08:42:26 +02:00
template<Operator Op> bool compare( unsigned int lhs, int rhs ) {
return applyEvaluator<Op>( lhs, static_cast<unsigned int>( rhs ) );
2011-03-09 10:33:33 +01:00
}
2012-05-15 08:42:26 +02:00
template<Operator Op> bool compare( unsigned long lhs, int rhs ) {
return applyEvaluator<Op>( lhs, static_cast<unsigned int>( rhs ) );
2011-03-09 10:33:33 +01:00
}
2012-05-15 08:42:26 +02:00
template<Operator Op> bool compare( unsigned char lhs, int rhs ) {
return applyEvaluator<Op>( lhs, static_cast<unsigned int>( rhs ) );
2011-03-09 10:33:33 +01:00
}
// unsigned X to long
2012-05-15 08:42:26 +02:00
template<Operator Op> bool compare( unsigned int lhs, long rhs ) {
return applyEvaluator<Op>( lhs, static_cast<unsigned long>( rhs ) );
2011-03-09 10:33:33 +01:00
}
2012-05-15 08:42:26 +02:00
template<Operator Op> bool compare( unsigned long lhs, long rhs ) {
return applyEvaluator<Op>( lhs, static_cast<unsigned long>( rhs ) );
2011-03-09 10:33:33 +01:00
}
2012-05-15 08:42:26 +02:00
template<Operator Op> bool compare( unsigned char lhs, long rhs ) {
return applyEvaluator<Op>( lhs, static_cast<unsigned long>( rhs ) );
2011-03-09 10:33:33 +01:00
}
// int to unsigned X
2012-05-15 08:42:26 +02:00
template<Operator Op> bool compare( int lhs, unsigned int rhs ) {
return applyEvaluator<Op>( static_cast<unsigned int>( lhs ), rhs );
2011-03-09 10:33:33 +01:00
}
2012-05-15 08:42:26 +02:00
template<Operator Op> bool compare( int lhs, unsigned long rhs ) {
return applyEvaluator<Op>( static_cast<unsigned int>( lhs ), rhs );
2011-03-09 10:33:33 +01:00
}
2012-05-15 08:42:26 +02:00
template<Operator Op> bool compare( int lhs, unsigned char rhs ) {
return applyEvaluator<Op>( static_cast<unsigned int>( lhs ), rhs );
2011-03-09 10:33:33 +01:00
}
// long to unsigned X
2012-05-15 08:42:26 +02:00
template<Operator Op> bool compare( long lhs, unsigned int rhs ) {
return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs );
2011-03-09 10:33:33 +01:00
}
2012-05-15 08:42:26 +02:00
template<Operator Op> bool compare( long lhs, unsigned long rhs ) {
return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs );
2011-03-09 10:33:33 +01:00
}
2012-05-15 08:42:26 +02:00
template<Operator Op> bool compare( long lhs, unsigned char rhs ) {
return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs );
2011-03-09 10:33:33 +01:00
}
2011-03-18 20:08:33 +01:00
template<Operator Op, typename T>
2012-05-15 08:42:26 +02:00
bool compare( long lhs, const T* rhs ) {
return Evaluator<const T*, const T*, Op>::evaluate( reinterpret_cast<const T*>( lhs ), rhs );
2011-03-18 20:08:33 +01:00
}
template<Operator Op, typename T>
2012-05-15 08:42:26 +02:00
bool compare( long lhs, T* rhs ) {
2011-03-18 20:08:33 +01:00
return Evaluator<T*, T*, Op>::evaluate( reinterpret_cast<T*>( lhs ), rhs );
}
template<Operator Op, typename T>
2012-05-15 08:42:26 +02:00
bool compare( const T* lhs, long rhs ) {
return Evaluator<const T*, const T*, Op>::evaluate( lhs, reinterpret_cast<const T*>( rhs ) );
2012-05-15 08:42:26 +02:00
}
template<Operator Op, typename T>
2012-05-15 08:42:26 +02:00
bool compare( T* lhs, long rhs ) {
return Evaluator<T*, T*, Op>::evaluate( lhs, reinterpret_cast<T*>( rhs ) );
2012-05-15 08:42:26 +02:00
}
} // end of namespace Internal
} // end of namespace Catch
2011-03-09 10:38:33 +01:00
#endif // TWOBLUECUBES_CATCH_EVALUATE_HPP_INCLUDED