1/*
2 * Copyright 2013-present 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 <folly/portability/GTest.h>
24
25#include <glog/logging.h>
26
27#include <string>
28
29using namespace std;
30using namespace folly;
31
32FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test, test);
33
34struct Foo {
35 int test();
36 int test() const;
37 string test(const string&) const;
38};
39
40struct Bar {
41 int test();
42 double test(int, long);
43 long test(int) const;
44};
45
46struct Gaz {
47 void test();
48 void test() const;
49 void test() /* nolint */ volatile;
50 void test() const /* nolint */ volatile;
51};
52
53struct NoCV {
54 void test();
55};
56
57struct Const {
58 void test() const;
59};
60
61struct Volatile {
62 void test() /* nolint */ volatile;
63};
64
65struct CV {
66 void test() const /* nolint */ volatile;
67};
68
69bool log_value(const char* what, bool result) {
70 LOG(INFO) << what << ": " << boolalpha << result;
71 return result;
72}
73
74#define LOG_VALUE(x) log_value(#x, x)
75
76TEST(HasMemberFnTraits, DirectMembers) {
77 EXPECT_TRUE(LOG_VALUE((has_test<Foo, int()>::value)));
78 EXPECT_TRUE(LOG_VALUE((has_test<Foo, int() const>::value)));
79 EXPECT_FALSE(LOG_VALUE((has_test<Foo, double(int, long)>::value)));
80 EXPECT_TRUE(LOG_VALUE((has_test<Foo, string(const string&) const>::value)));
81 EXPECT_FALSE(LOG_VALUE((has_test<Foo, long(int) const>::value)));
82 EXPECT_FALSE(LOG_VALUE((has_test<Foo, string(string) const>::value)));
83
84 EXPECT_TRUE(LOG_VALUE((has_test<Bar, int()>::value)));
85 EXPECT_FALSE(LOG_VALUE((has_test<Bar, int() const>::value)));
86 EXPECT_TRUE(LOG_VALUE((has_test<Bar, double(int, long)>::value)));
87 EXPECT_FALSE(LOG_VALUE((has_test<Bar, string(const string&) const>::value)));
88 EXPECT_TRUE(LOG_VALUE((has_test<Bar, long(int) const>::value)));
89 EXPECT_FALSE(LOG_VALUE((has_test<Bar, string(string) const>::value)));
90
91 EXPECT_TRUE(LOG_VALUE((has_test<Gaz, void()>::value)));
92 EXPECT_TRUE(LOG_VALUE((has_test<Gaz, void() const>::value)));
93 EXPECT_TRUE(LOG_VALUE((has_test<Gaz, void() /* nolint */ volatile>::value)));
94 EXPECT_TRUE(
95 LOG_VALUE((has_test<Gaz, void() const /* nolint */ volatile>::value)));
96 EXPECT_TRUE(
97 LOG_VALUE((has_test<Gaz, void() /* nolint */ volatile const>::value)));
98
99 EXPECT_TRUE(LOG_VALUE((has_test<NoCV, void()>::value)));
100 EXPECT_FALSE(LOG_VALUE((has_test<NoCV, void() const>::value)));
101 EXPECT_FALSE(
102 LOG_VALUE((has_test<NoCV, void() /* nolint */ volatile>::value)));
103 EXPECT_FALSE(
104 LOG_VALUE((has_test<NoCV, void() const /* nolint */ volatile>::value)));
105 EXPECT_FALSE(
106 LOG_VALUE((has_test<NoCV, void() /* nolint */ volatile const>::value)));
107
108 EXPECT_FALSE(LOG_VALUE((has_test<Const, void()>::value)));
109 EXPECT_TRUE(LOG_VALUE((has_test<Const, void() const>::value)));
110 EXPECT_FALSE(
111 LOG_VALUE((has_test<Const, void() /* nolint */ volatile>::value)));
112 EXPECT_FALSE(
113 LOG_VALUE((has_test<Const, void() const /* nolint */ volatile>::value)));
114 EXPECT_FALSE(
115 LOG_VALUE((has_test<Const, void() /* nolint */ volatile const>::value)));
116
117 EXPECT_FALSE(LOG_VALUE((has_test<Volatile, void()>::value)));
118 EXPECT_FALSE(LOG_VALUE((has_test<Volatile, void() const>::value)));
119 EXPECT_TRUE(
120 LOG_VALUE((has_test<Volatile, void() /* nolint */ volatile>::value)));
121 EXPECT_FALSE(LOG_VALUE(
122 (has_test<Volatile, void() const /* nolint */ volatile>::value)));
123 EXPECT_FALSE(LOG_VALUE(
124 (has_test<Volatile, void() /* nolint */ volatile const>::value)));
125
126 EXPECT_FALSE(LOG_VALUE((has_test<CV, void()>::value)));
127 EXPECT_FALSE(LOG_VALUE((has_test<CV, void() const>::value)));
128 EXPECT_FALSE(LOG_VALUE((has_test<CV, void() /* nolint */ volatile>::value)));
129 EXPECT_TRUE(
130 LOG_VALUE((has_test<CV, void() const /* nolint */ volatile>::value)));
131 EXPECT_TRUE(
132 LOG_VALUE((has_test<CV, void() /* nolint */ volatile const>::value)));
133}
134
135int main(int argc, char* argv[]) {
136 testing::InitGoogleTest(&argc, argv);
137 return RUN_ALL_TESTS();
138}
139