uTL
micro Template library
TmetaBasic.cpp
Go to the documentation of this file.
1 
20 #include <utl/meta/meta.h>
21 #include <gtest/gtest.h>
22 #include <type_traits>
23 
24 namespace TmetaBasic {
25  using namespace utl;
26  using namespace meta;
27 
28  /*
29  * Types to behave like Fixtures
30  */
31  // Test type_of fixture
32  template<class T> struct Identity {
33  using type = T;
34  };
35 
36  /*
37  * Test integral constant
38  */
39  TEST(TmetaBasic, IntegrealType) {
40  EXPECT_EQ(true, (std::is_same<int, eval<Identity<int>>>::value));
41  EXPECT_EQ(true, (std::is_same<nil_, eval<nil_>>::value));
42  EXPECT_EQ(true, (std::is_same<nil_, eval<eval<nil_>>>::value));
43  EXPECT_EQ(true, (std::is_same<nil_, eval<eval<eval<nil_>>>>::value));
44  }
45  TEST(TmetaBasic, IntegrealConstant) {
46  EXPECT_EQ(true, (std::is_same<int, integral_<int, 42>::value_type>::value));
47  EXPECT_EQ(true, (std::is_same<int, integral_<int, 42>::type::value_type>::value));
50  }
51  TEST(TmetaBasic, BasicTypes) {
52  EXPECT_EQ(true, (std::is_same<bool, bool_<false>::value_type>::value));
54  EXPECT_EQ(true, (std::is_same<bool, false_::value_type>::value));
55  EXPECT_EQ(false, false_::value);
56  EXPECT_EQ(true, (std::is_same<bool, true_::value_type>::value));
57  EXPECT_EQ(true, true_::value);
58 
59  EXPECT_EQ(true, (std::is_same<int8_t, int8_<0>::value_type>::value));
61  EXPECT_EQ(true, (std::is_same<uint8_t, uint8_<0>::value_type>::value));
63  EXPECT_EQ(true, (std::is_same<int16_t, int16_<0>::value_type>::value));
65  EXPECT_EQ(true, (std::is_same<uint16_t, uint16_<0>::value_type>::value));
67  EXPECT_EQ(true, (std::is_same<int32_t, int32_<0>::value_type>::value));
69  EXPECT_EQ(true, (std::is_same<uint32_t, uint32_<0>::value_type>::value));
71 
72  EXPECT_EQ(true, (std::is_same<char, char_<0>::value_type>::value));
74  EXPECT_EQ(true, (std::is_same<int, int_<0>::value_type>::value));
76  EXPECT_EQ(true, (std::is_same<long, long_<0>::value_type>::value));
78 
79  EXPECT_EQ(true, (std::is_same<index_t, index_<0>::value_type>::value));
81  EXPECT_EQ(true, (std::is_same<size_t, size_<0>::value_type>::value));
83 
84  EXPECT_EQ(sizeof(int), sizeof_<int>::value);
85  EXPECT_EQ(alignof(int), alignof_<int>::value);
86  EXPECT_EQ(static_cast<index_t>(-1), Npos::value);
87  }
88 
89  /*
90  * Test integral constant selection operations
91  */
92  TEST(TmetaBasic, Selection) {
93  struct Foo {};
94  struct Bar {};
95 
96  EXPECT_EQ (true, (std::is_same<int_<42>, if_c<true, int_<42>, false_>>()));
97  EXPECT_EQ (true, (std::is_same<Foo, if_c<false, int_<42>, Foo>>()));
98  EXPECT_EQ (true, (std::is_same<Foo, if_c<42, Foo, Bar>>()));
99 
100  EXPECT_EQ (true, (std::is_same<int_<42>, if_<true_, int_<42>, Bar>>()));
101  EXPECT_EQ (true, (std::is_same<Bar, if_<false_, int_<42>, Bar>>()));
102  EXPECT_EQ (true, (std::is_same<int_<42>, if_<int_<1>, int_<42>, Bar>>()));
103  EXPECT_EQ (true, (std::is_same<Foo, if_<int_<0>, int_<42>, Foo>>()));
104 
105  EXPECT_EQ (true, (std::is_same<true_, first_of<true_, false_>>()));
106  EXPECT_EQ (false,(std::is_same<true_, first_of<false_, true_>>()));
107  EXPECT_EQ (false,(std::is_same<true_, second_of<true_, false_>>()));
108  EXPECT_EQ (true, (std::is_same<true_, second_of<false_, true_>>()));
109 
110  }
111 
112  TEST(TmetaBasic, LogicalOperations) {
113  struct Foo {};
114  struct Bar {};
115 
116  EXPECT_EQ (true, (std::is_same<true_, not_c<false>>::value));
117  EXPECT_EQ (true, (std::is_same<false_, not_c<true>>::value));
118  EXPECT_EQ (true, (std::is_same<false_, not_c<1>>::value));
119  EXPECT_EQ (true, (std::is_same<true_, not_c<0>>::value));
120 
121  EXPECT_EQ (true, (std::is_same<true_, not_<false_>>()));
122  EXPECT_EQ (true, (std::is_same<false_, not_<true_>>()));
123  EXPECT_EQ (true, (std::is_same<true_, not_<int_<0>>>()));
124  EXPECT_EQ (true, (std::is_same<false_, not_<int_<1>>>()));
125 
126  EXPECT_EQ (true, (std::is_same<false_, or_<false_, false_, not_c<true>, int_<0>, not_<true_>>>()));
127  EXPECT_EQ (true, (std::is_same<false_, or_<>>()));
128  EXPECT_EQ (true, (std::is_same<int_<1>,or_<int_<1>>>()));
129  EXPECT_EQ (true, (std::is_same<true_, or_<true_>>()));
130  EXPECT_EQ (true, (std::is_same<true_, or_<false_, true_>>()));
131  EXPECT_EQ (true, (std::is_same<true_, or_<false_, false_, true_>>()));
132  EXPECT_EQ (true, (std::is_same<int_<1>,or_<int_<0>, false_, not_<true_>, not_c<true>, int_<1>>>()));
133 
134  EXPECT_EQ (true, (std::is_same<true_, and_<true_, true_, int_<1>, not_<false_>, not_c<false>>>()));
135  EXPECT_EQ (true, (std::is_same<true_, and_<>>()));
136  EXPECT_EQ (true, (std::is_same<true_, and_<true_>>()));
137  EXPECT_EQ (true, (std::is_same<false_, and_<false_>>()));
138  EXPECT_EQ (true, (std::is_same<true_, and_<true_, true_>>()));
139  EXPECT_EQ (true, (std::is_same<false_, and_<true_, false_>>()));
140  EXPECT_EQ (true, (std::is_same<true_, and_<true_, true_, true_>>()));
141  EXPECT_EQ (true, (std::is_same<false_, and_<true_, true_, false_>>()));
142 
143  EXPECT_EQ (true, (same_<Foo, Foo>()));
144  EXPECT_EQ (false, (same_<Foo, Bar>()));
145  EXPECT_EQ (true, (not_same_<Foo, Bar>()));
146 
147  }
148 
149  /*
150  * Test integral constant arithmetic operations
151  */
152  TEST(TmetaBasic, ArithmeticOperations) {
153  EXPECT_EQ (int_<42>(), inc<int_<41>>());
154  EXPECT_EQ (int_<42>(), dec<int_<43>>());
161  }
162 
163  /*
164  * Test integral constant comparison operations
165  */
166  TEST(TmetaBasic, ComparisonOperations) {
167  EXPECT_EQ (true, (comp_eq<int_<7>, int_<7>>()));
168  EXPECT_EQ (true, (comp_eq<int_<-7>, int_<-7>>()));
169  EXPECT_EQ (false, (comp_eq<int_<42>, int_<7>>()));
170 
171  EXPECT_EQ (true, (comp_ne<int_<42>, int_<7>>()));
172  EXPECT_EQ (false, (comp_ne<int_<42>, int_<42>>()));
173  EXPECT_EQ (true, (comp_ne<int_<42>, int_<-42>>()));
174 
175  EXPECT_EQ (true, (comp_lt<int_<42>, int_<43>>()));
176  EXPECT_EQ (true, (comp_lt<int_<-7>, int_<-5>>()));
177  EXPECT_EQ (false, (comp_lt<int_<43>, int_<42>>()));
178 
179  EXPECT_EQ (true, (comp_gt<int_<42>, int_<7>>()));
180  EXPECT_EQ (true, (comp_gt<int_<7>, int_<-42>>()));
181  EXPECT_EQ (false, (comp_gt<int_<7>, int_<42>>()));
182 
183  EXPECT_EQ (true, (comp_le<int_<7>, int_<42>>()));
184  EXPECT_EQ (true, (comp_le<int_<-7>, int_<42>>()));
185  EXPECT_EQ (true, (comp_le<int_<42>, int_<42>>()));
186  EXPECT_EQ (true, (comp_le<int_<-7>, int_<-7>>()));
187  EXPECT_EQ (false, (comp_le<int_<42>, int_<7>>()));
188  EXPECT_EQ (false, (comp_le<int_<7>, int_<-42>>()));
189 
190  EXPECT_EQ (true, (comp_ge<int_<42>, int_<7>>()));
191  EXPECT_EQ (true, (comp_ge<int_<42>, int_<-7>>()));
192  EXPECT_EQ (true, (comp_ge<int_<42>, int_<42>>()));
193  EXPECT_EQ (true, (comp_ge<int_<-7>, int_<-7>>()));
194  EXPECT_EQ (false, (comp_ge<int_<42>, int_<43>>()));
195  EXPECT_EQ (false, (comp_ge<int_<-7>, int_<42>>()));
196  }
197 
198  /*
199  * Test integral constant bit operations
200  */
201  TEST(TmetaBasic, BitOperations) {
203  EXPECT_EQ (0xFF, (bitor_ <uint8_<0x55>, uint8_<0xAA>>()));
205  EXPECT_EQ (0x00, (bitnot_<uint8_<-1>>()));
206 
210  EXPECT_EQ (0x0000, (bitnot_<uint16_<-1>>()));
211 
216  }
217 
218  /*
219  * SFINAE
220  */
221  template <typename T, typename =when<same_<T, int>::type::value>>
222  int check1 (T x) { return x; }
223  int check1 (...) { return 0; }
224 
225  template <typename T, typename =enable_if_t<same_<T, int>::type::value, void>>
226  int check2 (T x) { return x; }
227  int check2 (...) { return 0; }
228 
229  TEST(TmetaBasic, Sfinae) {
230  EXPECT_EQ (42, check1(42));
231  EXPECT_EQ (0, check1(42.0));
232  EXPECT_EQ (0, check1());
233 
234  EXPECT_EQ (42, check2(42));
235  EXPECT_EQ (0, check2(42.0));
236  EXPECT_EQ (0, check2());
237  }
238 
239 }
T1 first_of
Select the first type of a type sequence.
Definition: selection.h:76
Include all meta library.
integral_< uint8_t, v > uint8_
uint8_ type: integral constant wrapper for uint8_t
Definition: integral.h:76
bool_< _Tp1()==_Tp2()> comp_eq
Definition: operations.h:184
integral_< typename _Tp::value_type,(typename _Tp::value_type)(_Tp() > > shift())> shift_right
Definition: operations.h:221
not_< comp_eq< _Tp1, _Tp2 > > comp_ne
Not equal.
Definition: operations.h:189
integral_< int8_t, v > int8_
int8_ type: integral constant wrapper for int8_t
Definition: integral.h:73
if_c< If::type::value, Args... > if_
Select one type or another depending on a compile-time Boolean type.
Definition: selection.h:66
integral_< decltype(_Tp1() &_Tp2()), _Tp1() &_Tp2()> bitand_
Definition: operations.h:208
integral_< decltype(-_Tp()), -_Tp()> negate
Negation.
Definition: operations.h:138
add< _Tp1, negate< _Tp2 > > sub
Substruction.
Definition: operations.h:165
integral_< decltype(_Tp2() *_Tp2()), _Tp1() *_Tp2() > mult
Multiplication.
Definition: operations.h:150
bool_< true > true_
The type used as a compile-time boolean with true value.
Definition: integral.h:68
eval< detail::if_c_< B, Args... > > if_c
Select one type or another depending on a compile-time Boolean.
Definition: selection.h:62
TEST(TmetaBasic, IntegrealType)
Definition: TmetaBasic.cpp:39
T2 second_of
Select the second type of a type sequence.
Definition: selection.h:79
bool_<(_Tp1()< _Tp2())> comp_lt
Definition: operations.h:186
integral_< size_t, v > size_
size_ type: integral constant wrapper for size_t a.k.a std::size_t
Definition: integral.h:110
integral_< decltype(_Tp1()|_Tp2()), _Tp1()|_Tp2()> bitor_
Definition: operations.h:211
integral_< typename _T::value_type,(typename _T::value_type)(~_T())> bitnot_
Definition: operations.h:205
integral_< long, v > long_
long_ type: integral constant wrapper for long
Definition: integral.h:102
std::integral_constant< Tp, v > integral_
Definition: integral.h:58
integral_< decltype(_Tp1() % _Tp2()), _Tp1() % _Tp2() > modulo
Modulo.
Definition: operations.h:162
comp_lt< _Tp2, _Tp1 > comp_gt
Greater than.
Definition: operations.h:191
integral_< typename _Tp::value_type,(typename _Tp::value_type)(_Tp()<< shift())> shift_left
Definition: operations.h:218
integral_< int, v > int_
int_ type: integral constant wrapper for int
Definition: integral.h:98
bool_< false > false_
The type used as a compile-time boolean with false value.
Definition: integral.h:69
integral_< decltype(_Tp1()+_Tp2()), _Tp1()+_Tp2() > add
Addition.
Definition: operations.h:144
typename Tp::type eval
Type alias for Tp::type. Used to evaluate/extract return type of metafunctions.
Definition: integral.h:49
integral_< char, v > char_
char_ type: integral constant wrapper for char
Definition: integral.h:94
bool_<!B > not_c
Negate the bool constant parameter.
Definition: operations.h:43
integral_< uint16_t, v > uint16_
uint16_ type: integral constant wrapper for uint16_t
Definition: integral.h:83
STL&#39;s core language concepts.
Definition: _1wire.h:30
integral_< decltype(_Tp1() ^ _Tp2()), _Tp1() ^ _Tp2()> bitxor_
Definition: operations.h:215
integral_< index_t, v > index_
index_ type: integral constant wrapper for index_t a.k.a std::size_t
Definition: integral.h:106
size_< alignof(Tp)> alignof_
Definition: integral.h:120
add< _Tp, int_<-1 > > dec
decrease
Definition: operations.h:173
int check1(T x)
Definition: TmetaBasic.cpp:222
not_< comp_lt< _Tp1, _Tp2 > > comp_ge
Greater or equal.
Definition: operations.h:195
integral_< bool, v > bool_
bool_ type: integral constant wrapper for bool
Definition: integral.h:66
size_t index_t
index_t and size_t mend to be interchangeable
Definition: types.h:38
integral_< decltype(_Tp2()/_Tp2()), _Tp1()/_Tp2() > divide
Division.
Definition: operations.h:156
integral_< int32_t, v > int32_
int32_ type: integral constant wrapper for int32_t
Definition: integral.h:87
int check2(T x)
Definition: TmetaBasic.cpp:226
size_< sizeof(Tp)> sizeof_
Definition: integral.h:115
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:16643
not_< eval< same_< _T1, _T2 > >> not_same_
Definition: operations.h:111
eval< detail::_or_< _Ts... > > or_
Definition: operations.h:70
not_c< _Tp::type::value > not_
not
Definition: operations.h:47
eval< detail::_and_< _Ts... > > and_
Definition: operations.h:96
integral_< uint32_t, v > uint32_
uint32_ type: integral constant wrapper for uint32_t
Definition: integral.h:90
integral_< int16_t, v > int16_
int16_ type: integral constant wrapper for int16_t
Definition: integral.h:80
not_< comp_lt< _Tp2, _Tp1 > > comp_le
Less or equal.
Definition: operations.h:193
add< _Tp, int_< 1 > > inc
Increase.
Definition: operations.h:169