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
|
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/config.hpp"
#define CAF_SUITE metaprogramming
#include "caf/test/unit_test.hpp"
#include <string>
#include <cstdint>
#include <typeinfo>
#include <type_traits>
#include "caf/all.hpp"
#include "caf/detail/int_list.hpp"
#include "caf/detail/type_list.hpp"
using namespace caf;
using namespace caf::detail;
namespace {
// misc
template <class T>
struct is_int : std::false_type {};
template <>
struct is_int<int> : std::true_type {};
} // namespace
CAF_TEST(metaprogramming) {
using std::is_same;
using l1 = type_list<int, float, std::string>;
using r1 = tl_reverse<l1>::type;
CAF_CHECK((is_same<int, tl_at<l1, 0>::type>::value));
CAF_CHECK((is_same<float, tl_at<l1, 1>::type>::value));
CAF_CHECK((is_same<std::string, tl_at<l1, 2>::type>::value));
CAF_CHECK_EQUAL(3u, tl_size<l1>::value);
CAF_CHECK_EQUAL(tl_size<r1>::value, tl_size<l1>::value);
CAF_CHECK((is_same<tl_at<l1, 0>::type, tl_at<r1, 2>::type>::value));
CAF_CHECK((is_same<tl_at<l1, 1>::type, tl_at<r1, 1>::type>::value));
CAF_CHECK((is_same<tl_at<l1, 2>::type, tl_at<r1, 0>::type>::value));
using l2 = tl_concat<type_list<int>, l1>::type;
CAF_CHECK((is_same<int, tl_head<l2>::type>::value));
CAF_CHECK((is_same<l1, tl_tail<l2>::type>::value));
CAF_CHECK_EQUAL((detail::tl_count<l1, is_int>::value), 1u);
CAF_CHECK_EQUAL((detail::tl_count<l2, is_int>::value), 2u);
using il0 = int_list<0, 1, 2, 3, 4, 5>;
using il1 = int_list<4, 5>;
using il2 = il_right<il0, 2>::type;
CAF_CHECK((is_same<il2, il1>::value));
/* test tl_subset_of */ {
using list_a = type_list<int, float, double>;
using list_b = type_list<float, int, double, std::string>;
CAF_CHECK((tl_subset_of<list_a, list_b>::value));
CAF_CHECK(!(tl_subset_of<list_b, list_a>::value));
CAF_CHECK((tl_subset_of<list_a, list_a>::value));
CAF_CHECK((tl_subset_of<list_b, list_b>::value));
}
}
template <class T>
struct token { };
template <class T>
std::ostream& operator<<(std::ostream& out, token<T>) {
return out << typeid(T).name();
}
template <class T, class U>
constexpr bool operator==(token<T>, token<U>) {
return std::is_same<T, U>::value;
}
template <class T>
constexpr token<T> tk() {
return {};
}
template <class T, class U>
constexpr token<response_type_unbox_t<T, U>> res(token<T>, token<U>) {
return {};
}
template <class T, class U>
constexpr token<none_t> res(T, U) {
return {};
}
template <class T, class U>
constexpr token<composed_type_t<T, U>> dot_op(token<T>, token<U>) {
return {};
}
// -- lift a list of callback types into a list of MPIs
// -- typed behavior dummy class
template <class... Ifs>
struct typed_beh {
template <class... Ts>
typed_beh(Ts&&... xs) {
assign(std::forward<Ts>(xs)...);
}
template <class... Ts>
typename std::enable_if<sizeof...(Ifs) == sizeof...(Ts)>::type
assign(Ts...) {
using expected = type_list<Ifs...>;
using found = type_list<deduce_mpi_t<Ts>...>;
pos = interface_mismatch_t<found, expected>::value;
valid = pos == sizeof...(Ifs);
}
template <class... Ts>
typename std::enable_if<sizeof...(Ifs) != sizeof...(Ts)>::type
assign(Ts...) {
// too many or too few handlers present
pos = -1;
valid = false;
}
bool valid = false;
int pos = 0;
};
using bi_pair = std::pair<bool, int>;
template <class TypedBehavior, class... Ts>
bi_pair tb_assign(Ts&&... xs) {
TypedBehavior x{std::forward<Ts>(xs)...};
return {x.valid, x.pos};
}
namespace std {
ostream& operator<<(ostream& out, const pair<bool, int>& x) {
// do not modify stream with boolalpha
return out << '(' << (x.first ? "true" : "false") << ", " << x.second << ')';
}
} // namespace std
CAF_TEST(typed_behavior_assignment) {
using bh1 = typed_beh<replies_to<int>::with<double>,
replies_to<double, double>::with<int, int>>;
// compatible handlers resulting in perfect match
auto f1 = [=](int) { return 0.; };
auto f2 = [=](double, double) { return std::make_tuple(0, 0); };
// compatible handlers using skip
auto g1 = [=](int) { return skip(); };
auto g2 = [=](double, double) { return skip(); };
// incompatbile handlers
auto e1 = [=](int) { return 0.f; };
auto e2 = [=](double, double) { return std::make_tuple(0.f, 0.f); };
// omit one handler
CAF_CHECK_EQUAL(bi_pair(false, -1), tb_assign<bh1>(f1));
CAF_CHECK_EQUAL(bi_pair(false, -1), tb_assign<bh1>(f2));
CAF_CHECK_EQUAL(bi_pair(false, -1), tb_assign<bh1>(g1));
CAF_CHECK_EQUAL(bi_pair(false, -1), tb_assign<bh1>(g2));
CAF_CHECK_EQUAL(bi_pair(false, -1), tb_assign<bh1>(e1));
CAF_CHECK_EQUAL(bi_pair(false, -1), tb_assign<bh1>(e2));
// any valid alteration of (f1, f2, g1, g2)
CAF_CHECK_EQUAL(bi_pair(true, 2), tb_assign<bh1>(f1, f2));
CAF_CHECK_EQUAL(bi_pair(true, 2), tb_assign<bh1>(f2, f1));
CAF_CHECK_EQUAL(bi_pair(true, 2), tb_assign<bh1>(g1, g2));
CAF_CHECK_EQUAL(bi_pair(true, 2), tb_assign<bh1>(g2, g1));
CAF_CHECK_EQUAL(bi_pair(true, 2), tb_assign<bh1>(g1, f2));
CAF_CHECK_EQUAL(bi_pair(true, 2), tb_assign<bh1>(f2, g1));
CAF_CHECK_EQUAL(bi_pair(true, 2), tb_assign<bh1>(f1, g2));
CAF_CHECK_EQUAL(bi_pair(true, 2), tb_assign<bh1>(g2, f1));
// any invalid alteration of (f1, f2, g1, g2)
CAF_CHECK_EQUAL(bi_pair(false, 1), tb_assign<bh1>(f1, g1));
CAF_CHECK_EQUAL(bi_pair(false, 1), tb_assign<bh1>(g1, f1));
CAF_CHECK_EQUAL(bi_pair(false, 1), tb_assign<bh1>(f2, g2));
CAF_CHECK_EQUAL(bi_pair(false, 1), tb_assign<bh1>(g2, g2));
// any invalid alteration of (f1, f2, e1, e2)
CAF_CHECK_EQUAL(bi_pair(false, 1), tb_assign<bh1>(f1, e1));
CAF_CHECK_EQUAL(bi_pair(false, 1), tb_assign<bh1>(f1, e2));
CAF_CHECK_EQUAL(bi_pair(false, 0), tb_assign<bh1>(e1, f1));
CAF_CHECK_EQUAL(bi_pair(false, 0), tb_assign<bh1>(e1, f2));
CAF_CHECK_EQUAL(bi_pair(false, 0), tb_assign<bh1>(e1, e2));
CAF_CHECK_EQUAL(bi_pair(false, 1), tb_assign<bh1>(f2, e1));
CAF_CHECK_EQUAL(bi_pair(false, 1), tb_assign<bh1>(f2, e2));
CAF_CHECK_EQUAL(bi_pair(false, 0), tb_assign<bh1>(e2, f1));
CAF_CHECK_EQUAL(bi_pair(false, 0), tb_assign<bh1>(e2, f2));
CAF_CHECK_EQUAL(bi_pair(false, 0), tb_assign<bh1>(e2, e1));
using bh2 = typed_beh<reacts_to<int>,
reacts_to<int, int>,
reacts_to<int, int, int>,
reacts_to<int, int, int, int>,
reacts_to<int, int, int, int, int>,
reacts_to<int, int, int, int, int, int>,
reacts_to<int, int, int, int, int, int, int>,
reacts_to<int, int, int, int, int, int, int, int>,
reacts_to<int, int, int, int, int,
int, int, int, int>,
reacts_to<int, int, int, int, int,
int, int, int, int, int>>;
auto h0 = [](int) {};
auto h1 = [](int, int) {};
auto h2 = [](int, int, int) {};
auto h3 = [](int, int, int, int) {};
auto h4 = [](int, int, int, int, int) {};
auto h5 = [](int, int, int, int, int, int) {};
auto h6 = [](int, int, int, int, int, int, int) {};
auto h7 = [](int, int, int, int, int, int, int, int) {};
auto h8 = [](int, int, int, int, int, int, int, int, int) {};
auto h9 = [](int, int, int, int, int, int, int, int, int, int) {};
CAF_CHECK_EQUAL(bi_pair(true, 10), tb_assign<bh2>(h0, h1, h2, h3, h4,
h5, h6, h7, h8, h9));
CAF_CHECK_EQUAL(bi_pair(false, 0), tb_assign<bh2>(e1, h1, h2, h3, h4,
h5, h6, h7, h8, h9));
CAF_CHECK_EQUAL(bi_pair(false, 1), tb_assign<bh2>(h0, e1, h2, h3, h4,
h5, h6, h7, h8, h9));
CAF_CHECK_EQUAL(bi_pair(false, 2), tb_assign<bh2>(h0, h1, e1, h3, h4,
h5, h6, h7, h8, h9));
CAF_CHECK_EQUAL(bi_pair(false, 3), tb_assign<bh2>(h0, h1, h2, e1, h4,
h5, h6, h7, h8, h9));
CAF_CHECK_EQUAL(bi_pair(false, 4), tb_assign<bh2>(h0, h1, h2, h3, e1,
h5, h6, h7, h8, h9));
CAF_CHECK_EQUAL(bi_pair(false, 5), tb_assign<bh2>(h0, h1, h2, h3, h4,
e1, h6, h7, h8, h9));
CAF_CHECK_EQUAL(bi_pair(false, 6), tb_assign<bh2>(h0, h1, h2, h3, h4,
h5, e1, h7, h8, h9));
CAF_CHECK_EQUAL(bi_pair(false, 7), tb_assign<bh2>(h0, h1, h2, h3, h4,
h5, h6, e1, h8, h9));
CAF_CHECK_EQUAL(bi_pair(false, 8), tb_assign<bh2>(h0, h1, h2, h3, h4,
h5, h6, h7, e1, h9));
CAF_CHECK_EQUAL(bi_pair(false, 9), tb_assign<bh2>(h0, h1, h2, h3, h4,
h5, h6, h7, h8, e1));
CAF_CHECK_EQUAL(bi_pair(false, -1), tb_assign<bh2>(h0, h1, h2, h3, h4,
h5, h6, h7, h8));
}
struct foo {};
struct bar {};
bool operator==(const bar&, const bar&);
class baz {
public:
baz() = default;
explicit baz(std::string);
friend bool operator==(const baz&, const baz&);
private:
std::string str_;
};
CAF_TEST(is_comparable) {
CAF_CHECK((is_comparable<double, std::string>::value) == false);
CAF_CHECK((is_comparable<foo, foo>::value) == false);
CAF_CHECK((is_comparable<bar, bar>::value) == true);
CAF_CHECK((is_comparable<double, bar>::value) == false);
CAF_CHECK((is_comparable<bar, double>::value) == false);
CAF_CHECK((is_comparable<baz, baz>::value) == true);
CAF_CHECK((is_comparable<double, baz>::value) == false);
CAF_CHECK((is_comparable<baz, double>::value) == false);
CAF_CHECK((is_comparable<std::string, baz>::value) == false);
CAF_CHECK((is_comparable<baz, std::string>::value) == false);
}
|