2 * Copyright 2015 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/Benchmark.h>
18 #include <folly/Traits.h>
20 #include <gflags/gflags.h>
21 #include <gtest/gtest.h>
23 using namespace folly;
26 struct T1 {}; // old-style IsRelocatable, below
27 struct T2 {}; // old-style IsRelocatable, below
28 struct T3 { typedef std::true_type IsRelocatable; };
29 struct T4 { typedef std::true_type IsTriviallyCopyable; };
33 struct F2 { typedef int IsRelocatable; };
34 struct F3 : T3 { typedef std::false_type IsRelocatable; };
38 template <> struct IsRelocatable<T1> : std::true_type {};
39 template <> FOLLY_ASSUME_RELOCATABLE(T2);
42 TEST(Traits, scalars) {
43 EXPECT_TRUE(IsRelocatable<int>::value);
44 EXPECT_TRUE(IsRelocatable<bool>::value);
45 EXPECT_TRUE(IsRelocatable<double>::value);
46 EXPECT_TRUE(IsRelocatable<void*>::value);
49 TEST(Traits, containers) {
50 EXPECT_TRUE (IsRelocatable<vector<F1>>::value);
51 EXPECT_FALSE((IsRelocatable<pair<F1, F1>>::value));
52 EXPECT_TRUE ((IsRelocatable<pair<T1, T2>>::value));
55 TEST(Traits, original) {
56 EXPECT_TRUE(IsRelocatable<T1>::value);
57 EXPECT_TRUE(IsRelocatable<T2>::value);
60 TEST(Traits, typedefd) {
61 EXPECT_TRUE (IsRelocatable<T3>::value);
62 EXPECT_TRUE (IsRelocatable<T5>::value);
63 EXPECT_FALSE(IsRelocatable<F2>::value);
64 EXPECT_FALSE(IsRelocatable<F3>::value);
68 EXPECT_FALSE(IsRelocatable<F1>::value);
69 EXPECT_FALSE(IsRelocatable<F4>::value);
72 TEST(Traits, bitprop) {
73 EXPECT_TRUE(IsTriviallyCopyable<T4>::value);
74 EXPECT_TRUE(IsRelocatable<T4>::value);
77 TEST(Traits, bitAndInit) {
78 EXPECT_TRUE (IsTriviallyCopyable<int>::value);
79 EXPECT_FALSE(IsTriviallyCopyable<vector<int>>::value);
80 EXPECT_TRUE (IsZeroInitializable<int>::value);
81 EXPECT_FALSE(IsZeroInitializable<vector<int>>::value);
84 TEST(Traits, is_negative) {
85 EXPECT_TRUE(folly::is_negative(-1));
86 EXPECT_FALSE(folly::is_negative(0));
87 EXPECT_FALSE(folly::is_negative(1));
88 EXPECT_FALSE(folly::is_negative(0u));
89 EXPECT_FALSE(folly::is_negative(1u));
91 EXPECT_TRUE(folly::is_non_positive(-1));
92 EXPECT_TRUE(folly::is_non_positive(0));
93 EXPECT_FALSE(folly::is_non_positive(1));
94 EXPECT_TRUE(folly::is_non_positive(0u));
95 EXPECT_FALSE(folly::is_non_positive(1u));
98 TEST(Traits, relational) {
99 // We test, especially, the edge cases to make sure we don't
100 // trip -Wtautological-comparisons
102 EXPECT_FALSE((folly::less_than<uint8_t, 0u, uint8_t>(0u)));
103 EXPECT_FALSE((folly::less_than<uint8_t, 0u, uint8_t>(254u)));
104 EXPECT_FALSE((folly::less_than<uint8_t, 255u, uint8_t>(255u)));
105 EXPECT_TRUE( (folly::less_than<uint8_t, 255u, uint8_t>(254u)));
107 EXPECT_FALSE((folly::greater_than<uint8_t, 0u, uint8_t>(0u)));
108 EXPECT_TRUE( (folly::greater_than<uint8_t, 0u, uint8_t>(254u)));
109 EXPECT_FALSE((folly::greater_than<uint8_t, 255u, uint8_t>(255u)));
110 EXPECT_FALSE((folly::greater_than<uint8_t, 255u, uint8_t>(254u)));
113 struct membership_no {};
114 struct membership_yes { using x = void; };
115 FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_member_type_x, x);
117 TEST(Traits, has_member_type) {
118 EXPECT_FALSE(bool(has_member_type_x<membership_no>::value));
119 EXPECT_TRUE(bool(has_member_type_x<membership_yes>::value));
122 int main(int argc, char ** argv) {
123 testing::InitGoogleTest(&argc, argv);
124 gflags::ParseCommandLineFlags(&argc, &argv, true);
125 if (FLAGS_benchmark) {
126 folly::runBenchmarks();
128 return RUN_ALL_TESTS();