ErrorOrTest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //===- unittests/ErrorOrTest.cpp - ErrorOr.h tests ------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Support/ErrorOr.h"
  9. #include "llvm/Support/Errc.h"
  10. #include "gtest/gtest.h"
  11. #include <memory>
  12. using namespace llvm;
  13. namespace {
  14. ErrorOr<int> t1() { return 1; }
  15. ErrorOr<int> t2() { return errc::invalid_argument; }
  16. TEST(ErrorOr, SimpleValue) {
  17. ErrorOr<int> a = t1();
  18. // FIXME: This is probably a bug in gtest. EXPECT_TRUE should expand to
  19. // include the !! to make it friendly to explicit bool operators.
  20. EXPECT_TRUE(!!a);
  21. EXPECT_EQ(1, *a);
  22. ErrorOr<int> b = a;
  23. EXPECT_EQ(1, *b);
  24. a = t2();
  25. EXPECT_FALSE(a);
  26. EXPECT_EQ(a.getError(), errc::invalid_argument);
  27. #ifdef EXPECT_DEBUG_DEATH
  28. EXPECT_DEBUG_DEATH(*a, "Cannot get value when an error exists");
  29. #endif
  30. }
  31. ErrorOr<std::unique_ptr<int> > t3() {
  32. return std::unique_ptr<int>(new int(3));
  33. }
  34. TEST(ErrorOr, Types) {
  35. int x;
  36. ErrorOr<int&> a(x);
  37. *a = 42;
  38. EXPECT_EQ(42, x);
  39. // Move only types.
  40. EXPECT_EQ(3, **t3());
  41. }
  42. struct B {};
  43. struct D : B {};
  44. TEST(ErrorOr, Covariant) {
  45. ErrorOr<B*> b(ErrorOr<D*>(nullptr));
  46. b = ErrorOr<D*>(nullptr);
  47. ErrorOr<std::unique_ptr<B> > b1(ErrorOr<std::unique_ptr<D> >(nullptr));
  48. b1 = ErrorOr<std::unique_ptr<D> >(nullptr);
  49. ErrorOr<std::unique_ptr<int>> b2(ErrorOr<int *>(nullptr));
  50. ErrorOr<int *> b3(nullptr);
  51. ErrorOr<std::unique_ptr<int>> b4(b3);
  52. }
  53. TEST(ErrorOr, Comparison) {
  54. ErrorOr<int> x(errc::no_such_file_or_directory);
  55. EXPECT_EQ(x, errc::no_such_file_or_directory);
  56. }
  57. TEST(ErrorOr, ImplicitConversion) {
  58. ErrorOr<std::string> x("string literal");
  59. EXPECT_TRUE(!!x);
  60. }
  61. TEST(ErrorOr, ImplicitConversionCausesMove) {
  62. struct Source {};
  63. struct Destination {
  64. Destination(const Source&) {}
  65. Destination(Source&&) = delete;
  66. };
  67. Source s;
  68. ErrorOr<Destination> x = s;
  69. EXPECT_TRUE(!!x);
  70. }
  71. TEST(ErrorOr, ImplicitConversionNoAmbiguity) {
  72. struct CastsToErrorCode {
  73. CastsToErrorCode() = default;
  74. CastsToErrorCode(std::error_code) {}
  75. operator std::error_code() { return errc::invalid_argument; }
  76. } casts_to_error_code;
  77. ErrorOr<CastsToErrorCode> x1(casts_to_error_code);
  78. ErrorOr<CastsToErrorCode> x2 = casts_to_error_code;
  79. ErrorOr<CastsToErrorCode> x3 = {casts_to_error_code};
  80. ErrorOr<CastsToErrorCode> x4{casts_to_error_code};
  81. ErrorOr<CastsToErrorCode> x5(errc::no_such_file_or_directory);
  82. ErrorOr<CastsToErrorCode> x6 = errc::no_such_file_or_directory;
  83. ErrorOr<CastsToErrorCode> x7 = {errc::no_such_file_or_directory};
  84. ErrorOr<CastsToErrorCode> x8{errc::no_such_file_or_directory};
  85. EXPECT_TRUE(!!x1);
  86. EXPECT_TRUE(!!x2);
  87. EXPECT_TRUE(!!x3);
  88. EXPECT_TRUE(!!x4);
  89. EXPECT_FALSE(x5);
  90. EXPECT_FALSE(x6);
  91. EXPECT_FALSE(x7);
  92. EXPECT_FALSE(x8);
  93. }
  94. // ErrorOr<int*> x(nullptr);
  95. // ErrorOr<std::unique_ptr<int>> y = x; // invalid conversion
  96. static_assert(
  97. !std::is_convertible<const ErrorOr<int *> &,
  98. ErrorOr<std::unique_ptr<int>>>::value,
  99. "do not invoke explicit ctors in implicit conversion from lvalue");
  100. // ErrorOr<std::unique_ptr<int>> y = ErrorOr<int*>(nullptr); // invalid
  101. // // conversion
  102. static_assert(
  103. !std::is_convertible<ErrorOr<int *> &&,
  104. ErrorOr<std::unique_ptr<int>>>::value,
  105. "do not invoke explicit ctors in implicit conversion from rvalue");
  106. // ErrorOr<int*> x(nullptr);
  107. // ErrorOr<std::unique_ptr<int>> y;
  108. // y = x; // invalid conversion
  109. static_assert(!std::is_assignable<ErrorOr<std::unique_ptr<int>>&,
  110. const ErrorOr<int *> &>::value,
  111. "do not invoke explicit ctors in assignment");
  112. // ErrorOr<std::unique_ptr<int>> x;
  113. // x = ErrorOr<int*>(nullptr); // invalid conversion
  114. static_assert(!std::is_assignable<ErrorOr<std::unique_ptr<int>>&,
  115. ErrorOr<int *> &&>::value,
  116. "do not invoke explicit ctors in assignment");
  117. } // end anon namespace