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
|
2017-02-22 14:14:59 +01:00
|
|
|
#pragma warning(disable:4312) // Converting int to T* using reinterpret_cast (issue on x64 platform)
|
2013-03-25 09:46:48 +01:00
|
|
|
#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
|
|
|
|
2017-08-01 21:58:09 +02:00
|
|
|
const char* operatorName(Operator op);
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-04-22 19:55:12 +02:00
|
|
|
template<typename T>
|
2017-08-06 01:13:00 +02:00
|
|
|
T& removeConst(T const &t) { return const_cast<T&>(t); }
|
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 23:37:23 +01:00
|
|
|
template<typename T1, typename T2, Operator Op>
|
2017-07-25 17:16:28 +02:00
|
|
|
struct Evaluator{};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-02-06 23:37:23 +01:00
|
|
|
template<typename T1, typename T2>
|
|
|
|
struct Evaluator<T1, T2, IsEqualTo> {
|
|
|
|
static bool evaluate( T1 const& lhs, T2 const& rhs) {
|
2017-08-06 01:13:00 +02:00
|
|
|
return bool(removeConst(lhs) == removeConst(rhs) );
|
2011-03-09 10:33:33 +01:00
|
|
|
}
|
2011-03-09 10:32:03 +01:00
|
|
|
};
|
2017-02-06 23:37:23 +01:00
|
|
|
template<typename T1, typename T2>
|
|
|
|
struct Evaluator<T1, T2, IsNotEqualTo> {
|
|
|
|
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
|
2017-08-06 01:13:00 +02:00
|
|
|
return bool(removeConst(lhs) != removeConst(rhs) );
|
2011-03-09 10:33:33 +01:00
|
|
|
}
|
2011-03-09 10:32:03 +01:00
|
|
|
};
|
2017-02-06 23:37:23 +01:00
|
|
|
template<typename T1, typename T2>
|
|
|
|
struct Evaluator<T1, T2, IsLessThan> {
|
|
|
|
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
|
2017-08-06 01:13:00 +02:00
|
|
|
return bool(removeConst(lhs) < removeConst(rhs) );
|
2011-03-09 10:32:03 +01:00
|
|
|
}
|
2011-03-09 10:33:33 +01:00
|
|
|
};
|
2017-02-06 23:37:23 +01:00
|
|
|
template<typename T1, typename T2>
|
|
|
|
struct Evaluator<T1, T2, IsGreaterThan> {
|
|
|
|
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
|
2017-08-06 01:13:00 +02:00
|
|
|
return bool(removeConst(lhs) > removeConst(rhs) );
|
2011-03-09 10:32:03 +01:00
|
|
|
}
|
|
|
|
};
|
2017-02-06 23:37:23 +01:00
|
|
|
template<typename T1, typename T2>
|
|
|
|
struct Evaluator<T1, T2, IsGreaterThanOrEqualTo> {
|
|
|
|
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
|
2017-08-06 01:13:00 +02:00
|
|
|
return bool(removeConst(lhs) >= removeConst(rhs) );
|
2011-03-09 10:32:03 +01:00
|
|
|
}
|
2011-03-09 10:33:33 +01:00
|
|
|
};
|
2017-02-06 23:37:23 +01:00
|
|
|
template<typename T1, typename T2>
|
|
|
|
struct Evaluator<T1, T2, IsLessThanOrEqualTo> {
|
|
|
|
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
|
2017-08-06 01:13:00 +02:00
|
|
|
return bool(removeConst(lhs) <= removeConst(rhs) );
|
2011-03-09 10:32:03 +01:00
|
|
|
}
|
|
|
|
};
|
2012-11-06 20:13:25 +01:00
|
|
|
|
2017-08-06 01:13:00 +02:00
|
|
|
// Special case for comparing a pointer to an int (deduced for p==0)
|
|
|
|
template<typename T>
|
|
|
|
struct Evaluator<int const&, T* const&, IsEqualTo> {
|
|
|
|
static bool evaluate( int lhs, T* rhs) {
|
|
|
|
return reinterpret_cast<void const*>( lhs ) == rhs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename T>
|
|
|
|
struct Evaluator<T* const&, int const&, IsEqualTo> {
|
|
|
|
static bool evaluate( T* lhs, int rhs) {
|
|
|
|
return lhs == reinterpret_cast<void const*>( rhs );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename T>
|
|
|
|
struct Evaluator<int const&, T* const&, IsNotEqualTo> {
|
|
|
|
static bool evaluate( int lhs, T* rhs) {
|
|
|
|
return reinterpret_cast<void const*>( lhs ) != rhs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename T>
|
|
|
|
struct Evaluator<T* const&, int const&, IsNotEqualTo> {
|
|
|
|
static bool evaluate( T* lhs, int rhs) {
|
|
|
|
return lhs != reinterpret_cast<void const*>( rhs );
|
|
|
|
}
|
|
|
|
};
|
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
|