Codemod: use #include angle brackets in folly and thrift
[folly.git] / folly / test / TraitsTest.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/Benchmark.h>
18 #include <folly/Traits.h>
19
20 #include <gflags/gflags.h>
21 #include <gtest/gtest.h>
22
23 using namespace folly;
24 using namespace std;
25
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; };
30 struct T5 : T3 {};
31
32 struct F1 {};
33 struct F2 { typedef int IsRelocatable; };
34 struct F3 : T3 { typedef std::false_type IsRelocatable; };
35 struct F4 : T1 {};
36
37 namespace folly {
38   template <> struct IsRelocatable<T1> : std::true_type {};
39   template <> FOLLY_ASSUME_RELOCATABLE(T2);
40 }
41
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);
47 }
48
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));
53 }
54
55 TEST(Traits, original) {
56   EXPECT_TRUE(IsRelocatable<T1>::value);
57   EXPECT_TRUE(IsRelocatable<T2>::value);
58 }
59
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);
65 }
66
67 TEST(Traits, unset) {
68   EXPECT_FALSE(IsRelocatable<F1>::value);
69   EXPECT_FALSE(IsRelocatable<F4>::value);
70 }
71
72 TEST(Traits, bitprop) {
73   EXPECT_TRUE(IsTriviallyCopyable<T4>::value);
74   EXPECT_TRUE(IsRelocatable<T4>::value);
75 }
76
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);
82 }
83
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));
90
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));
96 }
97
98 TEST(Traits, relational) {
99   // We test, especially, the edge cases to make sure we don't
100   // trip -Wtautological-comparisons
101
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)));
106
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)));
111 }
112
113 struct CompleteType {};
114 struct IncompleteType;
115 TEST(Traits, is_complete) {
116   EXPECT_TRUE((folly::is_complete<int>::value));
117   EXPECT_TRUE((folly::is_complete<CompleteType>::value));
118   EXPECT_FALSE((folly::is_complete<IncompleteType>::value));
119 }
120
121 int main(int argc, char ** argv) {
122   testing::InitGoogleTest(&argc, argv);
123   google::ParseCommandLineFlags(&argc, &argv, true);
124   if (FLAGS_benchmark) {
125     folly::runBenchmarks();
126   }
127   return RUN_ALL_TESTS();
128 }
129