2 * Copyright 2016 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>
23 #include <folly/ScopeGuard.h>
24 #include <gtest/gtest.h>
26 using namespace folly;
29 struct T1 {}; // old-style IsRelocatable, below
30 struct T2 {}; // old-style IsRelocatable, below
31 struct T3 { typedef std::true_type IsRelocatable; };
32 struct T4 { typedef std::true_type IsTriviallyCopyable; };
36 struct F2 { typedef int IsRelocatable; };
37 struct F3 : T3 { typedef std::false_type IsRelocatable; };
41 template <> struct IsRelocatable<T1> : std::true_type {};
42 template <> FOLLY_ASSUME_RELOCATABLE(T2);
45 TEST(Traits, scalars) {
46 EXPECT_TRUE(IsRelocatable<int>::value);
47 EXPECT_TRUE(IsRelocatable<bool>::value);
48 EXPECT_TRUE(IsRelocatable<double>::value);
49 EXPECT_TRUE(IsRelocatable<void*>::value);
52 TEST(Traits, containers) {
53 EXPECT_TRUE (IsRelocatable<vector<F1>>::value);
54 EXPECT_FALSE((IsRelocatable<pair<F1, F1>>::value));
55 EXPECT_TRUE ((IsRelocatable<pair<T1, T2>>::value));
58 TEST(Traits, original) {
59 EXPECT_TRUE(IsRelocatable<T1>::value);
60 EXPECT_TRUE(IsRelocatable<T2>::value);
63 TEST(Traits, typedefd) {
64 EXPECT_TRUE (IsRelocatable<T3>::value);
65 EXPECT_TRUE (IsRelocatable<T5>::value);
66 EXPECT_FALSE(IsRelocatable<F2>::value);
67 EXPECT_FALSE(IsRelocatable<F3>::value);
71 EXPECT_FALSE(IsRelocatable<F1>::value);
72 EXPECT_FALSE(IsRelocatable<F4>::value);
75 TEST(Traits, bitprop) {
76 EXPECT_TRUE(IsTriviallyCopyable<T4>::value);
77 EXPECT_TRUE(IsRelocatable<T4>::value);
80 TEST(Traits, bitAndInit) {
81 EXPECT_TRUE (IsTriviallyCopyable<int>::value);
82 EXPECT_FALSE(IsTriviallyCopyable<vector<int>>::value);
83 EXPECT_TRUE (IsZeroInitializable<int>::value);
84 EXPECT_FALSE(IsZeroInitializable<vector<int>>::value);
87 TEST(Traits, is_negative) {
88 EXPECT_TRUE(folly::is_negative(-1));
89 EXPECT_FALSE(folly::is_negative(0));
90 EXPECT_FALSE(folly::is_negative(1));
91 EXPECT_FALSE(folly::is_negative(0u));
92 EXPECT_FALSE(folly::is_negative(1u));
94 EXPECT_TRUE(folly::is_non_positive(-1));
95 EXPECT_TRUE(folly::is_non_positive(0));
96 EXPECT_FALSE(folly::is_non_positive(1));
97 EXPECT_TRUE(folly::is_non_positive(0u));
98 EXPECT_FALSE(folly::is_non_positive(1u));
101 TEST(Traits, relational) {
102 // We test, especially, the edge cases to make sure we don't
103 // trip -Wtautological-comparisons
105 EXPECT_FALSE((folly::less_than<uint8_t, 0u, uint8_t>(0u)));
106 EXPECT_FALSE((folly::less_than<uint8_t, 0u, uint8_t>(254u)));
107 EXPECT_FALSE((folly::less_than<uint8_t, 255u, uint8_t>(255u)));
108 EXPECT_TRUE( (folly::less_than<uint8_t, 255u, uint8_t>(254u)));
110 EXPECT_FALSE((folly::greater_than<uint8_t, 0u, uint8_t>(0u)));
111 EXPECT_TRUE( (folly::greater_than<uint8_t, 0u, uint8_t>(254u)));
112 EXPECT_FALSE((folly::greater_than<uint8_t, 255u, uint8_t>(255u)));
113 EXPECT_FALSE((folly::greater_than<uint8_t, 255u, uint8_t>(254u)));
116 template <typename T, typename... Args>
117 void testIsRelocatable(Args&&... args) {
118 if (!IsRelocatable<T>::value) return;
120 // We use placement new on zeroed memory to avoid garbage subsections
121 char vsrc[sizeof(T)] = { 0 };
122 char vdst[sizeof(T)] = { 0 };
123 char vcpy[sizeof(T)];
125 T* src = new (vsrc) T(std::forward<Args>(args)...);
126 SCOPE_EXIT { src->~T(); };
127 std::memcpy(vcpy, vsrc, sizeof(T));
129 T* dst = new (vdst) T(std::move(*src));
130 SCOPE_EXIT { dst->~T(); };
132 EXPECT_EQ(deep, *dst);
133 #pragma GCC diagnostic push
134 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
135 EXPECT_EQ(deep, *reinterpret_cast<T*>(vcpy));
136 #pragma GCC diagnostic pop
138 // This test could technically fail; however, this is what relocation
139 // almost always means, so it's a good test to have
140 EXPECT_EQ(std::memcmp(vcpy, vdst, sizeof(T)), 0);
143 TEST(Traits, actuallyRelocatable) {
144 // Ensure that we test stack and heap allocation for strings with in-situ
146 testIsRelocatable<std::string>("1");
147 testIsRelocatable<std::string>(sizeof(std::string) + 1, 'x');
149 testIsRelocatable<std::vector<char>>(5, 'g');
152 struct membership_no {};
153 struct membership_yes { using x = void; };
154 FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_member_type_x, x);
156 TEST(Traits, has_member_type) {
157 EXPECT_FALSE(bool(has_member_type_x<membership_no>::value));
158 EXPECT_TRUE(bool(has_member_type_x<membership_yes>::value));