Sprout/testspr/tools.hpp

335 lines
8.7 KiB
C++
Raw Normal View History

2011-12-23 11:59:33 +00:00
#ifndef TESTSPR_TOOLS_HPP
#define TESTSPR_TOOLS_HPP
#include <cassert>
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/container.hpp>
2011-12-23 11:59:33 +00:00
#ifdef TESTSPR_CONFIG_ENABLE_STATIC_WARNING
# include <boost/serialization/static_warning.hpp>
#endif
//
// TESTSPR_STATIC_ASSERT
// TESTSPR_ASSERT
// TESTSPR_BOTH_ASSERT
2011-12-23 11:59:33 +00:00
//
2012-04-13 13:42:44 +00:00
#define TESTSPR_STATIC_ASSERT(expr) static_assert(expr, #expr)
#define TESTSPR_ASSERT(expr) assert(expr)
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
# define TESTSPR_BOTH_ASSERT(expr) TESTSPR_STATIC_ASSERT(expr); TESTSPR_ASSERT(expr)
#else // #ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
# define TESTSPR_BOTH_ASSERT(expr) TESTSPR_ASSERT(expr)
#endif // #ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
2011-12-23 11:59:33 +00:00
//
// TESTSPR_STATIC_WARNING
//
#ifdef TESTSPR_CONFIG_ENABLE_STATIC_WARNING
# define TESTSPR_STATIC_WARNING(expr) BOOST_STATIC_WARNING(expr)
#else
# define TESTSPR_STATIC_WARNING(expr)
#endif
//
// TESTSPR_STATIC_UNCHECKED
// TESTSPR_UNCHECKED
// TESTSPR_BOTH_UNCHECKED
2011-12-23 11:59:33 +00:00
//
2012-04-13 13:42:44 +00:00
#define TESTSPR_STATIC_UNCHECKED(expr) TESTSPR_STATIC_WARNING(expr)
#define TESTSPR_UNCHECKED(expr) (expr)
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
# define TESTSPR_BOTH_UNCHECKED(expr) TESTSPR_STATIC_UNCHECKED(expr); TESTSPR_UNCHECKED(expr)
#else // #ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
# define TESTSPR_BOTH_UNCHECKED(expr) TESTSPR_UNCHECKED(expr)
#endif // #ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
2011-12-23 11:59:33 +00:00
namespace testspr {
//
// do_nothing
//
template<typename T>
inline SPROUT_CONSTEXPR bool
do_nothing(T const&) SPROUT_NOEXCEPT {
return true;
}
2011-12-23 11:59:33 +00:00
//
// is_even
//
template<typename T>
struct is_even {
2012-04-13 13:42:44 +00:00
public:
typedef T argument_type;
typedef bool result_type;
public:
2011-12-23 11:59:33 +00:00
SPROUT_CONSTEXPR bool operator()(T const& t) const { return t % 2 == 0; }
};
//
// is_odd
//
template<typename T>
struct is_odd {
2012-04-13 13:42:44 +00:00
public:
typedef T argument_type;
typedef bool result_type;
public:
2011-12-23 11:59:33 +00:00
SPROUT_CONSTEXPR bool operator()(T const& t) const { return t % 2 != 0; }
};
2012-09-21 11:07:31 +00:00
//
// is_multiple_of
//
template<typename T, typename U = T>
struct is_multiple_of {
public:
typedef T first_argument_type;
typedef U second_argument_type;
typedef bool result_type;
public:
SPROUT_CONSTEXPR bool operator()(T const& t, U const& u) const { return t % u == 0; }
};
2011-12-23 11:59:33 +00:00
//
// less
//
template<typename T>
struct less {
2012-04-13 13:42:44 +00:00
public:
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
public:
2011-12-23 11:59:33 +00:00
SPROUT_CONSTEXPR bool operator()(T const& lhs, T const& rhs) const { return lhs < rhs; }
};
//
// greater
//
template<typename T>
struct greater {
2012-04-13 13:42:44 +00:00
public:
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
public:
2011-12-23 11:59:33 +00:00
SPROUT_CONSTEXPR bool operator()(T const& lhs, T const& rhs) const { return lhs > rhs; }
};
//
// equal_to
//
template<typename T>
struct equal_to {
2012-04-13 13:42:44 +00:00
public:
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
public:
2011-12-23 11:59:33 +00:00
SPROUT_CONSTEXPR bool operator()(T const& lhs, T const& rhs) const { return lhs == rhs; }
};
//
// mod_less
//
template<typename T, T mod>
struct mod_less {
2012-04-13 13:42:44 +00:00
public:
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
public:
2011-12-23 11:59:33 +00:00
SPROUT_CONSTEXPR bool operator()(T const& lhs, T const& rhs) const { return lhs % mod < rhs % mod; }
};
2012-10-13 13:11:32 +00:00
//
// less_than
//
template<typename T>
struct less_than {
public:
typedef T argument_type;
typedef bool result_type;
public:
T value;
public:
explicit SPROUT_CONSTEXPR less_than(T const& value) : value(value) {}
SPROUT_CONSTEXPR bool operator()(T const& x) const { return x < value; }
};
//
// greater_than
//
template<typename T>
struct greater_than {
public:
typedef T argument_type;
typedef bool result_type;
public:
T value;
public:
explicit SPROUT_CONSTEXPR greater_than(T const& value) : value(value) {}
SPROUT_CONSTEXPR bool operator()(T const& x) const { return x > value; }
};
2011-12-23 11:59:33 +00:00
//
// x2
//
template<typename T>
struct x2 {
2012-04-13 13:42:44 +00:00
public:
typedef T argument_type;
typedef T result_type;
public:
2011-12-23 11:59:33 +00:00
SPROUT_CONSTEXPR T operator()(T const& t) const { return t + t; }
};
2012-04-13 13:42:44 +00:00
2011-12-23 11:59:33 +00:00
//
// plus
//
template<typename T>
struct plus {
2012-04-13 13:42:44 +00:00
public:
typedef T first_argument_type;
typedef T second_argument_type;
typedef T result_type;
public:
2011-12-23 11:59:33 +00:00
SPROUT_CONSTEXPR T operator()(T const& lhs, T const& rhs) const { return lhs + rhs; }
};
//
// gen_iota
//
template<typename T>
struct gen_iota {
public:
struct result {
public:
T val;
gen_iota gen;
public:
SPROUT_CONSTEXPR T const& generated_value() const { return val; }
SPROUT_CONSTEXPR gen_iota const& next_generator() const { return gen; }
};
private:
T val;
public:
explicit SPROUT_CONSTEXPR gen_iota(T const& val = T())
: val(val)
{}
SPROUT_CONSTEXPR result operator()() const { return result{val, gen_iota(val + 1)}; }
};
//
// unf_iota
//
template<typename T>
struct unf_iota {
public:
struct result {
public:
T val;
public:
SPROUT_CONSTEXPR T const& generated_value() const { return val; }
2012-09-28 07:05:09 +00:00
SPROUT_CONSTEXPR T next_generator() const { return val + 1; }
};
public:
2012-09-28 07:05:09 +00:00
SPROUT_CONSTEXPR result operator()(T const& val) const { return result{val}; }
};
2011-12-23 11:59:33 +00:00
//
// distance
//
2012-04-13 13:42:44 +00:00
template<typename InputIterator>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
2012-04-13 13:42:44 +00:00
distance(InputIterator first, InputIterator last) {
2011-12-23 11:59:33 +00:00
return first == last ? 0
: 1 + testspr::distance(first + 1, last)
;
}
//
// equal
//
2012-04-13 13:42:44 +00:00
template<typename InputIterator1, typename InputIterator2>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR bool
equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2) {
2011-12-23 11:59:33 +00:00
return first1 == last1 ? first2 == last2
: first2 == last2 ? false
: !(*first1 == *first2) ? false
: testspr::equal(first1 + 1, last1, first2 + 1, last2)
;
}
template<typename Range1, typename Range2>
inline SPROUT_CONSTEXPR bool
2012-10-06 04:53:07 +00:00
equal(Range1 const& range1, Range2 const& range2) {
2011-12-23 11:59:33 +00:00
return testspr::equal(sprout::begin(range1), sprout::end(range1), sprout::begin(range2), sprout::end(range2));
}
//
// is_found
//
2012-04-13 13:42:44 +00:00
template<class InputIterator, typename T>
inline SPROUT_CONSTEXPR bool
2012-10-06 04:53:07 +00:00
is_found(InputIterator first, InputIterator last, T const& value) {
2011-12-23 11:59:33 +00:00
return first == last ? false
: *first == value ? true
: testspr::is_found(first + 1, last, value)
;
}
template<typename Range, typename T>
inline SPROUT_CONSTEXPR bool
2012-10-06 04:53:07 +00:00
is_found(Range const& range, T const& value) {
2011-12-23 11:59:33 +00:00
return testspr::is_found(sprout::begin(range), sprout::end(range), value);
}
//
// count
//
2012-04-13 13:42:44 +00:00
template<typename InputIterator, typename T>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
2012-04-13 13:42:44 +00:00
count(InputIterator first, InputIterator last, T const& value) {
2011-12-23 11:59:33 +00:00
return first == last ? 0
: (*first == value ? 1 : 0) + testspr::count(first + 1, last, value)
;
}
template<typename Range, typename T>
inline SPROUT_CONSTEXPR typename std::iterator_traits<typename Range::const_iterator>::difference_type
2011-12-23 11:59:33 +00:00
count(Range const& range, T const& value) {
return testspr::count(sprout::begin(range), sprout::end(range), value);
}
namespace detail {
2012-04-13 13:42:44 +00:00
template<typename ForwardIterator1, typename ForwardIterator2>
inline SPROUT_CONSTEXPR bool
2012-10-06 04:53:07 +00:00
is_permutation_impl(
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
ForwardIterator1 first1_, ForwardIterator2 first2_
2012-04-13 13:42:44 +00:00
)
2011-12-23 11:59:33 +00:00
{
return first1_ == last1 && first2_ == last2 ? true
2012-10-10 15:15:34 +00:00
: testspr::count(first1, last1, *first1_) != testspr::count(first2, first2 + testspr::distance(first1, last1), *first1_)
? false
2011-12-23 11:59:33 +00:00
: testspr::detail::is_permutation_impl(first1, last1, first2, last2, first1_ + 1, first2_ + 1)
;
}
} // namespace detail
//
// is_permutation
//
2012-04-13 13:42:44 +00:00
template<typename ForwardIterator1, typename ForwardIterator2>
inline SPROUT_CONSTEXPR bool
2012-10-06 04:53:07 +00:00
is_permutation(
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2
2012-04-13 13:42:44 +00:00
)
{
2011-12-23 11:59:33 +00:00
return testspr::detail::is_permutation_impl(first1, last1, first2, last2, first1, first2);
}
template<typename Range1, typename Range2>
inline SPROUT_CONSTEXPR bool
2012-10-06 04:53:07 +00:00
is_permutation(Range1 const& range1, Range2 const& range2) {
return testspr::is_permutation(
sprout::begin(range1), sprout::end(range1),
sprout::begin(range2), sprout::end(range2)
);
2011-12-23 11:59:33 +00:00
}
} // namespace testspr
#endif // #ifndef TESTSPR_TOOLS_HPP