Add a unit test for the copy constructor.
[oota-llvm.git] / unittests / Support / ErrorOrTest.cpp
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 "gtest/gtest.h"
12 #include <memory>
13
14 using namespace llvm;
15
16 namespace {
17
18 ErrorOr<int> t1() {return 1;}
19 ErrorOr<int> t2() { return errc::invalid_argument; }
20
21 TEST(ErrorOr, SimpleValue) {
22   ErrorOr<int> a = t1();
23   EXPECT_TRUE(a);
24   EXPECT_EQ(1, *a);
25
26   ErrorOr<int> b = a;
27   EXPECT_EQ(1, *b);
28
29   a = t2();
30   EXPECT_FALSE(a);
31   EXPECT_EQ(errc::invalid_argument, a.getError());
32 #ifdef EXPECT_DEBUG_DEATH
33   EXPECT_DEBUG_DEATH(*a, "Cannot get value when an error exists");
34 #endif
35 }
36
37 #if LLVM_HAS_CXX11_STDLIB
38 ErrorOr<std::unique_ptr<int> > t3() {
39   return std::unique_ptr<int>(new int(3));
40 }
41 #endif
42
43 TEST(ErrorOr, Types) {
44   int x;
45   ErrorOr<int&> a(x);
46   *a = 42;
47   EXPECT_EQ(42, x);
48
49 #if LLVM_HAS_CXX11_STDLIB
50   // Move only types.
51   EXPECT_EQ(3, **t3());
52 #endif
53 }
54
55 struct B {};
56 struct D : B {};
57
58 TEST(ErrorOr, Covariant) {
59   ErrorOr<B*> b(ErrorOr<D*>(0));
60   b = ErrorOr<D*>(0);
61
62 #if LLVM_HAS_CXX11_STDLIB
63   ErrorOr<std::unique_ptr<B> > b1(ErrorOr<std::unique_ptr<D> >(0));
64   b1 = ErrorOr<std::unique_ptr<D> >(0);
65 #endif
66 }
67 } // end anon namespace