ae13d66d02b356f0fccfffed89ff729c2c7cbccd
[folly.git] / folly / test / TraitsTest.cpp
1 /*
2  * Copyright 2012 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   EXPECT_TRUE  (IsRelocatable<set<F1>>::value);
54 }
55
56 TEST(Traits, original) {
57   EXPECT_TRUE(IsRelocatable<T1>::value);
58   EXPECT_TRUE(IsRelocatable<T2>::value);
59 }
60
61 TEST(Traits, typedefd) {
62   EXPECT_TRUE (IsRelocatable<T3>::value);
63   EXPECT_TRUE (IsRelocatable<T5>::value);
64   EXPECT_FALSE(IsRelocatable<F2>::value);
65   EXPECT_FALSE(IsRelocatable<F3>::value);
66 }
67
68 TEST(Traits, unset) {
69   EXPECT_FALSE(IsRelocatable<F1>::value);
70   EXPECT_FALSE(IsRelocatable<F4>::value);
71 }
72
73 TEST(Traits, bitprop) {
74   EXPECT_TRUE(IsTriviallyCopyable<T4>::value);
75   EXPECT_TRUE(IsRelocatable<T4>::value);
76 }
77
78 TEST(Traits, bitAndInit) {
79   EXPECT_TRUE (IsTriviallyCopyable<int>::value);
80   EXPECT_FALSE(IsTriviallyCopyable<vector<int>>::value);
81   EXPECT_TRUE (IsZeroInitializable<int>::value);
82   EXPECT_FALSE(IsZeroInitializable<vector<int>>::value);
83 }
84
85 int main(int argc, char ** argv) {
86   testing::InitGoogleTest(&argc, argv);
87   google::ParseCommandLineFlags(&argc, &argv, true);
88   if (FLAGS_benchmark) {
89     folly::runBenchmarks();
90   }
91   return RUN_ALL_TESTS();
92 }
93