2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/Traits.h>
21 #include <type_traits>
24 #include <folly/ScopeGuard.h>
25 #include <folly/portability/GTest.h>
27 using namespace folly;
32 FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_member_type_x, x);
35 TEST(Traits, has_member_type) {
36 struct membership_no {};
37 struct membership_yes {
41 EXPECT_TRUE((is_same<false_type, has_member_type_x<membership_no>>::value));
42 EXPECT_TRUE((is_same<true_type, has_member_type_x<membership_yes>>::value));
45 // Note: FOLLY_CREATE_HAS_MEMBER_FN_TRAITS tests are in
46 // folly/test/HasMemberFnTraitsTest.cpp.
48 struct T1 {}; // old-style IsRelocatable, below
49 struct T2 {}; // old-style IsRelocatable, below
50 struct T3 { typedef std::true_type IsRelocatable; };
51 struct T4 { typedef std::true_type IsTriviallyCopyable; };
55 struct F2 { typedef int IsRelocatable; };
56 struct F3 : T3 { typedef std::false_type IsRelocatable; };
60 template <> struct IsRelocatable<T1> : std::true_type {};
61 template <> FOLLY_ASSUME_RELOCATABLE(T2);
64 TEST(Traits, scalars) {
65 EXPECT_TRUE(IsRelocatable<int>::value);
66 EXPECT_TRUE(IsRelocatable<bool>::value);
67 EXPECT_TRUE(IsRelocatable<double>::value);
68 EXPECT_TRUE(IsRelocatable<void*>::value);
71 TEST(Traits, containers) {
72 EXPECT_TRUE (IsRelocatable<vector<F1>>::value);
73 EXPECT_TRUE((IsRelocatable<pair<F1, F1>>::value));
74 EXPECT_TRUE ((IsRelocatable<pair<T1, T2>>::value));
77 TEST(Traits, original) {
78 EXPECT_TRUE(IsRelocatable<T1>::value);
79 EXPECT_TRUE(IsRelocatable<T2>::value);
82 TEST(Traits, typedefd) {
83 EXPECT_TRUE (IsRelocatable<T3>::value);
84 EXPECT_TRUE (IsRelocatable<T5>::value);
85 EXPECT_FALSE(IsRelocatable<F2>::value);
86 EXPECT_FALSE(IsRelocatable<F3>::value);
90 EXPECT_TRUE(IsRelocatable<F1>::value);
91 EXPECT_TRUE(IsRelocatable<F4>::value);
94 TEST(Traits, bitprop) {
95 EXPECT_TRUE(IsTriviallyCopyable<T4>::value);
96 EXPECT_TRUE(IsRelocatable<T4>::value);
99 TEST(Traits, bitAndInit) {
100 EXPECT_TRUE (IsTriviallyCopyable<int>::value);
101 EXPECT_FALSE(IsTriviallyCopyable<vector<int>>::value);
102 EXPECT_TRUE (IsZeroInitializable<int>::value);
103 EXPECT_FALSE(IsZeroInitializable<vector<int>>::value);
106 TEST(Trait, logicOperators) {
107 static_assert(Conjunction<true_type>::value, "");
108 static_assert(!Conjunction<false_type>::value, "");
109 static_assert(is_same<Conjunction<true_type>::type, true_type>::value, "");
110 static_assert(is_same<Conjunction<false_type>::type, false_type>::value, "");
111 static_assert(Conjunction<true_type, true_type>::value, "");
112 static_assert(!Conjunction<true_type, false_type>::value, "");
114 static_assert(Disjunction<true_type>::value, "");
115 static_assert(!Disjunction<false_type>::value, "");
116 static_assert(is_same<Disjunction<true_type>::type, true_type>::value, "");
117 static_assert(is_same<Disjunction<false_type>::type, false_type>::value, "");
118 static_assert(Disjunction<true_type, true_type>::value, "");
119 static_assert(Disjunction<true_type, false_type>::value, "");
121 static_assert(!Negation<true_type>::value, "");
122 static_assert(Negation<false_type>::value, "");
125 TEST(Traits, is_negative) {
126 EXPECT_TRUE(folly::is_negative(-1));
127 EXPECT_FALSE(folly::is_negative(0));
128 EXPECT_FALSE(folly::is_negative(1));
129 EXPECT_FALSE(folly::is_negative(0u));
130 EXPECT_FALSE(folly::is_negative(1u));
132 EXPECT_TRUE(folly::is_non_positive(-1));
133 EXPECT_TRUE(folly::is_non_positive(0));
134 EXPECT_FALSE(folly::is_non_positive(1));
135 EXPECT_TRUE(folly::is_non_positive(0u));
136 EXPECT_FALSE(folly::is_non_positive(1u));
139 TEST(Traits, relational) {
140 // We test, especially, the edge cases to make sure we don't
141 // trip -Wtautological-comparisons
143 EXPECT_FALSE((folly::less_than<uint8_t, 0u, uint8_t>(0u)));
144 EXPECT_FALSE((folly::less_than<uint8_t, 0u, uint8_t>(254u)));
145 EXPECT_FALSE((folly::less_than<uint8_t, 255u, uint8_t>(255u)));
146 EXPECT_TRUE( (folly::less_than<uint8_t, 255u, uint8_t>(254u)));
148 EXPECT_FALSE((folly::greater_than<uint8_t, 0u, uint8_t>(0u)));
149 EXPECT_TRUE( (folly::greater_than<uint8_t, 0u, uint8_t>(254u)));
150 EXPECT_FALSE((folly::greater_than<uint8_t, 255u, uint8_t>(255u)));
151 EXPECT_FALSE((folly::greater_than<uint8_t, 255u, uint8_t>(254u)));
154 #if FOLLY_HAVE_INT128_T
156 TEST(Traits, int128) {
158 (::std::is_same<::std::make_unsigned<__int128_t>::type, __uint128_t>::
161 ::std::is_same<::std::make_signed<__int128_t>::type, __int128_t>::value));
163 (::std::is_same<::std::make_unsigned<__uint128_t>::type, __uint128_t>::
166 (::std::is_same<::std::make_signed<__uint128_t>::type, __int128_t>::
168 EXPECT_TRUE((::std::is_arithmetic<__int128_t>::value));
169 EXPECT_TRUE((::std::is_arithmetic<__uint128_t>::value));
170 EXPECT_TRUE((::std::is_integral<__int128_t>::value));
171 EXPECT_TRUE((::std::is_integral<__uint128_t>::value));
172 EXPECT_FALSE((::std::is_unsigned<__int128_t>::value));
173 EXPECT_TRUE((::std::is_signed<__int128_t>::value));
174 EXPECT_TRUE((::std::is_unsigned<__uint128_t>::value));
175 EXPECT_FALSE((::std::is_signed<__uint128_t>::value));
176 EXPECT_TRUE((::std::is_fundamental<__int128_t>::value));
177 EXPECT_TRUE((::std::is_fundamental<__uint128_t>::value));
178 EXPECT_TRUE((::std::is_scalar<__int128_t>::value));
179 EXPECT_TRUE((::std::is_scalar<__uint128_t>::value));
182 #endif // FOLLY_HAVE_INT128_T
184 template <typename T, typename... Args>
185 void testIsRelocatable(Args&&... args) {
186 if (!IsRelocatable<T>::value) return;
188 // We use placement new on zeroed memory to avoid garbage subsections
189 char vsrc[sizeof(T)] = { 0 };
190 char vdst[sizeof(T)] = { 0 };
191 char vcpy[sizeof(T)];
193 T* src = new (vsrc) T(std::forward<Args>(args)...);
194 SCOPE_EXIT { src->~T(); };
195 std::memcpy(vcpy, vsrc, sizeof(T));
197 T* dst = new (vdst) T(std::move(*src));
198 SCOPE_EXIT { dst->~T(); };
200 EXPECT_EQ(deep, *dst);
201 #pragma GCC diagnostic push
202 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
203 EXPECT_EQ(deep, *reinterpret_cast<T*>(vcpy));
204 #pragma GCC diagnostic pop
206 // This test could technically fail; however, this is what relocation
207 // almost always means, so it's a good test to have
208 EXPECT_EQ(std::memcmp(vcpy, vdst, sizeof(T)), 0);
211 TEST(Traits, actuallyRelocatable) {
212 // Ensure that we test stack and heap allocation for strings with in-situ
214 testIsRelocatable<std::string>("1");
215 testIsRelocatable<std::string>(sizeof(std::string) + 1, 'x');
217 testIsRelocatable<std::vector<char>>(5, 'g');
221 // has_value_type<T>::value is true if T has a nested type `value_type`
222 template <class T, class = void>
223 struct has_value_type : std::false_type {};
226 struct has_value_type<T, folly::void_t<typename T::value_type>>
230 TEST(Traits, void_t) {
231 EXPECT_TRUE((::std::is_same<folly::void_t<>, void>::value));
232 EXPECT_TRUE((::std::is_same<folly::void_t<int>, void>::value));
233 EXPECT_TRUE((::std::is_same<folly::void_t<int, short>, void>::value));
235 (::std::is_same<folly::void_t<int, short, std::string>, void>::value));
236 EXPECT_TRUE((::has_value_type<std::string>::value));
237 EXPECT_FALSE((::has_value_type<int>::value));