// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2008 Gael Guennebaud // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. #include "main.h" #include #include template bool check_is_convertible(const From&, const To&) { return internal::is_convertible::value; } struct FooReturnType { typedef int ReturnType; }; struct MyInterface { virtual void func() = 0; virtual ~MyInterface() {} }; struct MyImpl : public MyInterface { void func() {} }; EIGEN_DECLARE_TEST(meta) { VERIFY((internal::is_same::value)); VERIFY((!internal::is_same::value)); VERIFY((!internal::is_same::value)); VERIFY((!internal::is_same::value)); VERIFY((internal::is_same>::value)); VERIFY((internal::is_same>::value)); VERIFY((internal::is_same>::value)); VERIFY((internal::is_same>::value)); VERIFY((internal::is_same>::value)); VERIFY((internal::is_same>::value)); VERIFY((internal::is_same>::value)); // test add_const_on_value_type VERIFY((internal::is_same, float const&>::value)); VERIFY((internal::is_same, float const*>::value)); VERIFY((internal::is_same, const float>::value)); VERIFY((internal::is_same, const float>::value)); VERIFY((internal::is_same, const float* const>::value)); VERIFY((internal::is_same, const float* const>::value)); // is_convertible STATIC_CHECK((internal::is_convertible::value)); STATIC_CHECK((internal::is_convertible::value)); STATIC_CHECK((internal::is_convertible::value)); STATIC_CHECK((internal::is_convertible::value)); STATIC_CHECK((internal::is_convertible::value)); STATIC_CHECK((internal::is_convertible>::value)); STATIC_CHECK((!internal::is_convertible, double>::value)); STATIC_CHECK((internal::is_convertible::value)); STATIC_CHECK((internal::is_convertible::value)); STATIC_CHECK((internal::is_convertible::value)); STATIC_CHECK((internal::is_convertible::value)); STATIC_CHECK((internal::is_convertible::value)); STATIC_CHECK((internal::is_convertible::value)); STATIC_CHECK((!internal::is_convertible::value)); STATIC_CHECK((!internal::is_convertible::value)); STATIC_CHECK(!(internal::is_convertible::value)); STATIC_CHECK(!(internal::is_convertible::value)); STATIC_CHECK((internal::is_convertible::value)); // STATIC_CHECK((!internal::is_convertible::value )); //does not even compile because the // conversion is prevented by a static assertion STATIC_CHECK((!internal::is_convertible::value)); STATIC_CHECK((!internal::is_convertible::value)); { float f = 0.0f; MatrixXf A, B; VectorXf a, b; VERIFY((check_is_convertible(a.dot(b), f))); VERIFY((check_is_convertible(a.transpose() * b, f))); VERIFY((!check_is_convertible(A * B, f))); VERIFY((check_is_convertible(A * B, A))); } #if (EIGEN_COMP_GNUC_STRICT && EIGEN_COMP_GNUC <= 990) || (EIGEN_COMP_CLANG_STRICT && EIGEN_COMP_CLANG <= 990) || \ (EIGEN_COMP_MSVC && EIGEN_COMP_MSVC <= 1914) // See http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1752, // basically, a fix in the c++ standard breaks our c++98 implementation // of is_convertible for abstract classes. // So the following tests are expected to fail with recent compilers. STATIC_CHECK((!internal::is_convertible::value)); STATIC_CHECK((!internal::is_convertible::value)); STATIC_CHECK((internal::is_convertible::value)); #endif { int i = 0; VERIFY((check_is_convertible(fix<3>(), i))); VERIFY((!check_is_convertible(i, fix()))); } VERIFY((internal::has_ReturnType::value)); VERIFY((internal::has_ReturnType>::value)); VERIFY((!internal::has_ReturnType::value)); VERIFY((!internal::has_ReturnType::value)); VERIFY(internal::meta_sqrt<1>::ret == 1); #define VERIFY_META_SQRT(X) VERIFY(internal::meta_sqrt::ret == int(std::sqrt(double(X)))) VERIFY_META_SQRT(2); VERIFY_META_SQRT(3); VERIFY_META_SQRT(4); VERIFY_META_SQRT(5); VERIFY_META_SQRT(6); VERIFY_META_SQRT(8); VERIFY_META_SQRT(9); VERIFY_META_SQRT(15); VERIFY_META_SQRT(16); VERIFY_META_SQRT(17); VERIFY_META_SQRT(255); VERIFY_META_SQRT(256); VERIFY_META_SQRT(257); VERIFY_META_SQRT(1023); VERIFY_META_SQRT(1024); VERIFY_META_SQRT(1025); } using Eigen::internal::apply_op_from_left; using Eigen::internal::apply_op_from_right; using Eigen::internal::arg_prod; using Eigen::internal::arg_sum; using Eigen::internal::array_apply; using Eigen::internal::array_apply_and_reduce; using Eigen::internal::array_prod; using Eigen::internal::array_reduce; using Eigen::internal::array_reverse; using Eigen::internal::array_sum; using Eigen::internal::array_zip; using Eigen::internal::array_zip_and_reduce; using Eigen::internal::concat; using Eigen::internal::contained_in_list; using Eigen::internal::contained_in_list_gf; using Eigen::internal::gen_numeric_list; using Eigen::internal::gen_numeric_list_repeated; using Eigen::internal::gen_numeric_list_reversed; using Eigen::internal::gen_numeric_list_swapped_pair; using Eigen::internal::get; using Eigen::internal::id_numeric; using Eigen::internal::id_type; using Eigen::internal::instantiate_by_c_array; using Eigen::internal::is_same; using Eigen::internal::is_same_gf; using Eigen::internal::mconcat; using Eigen::internal::numeric_list; using Eigen::internal::product_op; using Eigen::internal::repeat; using Eigen::internal::skip; using Eigen::internal::slice; using Eigen::internal::sum_op; using Eigen::internal::take; using Eigen::internal::type_list; struct dummy_a {}; struct dummy_b {}; struct dummy_c {}; struct dummy_d {}; struct dummy_e {}; // dummy operation for testing apply template struct dummy_op; template <> struct dummy_op { typedef dummy_c type; }; template <> struct dummy_op { typedef dummy_d type; }; template <> struct dummy_op { typedef dummy_a type; }; template <> struct dummy_op { typedef dummy_d type; }; template <> struct dummy_op { typedef dummy_b type; }; template <> struct dummy_op { typedef dummy_d type; }; template <> struct dummy_op { typedef dummy_e type; }; template <> struct dummy_op { typedef dummy_e type; }; template <> struct dummy_op { typedef dummy_e type; }; template struct dummy_test { constexpr static bool value = false; constexpr static int global_flags = 0; }; template <> struct dummy_test { constexpr static bool value = true; constexpr static int global_flags = 1; }; template <> struct dummy_test { constexpr static bool value = true; constexpr static int global_flags = 2; }; template <> struct dummy_test { constexpr static bool value = true; constexpr static int global_flags = 4; }; struct times2_op { template static A run(A v) { return v * 2; } }; struct dummy_inst { int c; dummy_inst() : c(0) {} explicit dummy_inst(int) : c(1) {} dummy_inst(int, int) : c(2) {} dummy_inst(int, int, int) : c(3) {} dummy_inst(int, int, int, int) : c(4) {} dummy_inst(int, int, int, int, int) : c(5) {} }; static void test_gen_numeric_list() { VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY( (is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); } static void test_concat() { VERIFY( (is_same, type_list<>>::type, type_list>::value)); VERIFY( (is_same, type_list>::type, type_list>::value)); VERIFY((is_same, type_list>::type, type_list>::value)); VERIFY((is_same, type_list>::type, type_list>::value)); VERIFY((is_same, type_list>::type, type_list>::value)); VERIFY((is_same, numeric_list>::type, numeric_list>::value)); VERIFY((is_same, numeric_list>::type, numeric_list>::value)); VERIFY((is_same, numeric_list>::type, numeric_list>::value)); VERIFY((is_same, numeric_list>::type, numeric_list>::value)); VERIFY((is_same, numeric_list>::type, numeric_list>::value)); VERIFY((is_same>::type, type_list>::value)); VERIFY((is_same, type_list>::type, type_list>::value)); VERIFY((is_same, type_list, type_list>::type, type_list>::value)); VERIFY((is_same, type_list>::type, type_list>::value)); VERIFY((is_same, type_list>::type, type_list>::value)); VERIFY((is_same>::type, numeric_list>::value)); VERIFY((is_same, numeric_list>::type, numeric_list>::value)); VERIFY((is_same, numeric_list, numeric_list>::type, numeric_list>::value)); VERIFY((is_same, numeric_list>::type, numeric_list>::value)); VERIFY((is_same, numeric_list>::type, numeric_list>::value)); } static void test_slice() { typedef type_list tl; typedef numeric_list il; VERIFY((is_same::type, type_list<>>::value)); VERIFY((is_same::type, type_list>::value)); VERIFY((is_same::type, type_list>::value)); VERIFY((is_same::type, type_list>::value)); VERIFY((is_same::type, type_list>::value)); VERIFY((is_same::type, type_list>::value)); VERIFY((is_same::type, type_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, type_list>::value)); VERIFY((is_same::type, type_list>::value)); VERIFY((is_same::type, type_list>::value)); VERIFY((is_same::type, type_list>::value)); VERIFY((is_same::type, type_list>::value)); VERIFY((is_same::type, type_list>::value)); VERIFY((is_same::type, type_list<>>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, numeric_list>::value)); VERIFY((is_same::type, typename take<3, tl>::type>::value)); VERIFY((is_same::type, typename take<3, il>::type>::value)); VERIFY((is_same::type, type_list>::value)); VERIFY((is_same::type, numeric_list>::value)); } static void test_get() { typedef type_list tl; typedef numeric_list il; VERIFY((is_same::type, dummy_a>::value)); VERIFY((is_same::type, dummy_a>::value)); VERIFY((is_same::type, dummy_b>::value)); VERIFY((is_same::type, dummy_b>::value)); VERIFY((is_same::type, dummy_c>::value)); VERIFY((is_same::type, dummy_c>::value)); VERIFY_IS_EQUAL(((int)get<0, il>::value), 4); VERIFY_IS_EQUAL(((int)get<1, il>::value), 8); VERIFY_IS_EQUAL(((int)get<2, il>::value), 15); VERIFY_IS_EQUAL(((int)get<3, il>::value), 16); VERIFY_IS_EQUAL(((int)get<4, il>::value), 23); VERIFY_IS_EQUAL(((int)get<5, il>::value), 42); } static void test_id_helper(dummy_a a, dummy_a b, dummy_a c) { (void)a; (void)b; (void)c; } template static void test_id_numeric() { test_id_helper(typename id_numeric::type()...); } template static void test_id_type() { test_id_helper(typename id_type::type()...); } static void test_id() { // don't call VERIFY here, just assume it works if it compiles // (otherwise it will complain that it can't find the function) test_id_numeric<1, 4, 6>(); test_id_type(); } static void test_is_same_gf() { VERIFY((!is_same_gf::value)); VERIFY((!!is_same_gf::value)); VERIFY_IS_EQUAL((!!is_same_gf::global_flags), false); VERIFY_IS_EQUAL((!!is_same_gf::global_flags), false); } static void test_apply_op() { typedef type_list tl; VERIFY((!!is_same::type, type_list>::value)); VERIFY((!!is_same::type, type_list>::value)); } static void test_contained_in_list() { typedef type_list tl; VERIFY((!!contained_in_list::value)); VERIFY((!!contained_in_list::value)); VERIFY((!!contained_in_list::value)); VERIFY((!contained_in_list::value)); VERIFY((!contained_in_list::value)); VERIFY((!!contained_in_list_gf::value)); VERIFY((!!contained_in_list_gf::value)); VERIFY((!!contained_in_list_gf::value)); VERIFY((!contained_in_list_gf::value)); VERIFY((!contained_in_list_gf::value)); VERIFY_IS_EQUAL(((int)contained_in_list_gf::global_flags), 1); VERIFY_IS_EQUAL(((int)contained_in_list_gf::global_flags), 2); VERIFY_IS_EQUAL(((int)contained_in_list_gf::global_flags), 4); VERIFY_IS_EQUAL(((int)contained_in_list_gf::global_flags), 0); VERIFY_IS_EQUAL(((int)contained_in_list_gf::global_flags), 0); } static void test_arg_reductions() { VERIFY_IS_EQUAL(arg_sum(1, 2, 3, 4), 10); VERIFY_IS_EQUAL(arg_prod(1, 2, 3, 4), 24); VERIFY_IS_APPROX(arg_sum(0.5, 2, 5), 7.5); VERIFY_IS_APPROX(arg_prod(0.5, 2, 5), 5.0); } static void test_array_reverse_and_reduce() { array a{{4, 8, 15, 16, 23, 42}}; array b{{42, 23, 16, 15, 8, 4}}; // there is no operator<< for std::array, so VERIFY_IS_EQUAL will // not compile VERIFY((array_reverse(a) == b)); VERIFY((array_reverse(b) == a)); VERIFY_IS_EQUAL((array_sum(a)), 108); VERIFY_IS_EQUAL((array_sum(b)), 108); VERIFY_IS_EQUAL((array_prod(a)), 7418880); VERIFY_IS_EQUAL((array_prod(b)), 7418880); } static void test_array_zip_and_apply() { array a{{4, 8, 15, 16, 23, 42}}; array b{{0, 1, 2, 3, 4, 5}}; array c{{4, 9, 17, 19, 27, 47}}; array d{{0, 8, 30, 48, 92, 210}}; array e{{0, 2, 4, 6, 8, 10}}; VERIFY((array_zip(a, b) == c)); VERIFY((array_zip(a, b) == d)); VERIFY((array_apply(b) == e)); VERIFY_IS_EQUAL((array_apply_and_reduce(a)), 216); VERIFY_IS_EQUAL((array_apply_and_reduce(b)), 30); VERIFY_IS_EQUAL((array_zip_and_reduce(a, b)), 14755932); VERIFY_IS_EQUAL((array_zip_and_reduce(a, b)), 388); } static void test_array_misc() { array a3{{1, 1, 1}}; array a6{{2, 2, 2, 2, 2, 2}}; VERIFY((repeat<3, int>(1) == a3)); VERIFY((repeat<6, int>(2) == a6)); int data[5] = {0, 1, 2, 3, 4}; VERIFY_IS_EQUAL((instantiate_by_c_array(data).c), 0); VERIFY_IS_EQUAL((instantiate_by_c_array(data).c), 1); VERIFY_IS_EQUAL((instantiate_by_c_array(data).c), 2); VERIFY_IS_EQUAL((instantiate_by_c_array(data).c), 3); VERIFY_IS_EQUAL((instantiate_by_c_array(data).c), 4); VERIFY_IS_EQUAL((instantiate_by_c_array(data).c), 5); } EIGEN_DECLARE_TEST(cxx11_meta) { CALL_SUBTEST(test_gen_numeric_list()); CALL_SUBTEST(test_concat()); CALL_SUBTEST(test_slice()); CALL_SUBTEST(test_get()); CALL_SUBTEST(test_id()); CALL_SUBTEST(test_is_same_gf()); CALL_SUBTEST(test_apply_op()); CALL_SUBTEST(test_contained_in_list()); CALL_SUBTEST(test_arg_reductions()); CALL_SUBTEST(test_array_reverse_and_reduce()); CALL_SUBTEST(test_array_zip_and_apply()); CALL_SUBTEST(test_array_misc()); }