DiscriminatedPtrTest.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2011-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. #include <folly/DiscriminatedPtr.h>
  17. #include <folly/portability/GTest.h>
  18. using namespace folly;
  19. TEST(DiscriminatedPtr, Basic) {
  20. struct Foo {};
  21. struct Bar {};
  22. typedef DiscriminatedPtr<void, int, Foo, Bar> Ptr;
  23. int a = 10;
  24. Ptr p;
  25. EXPECT_TRUE(p.empty());
  26. EXPECT_FALSE(p.hasType<void>());
  27. EXPECT_FALSE(p.hasType<int>());
  28. EXPECT_FALSE(p.hasType<Foo>());
  29. EXPECT_FALSE(p.hasType<Bar>());
  30. p.set(&a);
  31. EXPECT_FALSE(p.empty());
  32. EXPECT_FALSE(p.hasType<void>());
  33. EXPECT_TRUE(p.hasType<int>());
  34. EXPECT_FALSE(p.hasType<Foo>());
  35. EXPECT_FALSE(p.hasType<Bar>());
  36. EXPECT_EQ(&a, p.get_nothrow<int>());
  37. EXPECT_EQ(&a, static_cast<const Ptr&>(p).get_nothrow<int>());
  38. EXPECT_EQ(&a, p.get<int>());
  39. EXPECT_EQ(&a, static_cast<const Ptr&>(p).get<int>());
  40. EXPECT_EQ(static_cast<void*>(nullptr), p.get_nothrow<void>());
  41. EXPECT_THROW({ p.get<void>(); }, std::invalid_argument);
  42. Foo foo;
  43. p.set(&foo);
  44. EXPECT_FALSE(p.empty());
  45. EXPECT_FALSE(p.hasType<void>());
  46. EXPECT_FALSE(p.hasType<int>());
  47. EXPECT_TRUE(p.hasType<Foo>());
  48. EXPECT_FALSE(p.hasType<Bar>());
  49. EXPECT_EQ(static_cast<int*>(nullptr), p.get_nothrow<int>());
  50. p.clear();
  51. EXPECT_TRUE(p.empty());
  52. EXPECT_FALSE(p.hasType<void>());
  53. EXPECT_FALSE(p.hasType<int>());
  54. EXPECT_FALSE(p.hasType<Foo>());
  55. EXPECT_FALSE(p.hasType<Bar>());
  56. }
  57. TEST(DiscriminatedPtr, Apply) {
  58. struct Foo {};
  59. struct Visitor {
  60. std::string operator()(int* /* ptr */) {
  61. return "int";
  62. }
  63. std::string operator()(const int* /* ptr */) {
  64. return "const int";
  65. }
  66. std::string operator()(Foo* /* ptr */) {
  67. return "Foo";
  68. }
  69. std::string operator()(const Foo* /* ptr */) {
  70. return "const Foo";
  71. }
  72. };
  73. typedef DiscriminatedPtr<int, Foo> Ptr;
  74. Ptr p;
  75. int a = 0;
  76. p.set(&a);
  77. EXPECT_EQ("int", p.apply(Visitor()));
  78. EXPECT_EQ("const int", static_cast<const Ptr&>(p).apply(Visitor()));
  79. Foo foo;
  80. p.set(&foo);
  81. EXPECT_EQ("Foo", p.apply(Visitor()));
  82. EXPECT_EQ("const Foo", static_cast<const Ptr&>(p).apply(Visitor()));
  83. EXPECT_EQ("Foo", apply_visitor(Visitor(), p));
  84. EXPECT_EQ("const Foo", apply_visitor(Visitor(), static_cast<const Ptr&>(p)));
  85. EXPECT_EQ("Foo", apply_visitor(Visitor(), std::move(p)));
  86. p.clear();
  87. EXPECT_THROW({ p.apply(Visitor()); }, std::invalid_argument);
  88. }
  89. TEST(DiscriminatedPtr, ApplyVoid) {
  90. struct Foo {};
  91. struct Visitor {
  92. void operator()(int* /* ptr */) {
  93. result = "int";
  94. }
  95. void operator()(const int* /* ptr */) {
  96. result = "const int";
  97. }
  98. void operator()(Foo* /* ptr */) {
  99. result = "Foo";
  100. }
  101. void operator()(const Foo* /* ptr */) {
  102. result = "const Foo";
  103. }
  104. std::string result;
  105. };
  106. typedef DiscriminatedPtr<int, Foo> Ptr;
  107. Ptr p;
  108. Visitor v;
  109. int a = 0;
  110. p.set(&a);
  111. p.apply(v);
  112. EXPECT_EQ("int", v.result);
  113. static_cast<const Ptr&>(p).apply(v);
  114. EXPECT_EQ("const int", v.result);
  115. Foo foo;
  116. p.set(&foo);
  117. p.apply(v);
  118. EXPECT_EQ("Foo", v.result);
  119. static_cast<const Ptr&>(p).apply(v);
  120. EXPECT_EQ("const Foo", v.result);
  121. p.clear();
  122. EXPECT_THROW({ p.apply(v); }, std::invalid_argument);
  123. }