ReplaceableTest.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright 2017-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/Replaceable.h>
  17. #include <folly/portability/GTest.h>
  18. using namespace ::testing;
  19. using namespace ::folly;
  20. namespace {
  21. struct Basic {};
  22. struct alignas(128) BigAlign {};
  23. struct HasConst final {
  24. bool const b1;
  25. HasConst() noexcept : b1(true) {}
  26. explicit HasConst(bool b) noexcept : b1(b) {}
  27. HasConst(HasConst const& b) noexcept : b1(b.b1) {}
  28. HasConst(HasConst&& b) noexcept : b1(b.b1) {}
  29. HasConst& operator=(HasConst const&) = delete;
  30. HasConst& operator=(HasConst&&) = delete;
  31. };
  32. struct HasRef final {
  33. int& i1;
  34. explicit HasRef(int& i) noexcept(false) : i1(i) {}
  35. HasRef(HasRef const& i) noexcept(false) : i1(i.i1) {}
  36. HasRef(HasRef&& i) noexcept(false) : i1(i.i1) {}
  37. HasRef& operator=(HasRef const&) = delete;
  38. HasRef& operator=(HasRef&&) = delete;
  39. ~HasRef() noexcept(false) {
  40. ++i1;
  41. }
  42. };
  43. struct OddA;
  44. struct OddB {
  45. OddB() = delete;
  46. OddB(std::initializer_list<int>, int) noexcept(false) {}
  47. explicit OddB(OddA&&) {}
  48. explicit OddB(OddA const&) noexcept(false) {}
  49. OddB(OddB&&) = delete;
  50. OddB(OddB const&) = delete;
  51. OddB& operator=(OddB&&) = delete;
  52. OddB& operator=(OddB const&) = delete;
  53. ~OddB() = default;
  54. };
  55. struct OddA {
  56. OddA() = delete;
  57. explicit OddA(OddB&&) noexcept {}
  58. explicit OddA(OddB const&) = delete;
  59. OddA(OddA&&) = delete;
  60. OddA(OddA const&) = delete;
  61. OddA& operator=(OddA&&) = delete;
  62. OddA& operator=(OddA const&) = delete;
  63. ~OddA() noexcept(false) {}
  64. };
  65. struct Indestructible {
  66. ~Indestructible() = delete;
  67. };
  68. } // namespace
  69. template <typename T>
  70. struct ReplaceableStaticAttributeTest : Test {};
  71. using StaticAttributeTypes = ::testing::Types<
  72. char,
  73. short,
  74. int,
  75. long,
  76. float,
  77. double,
  78. char[11],
  79. Basic,
  80. BigAlign,
  81. HasConst,
  82. HasRef,
  83. OddA,
  84. OddB,
  85. Indestructible>;
  86. TYPED_TEST_CASE(ReplaceableStaticAttributeTest, StaticAttributeTypes);
  87. template <typename T>
  88. struct ReplaceableStaticAttributePairTest : Test {};
  89. using StaticAttributePairTypes = ::testing::
  90. Types<std::pair<int, long>, std::pair<OddA, OddB>, std::pair<OddB, OddA>>;
  91. TYPED_TEST_CASE(ReplaceableStaticAttributePairTest, StaticAttributePairTypes);
  92. TYPED_TEST(ReplaceableStaticAttributeTest, size) {
  93. EXPECT_EQ(sizeof(TypeParam), sizeof(Replaceable<TypeParam>));
  94. }
  95. TYPED_TEST(ReplaceableStaticAttributeTest, align) {
  96. EXPECT_EQ(alignof(TypeParam), alignof(Replaceable<TypeParam>));
  97. }
  98. TYPED_TEST(ReplaceableStaticAttributeTest, destructible) {
  99. EXPECT_EQ(
  100. std::is_destructible<TypeParam>::value,
  101. std::is_destructible<Replaceable<TypeParam>>::value);
  102. }
  103. TYPED_TEST(ReplaceableStaticAttributeTest, trivially_destructible) {
  104. EXPECT_EQ(
  105. std::is_trivially_destructible<TypeParam>::value,
  106. std::is_trivially_destructible<Replaceable<TypeParam>>::value);
  107. }
  108. TYPED_TEST(ReplaceableStaticAttributeTest, default_constructible) {
  109. EXPECT_EQ(
  110. std::is_default_constructible<TypeParam>::value,
  111. std::is_default_constructible<Replaceable<TypeParam>>::value);
  112. }
  113. TYPED_TEST(ReplaceableStaticAttributeTest, move_constructible) {
  114. EXPECT_EQ(
  115. std::is_move_constructible<TypeParam>::value,
  116. std::is_move_constructible<Replaceable<TypeParam>>::value);
  117. }
  118. TYPED_TEST(ReplaceableStaticAttributeTest, copy_constructible) {
  119. EXPECT_EQ(
  120. std::is_copy_constructible<TypeParam>::value,
  121. std::is_copy_constructible<Replaceable<TypeParam>>::value);
  122. }
  123. TYPED_TEST(ReplaceableStaticAttributeTest, move_assignable) {
  124. EXPECT_EQ(
  125. std::is_move_constructible<TypeParam>::value,
  126. std::is_move_assignable<Replaceable<TypeParam>>::value);
  127. }
  128. TYPED_TEST(ReplaceableStaticAttributeTest, copy_assignable) {
  129. EXPECT_EQ(
  130. std::is_copy_constructible<TypeParam>::value,
  131. std::is_copy_assignable<Replaceable<TypeParam>>::value);
  132. }
  133. TYPED_TEST(ReplaceableStaticAttributeTest, nothrow_destructible) {
  134. EXPECT_EQ(
  135. std::is_nothrow_destructible<TypeParam>::value,
  136. std::is_nothrow_destructible<Replaceable<TypeParam>>::value);
  137. }
  138. TYPED_TEST(ReplaceableStaticAttributeTest, nothrow_default_constructible) {
  139. EXPECT_EQ(
  140. std::is_nothrow_default_constructible<TypeParam>::value,
  141. std::is_nothrow_default_constructible<Replaceable<TypeParam>>::value);
  142. }
  143. TYPED_TEST(ReplaceableStaticAttributeTest, nothrow_move_constructible) {
  144. EXPECT_EQ(
  145. std::is_nothrow_move_constructible<TypeParam>::value,
  146. std::is_nothrow_move_constructible<Replaceable<TypeParam>>::value);
  147. }
  148. TYPED_TEST(ReplaceableStaticAttributeTest, nothrow_copy_constructible) {
  149. EXPECT_EQ(
  150. std::is_nothrow_copy_constructible<TypeParam>::value,
  151. std::is_nothrow_copy_constructible<Replaceable<TypeParam>>::value);
  152. }
  153. TYPED_TEST(ReplaceableStaticAttributeTest, nothrow_move_assignable) {
  154. EXPECT_EQ(
  155. std::is_nothrow_destructible<TypeParam>::value &&
  156. std::is_nothrow_copy_constructible<TypeParam>::value,
  157. std::is_nothrow_move_assignable<Replaceable<TypeParam>>::value);
  158. }
  159. TYPED_TEST(ReplaceableStaticAttributeTest, nothrow_copy_assignable) {
  160. EXPECT_EQ(
  161. std::is_nothrow_destructible<TypeParam>::value &&
  162. std::is_nothrow_copy_constructible<TypeParam>::value,
  163. std::is_nothrow_copy_assignable<Replaceable<TypeParam>>::value);
  164. }
  165. TYPED_TEST(ReplaceableStaticAttributeTest, replaceable) {
  166. EXPECT_FALSE(is_replaceable<TypeParam>::value);
  167. EXPECT_TRUE(is_replaceable<Replaceable<TypeParam>>::value);
  168. }
  169. TYPED_TEST(ReplaceableStaticAttributePairTest, copy_construct) {
  170. using T = typename TypeParam::first_type;
  171. using U = typename TypeParam::second_type;
  172. EXPECT_EQ(
  173. (std::is_constructible<T, U const&>::value),
  174. (std::is_constructible<Replaceable<T>, Replaceable<U> const&>::value));
  175. }
  176. TYPED_TEST(ReplaceableStaticAttributePairTest, move_construct) {
  177. using T = typename TypeParam::first_type;
  178. using U = typename TypeParam::second_type;
  179. EXPECT_EQ(
  180. (std::is_constructible<T, U&&>::value),
  181. (std::is_constructible<Replaceable<T>, Replaceable<U>&&>::value));
  182. }
  183. TYPED_TEST(ReplaceableStaticAttributePairTest, copy_assign) {
  184. using T = typename TypeParam::first_type;
  185. using U = typename TypeParam::second_type;
  186. EXPECT_EQ(
  187. (std::is_convertible<U, T>::value && std::is_destructible<T>::value &&
  188. std::is_copy_constructible<T>::value),
  189. (std::is_assignable<Replaceable<T>, Replaceable<U> const&>::value));
  190. }
  191. TYPED_TEST(ReplaceableStaticAttributePairTest, move_assign) {
  192. using T = typename TypeParam::first_type;
  193. using U = typename TypeParam::second_type;
  194. EXPECT_EQ(
  195. (std::is_convertible<U, T>::value && std::is_destructible<T>::value &&
  196. std::is_move_constructible<T>::value),
  197. (std::is_assignable<Replaceable<T>, Replaceable<U>&&>::value));
  198. }
  199. TYPED_TEST(ReplaceableStaticAttributePairTest, nothrow_copy_construct) {
  200. using T = typename TypeParam::first_type;
  201. using U = typename TypeParam::second_type;
  202. EXPECT_EQ(
  203. (std::is_nothrow_constructible<T, U const&>::value &&
  204. std::is_nothrow_destructible<T>::value),
  205. (std::is_nothrow_constructible<Replaceable<T>, Replaceable<U> const&>::
  206. value));
  207. }
  208. TYPED_TEST(ReplaceableStaticAttributePairTest, nothrow_move_construct) {
  209. using T = typename TypeParam::first_type;
  210. using U = typename TypeParam::second_type;
  211. EXPECT_EQ(
  212. (std::is_nothrow_constructible<T, U&&>::value &&
  213. std::is_nothrow_destructible<T>::value),
  214. (std::is_nothrow_constructible<Replaceable<T>, Replaceable<U>&&>::value));
  215. }
  216. TYPED_TEST(ReplaceableStaticAttributePairTest, nothrow_copy_assign) {
  217. using T = typename TypeParam::first_type;
  218. using U = typename TypeParam::second_type;
  219. EXPECT_EQ(
  220. (std::is_nothrow_constructible<T, U const&>::value &&
  221. std::is_nothrow_destructible<T>::value),
  222. (std::is_nothrow_assignable<Replaceable<T>, Replaceable<U> const&>::
  223. value));
  224. }
  225. TYPED_TEST(ReplaceableStaticAttributePairTest, nothrow_move_assign) {
  226. using T = typename TypeParam::first_type;
  227. using U = typename TypeParam::second_type;
  228. EXPECT_EQ(
  229. (std::is_nothrow_constructible<T, U&&>::value &&
  230. std::is_nothrow_destructible<T>::value),
  231. (std::is_nothrow_assignable<Replaceable<T>, Replaceable<U>&&>::value));
  232. }
  233. TEST(ReplaceableTest, Basics) {
  234. auto rHasConstA = make_replaceable<HasConst>();
  235. auto rHasConstB = make_replaceable<HasConst>(false);
  236. EXPECT_TRUE(rHasConstA->b1);
  237. EXPECT_FALSE(rHasConstB->b1);
  238. rHasConstA = rHasConstB;
  239. EXPECT_FALSE(rHasConstA->b1);
  240. EXPECT_FALSE(rHasConstB->b1);
  241. rHasConstB.emplace(true);
  242. EXPECT_FALSE(rHasConstA->b1);
  243. EXPECT_TRUE(rHasConstB->b1);
  244. rHasConstA = std::move(rHasConstB);
  245. EXPECT_TRUE(rHasConstA->b1);
  246. EXPECT_TRUE(rHasConstB->b1);
  247. }
  248. TEST(ReplaceableTest, Constructors) {
  249. Basic b{};
  250. // From existing `T`
  251. auto rBasicCopy1 = Replaceable<Basic>(b);
  252. auto rBasicMove1 = Replaceable<Basic>(std::move(b));
  253. // From existing `Replaceable<T>`
  254. auto rBasicCopy2 = Replaceable<Basic>(rBasicCopy1);
  255. auto rBasicMove2 = Replaceable<Basic>(std::move(rBasicMove1));
  256. (void)rBasicCopy2;
  257. (void)rBasicMove2;
  258. }
  259. TEST(ReplaceableTest, DestructsWhenExpected) {
  260. int i{0};
  261. {
  262. Replaceable<HasRef> rHasRefA{i};
  263. Replaceable<HasRef> rHasRefB{i};
  264. EXPECT_EQ(0, i);
  265. rHasRefA = rHasRefB;
  266. EXPECT_EQ(1, i);
  267. rHasRefB.emplace(i);
  268. EXPECT_EQ(2, i);
  269. rHasRefA = std::move(rHasRefB);
  270. EXPECT_EQ(3, i);
  271. }
  272. EXPECT_EQ(5, i);
  273. }
  274. TEST(ReplaceableTest, Conversions) {
  275. Replaceable<OddB> rOddB{in_place, {1, 2, 3}, 4};
  276. Replaceable<OddA> rOddA{std::move(rOddB)};
  277. Replaceable<OddB> rOddB2{rOddA};
  278. }