ErrorOrTest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //===- unittests/ErrorOrTest.cpp - ErrorOr.h tests ------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/Support/ErrorOr.h"
  10. #include "llvm/Support/Errc.h"
  11. #include "gtest/gtest.h"
  12. #include <memory>
  13. using namespace llvm;
  14. namespace {
  15. ErrorOr<int> t1() {return 1;}
  16. ErrorOr<int> t2() { return errc::invalid_argument; }
  17. TEST(ErrorOr, SimpleValue) {
  18. ErrorOr<int> a = t1();
  19. // FIXME: This is probably a bug in gtest. EXPECT_TRUE should expand to
  20. // include the !! to make it friendly to explicit bool operators.
  21. EXPECT_TRUE(!!a);
  22. EXPECT_EQ(1, *a);
  23. ErrorOr<int> b = a;
  24. EXPECT_EQ(1, *b);
  25. a = t2();
  26. EXPECT_FALSE(a);
  27. EXPECT_EQ(a.getError(), errc::invalid_argument);
  28. #ifdef EXPECT_DEBUG_DEATH
  29. EXPECT_DEBUG_DEATH(*a, "Cannot get value when an error exists");
  30. #endif
  31. }
  32. ErrorOr<std::unique_ptr<int> > t3() {
  33. return std::unique_ptr<int>(new int(3));
  34. }
  35. TEST(ErrorOr, Types) {
  36. int x;
  37. ErrorOr<int&> a(x);
  38. *a = 42;
  39. EXPECT_EQ(42, x);
  40. // Move only types.
  41. EXPECT_EQ(3, **t3());
  42. }
  43. struct B {};
  44. struct D : B {};
  45. TEST(ErrorOr, Covariant) {
  46. ErrorOr<B*> b(ErrorOr<D*>(nullptr));
  47. b = ErrorOr<D*>(nullptr);
  48. ErrorOr<std::unique_ptr<B> > b1(ErrorOr<std::unique_ptr<D> >(nullptr));
  49. b1 = ErrorOr<std::unique_ptr<D> >(nullptr);
  50. ErrorOr<std::unique_ptr<int>> b2(ErrorOr<int *>(nullptr));
  51. ErrorOr<int *> b3(nullptr);
  52. ErrorOr<std::unique_ptr<int>> b4(b3);
  53. }
  54. TEST(ErrorOr, Comparison) {
  55. ErrorOr<int> x(errc::no_such_file_or_directory);
  56. EXPECT_EQ(x, errc::no_such_file_or_directory);
  57. }
  58. // ErrorOr<int*> x(nullptr);
  59. // ErrorOr<std::unique_ptr<int>> y = x; // invalid conversion
  60. static_assert(
  61. !std::is_convertible<const ErrorOr<int *> &,
  62. ErrorOr<std::unique_ptr<int>>>::value,
  63. "do not invoke explicit ctors in implicit conversion from lvalue");
  64. // ErrorOr<std::unique_ptr<int>> y = ErrorOr<int*>(nullptr); // invalid
  65. // // conversion
  66. static_assert(
  67. !std::is_convertible<ErrorOr<int *> &&,
  68. ErrorOr<std::unique_ptr<int>>>::value,
  69. "do not invoke explicit ctors in implicit conversion from rvalue");
  70. // ErrorOr<int*> x(nullptr);
  71. // ErrorOr<std::unique_ptr<int>> y;
  72. // y = x; // invalid conversion
  73. static_assert(!std::is_assignable<ErrorOr<std::unique_ptr<int>>,
  74. const ErrorOr<int *> &>::value,
  75. "do not invoke explicit ctors in assignment");
  76. // ErrorOr<std::unique_ptr<int>> x;
  77. // x = ErrorOr<int*>(nullptr); // invalid conversion
  78. static_assert(!std::is_assignable<ErrorOr<std::unique_ptr<int>>,
  79. ErrorOr<int *> &&>::value,
  80. "do not invoke explicit ctors in assignment");
  81. } // end anon namespace