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