|
- /*!
- * \file Tmeta.cpp
- *
- * Copyright (C) 2018 Christos Choutouridis
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation, either version 3
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- #include <utl/meta/meta.h>
- #include <gtest/gtest.h>
- #include <type_traits>
-
- namespace test_meta {
- using namespace utl;
- using namespace meta;
-
- /*
- * Test integral constant
- */
- // Test type_of fixture
- template<class T> struct TestTypeOf {
- using type = T;
- };
- TEST(Tmeta_integral, Integreal_type_) {
- EXPECT_EQ(true, (std::is_same<int, type_<TestTypeOf<int>>>::value));
- }
- TEST(Tmeta_integral, IntegrealConstant) {
- EXPECT_EQ(true, (std::is_same<int, integral_constant<int, 42>::value_type>::value));
- EXPECT_EQ(true, (std::is_same<int, integral_constant<int, 42>::type::value_type>::value));
- EXPECT_EQ(42, (integral_constant<int, 0>::value_type(42)));
- EXPECT_EQ(42, (integral_constant<int, 42>()));
- }
- TEST(Tmeta_integral, BasicTypes) {
- EXPECT_EQ(true, (std::is_same<bool, bool_<false>::value_type>::value));
- EXPECT_EQ(true, bool_<true>::value);
- EXPECT_EQ(true, (std::is_same<bool, false_::value_type>::value));
- EXPECT_EQ(false, false_::value);
- EXPECT_EQ(true, (std::is_same<bool, true_::value_type>::value));
- EXPECT_EQ(true, true_::value);
- EXPECT_EQ(true, (std::is_same<int, int_<0>::value_type>::value));
- EXPECT_EQ(42, int_<42>::value);
-
- EXPECT_EQ(true, (std::is_same<index_t, index_t_<0>::value_type>::value));
- EXPECT_EQ(42U, index_t_<42U>::value);
- EXPECT_EQ(true, (std::is_same<size_t, size_t_<0>::value_type>::value));
- EXPECT_EQ(42U, size_t_<42U>::value);
-
- EXPECT_EQ(sizeof(int), sizeof_<int>::value);
- EXPECT_EQ(alignof(int), alignof_<int>::value);
- EXPECT_EQ(static_cast<index_t>(-1), Npos::value);
- }
- /*
- * Test integral constant arithmetic operations
- */
- TEST(Tmeta_integral, ArithmeticOperations) {
- EXPECT_EQ (int_<42>(), inc<int_<41>>());
- EXPECT_EQ (int_<42>(), dec<int_<43>>());
- EXPECT_EQ (int_<42>(), (add<int_<23>, add<int_<17>, int_<2>>>()));
- EXPECT_EQ (int_<42>(), (sub<int_<108>, int_<66>>()));
- EXPECT_EQ (int_<42>(), (mult<int_<7>, mult<int_<3>, int_<2>>>()));
- EXPECT_EQ (int_<42>(), (divide<int_<210>, int_<5>>()));
- EXPECT_EQ (int_<42>(), negate<int_<-42>>());
- EXPECT_EQ (int_< 1>(), (modulo<int_<43>, int_<42>>()));
- }
-
- /*
- * Test logical
- */
- TEST(Tmeta_logical, ComparisonOperations) {
- EXPECT_EQ (true, (std::is_same<bool_<true>, not_c<false>>::value));
- EXPECT_EQ (true, (comp_eq<int_<7>, int_<7>>()));
- EXPECT_EQ (true, (comp_ne<int_<42>, int_<7>>()));
- EXPECT_EQ (true, (comp_lt<int_<42>, int_<43>>()));
- EXPECT_EQ (true, (comp_gt<int_<43>, int_<42>>()));
- EXPECT_EQ (true, (comp_le<int_<42>, int_<42>>()));
- EXPECT_EQ (true, (comp_ge<int_<42>, int_<42>>()));
- }
-
- TEST(Tmeta_logical, BitOperations) {
- EXPECT_EQ (0x00, (bitand_<int_<0x55>, int_<0xAA>>()));
- EXPECT_EQ (0xFF, (bitor_<int_<0x55>, int_<0xAA>>()));
- EXPECT_EQ (0xFA, (bitxor_<int_<0x55>, int_<0xAF>>()));
- EXPECT_EQ (0x00, (bitnot_<int_<-1>>()));
- //XXX: add shifts
- }
-
- TEST(Tmeta_logical, TypeOperations) {
- struct Foo {};
- struct Bar {};
-
- EXPECT_EQ (true, (std::is_same<bool_<true>, not_<bool_<false>>>()));
- EXPECT_EQ (true, (std::is_same<int_<42>, if_c<true, int_<42>, bool_<false>>>()));
- EXPECT_EQ (true, (std::is_same<int_<42>, if_<bool_<true>, int_<42>, bool_<false>>>()));
-
- EXPECT_EQ (true, (std::is_same<bool_<true>, or_<bool_<true>, bool_<false>>>()));
- EXPECT_EQ (true, (std::is_same<bool_<false>, or_<bool_<false>, bool_<false>>>()));
- EXPECT_EQ (true, (std::is_same<bool_<false>, and_<bool_<true>, bool_<false>>>()));
- EXPECT_EQ (true, (std::is_same<bool_<true>, and_<bool_<true>, bool_<true>>>()));
-
- EXPECT_EQ (true, (same_<Foo, Foo>()));
- EXPECT_EQ (false, (same_<Foo, Bar>()));
- EXPECT_EQ (true, (not_same_<Foo, Bar>()));
- }
-
- /*
- * Test void
- */
- TEST(Tmeta_void, VoidType) {
- struct Foo {};
- struct Bar {};
- EXPECT_EQ(true, (std::is_same<void, void_t<int, long, Foo, Bar>>()));
- }
- /*
- * Test invoke
- */
- template<class T1, class T2> struct MetaFunction { using type = int; };
- template<typename... Args> struct MetaClass {using apply = int; };
- TEST(Tmeta_invoke, Invoke) {
- //EXPECT_EQ (true, (is_evaluable<MetaFunction, int .long>()));
- }
- /*
- * Test typelist
- */
- template<class T1, class T2> struct F {};
- TEST(Tmeta_typelist, List_fold) {
- struct X1 {};
- struct X2 {};
- struct X3 {};
- struct X4 {};
- using Q = quote<F>;
-
- EXPECT_EQ(true, (std::is_same<fold<typelist<>, void, Q>, void>()));
- EXPECT_EQ(true, (std::is_same<fold<typelist<X1>, void, Q>, F<void, X1>>()));
- EXPECT_EQ(true, (std::is_same<fold<typelist<X1, X2>, void, Q>, F<F<void, X1>, X2>>()));
- EXPECT_EQ(true, (std::is_same<fold<typelist<X1, X2, X3>, void, Q>, F<F<F<void, X1>, X2>, X3>>()));
- EXPECT_EQ(true, (std::is_same<fold<typelist<X1, X2, X3, X4>, void, Q>, F<F<F<F<void, X1>, X2>, X3>, X4>>()));
-
- //EXPECT_EQ(true, (std::is_same<rev_fold<typelist<>, void, Q>, void>()));
- EXPECT_EQ(true, (std::is_same<rev_fold<typelist<X1>, void, Q>, F<X1, void>>()));
- EXPECT_EQ(true, (std::is_same<rev_fold<typelist<X1, X2>, void, Q>, F<X1, F<X2, void>>>()));
- EXPECT_EQ(true, (std::is_same<rev_fold<typelist<X1, X2, X3>, void, Q>, F<X1, F<X2, F<X3, void>>>>()));
- EXPECT_EQ(true, (std::is_same<rev_fold<typelist<X1, X2, X3, X4>, void, Q>, F<X1, F<X2, F<X3, F<X4, void>>>>>()));
- }
- TEST(Tmeta_typelist, ListOperations) {
- using l1 = typelist<int, short, long>;
- using l2 = typelist<>;
- using l3 = typelist<float, double, long double>;
- using l4 = typelist<void*, char*>;
- using conc = typelist<int, short, long, float, double, long double, void*, char*>;
- using rep = typelist<int, int, int>;
-
- EXPECT_EQ(3, size<l1>());
- EXPECT_EQ(true, empty<l2>());
-
- EXPECT_EQ(true, (std::is_same<conc, concat<l1, l2, l3, l4>>()));
- EXPECT_EQ(true, (std::is_same<rep, repeat_n<int_<3>, int>>()));
- EXPECT_EQ(true, (std::is_same<int, front<l1>>()));
- EXPECT_EQ(true, (std::is_same<long, back<l1>>()));
- EXPECT_EQ(true, (std::is_same<double, at<l3, int_<1>>>()));
-
- EXPECT_EQ(true, (std::is_same<typelist<short, long>, pop_front<l1>>()));
- }
- }
|