HasMemberFnTraitsTest.cpp 4.4 KB

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