expected_unittest.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/types/expected.h"
  5. #include <utility>
  6. #include <vector>
  7. #include "base/test/gtest_util.h"
  8. #include "base/types/strong_alias.h"
  9. #include "testing/gmock/include/gmock/gmock.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace base {
  12. namespace {
  13. template <typename T>
  14. struct Strong {
  15. constexpr explicit Strong(T value) : value(std::move(value)) {}
  16. T value;
  17. };
  18. template <typename T>
  19. struct Weak {
  20. // NOLINTNEXTLINE(google-explicit-constructor)
  21. constexpr Weak(T value) : value(std::move(value)) {}
  22. T value;
  23. };
  24. template <typename T>
  25. struct StrongMoveOnly {
  26. constexpr explicit StrongMoveOnly(T&& value) : value(std::move(value)) {}
  27. constexpr StrongMoveOnly(StrongMoveOnly&& other)
  28. : value(std::exchange(other.value, {})) {}
  29. constexpr StrongMoveOnly& operator=(StrongMoveOnly&& other) {
  30. value = std::exchange(other.value, {});
  31. return *this;
  32. }
  33. T value;
  34. };
  35. template <typename T>
  36. struct WeakMoveOnly {
  37. // NOLINTNEXTLINE(google-explicit-constructor)
  38. constexpr WeakMoveOnly(T&& value) : value(std::move(value)) {}
  39. constexpr WeakMoveOnly(WeakMoveOnly&& other)
  40. : value(std::exchange(other.value, {})) {}
  41. T value;
  42. };
  43. TEST(Unexpected, ValueConstructor) {
  44. constexpr unexpected<int> unex(42);
  45. static_assert(unex.value() == 42);
  46. }
  47. TEST(Unexpected, DefaultConstructor) {
  48. constexpr unexpected<int> unex(absl::in_place);
  49. static_assert(unex.value() == 0);
  50. }
  51. TEST(Unexpected, InPlaceConstructor) {
  52. constexpr unexpected<std::pair<int, double>> unex(absl::in_place, 42, 3.14);
  53. static_assert(unex.value() == std::pair(42, 3.14));
  54. }
  55. TEST(Unexpected, InPlaceListConstructor) {
  56. unexpected<std::vector<int>> unex(absl::in_place, {1, 2, 3});
  57. EXPECT_EQ(unex.value(), std::vector({1, 2, 3}));
  58. }
  59. TEST(Unexpected, ValueIsQualified) {
  60. using Unex = unexpected<int>;
  61. static_assert(std::is_same_v<decltype(std::declval<Unex&>().value()), int&>);
  62. static_assert(std::is_same_v<decltype(std::declval<const Unex&>().value()),
  63. const int&>);
  64. static_assert(std::is_same_v<decltype(std::declval<Unex>().value()), int&&>);
  65. static_assert(std::is_same_v<decltype(std::declval<const Unex>().value()),
  66. const int&&>);
  67. }
  68. TEST(Unexpected, MemberSwap) {
  69. unexpected u1(42);
  70. unexpected u2(123);
  71. u1.swap(u2);
  72. EXPECT_EQ(u1.value(), 123);
  73. EXPECT_EQ(u2.value(), 42);
  74. }
  75. TEST(Unexpected, EqualityOperators) {
  76. static_assert(unexpected(42) == unexpected(42.0));
  77. static_assert(unexpected(42) != unexpected(43));
  78. }
  79. TEST(Unexpected, FreeSwap) {
  80. unexpected u1(42);
  81. unexpected u2(123);
  82. swap(u1, u2);
  83. EXPECT_EQ(u1.value(), 123);
  84. EXPECT_EQ(u2.value(), 42);
  85. }
  86. TEST(Expected, Triviality) {
  87. using TrivialExpected = expected<int, int>;
  88. static_assert(std::is_trivially_destructible_v<TrivialExpected>);
  89. using NonTrivialExpected = expected<int, std::string>;
  90. static_assert(!std::is_trivially_destructible_v<NonTrivialExpected>);
  91. }
  92. TEST(Expected, DefaultConstructor) {
  93. constexpr expected<int, int> ex;
  94. static_assert(ex.has_value());
  95. EXPECT_EQ(ex.value(), 0);
  96. static_assert(std::is_default_constructible_v<expected<int, int>>);
  97. static_assert(!std::is_default_constructible_v<expected<Strong<int>, int>>);
  98. }
  99. TEST(Expected, CopyConstructor) {
  100. {
  101. constexpr expected<int, int> ex1 = 42;
  102. constexpr expected<int, int> ex2 = ex1;
  103. static_assert(ex2.has_value());
  104. // Note: In theory this could be constexpr, but is currently not due to
  105. // implementation details of absl::get [1].
  106. // TODO: Make this a static_assert once this is fixed in Abseil, or we use
  107. // std::variant. Similarly in the tests below.
  108. // [1] https://github.com/abseil/abseil-cpp/blob/50739/absl/types/internal/variant.h#L548
  109. EXPECT_EQ(ex2.value(), 42);
  110. }
  111. {
  112. constexpr expected<int, int> ex1 = unexpected(42);
  113. constexpr expected<int, int> ex2 = ex1;
  114. static_assert(!ex2.has_value());
  115. EXPECT_EQ(ex2.error(), 42);
  116. }
  117. }
  118. TEST(Expected, MoveConstructor) {
  119. {
  120. expected<StrongMoveOnly<int>, int> ex1 = StrongMoveOnly(42);
  121. expected<StrongMoveOnly<int>, int> ex2 = std::move(ex1);
  122. ASSERT_TRUE(ex2.has_value());
  123. EXPECT_EQ(ex2.value().value, 42);
  124. }
  125. {
  126. expected<int, StrongMoveOnly<int>> ex1 = unexpected(StrongMoveOnly(42));
  127. expected<int, StrongMoveOnly<int>> ex2 = std::move(ex1);
  128. ASSERT_FALSE(ex2.has_value());
  129. EXPECT_EQ(ex2.error().value, 42);
  130. }
  131. }
  132. TEST(Expected, ExplicitConvertingCopyConstructor) {
  133. {
  134. expected<int, int> ex1 = 42;
  135. expected<Strong<int>, int> ex2(ex1);
  136. static_assert(!std::is_convertible_v<decltype(ex1), decltype(ex2)>);
  137. ASSERT_TRUE(ex2.has_value());
  138. EXPECT_EQ(ex2.value().value, 42);
  139. }
  140. {
  141. expected<int, int> ex1 = unexpected(42);
  142. expected<int, Strong<int>> ex2(ex1);
  143. static_assert(!std::is_convertible_v<decltype(ex1), decltype(ex2)>);
  144. ASSERT_FALSE(ex2.has_value());
  145. EXPECT_EQ(ex2.error().value, 42);
  146. }
  147. }
  148. TEST(Expected, ImplicitConvertingCopyConstructor) {
  149. {
  150. expected<int, int> ex1 = 42;
  151. expected<Weak<int>, Weak<int>> ex2 = ex1;
  152. ASSERT_TRUE(ex2.has_value());
  153. EXPECT_EQ(ex2.value().value, 42);
  154. }
  155. {
  156. expected<int, int> ex1 = unexpected(42);
  157. expected<Weak<int>, Weak<int>> ex2 = ex1;
  158. ASSERT_FALSE(ex2.has_value());
  159. EXPECT_EQ(ex2.error().value, 42);
  160. }
  161. }
  162. TEST(Expected, ExplicitConvertingMoveConstructor) {
  163. {
  164. expected<int, int> ex1 = 42;
  165. expected<StrongMoveOnly<int>, int> ex2(std::move(ex1));
  166. static_assert(
  167. !std::is_convertible_v<decltype(std::move(ex1)), decltype(ex2)>);
  168. ASSERT_TRUE(ex2.has_value());
  169. EXPECT_EQ(ex2.value().value, 42);
  170. }
  171. {
  172. expected<int, int> ex1 = unexpected(42);
  173. expected<int, StrongMoveOnly<int>> ex2(std::move(ex1));
  174. static_assert(
  175. !std::is_convertible_v<decltype(std::move(ex1)), decltype(ex2)>);
  176. ASSERT_FALSE(ex2.has_value());
  177. EXPECT_EQ(ex2.error().value, 42);
  178. }
  179. }
  180. TEST(Expected, ImplicitConvertingMoveConstructor) {
  181. {
  182. expected<int, int> ex1 = 42;
  183. expected<WeakMoveOnly<int>, int> ex2 = std::move(ex1);
  184. ASSERT_TRUE(ex2.has_value());
  185. EXPECT_EQ(ex2.value().value, 42);
  186. }
  187. {
  188. expected<int, int> ex1 = unexpected(42);
  189. expected<int, WeakMoveOnly<int>> ex2 = std::move(ex1);
  190. ASSERT_FALSE(ex2.has_value());
  191. EXPECT_EQ(ex2.error().value, 42);
  192. }
  193. }
  194. TEST(Expected, ExplicitValueConstructor) {
  195. {
  196. constexpr expected<Strong<int>, int> ex(42);
  197. static_assert(!std::is_convertible_v<int, decltype(ex)>);
  198. static_assert(ex.has_value());
  199. EXPECT_EQ(ex.value().value, 42);
  200. }
  201. {
  202. constexpr expected<StrongMoveOnly<int>, int> ex(42);
  203. static_assert(!std::is_constructible_v<decltype(ex), int&>);
  204. static_assert(!std::is_convertible_v<int, decltype(ex)>);
  205. static_assert(ex.has_value());
  206. EXPECT_EQ(ex.value().value, 42);
  207. }
  208. }
  209. TEST(Expected, ImplicitValueConstructor) {
  210. {
  211. constexpr expected<Weak<int>, int> ex = 42;
  212. static_assert(ex.has_value());
  213. EXPECT_EQ(ex.value().value, 42);
  214. }
  215. {
  216. constexpr expected<WeakMoveOnly<int>, int> ex = 42;
  217. static_assert(!std::is_convertible_v<int&, decltype(ex)>);
  218. static_assert(ex.has_value());
  219. EXPECT_EQ(ex.value().value, 42);
  220. }
  221. }
  222. TEST(Expected, ExplicitErrorConstructor) {
  223. {
  224. constexpr expected<int, Strong<int>> ex(unexpected(42));
  225. static_assert(!std::is_convertible_v<unexpected<int>, decltype(ex)>);
  226. static_assert(!ex.has_value());
  227. EXPECT_EQ(ex.error().value, 42);
  228. }
  229. {
  230. constexpr expected<int, StrongMoveOnly<int>> ex(unexpected(42));
  231. static_assert(!std::is_constructible_v<decltype(ex), unexpected<int>&>);
  232. static_assert(!std::is_convertible_v<unexpected<int>, decltype(ex)>);
  233. static_assert(!ex.has_value());
  234. EXPECT_EQ(ex.error().value, 42);
  235. }
  236. }
  237. TEST(Expected, ImplicitErrorConstructor) {
  238. {
  239. constexpr expected<int, Weak<int>> ex = unexpected(42);
  240. static_assert(!ex.has_value());
  241. EXPECT_EQ(ex.error().value, 42);
  242. }
  243. {
  244. constexpr expected<int, WeakMoveOnly<int>> ex = unexpected(42);
  245. static_assert(!std::is_convertible_v<unexpected<int>&, decltype(ex)>);
  246. static_assert(!ex.has_value());
  247. EXPECT_EQ(ex.error().value, 42);
  248. }
  249. }
  250. TEST(Expected, InPlaceConstructor) {
  251. constexpr expected<Strong<int>, int> ex(absl::in_place, 42);
  252. static_assert(ex.has_value());
  253. EXPECT_EQ(ex.value().value, 42);
  254. }
  255. TEST(Expected, InPlaceListConstructor) {
  256. expected<std::vector<int>, int> ex(absl::in_place, {1, 2, 3});
  257. ASSERT_TRUE(ex.has_value());
  258. EXPECT_EQ(ex.value(), std::vector({1, 2, 3}));
  259. }
  260. TEST(Expected, UnexpectConstructor) {
  261. constexpr expected<int, Strong<int>> ex(unexpect, 42);
  262. static_assert(!ex.has_value());
  263. EXPECT_EQ(ex.error().value, 42);
  264. }
  265. TEST(Expected, UnexpectListConstructor) {
  266. expected<int, std::vector<int>> ex(unexpect, {1, 2, 3});
  267. ASSERT_FALSE(ex.has_value());
  268. EXPECT_EQ(ex.error(), std::vector({1, 2, 3}));
  269. }
  270. TEST(Expected, AssignValue) {
  271. expected<int, int> ex = unexpected(0);
  272. EXPECT_FALSE(ex.has_value());
  273. ex = 42;
  274. ASSERT_TRUE(ex.has_value());
  275. EXPECT_EQ(ex.value(), 42);
  276. ex = 123;
  277. ASSERT_TRUE(ex.has_value());
  278. EXPECT_EQ(ex.value(), 123);
  279. }
  280. TEST(Expected, CopyAssignUnexpected) {
  281. expected<int, int> ex;
  282. EXPECT_TRUE(ex.has_value());
  283. ex = unexpected(42);
  284. ASSERT_FALSE(ex.has_value());
  285. EXPECT_EQ(ex.error(), 42);
  286. ex = unexpected(123);
  287. ASSERT_FALSE(ex.has_value());
  288. EXPECT_EQ(ex.error(), 123);
  289. }
  290. TEST(Expected, MoveAssignUnexpected) {
  291. expected<int, StrongMoveOnly<int>> ex;
  292. EXPECT_TRUE(ex.has_value());
  293. ex = unexpected(StrongMoveOnly(42));
  294. ASSERT_FALSE(ex.has_value());
  295. EXPECT_EQ(ex.error().value, 42);
  296. ex = unexpected(StrongMoveOnly(123));
  297. ASSERT_FALSE(ex.has_value());
  298. EXPECT_EQ(ex.error().value, 123);
  299. }
  300. TEST(Expected, Emplace) {
  301. expected<StrongMoveOnly<int>, int> ex = unexpected(0);
  302. EXPECT_FALSE(ex.has_value());
  303. ex.emplace(42);
  304. ASSERT_TRUE(ex.has_value());
  305. EXPECT_EQ(ex.value().value, 42);
  306. }
  307. TEST(Expected, EmplaceList) {
  308. expected<std::vector<int>, int> ex = unexpected(0);
  309. EXPECT_FALSE(ex.has_value());
  310. ex.emplace({1, 2, 3});
  311. ASSERT_TRUE(ex.has_value());
  312. EXPECT_EQ(ex.value(), std::vector({1, 2, 3}));
  313. }
  314. TEST(Expected, MemberSwap) {
  315. expected<int, int> ex1 = 42;
  316. expected<int, int> ex2 = unexpected(123);
  317. ex1.swap(ex2);
  318. ASSERT_FALSE(ex1.has_value());
  319. EXPECT_EQ(ex1.error(), 123);
  320. ASSERT_TRUE(ex2.has_value());
  321. EXPECT_EQ(ex2.value(), 42);
  322. }
  323. TEST(Expected, FreeSwap) {
  324. expected<int, int> ex1 = 42;
  325. expected<int, int> ex2 = unexpected(123);
  326. swap(ex1, ex2);
  327. ASSERT_FALSE(ex1.has_value());
  328. EXPECT_EQ(ex1.error(), 123);
  329. ASSERT_TRUE(ex2.has_value());
  330. EXPECT_EQ(ex2.value(), 42);
  331. }
  332. TEST(Expected, OperatorArrow) {
  333. expected<Strong<int>, int> ex(0);
  334. EXPECT_EQ(ex->value, 0);
  335. ex->value = 1;
  336. EXPECT_EQ(ex->value, 1);
  337. constexpr expected<Strong<int>, int> c_ex(0);
  338. EXPECT_EQ(c_ex->value, 0);
  339. static_assert(std::is_same_v<decltype((c_ex->value)), const int&>);
  340. }
  341. TEST(Expected, OperatorStar) {
  342. expected<int, int> ex;
  343. EXPECT_EQ(*ex, 0);
  344. *ex = 1;
  345. EXPECT_EQ(*ex, 1);
  346. using Ex = expected<int, int>;
  347. static_assert(std::is_same_v<decltype(*std::declval<Ex&>()), int&>);
  348. static_assert(
  349. std::is_same_v<decltype(*std::declval<const Ex&>()), const int&>);
  350. static_assert(std::is_same_v<decltype(*std::declval<Ex&&>()), int&&>);
  351. static_assert(
  352. std::is_same_v<decltype(*std::declval<const Ex&&>()), const int&&>);
  353. }
  354. TEST(Expected, HasValue) {
  355. constexpr expected<int, int> ex;
  356. static_assert(ex.has_value());
  357. constexpr expected<int, int> unex = unexpected(0);
  358. static_assert(!unex.has_value());
  359. }
  360. TEST(Expected, Value) {
  361. expected<int, int> ex;
  362. EXPECT_EQ(ex.value(), 0);
  363. ex.value() = 1;
  364. EXPECT_EQ(ex.value(), 1);
  365. using Ex = expected<int, int>;
  366. static_assert(std::is_same_v<decltype(std::declval<Ex&>().value()), int&>);
  367. static_assert(
  368. std::is_same_v<decltype(std::declval<const Ex&>().value()), const int&>);
  369. static_assert(std::is_same_v<decltype(std::declval<Ex&&>().value()), int&&>);
  370. static_assert(std::is_same_v<decltype(std::declval<const Ex&&>().value()),
  371. const int&&>);
  372. }
  373. TEST(Expected, Error) {
  374. expected<int, int> ex = unexpected(0);
  375. EXPECT_EQ(ex.error(), 0);
  376. ex.error() = 1;
  377. EXPECT_EQ(ex.error(), 1);
  378. using Ex = expected<int, int>;
  379. static_assert(std::is_same_v<decltype(std::declval<Ex&>().error()), int&>);
  380. static_assert(
  381. std::is_same_v<decltype(std::declval<const Ex&>().error()), const int&>);
  382. static_assert(std::is_same_v<decltype(std::declval<Ex&&>().error()), int&&>);
  383. static_assert(std::is_same_v<decltype(std::declval<const Ex&&>().error()),
  384. const int&&>);
  385. }
  386. TEST(Expected, ValueOr) {
  387. {
  388. expected<int, int> ex;
  389. EXPECT_EQ(ex.value_or(123), 0);
  390. expected<int, int> unex = unexpected(0);
  391. EXPECT_EQ(unex.value_or(123), 123);
  392. }
  393. {
  394. expected<WeakMoveOnly<int>, int> ex(0);
  395. EXPECT_EQ(std::move(ex).value_or(123).value, 0);
  396. expected<int, WeakMoveOnly<int>> unex = unexpected(WeakMoveOnly(0));
  397. EXPECT_EQ(std::move(unex).value_or(123), 123);
  398. }
  399. }
  400. TEST(Expected, EqualityOperators) {
  401. using ExInt = expected<int, int>;
  402. using ExLong = expected<long, long>;
  403. EXPECT_EQ(ExInt(42), ExLong(42));
  404. EXPECT_EQ(ExLong(42), ExInt(42));
  405. EXPECT_EQ(ExInt(42), 42);
  406. EXPECT_EQ(42, ExInt(42));
  407. EXPECT_EQ(ExInt(unexpect, 42), unexpected(42));
  408. EXPECT_EQ(unexpected(42), ExInt(unexpect, 42));
  409. EXPECT_NE(ExInt(42), ExLong(123));
  410. EXPECT_NE(ExLong(123), ExInt(42));
  411. EXPECT_NE(ExInt(42), 123);
  412. EXPECT_NE(123, ExInt(42));
  413. EXPECT_NE(ExInt(unexpect, 123), unexpected(42));
  414. EXPECT_NE(unexpected(42), ExInt(unexpect, 123));
  415. EXPECT_NE(ExInt(123), unexpected(123));
  416. EXPECT_NE(unexpected(123), ExInt(123));
  417. }
  418. TEST(ExpectedTest, DeathTests) {
  419. using ExpectedInt = expected<int, int>;
  420. using ExpectedDouble = expected<double, double>;
  421. ExpectedInt moved_from;
  422. ExpectedInt ex = std::move(moved_from);
  423. // Accessing moved from objects crashes.
  424. // NOLINTBEGIN(bugprone-use-after-move)
  425. EXPECT_DEATH_IF_SUPPORTED(ExpectedInt{moved_from}, "");
  426. EXPECT_DEATH_IF_SUPPORTED(ExpectedInt{std::move(moved_from)}, "");
  427. EXPECT_DEATH_IF_SUPPORTED(ExpectedDouble{moved_from}, "");
  428. EXPECT_DEATH_IF_SUPPORTED(ExpectedDouble{std::move(moved_from)}, "");
  429. EXPECT_DEATH_IF_SUPPORTED(ex = moved_from, "");
  430. EXPECT_DEATH_IF_SUPPORTED(ex = std::move(moved_from), "");
  431. EXPECT_DEATH_IF_SUPPORTED(ex.swap(moved_from), "");
  432. EXPECT_DEATH_IF_SUPPORTED(moved_from.swap(ex), "");
  433. EXPECT_DEATH_IF_SUPPORTED(moved_from.operator->(), "");
  434. EXPECT_DEATH_IF_SUPPORTED(*moved_from, "");
  435. EXPECT_DEATH_IF_SUPPORTED(moved_from.has_value(), "");
  436. EXPECT_DEATH_IF_SUPPORTED(moved_from.value(), "");
  437. EXPECT_DEATH_IF_SUPPORTED(moved_from.error(), "");
  438. EXPECT_DEATH_IF_SUPPORTED(moved_from.value_or(0), "");
  439. EXPECT_DEATH_IF_SUPPORTED(std::ignore = (ex == moved_from), "");
  440. EXPECT_DEATH_IF_SUPPORTED(std::ignore = (moved_from == ex), "");
  441. // NOLINTEND(bugprone-use-after-move)
  442. // Accessing inactive union-members crashes.
  443. EXPECT_DEATH_IF_SUPPORTED(ExpectedInt{}.error(), "");
  444. EXPECT_DEATH_IF_SUPPORTED(ExpectedInt{unexpect}.value(), "");
  445. }
  446. TEST(ExpectedVoid, Triviality) {
  447. using TrivialExpected = expected<void, int>;
  448. static_assert(std::is_trivially_destructible_v<TrivialExpected>);
  449. using NonTrivialExpected = expected<void, std::string>;
  450. static_assert(!std::is_trivially_destructible_v<NonTrivialExpected>);
  451. }
  452. TEST(ExpectedVoid, DefaultConstructor) {
  453. constexpr expected<void, int> ex;
  454. static_assert(ex.has_value());
  455. static_assert(std::is_default_constructible_v<expected<void, int>>);
  456. }
  457. TEST(ExpectedVoid, InPlaceConstructor) {
  458. constexpr expected<void, int> ex(absl::in_place);
  459. static_assert(ex.has_value());
  460. }
  461. TEST(ExpectedVoid, CopyConstructor) {
  462. constexpr expected<void, int> ex1 = unexpected(42);
  463. constexpr expected<void, int> ex2 = ex1;
  464. static_assert(!ex2.has_value());
  465. EXPECT_EQ(ex2.error(), 42);
  466. }
  467. TEST(ExpectedVoid, MoveConstructor) {
  468. expected<void, StrongMoveOnly<int>> ex1 = unexpected(StrongMoveOnly(42));
  469. expected<void, StrongMoveOnly<int>> ex2 = std::move(ex1);
  470. ASSERT_FALSE(ex2.has_value());
  471. EXPECT_EQ(ex2.error().value, 42);
  472. }
  473. TEST(ExpectedVoid, ExplicitConvertingCopyConstructor) {
  474. constexpr expected<void, int> ex1 = unexpected(42);
  475. expected<const void, Strong<int>> ex2(ex1);
  476. static_assert(!std::is_convertible_v<decltype(ex1), decltype(ex2)>);
  477. ASSERT_FALSE(ex2.has_value());
  478. EXPECT_EQ(ex2.error().value, 42);
  479. }
  480. TEST(ExpectedVoid, ImplicitConvertingCopyConstructor) {
  481. constexpr expected<void, int> ex1 = unexpected(42);
  482. expected<const void, Weak<int>> ex2 = ex1;
  483. ASSERT_FALSE(ex2.has_value());
  484. EXPECT_EQ(ex2.error().value, 42);
  485. }
  486. TEST(ExpectedVoid, ExplicitConvertingMoveConstructor) {
  487. expected<void, int> ex1 = unexpected(42);
  488. expected<const void, StrongMoveOnly<int>> ex2(std::move(ex1));
  489. static_assert(
  490. !std::is_convertible_v<decltype(std::move(ex1)), decltype(ex2)>);
  491. ASSERT_FALSE(ex2.has_value());
  492. EXPECT_EQ(ex2.error().value, 42);
  493. }
  494. TEST(ExpectedVoid, ImplicitConvertingMoveConstructor) {
  495. expected<void, int> ex1 = unexpected(42);
  496. expected<const void, WeakMoveOnly<int>> ex2 = std::move(ex1);
  497. ASSERT_FALSE(ex2.has_value());
  498. EXPECT_EQ(ex2.error().value, 42);
  499. }
  500. TEST(ExpectedVoid, ExplicitErrorConstructor) {
  501. {
  502. constexpr expected<void, Strong<int>> ex(unexpected(42));
  503. static_assert(!std::is_convertible_v<unexpected<int>, decltype(ex)>);
  504. static_assert(!ex.has_value());
  505. EXPECT_EQ(ex.error().value, 42);
  506. }
  507. {
  508. constexpr expected<void, StrongMoveOnly<int>> ex(unexpected(42));
  509. static_assert(!std::is_constructible_v<decltype(ex), unexpected<int>&>);
  510. static_assert(!std::is_convertible_v<unexpected<int>, decltype(ex)>);
  511. static_assert(!ex.has_value());
  512. EXPECT_EQ(ex.error().value, 42);
  513. }
  514. }
  515. TEST(ExpectedVoid, ImplicitErrorConstructor) {
  516. {
  517. constexpr expected<void, Weak<int>> ex = unexpected(42);
  518. static_assert(!ex.has_value());
  519. EXPECT_EQ(ex.error().value, 42);
  520. }
  521. {
  522. constexpr expected<void, WeakMoveOnly<int>> ex = unexpected(42);
  523. static_assert(!std::is_convertible_v<unexpected<int>&, decltype(ex)>);
  524. static_assert(!ex.has_value());
  525. EXPECT_EQ(ex.error().value, 42);
  526. }
  527. }
  528. TEST(ExpectedVoid, UnexpectConstructor) {
  529. constexpr expected<void, Strong<int>> ex(unexpect, 42);
  530. static_assert(!ex.has_value());
  531. EXPECT_EQ(ex.error().value, 42);
  532. }
  533. TEST(ExpectedVoid, UnexpectListConstructor) {
  534. expected<void, std::vector<int>> ex(unexpect, {1, 2, 3});
  535. ASSERT_FALSE(ex.has_value());
  536. EXPECT_EQ(ex.error(), std::vector({1, 2, 3}));
  537. }
  538. TEST(ExpectedVoid, CopyAssignUnexpected) {
  539. expected<void, int> ex;
  540. EXPECT_TRUE(ex.has_value());
  541. ex = unexpected(42);
  542. ASSERT_FALSE(ex.has_value());
  543. EXPECT_EQ(ex.error(), 42);
  544. ex = unexpected(123);
  545. ASSERT_FALSE(ex.has_value());
  546. EXPECT_EQ(ex.error(), 123);
  547. }
  548. TEST(ExpectedVoid, MoveAssignUnexpected) {
  549. expected<void, StrongMoveOnly<int>> ex;
  550. EXPECT_TRUE(ex.has_value());
  551. ex = unexpected(StrongMoveOnly(42));
  552. ASSERT_FALSE(ex.has_value());
  553. EXPECT_EQ(ex.error().value, 42);
  554. ex = unexpected(StrongMoveOnly(123));
  555. ASSERT_FALSE(ex.has_value());
  556. EXPECT_EQ(ex.error().value, 123);
  557. }
  558. TEST(ExpectedVoid, Emplace) {
  559. expected<void, int> ex = unexpected(0);
  560. EXPECT_FALSE(ex.has_value());
  561. ex.emplace();
  562. ASSERT_TRUE(ex.has_value());
  563. }
  564. TEST(ExpectedVoid, MemberSwap) {
  565. expected<void, int> ex1;
  566. expected<void, int> ex2 = unexpected(123);
  567. ex1.swap(ex2);
  568. ASSERT_FALSE(ex1.has_value());
  569. EXPECT_EQ(ex1.error(), 123);
  570. ASSERT_TRUE(ex2.has_value());
  571. }
  572. TEST(ExpectedVoid, FreeSwap) {
  573. expected<void, int> ex1;
  574. expected<void, int> ex2 = unexpected(123);
  575. swap(ex1, ex2);
  576. ASSERT_FALSE(ex1.has_value());
  577. EXPECT_EQ(ex1.error(), 123);
  578. ASSERT_TRUE(ex2.has_value());
  579. }
  580. TEST(ExpectedVoid, OperatorStar) {
  581. expected<void, int> ex;
  582. *ex;
  583. static_assert(std::is_void_v<decltype(*ex)>);
  584. }
  585. TEST(ExpectedVoid, HasValue) {
  586. constexpr expected<void, int> ex;
  587. static_assert(ex.has_value());
  588. constexpr expected<void, int> unex = unexpected(0);
  589. static_assert(!unex.has_value());
  590. }
  591. TEST(ExpectedVoid, Value) {
  592. expected<void, int> ex;
  593. ex.value();
  594. static_assert(std::is_void_v<decltype(ex.value())>);
  595. }
  596. TEST(ExpectedVoid, Error) {
  597. expected<void, int> ex = unexpected(0);
  598. EXPECT_EQ(ex.error(), 0);
  599. ex.error() = 1;
  600. EXPECT_EQ(ex.error(), 1);
  601. using Ex = expected<void, int>;
  602. static_assert(std::is_same_v<decltype(std::declval<Ex&>().error()), int&>);
  603. static_assert(
  604. std::is_same_v<decltype(std::declval<const Ex&>().error()), const int&>);
  605. static_assert(std::is_same_v<decltype(std::declval<Ex&&>().error()), int&&>);
  606. static_assert(std::is_same_v<decltype(std::declval<const Ex&&>().error()),
  607. const int&&>);
  608. }
  609. TEST(ExpectedVoid, EqualityOperators) {
  610. using Ex = expected<void, int>;
  611. using ConstEx = expected<const void, const int>;
  612. EXPECT_EQ(Ex(), ConstEx());
  613. EXPECT_EQ(ConstEx(), Ex());
  614. EXPECT_EQ(Ex(unexpect, 42), unexpected(42));
  615. EXPECT_EQ(unexpected(42), Ex(unexpect, 42));
  616. EXPECT_NE(Ex(unexpect, 123), unexpected(42));
  617. EXPECT_NE(unexpected(42), Ex(unexpect, 123));
  618. EXPECT_NE(Ex(), unexpected(0));
  619. EXPECT_NE(unexpected(0), Ex());
  620. }
  621. TEST(ExpectedVoidTest, DeathTests) {
  622. using ExpectedInt = expected<void, int>;
  623. using ExpectedDouble = expected<void, double>;
  624. ExpectedInt moved_from;
  625. ExpectedInt ex = std::move(moved_from);
  626. // Accessing moved from objects crashes.
  627. // NOLINTBEGIN(bugprone-use-after-move)
  628. EXPECT_DEATH_IF_SUPPORTED(ExpectedInt{moved_from}, "");
  629. EXPECT_DEATH_IF_SUPPORTED(ExpectedInt{std::move(moved_from)}, "");
  630. EXPECT_DEATH_IF_SUPPORTED(ExpectedDouble{moved_from}, "");
  631. EXPECT_DEATH_IF_SUPPORTED(ExpectedDouble{std::move(moved_from)}, "");
  632. EXPECT_DEATH_IF_SUPPORTED(ex = moved_from, "");
  633. EXPECT_DEATH_IF_SUPPORTED(ex = std::move(moved_from), "");
  634. EXPECT_DEATH_IF_SUPPORTED(ex.swap(moved_from), "");
  635. EXPECT_DEATH_IF_SUPPORTED(moved_from.swap(ex), "");
  636. EXPECT_DEATH_IF_SUPPORTED(*moved_from, "");
  637. EXPECT_DEATH_IF_SUPPORTED(moved_from.has_value(), "");
  638. EXPECT_DEATH_IF_SUPPORTED(moved_from.value(), "");
  639. EXPECT_DEATH_IF_SUPPORTED(moved_from.error(), "");
  640. EXPECT_DEATH_IF_SUPPORTED(std::ignore = (ex == moved_from), "");
  641. EXPECT_DEATH_IF_SUPPORTED(std::ignore = (moved_from == ex), "");
  642. // NOLINTEND(bugprone-use-after-move)
  643. // Accessing inactive union-members crashes.
  644. EXPECT_DEATH_IF_SUPPORTED(ExpectedInt{}.error(), "");
  645. EXPECT_DEATH_IF_SUPPORTED(ExpectedInt{unexpect}.value(), "");
  646. }
  647. } // namespace
  648. } // namespace base