2011-03-09 10:32:03 +01:00
|
|
|
/*
|
|
|
|
* 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
|
2011-03-09 10:32:03 +01:00
|
|
|
|
2013-03-25 09:46:48 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4389) // '==' : signed/unsigned mismatch
|
|
|
|
#endif
|
|
|
|
|
2014-04-23 19:19:19 +02:00
|
|
|
#include <cstddef>
|
|
|
|
|
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 10:32:03 +01:00
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-05-21 19:52:09 +02: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 ">="; } };
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-04-22 19:55:12 +02:00
|
|
|
template<typename T>
|
2013-04-23 19:58:56 +02:00
|
|
|
inline T& opCast(T const& t) { return const_cast<T&>(t); }
|
2013-04-22 19:55:12 +02:00
|
|
|
|
|
|
|
// nullptr_t support based on pull request #154 from Konstantin Baumann
|
|
|
|
#ifdef CATCH_CONFIG_CPP11_NULLPTR
|
|
|
|
inline std::nullptr_t opCast(std::nullptr_t) { return nullptr; }
|
|
|
|
#endif // CATCH_CONFIG_CPP11_NULLPTR
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-03-25 09:46:48 +01:00
|
|
|
|
2011-03-15 23:22:19 +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.
|
2017-02-06 16:15:43 +01:00
|
|
|
template<Operator Op>
|
2011-03-09 10:33:33 +01:00
|
|
|
class Evaluator{};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-02-06 16:15:43 +01:00
|
|
|
template<>
|
|
|
|
struct Evaluator<IsEqualTo> {
|
|
|
|
template<typename T1, typename T2>
|
|
|
|
static bool evaluate( T1& lhs, T2& rhs) {
|
|
|
|
return static_cast<bool>( lhs == rhs );
|
2011-03-09 10:33:33 +01:00
|
|
|
}
|
2011-03-09 10:32:03 +01:00
|
|
|
};
|
2017-02-06 16:15:43 +01:00
|
|
|
template<>
|
|
|
|
struct Evaluator<IsNotEqualTo> {
|
|
|
|
template<typename T1, typename T2>
|
|
|
|
static bool evaluate( T1& lhs, T2& rhs ) {
|
|
|
|
return bool( lhs != rhs );
|
2011-03-09 10:33:33 +01:00
|
|
|
}
|
2011-03-09 10:32:03 +01:00
|
|
|
};
|
2017-02-06 16:15:43 +01:00
|
|
|
template<>
|
|
|
|
struct Evaluator<IsLessThan> {
|
|
|
|
template<typename T1, typename T2>
|
|
|
|
static bool evaluate( T1& lhs, T2& rhs ) {
|
|
|
|
return bool( lhs < rhs );
|
2011-03-09 10:32:03 +01:00
|
|
|
}
|
2011-03-09 10:33:33 +01:00
|
|
|
};
|
2017-02-06 16:15:43 +01:00
|
|
|
template<>
|
|
|
|
struct Evaluator<IsGreaterThan> {
|
|
|
|
template<typename T1, typename T2>
|
|
|
|
static bool evaluate( T1& lhs, T2& rhs ) {
|
|
|
|
return bool( lhs > rhs );
|
2011-03-09 10:32:03 +01:00
|
|
|
}
|
|
|
|
};
|
2017-02-06 16:15:43 +01:00
|
|
|
template<>
|
|
|
|
struct Evaluator<IsGreaterThanOrEqualTo> {
|
|
|
|
template<typename T1, typename T2>
|
|
|
|
static bool evaluate( T1& lhs, T2& rhs ) {
|
|
|
|
return bool( lhs >= rhs );
|
2011-03-09 10:32:03 +01:00
|
|
|
}
|
2011-03-09 10:33:33 +01:00
|
|
|
};
|
2017-02-06 16:15:43 +01:00
|
|
|
template<>
|
|
|
|
struct Evaluator<IsLessThanOrEqualTo> {
|
|
|
|
template<typename T1, typename T2>
|
|
|
|
static bool evaluate( T1& lhs, T2& rhs ) {
|
|
|
|
return bool( lhs <= rhs );
|
2011-03-09 10:32:03 +01:00
|
|
|
}
|
|
|
|
};
|
2012-11-06 20:13:25 +01:00
|
|
|
|
|
|
|
// This level of indirection allows us to specialise for integer types
|
|
|
|
// to avoid signed/ unsigned warnings
|
|
|
|
|
2011-03-15 23:22:19 +01:00
|
|
|
// "base" overload
|
2011-03-09 10:33:33 +01:00
|
|
|
template<Operator Op, typename T1, typename T2>
|
2013-04-23 19:58:56 +02:00
|
|
|
bool compare( T1 const& lhs, T2 const& rhs ) {
|
2017-02-06 16:15:43 +01:00
|
|
|
return Evaluator<Op>::evaluate( opCast( lhs ), opCast( rhs ) );
|
2011-03-09 10:33:33 +01:00
|
|
|
}
|
2011-03-18 20:08:33 +01:00
|
|
|
|
2012-05-24 19:41:14 +02:00
|
|
|
// pointer to long (when comparing against NULL)
|
2012-11-06 20:13:25 +01:00
|
|
|
template<Operator Op, typename T> bool compare( long lhs, T* rhs ) {
|
2017-02-06 16:15:43 +01:00
|
|
|
return Evaluator<Op>::evaluate( opCast( reinterpret_cast<T*>( lhs ) ), opCast( rhs ) );
|
2011-03-18 20:08:33 +01:00
|
|
|
}
|
2012-11-06 20:13:25 +01:00
|
|
|
template<Operator Op, typename T> bool compare( T* lhs, long rhs ) {
|
2017-02-06 16:15:43 +01:00
|
|
|
return Evaluator<Op>::evaluate( opCast( lhs ), opCast( reinterpret_cast<T*>( rhs ) ) );
|
2012-05-15 08:42:26 +02:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-05-24 19:41:14 +02:00
|
|
|
// pointer to int (when comparing against NULL)
|
2012-11-06 20:13:25 +01:00
|
|
|
template<Operator Op, typename T> bool compare( int lhs, T* rhs ) {
|
2017-02-06 16:15:43 +01:00
|
|
|
return Evaluator<Op>::evaluate( opCast( reinterpret_cast<T*>( lhs ) ), opCast( rhs ) );
|
2012-05-24 19:41:14 +02:00
|
|
|
}
|
2012-11-06 20:13:25 +01:00
|
|
|
template<Operator Op, typename T> bool compare( T* lhs, int rhs ) {
|
2017-02-06 16:15:43 +01:00
|
|
|
return Evaluator<Op>::evaluate( opCast( lhs ), opCast( reinterpret_cast<T*>( rhs ) ) );
|
2012-05-24 19:41:14 +02:00
|
|
|
}
|
2013-04-22 19:55:12 +02:00
|
|
|
|
2017-02-06 16:15:43 +01:00
|
|
|
// Needed?
|
2013-04-22 19:55:12 +02:00
|
|
|
#ifdef CATCH_CONFIG_CPP11_NULLPTR
|
2017-02-06 16:15:43 +01:00
|
|
|
// // pointer to nullptr_t (when comparing against nullptr)
|
|
|
|
// template<Operator Op, typename T> bool compare( std::nullptr_t, T* rhs ) {
|
|
|
|
// return Evaluator<T*, T*, Op>::evaluate( nullptr, rhs );
|
|
|
|
// }
|
|
|
|
// template<Operator Op, typename T> bool compare( T* lhs, std::nullptr_t ) {
|
|
|
|
// return Evaluator<T*, T*, Op>::evaluate( lhs, nullptr );
|
|
|
|
// }
|
2013-04-22 19:55:12 +02:00
|
|
|
#endif // CATCH_CONFIG_CPP11_NULLPTR
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2011-03-15 23:22:19 +01:00
|
|
|
} // end of namespace Internal
|
|
|
|
} // end of namespace Catch
|
2011-03-09 10:32:03 +01:00
|
|
|
|
2013-03-25 09:46:48 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2011-03-09 10:38:33 +01:00
|
|
|
#endif // TWOBLUECUBES_CATCH_EVALUATE_HPP_INCLUDED
|