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>
19 #include <gtest/gtest.h>
21 using namespace folly;
24 struct T1 {}; // old-style IsRelocatable, below
25 struct T2 {}; // old-style IsRelocatable, below
26 struct T3 { typedef std::true_type IsRelocatable; };
27 struct T4 { typedef std::true_type IsTriviallyCopyable; };
31 struct F2 { typedef int IsRelocatable; };
32 struct F3 : T3 { typedef std::false_type IsRelocatable; };
36 template <> struct IsRelocatable<T1> : std::true_type {};
37 template <> FOLLY_ASSUME_RELOCATABLE(T2);
40 TEST(Traits, scalars) {
41 EXPECT_TRUE(IsRelocatable<int>::value);
42 EXPECT_TRUE(IsRelocatable<bool>::value);
43 EXPECT_TRUE(IsRelocatable<double>::value);
44 EXPECT_TRUE(IsRelocatable<void*>::value);
47 TEST(Traits, containers) {
48 EXPECT_TRUE (IsRelocatable<vector<F1>>::value);
49 EXPECT_FALSE((IsRelocatable<pair<F1, F1>>::value));
50 EXPECT_TRUE ((IsRelocatable<pair<T1, T2>>::value));
53 TEST(Traits, original) {
54 EXPECT_TRUE(IsRelocatable<T1>::value);
55 EXPECT_TRUE(IsRelocatable<T2>::value);
58 TEST(Traits, typedefd) {
59 EXPECT_TRUE (IsRelocatable<T3>::value);
60 EXPECT_TRUE (IsRelocatable<T5>::value);
61 EXPECT_FALSE(IsRelocatable<F2>::value);
62 EXPECT_FALSE(IsRelocatable<F3>::value);
66 EXPECT_FALSE(IsRelocatable<F1>::value);
67 EXPECT_FALSE(IsRelocatable<F4>::value);
70 TEST(Traits, bitprop) {
71 EXPECT_TRUE(IsTriviallyCopyable<T4>::value);
72 EXPECT_TRUE(IsRelocatable<T4>::value);
75 TEST(Traits, bitAndInit) {
76 EXPECT_TRUE (IsTriviallyCopyable<int>::value);
77 EXPECT_FALSE(IsTriviallyCopyable<vector<int>>::value);
78 EXPECT_TRUE (IsZeroInitializable<int>::value);
79 EXPECT_FALSE(IsZeroInitializable<vector<int>>::value);
82 TEST(Traits, is_negative) {
83 EXPECT_TRUE(folly::is_negative(-1));
84 EXPECT_FALSE(folly::is_negative(0));
85 EXPECT_FALSE(folly::is_negative(1));
86 EXPECT_FALSE(folly::is_negative(0u));
87 EXPECT_FALSE(folly::is_negative(1u));
89 EXPECT_TRUE(folly::is_non_positive(-1));
90 EXPECT_TRUE(folly::is_non_positive(0));
91 EXPECT_FALSE(folly::is_non_positive(1));
92 EXPECT_TRUE(folly::is_non_positive(0u));
93 EXPECT_FALSE(folly::is_non_positive(1u));
96 TEST(Traits, relational) {
97 // We test, especially, the edge cases to make sure we don't
98 // trip -Wtautological-comparisons
100 EXPECT_FALSE((folly::less_than<uint8_t, 0u, uint8_t>(0u)));
101 EXPECT_FALSE((folly::less_than<uint8_t, 0u, uint8_t>(254u)));
102 EXPECT_FALSE((folly::less_than<uint8_t, 255u, uint8_t>(255u)));
103 EXPECT_TRUE( (folly::less_than<uint8_t, 255u, uint8_t>(254u)));
105 EXPECT_FALSE((folly::greater_than<uint8_t, 0u, uint8_t>(0u)));
106 EXPECT_TRUE( (folly::greater_than<uint8_t, 0u, uint8_t>(254u)));
107 EXPECT_FALSE((folly::greater_than<uint8_t, 255u, uint8_t>(255u)));
108 EXPECT_FALSE((folly::greater_than<uint8_t, 255u, uint8_t>(254u)));
111 struct membership_no {};
112 struct membership_yes { using x = void; };
113 FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_member_type_x, x);
115 TEST(Traits, has_member_type) {
116 EXPECT_FALSE(bool(has_member_type_x<membership_no>::value));
117 EXPECT_TRUE(bool(has_member_type_x<membership_yes>::value));