// 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/. // SPDX-License-Identifier: MPL-2.0 #include "main.h" #include #include struct FooReturnType { typedef int ReturnType; }; 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_prod; using Eigen::internal::array_reduce; using Eigen::internal::array_sum; using Eigen::internal::concat; using Eigen::internal::contained_in_list; using Eigen::internal::contained_in_list_gf; // Eigen::internal::get is intentionally left fully-qualified below: test/main.h // does `using namespace Eigen;`, and Eigen/src/Core/StructuredBindings.h defines // free functions `Eigen::get` that would otherwise clash with this metafunction. using Eigen::internal::is_same_gf; using Eigen::internal::mconcat; using Eigen::internal::skip; using Eigen::internal::slice; using Eigen::internal::take; using Eigen::internal::type_list; struct dummy_a {}; struct dummy_b {}; struct dummy_c {}; struct dummy_d {}; struct dummy_e {}; struct widening_sum_op { template EIGEN_DEVICE_FUNC constexpr static long long run(A a, B b) { return static_cast(a) + static_cast(b); } }; // 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; }; static void test_concat() { VERIFY((std::is_same, type_list<>>::type, type_list>::value)); VERIFY((std::is_same, type_list>::type, type_list>::value)); VERIFY((std::is_same, type_list>::type, type_list>::value)); VERIFY((std::is_same, type_list>::type, type_list>::value)); VERIFY((std::is_same, type_list>::type, type_list>::value)); VERIFY((std::is_same>::type, type_list>::value)); VERIFY((std::is_same, type_list>::type, type_list>::value)); VERIFY((std::is_same, type_list, type_list>::type, type_list>::value)); VERIFY((std::is_same, type_list>::type, type_list>::value)); VERIFY((std::is_same, type_list>::type, type_list>::value)); } static void test_slice() { typedef type_list tl; VERIFY((std::is_same::type, type_list<>>::value)); VERIFY((std::is_same::type, type_list>::value)); VERIFY((std::is_same::type, type_list>::value)); VERIFY((std::is_same::type, type_list>::value)); VERIFY((std::is_same::type, type_list>::value)); VERIFY((std::is_same::type, type_list>::value)); VERIFY((std::is_same::type, type_list>::value)); VERIFY((std::is_same::type, type_list>::value)); VERIFY((std::is_same::type, type_list>::value)); VERIFY((std::is_same::type, type_list>::value)); VERIFY((std::is_same::type, type_list>::value)); VERIFY((std::is_same::type, type_list>::value)); VERIFY((std::is_same::type, type_list>::value)); VERIFY((std::is_same::type, type_list<>>::value)); VERIFY((std::is_same::type, type_list<>>::value)); VERIFY((std::is_same::type, typename take<3, tl>::type>::value)); VERIFY((std::is_same::type, type_list>::value)); } static void test_get() { typedef type_list tl; typedef std::integer_sequence il; VERIFY((std::is_same::type, dummy_a>::value)); VERIFY((std::is_same::type, dummy_a>::value)); VERIFY((std::is_same::type, dummy_b>::value)); VERIFY((std::is_same::type, dummy_b>::value)); VERIFY((std::is_same::type, dummy_c>::value)); VERIFY((std::is_same::type, dummy_c>::value)); VERIFY_IS_EQUAL(((int)Eigen::internal::get<0, il>::value), 4); VERIFY_IS_EQUAL(((int)Eigen::internal::get<1, il>::value), 8); VERIFY_IS_EQUAL(((int)Eigen::internal::get<2, il>::value), 15); VERIFY_IS_EQUAL(((int)Eigen::internal::get<3, il>::value), 16); VERIFY_IS_EQUAL(((int)Eigen::internal::get<4, il>::value), 23); VERIFY_IS_EQUAL(((int)Eigen::internal::get<5, il>::value), 42); } 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((!!std::is_same::type, type_list>::value)); VERIFY((!!std::is_same::type, type_list>::value)); VERIFY((!!std::is_same>::type, type_list<>>::value)); VERIFY((!!std::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_reductions() { array a{{4, 8, 15, 16, 23, 42}}; array b{{42, 23, 16, 15, 8, 4}}; array empty{}; array singleton{{200}}; array narrow{{200, 100}}; array custom{{200, 100, 50}}; 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); VERIFY((std::is_same::value)); VERIFY((std::is_same::value)); VERIFY((std::is_same::value)); VERIFY((std::is_same::value)); VERIFY_IS_EQUAL((array_sum(empty)), 0); VERIFY_IS_EQUAL((array_prod(empty)), 1); VERIFY_IS_EQUAL((array_sum(singleton)), 200); VERIFY_IS_EQUAL((array_prod(singleton)), 200); VERIFY_IS_EQUAL((array_sum(narrow)), 300); VERIFY_IS_EQUAL((array_prod(narrow)), 20000); VERIFY( (std::is_same(custom, static_cast(0))), long long>::value)); VERIFY_IS_EQUAL((array_reduce(custom, static_cast(0))), 350); } EIGEN_DECLARE_TEST(meta) { VERIFY((std::is_same::value)); VERIFY((!std::is_same::value)); VERIFY((!std::is_same::value)); VERIFY((!std::is_same::value)); VERIFY((std::is_same>::value)); VERIFY((std::is_same>::value)); VERIFY((std::is_same>::value)); VERIFY((std::is_same>::value)); VERIFY((std::is_same>::value)); VERIFY((std::is_same>::value)); VERIFY((std::is_same>::value)); // test add_const_on_value_type VERIFY((std::is_same, float const&>::value)); VERIFY((std::is_same, float const*>::value)); VERIFY((std::is_same, const float>::value)); VERIFY((std::is_same, const float>::value)); VERIFY((std::is_same, const float* const>::value)); VERIFY((std::is_same, const float* const>::value)); // is_convertible STATIC_CHECK((std::is_convertible::value)); STATIC_CHECK((std::is_convertible::value)); STATIC_CHECK((std::is_convertible::value)); STATIC_CHECK((std::is_convertible::value)); STATIC_CHECK((std::is_convertible::value)); STATIC_CHECK((std::is_convertible>::value)); STATIC_CHECK((!std::is_convertible, double>::value)); STATIC_CHECK((std::is_convertible::value)); STATIC_CHECK((std::is_convertible::value)); STATIC_CHECK((std::is_convertible::value)); STATIC_CHECK((std::is_convertible::value)); STATIC_CHECK((std::is_convertible::value)); STATIC_CHECK((std::is_convertible::value)); STATIC_CHECK((!std::is_convertible::value)); STATIC_CHECK((!std::is_convertible::value)); STATIC_CHECK(!(std::is_convertible::value)); STATIC_CHECK(!(std::is_convertible::value)); STATIC_CHECK((std::is_convertible::value)); // STATIC_CHECK((!std::is_convertible::value )); //does not even compile because the // conversion is prevented by a static assertion STATIC_CHECK((!std::is_convertible::value)); STATIC_CHECK((!std::is_convertible::value)); { MatrixXf A, B; VectorXf a, b; VERIFY((std::is_convertible::value)); VERIFY((std::is_convertible::value)); VERIFY((!std::is_convertible::value)); VERIFY((std::is_convertible::value)); } { VERIFY((std::is_convertible()), int>::value)); VERIFY((!std::is_convertible())>::value)); } VERIFY((internal::has_ReturnType::value)); VERIFY((internal::has_ReturnType>::value)); VERIFY((!internal::has_ReturnType::value)); VERIFY((!internal::has_ReturnType::value)); // size_at_compile_time falls back to Dynamic rather than overflowing int // (46341^2 is the first square past INT_MAX). STATIC_CHECK((internal::size_at_compile_time(0, Dynamic) == 0)); STATIC_CHECK((internal::size_at_compile_time(3, Dynamic) == Dynamic)); STATIC_CHECK((internal::size_at_compile_time(46340, 46340) == 46340 * 46340)); STATIC_CHECK((internal::size_at_compile_time(46341, 46341) == Dynamic)); STATIC_CHECK((internal::size_at_compile_time(1 << 16, 1 << 16) == Dynamic)); CALL_SUBTEST(test_concat()); CALL_SUBTEST(test_slice()); CALL_SUBTEST(test_get()); 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_reductions()); }