Codemod: use #include angle brackets in folly and thrift
[folly.git] / folly / test / HasMemberFnTraitsTest.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 /*
18  * @author: Marcelo Juchem <marcelo@fb.com>
19  */
20
21 #include <folly/Traits.h>
22
23 #include <gtest/gtest.h>
24 #include <glog/logging.h>
25
26 #include <string>
27
28 using namespace std;
29 using namespace folly;
30
31 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test, test);
32
33 struct Foo {
34   int test();
35   int test() const;
36   string test(const string&) const;
37 };
38
39 struct Bar {
40   int test();
41   double test(int,long);
42   long test(int) const;
43 };
44
45 struct Gaz {
46   void test();
47   void test() const;
48   void test() volatile;
49   void test() const volatile;
50 };
51
52 struct NoCV {
53   void test();
54 };
55
56 struct Const {
57   void test() const;
58 };
59
60 struct Volatile {
61   void test() volatile;
62 };
63
64 struct CV {
65   void test() const volatile;
66 };
67
68 bool log_value(const char* what, bool result) {
69   LOG(INFO) << what << ": " << boolalpha << result;
70   return result;
71 }
72
73 #define LOG_VALUE(x) log_value(#x, x)
74
75 TEST(HasMemberFnTraits, DirectMembers) {
76   EXPECT_TRUE(LOG_VALUE((has_test<Foo, int()>::value)));
77   EXPECT_TRUE(LOG_VALUE((has_test<Foo, int() const>::value)));
78   EXPECT_FALSE(LOG_VALUE((has_test<Foo, double(int, long)>::value)));
79   EXPECT_TRUE(LOG_VALUE((has_test<Foo, string(const string&) const>::value)));
80   EXPECT_FALSE(LOG_VALUE((has_test<Foo, long(int) const>::value)));
81   EXPECT_FALSE(LOG_VALUE((has_test<Foo, string(string) const>::value)));
82
83   EXPECT_TRUE(LOG_VALUE((has_test<Bar, int()>::value)));
84   EXPECT_FALSE(LOG_VALUE((has_test<Bar, int() const>::value)));
85   EXPECT_TRUE(LOG_VALUE((has_test<Bar, double(int, long)>::value)));
86   EXPECT_FALSE(LOG_VALUE((has_test<Bar, string(const string&) const>::value)));
87   EXPECT_TRUE(LOG_VALUE((has_test<Bar, long(int) const>::value)));
88   EXPECT_FALSE(LOG_VALUE((has_test<Bar, string(string) const>::value)));
89
90   EXPECT_TRUE(LOG_VALUE((has_test<Gaz, void()>::value)));
91   EXPECT_TRUE(LOG_VALUE((has_test<Gaz, void() const>::value)));
92   EXPECT_TRUE(LOG_VALUE((has_test<Gaz, void() volatile>::value)));
93   EXPECT_TRUE(LOG_VALUE((has_test<Gaz, void() const volatile>::value)));
94   EXPECT_TRUE(LOG_VALUE((has_test<Gaz, void() volatile const>::value)));
95
96   EXPECT_TRUE(LOG_VALUE((has_test<NoCV, void()>::value)));
97   EXPECT_FALSE(LOG_VALUE((has_test<NoCV, void() const>::value)));
98   EXPECT_FALSE(LOG_VALUE((has_test<NoCV, void() volatile>::value)));
99   EXPECT_FALSE(LOG_VALUE((has_test<NoCV, void() const volatile>::value)));
100   EXPECT_FALSE(LOG_VALUE((has_test<NoCV, void() volatile const>::value)));
101
102   EXPECT_FALSE(LOG_VALUE((has_test<Const, void()>::value)));
103   EXPECT_TRUE(LOG_VALUE((has_test<Const, void() const>::value)));
104   EXPECT_FALSE(LOG_VALUE((has_test<Const, void() volatile>::value)));
105   EXPECT_FALSE(LOG_VALUE((has_test<Const, void() const volatile>::value)));
106   EXPECT_FALSE(LOG_VALUE((has_test<Const, void() volatile const>::value)));
107
108   EXPECT_FALSE(LOG_VALUE((has_test<Volatile, void()>::value)));
109   EXPECT_FALSE(LOG_VALUE((has_test<Volatile, void() const>::value)));
110   EXPECT_TRUE(LOG_VALUE((has_test<Volatile, void() volatile>::value)));
111   EXPECT_FALSE(LOG_VALUE((has_test<Volatile, void() const volatile>::value)));
112   EXPECT_FALSE(LOG_VALUE((has_test<Volatile, void() volatile const>::value)));
113
114   EXPECT_FALSE(LOG_VALUE((has_test<CV, void()>::value)));
115   EXPECT_FALSE(LOG_VALUE((has_test<CV, void() const>::value)));
116   EXPECT_FALSE(LOG_VALUE((has_test<CV, void() volatile>::value)));
117   EXPECT_TRUE(LOG_VALUE((has_test<CV, void() const volatile>::value)));
118   EXPECT_TRUE(LOG_VALUE((has_test<CV, void() volatile const>::value)));
119 }
120
121 int main(int argc, char *argv[]) {
122   testing::InitGoogleTest(&argc, argv);
123   return RUN_ALL_TESTS();
124 }