1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
// Copyright Louis Dionne 2013-2022
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
#ifndef BOOST_HANA_TEST_LAWS_BASE_HPP
#define BOOST_HANA_TEST_LAWS_BASE_HPP
#include <boost/hana/and.hpp>
#include <boost/hana/bool.hpp>
#include <boost/hana/core/when.hpp>
#include <boost/hana/detail/wrong.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/eval_if.hpp>
#include <boost/hana/for_each.hpp>
#include <boost/hana/functional/compose.hpp>
#include <boost/hana/functional/infix.hpp>
#include <boost/hana/functional/partial.hpp>
#include <boost/hana/fwd/concept/integral_constant.hpp>
#include <boost/hana/fwd/core/to.hpp>
#include <boost/hana/fwd/less.hpp>
#include <boost/hana/not.hpp>
#include <boost/hana/or.hpp>
#include <boost/hana/tuple.hpp>
#include <support/tracked.hpp>
#include <type_traits>
#include <utility>
namespace boost { namespace hana {
//////////////////////////////////////////////////////////////////////////
// Misc
//////////////////////////////////////////////////////////////////////////
namespace test {
struct laws;
template <int i>
struct for_each_n_t {
static_assert(i > 0, "can't use for_each_n with i < 0");
template <typename Xs, typename F>
constexpr auto operator()(Xs const& xs, F const& f) const {
hana::for_each(xs,
hana::compose(
hana::partial(for_each_n_t<i - 1>{}, xs),
hana::partial(hana::partial, f)
)
);
}
};
template <>
struct for_each_n_t<1> {
template <typename Xs, typename F>
constexpr auto operator()(Xs const& xs, F const& f) const {
hana::for_each(xs, f);
}
};
template <int i>
constexpr for_each_n_t<i> for_each_n{};
auto foreach = hana::for_each;
constexpr auto foreach3 = for_each_n<3>;
constexpr auto foreach2 = for_each_n<2>;
struct implies_t {
template <typename P, typename Q>
constexpr decltype(auto) operator()(P&& p, Q&& q) const {
return hana::or_(hana::not_(static_cast<P&&>(p)),
static_cast<Q&&>(q));
}
};
constexpr auto implies = hana::infix(implies_t{});
struct iff_t {
template <typename P, typename Q>
constexpr decltype(auto) operator()(P&& p, Q&& q) const {
return hana::and_(implies(p, q), implies(q, p));
}
};
constexpr auto iff = hana::infix(iff_t{});
template <typename Cond, typename F>
constexpr decltype(auto) only_when_(Cond cond, F f) {
return hana::eval_if(cond, f, [](auto){ });
}
// A type with a constructor that must not be instantiated.
// This is to make sure we don't instantiate something else than
// the copy-constructor of the elements inside a container when we
// copy the container.
struct trap_construct {
trap_construct() = default;
trap_construct(trap_construct const&) = default;
#ifndef BOOST_HANA_WORKAROUND_MSVC_MULTIPLECTOR_106654
trap_construct(trap_construct&) = default;
#endif
trap_construct(trap_construct&&) = default;
template <typename X>
trap_construct(X&&) {
static_assert(detail::wrong<X>{},
"this constructor must not be instantiated");
}
};
// A move-only type. Useful for testing containers.
struct move_only {
move_only() = default;
move_only(move_only const&) = delete;
move_only(move_only&&) = default;
};
//////////////////////////////////////////////////////////////////////
// InjectionResult
//////////////////////////////////////////////////////////////////////
struct InjectionResult { };
template <int i, typename ...X>
struct injection_result {
using hana_tag = InjectionResult;
static constexpr int injection_id = i;
hana::tuple<X...> args;
Tracked tracker;
template <typename ...Y, typename = decltype(tuple<X...>{std::declval<Y>()...})>
constexpr explicit injection_result(Y&& ...y)
: args{static_cast<Y&&>(y)...}, tracker{i}
{ }
};
//! A monotonic injective function.
//!
//! This is used in the unit tests, where we often just need a function
//! which preserves equality and order, but which also satisfies the
//! following law for all `Injection`s `f` and `g`:
//! @code
//! f(x) == g(x) if and only if f === g
//! @endcode
//! where `===` means _was created by the same call to `injection`_.
//! This allows creating several such functions in the unit tests while
//! conserving precious information such as the fact that
//! `f(g(x)) != g(f(x))`.
template <int i>
struct _injection {
template <typename ...X>
constexpr auto operator()(X&& ...x) const {
return injection_result<i,
typename std::decay<X>::type...
>{static_cast<X&&>(x)...};
}
};
} // end namespace test
template <>
struct equal_impl<test::InjectionResult, test::InjectionResult> {
template <typename X, typename Y>
static constexpr auto apply(X x, Y y) {
return hana::and_(
hana::bool_c<X::injection_id == Y::injection_id>,
hana::equal(x.args, y.args)
);
}
};
template <>
struct less_impl<test::InjectionResult, test::InjectionResult> {
template <typename X, typename Y>
static constexpr auto apply(X x, Y y) {
static_assert(X::injection_id == Y::injection_id,
"can't order the result of two different injections");
return hana::less(x.args, y.args);
}
};
//////////////////////////////////////////////////////////////////////////
// Integer
//////////////////////////////////////////////////////////////////////////
namespace test {
enum class Policy : int {
// One of those is mandatory
Constant = 1 << 0
, Constexpr = 1 << 1
, Runtime = 1 << 2
// Those are optional
, Tracked = 1 << 3
, Comparable = 1 << 4
, Orderable = 1 << 5
};
constexpr bool operator&&(Policy a, Policy b) {
return static_cast<int>(a) && static_cast<int>(b);
}
constexpr bool operator&&(Policy a, bool b) {
return static_cast<int>(a) && b;
}
constexpr bool operator&&(bool a, Policy b) {
return a && static_cast<int>(b);
}
constexpr bool operator||(Policy a, Policy b) {
return static_cast<int>(a) || static_cast<int>(b);
}
constexpr bool operator||(Policy a, bool b) {
return static_cast<int>(a) || b;
}
constexpr bool operator||(bool a, Policy b) {
return a || static_cast<int>(b);
}
constexpr bool operator!(Policy a) {
return !static_cast<int>(a);
}
constexpr Policy operator|(Policy a, Policy b) {
return static_cast<Policy>(static_cast<int>(a) | static_cast<int>(b));
}
constexpr Policy operator&(Policy a, Policy b) {
return static_cast<Policy>(static_cast<int>(a) & static_cast<int>(b));
}
template <Policy policy, typename = void>
struct Integer { };
template <Policy policy>
struct Integer<policy, std::enable_if_t<!!(policy & Policy::Constant)>> {
using value_type = int;
};
template <int i, Policy policy, typename = void>
struct integer {
static_assert(
!!(policy & (Policy::Constant | Policy::Constexpr | Policy::Runtime))
, "You must choose at least one of Constant, Constexpr and Runtime.");
static constexpr int value = i;
constexpr operator int() const { return value; }
using hana_tag = Integer<policy>;
Tracked tracker{i};
};
template <int i, Policy policy>
struct integer <i, policy, std::enable_if_t<!!(policy & Policy::Constexpr)>> {
static constexpr int value = i;
constexpr operator int() const { return value; }
using hana_tag = Integer<policy>;
};
template <int i>
struct eq : integer<i, Policy::Comparable | Policy::Runtime> { };
template <int i>
struct ct_eq : integer<i, Policy::Comparable | Policy::Constant> { };
template <int i>
struct cx_eq : integer<i, Policy::Comparable | Policy::Constexpr> { };
template <int i>
struct ord : integer<i, Policy::Orderable | Policy::Runtime> { };
template <int i>
struct ct_ord : integer<i, Policy::Orderable | Policy::Constant> { };
template <int i>
struct cx_ord : integer<i, Policy::Orderable | Policy::Constexpr> { };
template <int i>
struct _constant
: integer<i, Policy::Constant | Policy::Comparable | Policy::Orderable>
{ };
}
//////////////////////////////////////////////////////////////////////////
// Model of Constant/IntegralConstant
//////////////////////////////////////////////////////////////////////////
template <test::Policy policy>
struct IntegralConstant<test::Integer<policy>> {
static constexpr bool value = static_cast<bool>(policy & test::Policy::Constant);
};
template <test::Policy policy, typename C>
struct to_impl<test::Integer<policy>, C, when<
(policy & test::Policy::Constant) &&
hana::IntegralConstant<C>::value
>>
: embedding<is_embedded<typename C::value_type, int>::value>
{
template <typename N>
static constexpr auto apply(N const&) {
return test::integer<N::value, policy>{};
}
};
//////////////////////////////////////////////////////////////////////////
// Model of Comparable
//////////////////////////////////////////////////////////////////////////
template <test::Policy p1, test::Policy p2>
struct equal_impl<test::Integer<p1>, test::Integer<p2>, when<
// both Comparable or Orderable
(p1 & (test::Policy::Comparable | test::Policy::Orderable)) &&
(p2 & (test::Policy::Comparable | test::Policy::Orderable)) &&
// one Constexpr and the other Constant, or both Constexpr
(((p1 & test::Policy::Constant) && (p2 & test::Policy::Constexpr)) ||
((p1 & test::Policy::Constexpr) && (p2 & test::Policy::Constant)) ||
((p1 & test::Policy::Constexpr) && (p2 & test::Policy::Constexpr)))
>> {
template <typename X, typename Y>
static constexpr bool apply(X const&, Y const&)
{ return X::value == Y::value; }
};
template <test::Policy p1, test::Policy p2>
struct equal_impl<test::Integer<p1>, test::Integer<p2>, when<
// both Comparable or Orderable
(p1 & (test::Policy::Comparable | test::Policy::Orderable)) &&
(p2 & (test::Policy::Comparable | test::Policy::Orderable)) &&
// either one is Runtime
((p1 & test::Policy::Runtime) || (p2 & test::Policy::Runtime))
>> {
template <typename X, typename Y>
static bool apply(X const&, Y const&)
{ return X::value == Y::value; }
};
//////////////////////////////////////////////////////////////////////////
// Model of Orderable
//////////////////////////////////////////////////////////////////////////
template <test::Policy p1, test::Policy p2>
struct less_impl<test::Integer<p1>, test::Integer<p2>, when<
// both Orderable
(p1 & test::Policy::Orderable) && (p2 & test::Policy::Orderable) &&
// one Constexpr and the other Constant, or both Constexpr
(((p1 & test::Policy::Constant) && (p2 & test::Policy::Constexpr)) ||
((p1 & test::Policy::Constexpr) && (p2 & test::Policy::Constant)) ||
((p1 & test::Policy::Constexpr) && (p2 & test::Policy::Constexpr)))
>> {
template <typename X, typename Y>
static constexpr bool apply(X const&, Y const&)
{ return X::value < Y::value; }
};
template <test::Policy p1, test::Policy p2>
struct less_impl<test::Integer<p1>, test::Integer<p2>, when<
// both Orderable
(p1 & test::Policy::Orderable) && (p2 & test::Policy::Orderable) &&
// either one is Runtime
((p1 & test::Policy::Runtime) || (p2 & test::Policy::Runtime))
>> {
template <typename X, typename Y>
static bool apply(X const&, Y const&)
{ return X::value < Y::value; }
};
}} // end namespace boost::hana
#endif // !BOOST_HANA_TEST_LAWS_BASE_HPP
|