• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #include "llvm/Support/ErrorOr.h"
11 #include "llvm/Support/Errc.h"
12 #include "gtest/gtest.h"
13 #include <memory>
14 
15 using namespace llvm;
16 
17 namespace {
18 
t1()19 ErrorOr<int> t1() {return 1;}
t2()20 ErrorOr<int> t2() { return errc::invalid_argument; }
21 
TEST(ErrorOr,SimpleValue)22 TEST(ErrorOr, SimpleValue) {
23   ErrorOr<int> a = t1();
24   // FIXME: This is probably a bug in gtest. EXPECT_TRUE should expand to
25   // include the !! to make it friendly to explicit bool operators.
26   EXPECT_TRUE(!!a);
27   EXPECT_EQ(1, *a);
28 
29   ErrorOr<int> b = a;
30   EXPECT_EQ(1, *b);
31 
32   a = t2();
33   EXPECT_FALSE(a);
34   EXPECT_EQ(a.getError(), errc::invalid_argument);
35 #ifdef EXPECT_DEBUG_DEATH
36   EXPECT_DEBUG_DEATH(*a, "Cannot get value when an error exists");
37 #endif
38 }
39 
t3()40 ErrorOr<std::unique_ptr<int> > t3() {
41   return std::unique_ptr<int>(new int(3));
42 }
43 
TEST(ErrorOr,Types)44 TEST(ErrorOr, Types) {
45   int x;
46   ErrorOr<int&> a(x);
47   *a = 42;
48   EXPECT_EQ(42, x);
49 
50   // Move only types.
51   EXPECT_EQ(3, **t3());
52 }
53 
54 struct B {};
55 struct D : B {};
56 
TEST(ErrorOr,Covariant)57 TEST(ErrorOr, Covariant) {
58   ErrorOr<B*> b(ErrorOr<D*>(nullptr));
59   b = ErrorOr<D*>(nullptr);
60 
61   ErrorOr<std::unique_ptr<B> > b1(ErrorOr<std::unique_ptr<D> >(nullptr));
62   b1 = ErrorOr<std::unique_ptr<D> >(nullptr);
63 }
64 } // end anon namespace
65