Bump version to 19.1.0-rc3
[llvm-project.git] / llvm / unittests / Support / ErrorOrTest.cpp
bloba271c02d341afdb0839e0112939f32bb55e54ea2
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/ErrorOr.h"
10 #include "llvm/Support/Errc.h"
11 #include "gtest/gtest.h"
12 #include <memory>
14 using namespace llvm;
16 namespace {
18 ErrorOr<int> t1() { return 1; }
19 ErrorOr<int> t2() { return errc::invalid_argument; }
21 TEST(ErrorOr, SimpleValue) {
22 ErrorOr<int> a = t1();
23 // FIXME: This is probably a bug in gtest. EXPECT_TRUE should expand to
24 // include the !! to make it friendly to explicit bool operators.
25 EXPECT_TRUE(!!a);
26 EXPECT_EQ(1, *a);
28 ErrorOr<int> b = a;
29 EXPECT_EQ(1, *b);
31 a = t2();
32 EXPECT_FALSE(a);
33 EXPECT_EQ(a.getError(), errc::invalid_argument);
34 #ifdef EXPECT_DEBUG_DEATH
35 EXPECT_DEBUG_DEATH(*a, "Cannot get value when an error exists");
36 #endif
39 ErrorOr<std::unique_ptr<int>> t3() { return std::make_unique<int>(3); }
41 TEST(ErrorOr, Types) {
42 int x;
43 ErrorOr<int&> a(x);
44 *a = 42;
45 EXPECT_EQ(42, x);
47 // Move only types.
48 EXPECT_EQ(3, **t3());
51 struct B {};
52 struct D : B {};
54 TEST(ErrorOr, Covariant) {
55 ErrorOr<B*> b(ErrorOr<D*>(nullptr));
56 b = ErrorOr<D*>(nullptr);
58 ErrorOr<std::unique_ptr<B> > b1(ErrorOr<std::unique_ptr<D> >(nullptr));
59 b1 = ErrorOr<std::unique_ptr<D> >(nullptr);
61 ErrorOr<std::unique_ptr<int>> b2(ErrorOr<int *>(nullptr));
62 ErrorOr<int *> b3(nullptr);
63 ErrorOr<std::unique_ptr<int>> b4(b3);
66 TEST(ErrorOr, Comparison) {
67 ErrorOr<int> x(errc::no_such_file_or_directory);
68 EXPECT_EQ(x, errc::no_such_file_or_directory);
71 TEST(ErrorOr, ImplicitConversion) {
72 ErrorOr<std::string> x("string literal");
73 EXPECT_TRUE(!!x);
76 TEST(ErrorOr, ImplicitConversionCausesMove) {
77 struct Source {};
78 struct Destination {
79 Destination(const Source&) {}
80 Destination(Source&&) = delete;
82 Source s;
83 ErrorOr<Destination> x = s;
84 EXPECT_TRUE(!!x);
87 TEST(ErrorOr, ImplicitConversionNoAmbiguity) {
88 struct CastsToErrorCode {
89 CastsToErrorCode() = default;
90 CastsToErrorCode(std::error_code) {}
91 operator std::error_code() { return errc::invalid_argument; }
92 } casts_to_error_code;
93 ErrorOr<CastsToErrorCode> x1(casts_to_error_code);
94 ErrorOr<CastsToErrorCode> x2 = casts_to_error_code;
95 ErrorOr<CastsToErrorCode> x3 = {casts_to_error_code};
96 ErrorOr<CastsToErrorCode> x4{casts_to_error_code};
97 ErrorOr<CastsToErrorCode> x5(errc::no_such_file_or_directory);
98 ErrorOr<CastsToErrorCode> x6 = errc::no_such_file_or_directory;
99 ErrorOr<CastsToErrorCode> x7 = {errc::no_such_file_or_directory};
100 ErrorOr<CastsToErrorCode> x8{errc::no_such_file_or_directory};
101 EXPECT_TRUE(!!x1);
102 EXPECT_TRUE(!!x2);
103 EXPECT_TRUE(!!x3);
104 EXPECT_TRUE(!!x4);
105 EXPECT_FALSE(x5);
106 EXPECT_FALSE(x6);
107 EXPECT_FALSE(x7);
108 EXPECT_FALSE(x8);
111 // ErrorOr<int*> x(nullptr);
112 // ErrorOr<std::unique_ptr<int>> y = x; // invalid conversion
113 static_assert(
114 !std::is_convertible_v<const ErrorOr<int *> &,
115 ErrorOr<std::unique_ptr<int>>>,
116 "do not invoke explicit ctors in implicit conversion from lvalue");
118 // ErrorOr<std::unique_ptr<int>> y = ErrorOr<int*>(nullptr); // invalid
119 // // conversion
120 static_assert(
121 !std::is_convertible_v<ErrorOr<int *> &&, ErrorOr<std::unique_ptr<int>>>,
122 "do not invoke explicit ctors in implicit conversion from rvalue");
124 // ErrorOr<int*> x(nullptr);
125 // ErrorOr<std::unique_ptr<int>> y;
126 // y = x; // invalid conversion
127 static_assert(!std::is_assignable_v<ErrorOr<std::unique_ptr<int>> &,
128 const ErrorOr<int *> &>,
129 "do not invoke explicit ctors in assignment");
131 // ErrorOr<std::unique_ptr<int>> x;
132 // x = ErrorOr<int*>(nullptr); // invalid conversion
133 static_assert(
134 !std::is_assignable_v<ErrorOr<std::unique_ptr<int>> &, ErrorOr<int *> &&>,
135 "do not invoke explicit ctors in assignment");
136 } // end anon namespace